/** * Finds the Test model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Test the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { $model = Test::findOne($id); if (is_null($model)) { throw new NotFoundHttpException('The requested page does not exist.'); } return $model; }
protected function findModel($id) { if (($model = Test::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionParsingLog($id) { $mod = LogUpload::findOne($id); $type = $mod->type; $keys = \yii\helpers\Json::decode($mod->keys); $values = \yii\helpers\Json::decode($mod->values); $modelAttribute = new Test(); $not = Util::excelNot(); foreach ($values as $value) { if ($type == LogUpload::TYPE_INSERT) { $model = new Test(); } else { $model = Test::findOne($value['id']); } foreach ($keys as $v) { $model->{$v} = $value[$v]; } $e = 0; if ($model->save()) { $model = NULL; $pos = NULL; } else { $error[] = \yii\helpers\Json::encode($model->getErrors()); $e = 1; } } if ($error) { foreach ($error as $err) { if ($err) { $er[] = $err; $e += 1; } } if ($e) { $mod->warning = \yii\helpers\Json::encode($er); $mod->save(); echo '<pre>'; print_r($er); } } }
public function actionDo() { $errors = []; $errors['answer'] = null; $errors['bad_answer'] = null; $post = null; $session = Yii::$app->session; $request = Yii::$app->request; if (!$session->isActive) { $session->open(); } if (!isset($session['test'])) { $this->redirect('new'); exit; } $question_answered_number = $this->getSessionQuestionAnsweredNumber(); $question_number = $this->getQuestionNumber(); if ($question_answered_number == $question_number) { if (!Yii::$app->user->isGuest) { $test_model = Test::findOne($session['test']['active_test_id']); $test_model->ended_at = date('y-m-d H:i:s'); $test_model->update(); $this->redirect(['results', 'id' => $test_model->id]); } else { $this->redirect(['results', 'id' => 0]); } } $question = $this->getQuestion(); if ($request->post('do')) { $post = $request->post(); if (!$request->post('answer')) { $errors['answer'] = 'Musisz wprowadzić odpowiedź!'; } else { if ($request->post('answer') != $question->correct_answer_id) { $errors['bad_answer'] = $request->post('answer'); } $ea = explode(',', $session['test']['answered']); $ea[] = $question->id; $ea = array_filter($ea); $ea = array_unique($ea); $_SESSION['test']['answered'] = implode(',', $ea); if ($request->post('answer') == $question->correct_answer_id) { $ec = []; if (isset($session['test']['answered_correctly'])) { $ec = explode(',', $session['test']['answered_correctly']); } $ec[] = $question->id; $ec = array_filter($ec); $ec = array_unique($ec); $_SESSION['test']['answered_correctly'] = implode(',', $ec); } $_SESSION['test']['answers'][$question->id] = $request->post('answer'); if (!Yii::$app->user->isGuest) { $model_tuqa = new TestUserQuestionAnswer(); $model_tuqa->test_id = $session['test']['active_test_id']; $model_tuqa->user_id = Yii::$app->user->id; $model_tuqa->question_id = $question->id; $model_tuqa->answer_id = $request->post('answer'); $model_tuqa->save(); } } } if ($request->post('do') == 'next') { $this->redirect('nextquestion'); } $datetime1 = new DateTime($session['test']['created_at']); $datetime2 = new DateTime(date('Y-m-d H:i:s')); $interval = $datetime1->diff($datetime2); $diff = $interval->format('%adni %hh %Im'); $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); return $this->render('test', ['question' => $question, 'question_number' => $question_number, 'question_answered_number' => $question_answered_number, 'time_spent' => $diff, 'errors' => $errors, 'post' => $post, 'number_of_correct_answers' => $number_of_correct_answers, 'percent_of_correct_answers' => $percent_of_correct_answers]); }
public function actionGetScore() { $request = Yii::$app->request; $username = $request->post('username'); $testId = $request->post('testId'); $user = Users::findByUsername($username); $test = Test::findOne($testId); if (!$user || !$test) { return ['success' => false, 'message' => 'Eiher the user or test was not found']; } return ['success' => true, 'score' => $test->score]; }