Ejemplo n.º 1
0
 /**
  * Тестирование
  * @return void
  */
 public function testingAction()
 {
     $objForm = new Form_Test_Testing();
     $link = $this->getRequest()->getParam('link');
     $objApplicantTests = new ApplicantTests();
     $applicantTest = $objApplicantTests->getTest($link);
     if (empty($applicantTest)) {
         exit;
     }
     $applicantId = $applicantTest->applicant_id;
     $testId = $applicantTest->test_id;
     $applicantTestId = $applicantTest->id;
     $applicantScore = $applicantTest->score;
     $applicantPercent = $applicantTest->percent;
     $objTests = new Tests();
     $test = $objTests->find($testId)->current()->toArray();
     $testName = $test['t_name'];
     $testTime = $test['time'];
     $objApplicants = new Applicants();
     $applicantName = $objApplicants->getName($applicantId);
     $objQuestion = new Questions();
     $questions = $objQuestion->getQuestions($testId);
     $questions = $this->convertArr($questions, 'tq_id');
     $countQuestions = count($questions);
     $objTestAnswers = new Answers();
     $answers = $objTestAnswers->getAnswers(array_keys($questions));
     $answers = $this->convertArr($answers, 'tq_id', true);
     // ключем $answers будет id вопроса
     $objForm->addElementsForm($questions, $answers);
     if ($this->getRequest()->isPost()) {
         //получаем ответы на вопросы и сохраняем
         if ($objForm->isValid($_POST)) {
             $objApplicantAnswers = new ApplicantAnswers();
             $newAnswers = $this->keyReplace($objForm->getValues(), 'answer_');
             foreach ($newAnswers as $answerId => $val) {
                 if ($val) {
                     $newAnswer = $objApplicantAnswers->createRow(array('applicant_tests_id' => $applicantTestId, 'answer_id' => $answerId));
                     $newAnswer->save();
                 } else {
                     unset($newAnswers[$answerId]);
                 }
             }
             $result = $this->calcTestScore($questions, $answers, $newAnswers);
             $applicantTest->score = $result['score'];
             $applicantTest->percent = $result['percent'];
             $applicantTest->save();
             $this->view->sendTest = true;
             return;
         }
     } else {
         $this->view->testName = $testName;
         $this->view->applicantName = $applicantName;
         $this->view->time = $testTime;
         $this->view->score = $applicantScore;
         $this->view->percent = $applicantPercent;
         if (is_null($applicantTest->date)) {
             // выводим тест //
             $this->view->objForm = $objForm;
             $applicantTest->date = new Zend_Db_Expr('NOW()');
             $applicantTest->save();
         } else {
             if ($this->_authorize('test', 'view')) {
                 // выводим результаты теста //
                 $objApplicantAnswers = new ApplicantAnswers();
                 $applicantAnswers = $objApplicantAnswers->getAnswers($applicantTestId);
                 $applicantAnswers = $this->convertArr($applicantAnswers, 'answer_id');
                 $questions = $this->makeQAArray($questions, $answers, $applicantAnswers);
                 unset($answers);
                 unset($applicantAnswers);
                 $questionCategories = $objTests->getQuestionCategoriesListByTestId($testId);
                 if ($questionCategories) {
                     $questionCategories = $this->calcCategoriesScore($questions, $questionCategories);
                 }
                 $this->view->countQuestions = $countQuestions;
                 $this->view->countQuestionFail = $this->countWrongAnswers($questions);
                 $this->view->questionsAndAnswers = $questions;
                 $this->view->questionCategories = $questionCategories;
             }
         }
     }
 }