public function __construct($testId, $password)
 {
     $this->user = UserSession::getInstance()->getUser();
     $this->testId = $testId;
     $this->password = $password;
     $tprbBS = new TestProblemsBS(null);
     $qbuilder = new QueryBuilder('testproblems');
     $qbuilder->addEqual("tpb_tst_id", $testId);
     $qbuilder->addOrder("tpb_questionNumber", QueryBuilder::$ASC);
     $this->problems = $tprbBS->findNotDeleted($qbuilder);
     $testBS = new TestBS(null);
     $qbuilder = new QueryBuilder('test');
     $qbuilder->addEqual("tst_id", $testId);
     $qbuilder->addEqual("tst_password", $password);
     $tests = $testBS->findNotDeleted($qbuilder);
     if (count($tests) < 1) {
         die("<h3>ACESSO NEGADO</h3>");
     }
     $this->test = $tests[0];
     $ttrialBS = new TestTrialBS(null);
     $ttrialBS->createUserTrials($this->problems);
     $qbuilder = new QueryBuilder('testtrial');
     $qbuilder->addEqual("ttl_tst_id", $testId);
     $qbuilder->addEqual("ttl_usr_id", $this->user->get('usr_id'));
     $trials = $ttrialBS->findNotDeleted($qbuilder);
     $this->trials = array();
     if (count($trials) > 0) {
         foreach ($trials as $t => $trial) {
             $this->trials[$trial->get('ttl_prb_id')] = $trial;
         }
     }
     // Gravando log
     if ($this->test->get("tst_enableLogging") != 0) {
         $testBS->logAction($this->user->get('usr_id'), $this->testId, "Usuário abriu a prova.");
     }
 }
 public function save()
 {
     if (UserSession::getInstance()->isLogged() !== true) {
         die("<h1>Forbidden resource for you.</h1>");
     }
     $model = new TestTrial();
     $model->setFields($this->params);
     $model->set('ttl_usr_id', UserSession::getInstance()->getUser()->get('usr_id'));
     $existent = $this->retrieve($model);
     $tproblemBS = new TestProblemsBS(null);
     $qbuilder = new QueryBuilder("testproblems");
     $qbuilder->addEqual("tpb_prb_id", $existent->get("ttl_prb_id"));
     $qbuilder->addEqual("tpb_tst_id", $existent->get("ttl_tst_id"));
     $tprobs = $tproblemBS->findNotDeleted($qbuilder);
     if (count($tprobs) < 1) {
         die("Invalid request.");
     }
     $tprob = $tprobs[0];
     $today = new DateTime('now');
     $model->set('ttl_lastTrial', $today->format(Model::$SQL_DATE_FORMAT));
     $model->set('ttl_remoteAddr', $_SERVER['REMOTE_ADDR']);
     $output = array();
     $status = 0;
     $text = exec("algoddecrypter " . $model->get('ttl_code'), $output, $status);
     //echo $text;
     $countTrial = true;
     $nTrials = $existent->get("ttl_trials");
     $attenuation = $existent->getForeignModel("ttl_tst_id")->get("tst_scoreAttenuationPerTrial");
     //echo "Att: ".$attenuation;
     //die("<br />Trials: ".$nTrials);
     if ($status != 0) {
         $countTrial = false;
         $model->set("ttl_score", "0");
         $model->set("ttl_hostname", "N/A");
         $model->set("ttl_reason", "<i>Código do corretor submetido foi inválido.</i>");
     } else {
         $mat = "";
         $host = "";
         $question = "";
         $nota = "";
         sscanf($output[0], "%s\t%s\t%d\t%d", $mat, $host, $question, $nota);
         if ($mat == UserSession::getInstance()->getUser()->get('usr_matricula')) {
             if ($question == $tprob->get("tpb_questionNumber")) {
                 $notaAttenuated = $nota * pow($attenuation, $nTrials);
                 //die("Nota: ".$notaAttenuated);
                 $model->set("ttl_score", $notaAttenuated);
                 $model->set("ttl_hostname", $host);
                 $model->set("ttl_reason", "Avaliado pelo corretor.");
             } else {
                 $countTrial = false;
                 $model->set("ttl_score", "0");
                 $model->set("ttl_hostname", $host);
                 $model->set("ttl_reason", "<i>O número da questão no código do corretor não corresponde à esta questão.</i>");
             }
         } else {
             $countTrial = false;
             $model->set("ttl_score", "0");
             $model->set("ttl_hostname", $host);
             $model->set("ttl_reason", "<i>A matrícula passada ao corretor não corresponde à matrícula de seu usuário.</i>");
         }
     }
     $model->set("ttl_sourcefile", mysql_real_escape_string($model->get("ttl_sourcefile")));
     $test = $existent->getForeignModel("ttl_tst_id");
     if ($test->get("tst_enableLogging") != 0) {
         $testBS = new TestBS(null);
         $testBS->logAction(UserSession::getInstance()->getUser()->get('usr_id'), $test->get('tst_id'), "Sumetida tentativa: " . $model->get('ttl_reason') . "\n" . $model->get('ttl_sourcefile'), $model->get('ttl_hostname'), $tprob->get("tpb_questionNumber"), $model->get('ttl_score'));
     }
     @($dao = new DAO(TestTrial));
     if ($countTrial === true) {
         $model->set("ttl_trials", $existent->get("ttl_trials") + 1);
     } else {
         $model->set("ttl_trials", $existent->get("ttl_trials"));
     }
     $status = $dao->update($model);
     if ($status !== true) {
         die("Um erro ocorreu ao tentar cadastrar o usuário, favor contatar um professor:<br />" . $status);
     }
     return true;
 }