예제 #1
0
 /**
  * Creates a new UserScore model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new UserScore();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
예제 #2
0
 /**
  * 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]);
 }