/** * 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]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = UserScore::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'exam_id' => $this->exam_id, 'score' => $this->score, 'finished_time' => $this->finished_time]); return $dataProvider; }
/** * 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.'); } }
use yii\widgets\ActiveForm; use yii\widgets\ListView; use yii\data\ActiveDataProvider; use app\models\Question; use app\models\Subject; use app\models\UserScore; use app\models\HardQuestion; /* @var $this yii\web\View */ /* @var $model app\models\SubjectTest */ $this->title = 'Тест № ' . $model->id; $this->params['breadcrumbs'][] = ['label' => 'Тест по предметам', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; $count = 1; $isFirst = true; $subjects = Subject::find()->where(['type' => 1, 'section' => $model->section])->all(); $user_score = UserScore::find()->where(['user_id' => Yii::$app->user->identity->id, 'test_id' => $model->id, 'test_type' => 1])->one(); ?> <div class="subject-test-view"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php if (Yii::$app->session->hasFlash('TestChecked')) { ?> <div class="alert alert-success"> Ваш результат: <?php if ($user_score->score) {