Ejemplo n.º 1
0
 public function actionResults()
 {
     $session = Yii::$app->session;
     $request = Yii::$app->request;
     if (!$session->isActive) {
         $session->open();
     }
     if (!isset($session['test'])) {
         $this->redirect('new');
         exit;
     }
     if (!Yii::$app->user->isGuest) {
         $questions = Question::find()->joinWith(['testUserQuestionAnswers', 'answers'])->leftJoin('test', 'test.id = test_user_question_answer.test_id')->where(['test_user_question_answer.test_id' => $request->get('test_id'), 'test_user_question_answer.user_id' => Yii::$app->user->id])->orderBy('test_user_question_answer.created_at ASC')->all();
     } else {
     }
     $question_number = $this->getQuestionNumber();
     if ($request->get('test_id') == 0) {
         $question_answered_number = $this->getSessionQuestionAnsweredNumber();
         $answered_correctly = isset($session['test']['answered_correctly']) ? $session['test']['answered_correctly'] : 0;
         if ($answered_correctly != 0) {
             $number_of_correct_answers = count(explode(',', $answered_correctly));
         } else {
             $number_of_correct_answers = 0;
         }
         $percent_of_correct_answers = $answered_correctly > 0 ? count(explode(',', $answered_correctly)) / count(explode(',', $session['test']['answered'])) * 100 : 0;
         $percent_of_correct_answers = Yii::$app->formatter->asDecimal($percent_of_correct_answers, 1);
         $date_of_start = $session['test']['created_at'];
         $date_of_end = date('Y-m-d H:i:s');
     } else {
         $question_answered_number = $this->getQuestionAnsweredNumber();
         $answers = TestUserQuestionAnswer::find()->with('question')->where(['test_user_question_answer.test_id' => $request->get('test_id'), 'test_user_question_answer.user_id' => Yii::$app->user->id])->all();
         $number_of_correct_answers = 0;
         foreach ($answers as $v) {
             if ($v->question->correct_answer_id == $v->answer_id) {
                 $number_of_correct_answers++;
             }
         }
         if ($question_answered_number > 0) {
             $percent_of_correct_answers = $number_of_correct_answers / $question_answered_number * 100;
         } else {
             $percent_of_correct_answers = 0;
         }
         $percent_of_correct_answers = Yii::$app->formatter->asDecimal($percent_of_correct_answers, 1);
         $date_of_start = Test::find()->where(['id' => $request->get('test_id')])->one()->created_at;
         $date_of_end = Test::find()->where(['id' => $request->get('test_id')])->one()->ended_at;
         if ($date_of_end == null) {
             $date_of_end = date('Y-m-d H:i:s');
         }
     }
     $datetime1 = new DateTime($date_of_start);
     $datetime2 = new DateTime($date_of_end);
     $interval = $datetime1->diff($datetime2);
     $diff = $interval->format('%adni %hh %Im');
     return $this->render('results', ['questions' => $questions, 'question_number' => $question_number, 'question_answered_number' => $question_answered_number, 'time_spent' => $diff, 'number_of_correct_answers' => $number_of_correct_answers, 'percent_of_correct_answers' => $percent_of_correct_answers]);
 }
Ejemplo n.º 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTestUserQuestionAnswers()
 {
     return $this->hasMany(TestUserQuestionAnswer::className(), ['question_id' => 'id']);
 }