/** * Displays a single EntTest model. * @param integer $id * @return mixed */ public function actionView($id) { $model = $this->findModel($id); $q_ids = explode(',', $model->questions_ids); $questions = []; $count = 0; $isAnyAnswers = false; foreach ($q_ids as $q_id) { $questions[$count++] = Question::find()->where(['id' => $q_id])->one(); if ($_POST[$q_id]) { $isAnyAnswers = true; } if ($count > 125) { break; } } if ($isAnyAnswers) { $user_score = new UserScore(); $user_score->user_id = Yii::$app->user->identity->id; $user_score->test_id = $model->id; $user_score->test_type = 1; $user_score->score = 0; foreach ($questions as $q) { $user_score->answers .= $_POST[$q->id] . ', '; if ($_POST[$q->id] == $q->answer) { $user_score->score++; } } if (!UserScore::findOne(['user_id' => Yii::$app->user->identity->id, 'test_id' => $model->id, 'test_type' => 1])) { $user_score->save(); Yii::$app->session->setFlash('TestChecked'); } else { $score = UserScore::findOne(['user_id' => Yii::$app->user->identity->id, 'test_id' => $model->id, 'test_type' => 1]); $score->score = $user_score->score; $score->save(); Yii::$app->session->setFlash('TestChecked'); } } return $this->render('view', ['model' => $model, 'questions' => $questions]); }
/** * Finds the UserScore model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return UserScore the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = UserScore::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }