/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ExamStudent::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['ExamStudentId' => $this->ExamStudentId, 'Score' => $this->Score, 'ExamId' => $this->ExamId, 'StudentId' => $this->StudentId]); return $dataProvider; }
<h1><?php echo Html::encode($this->title); ?> </h1> <?php $model = Exams::findOne($id); ?> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['Message', 'MaxScore', 'NumQuestions']]); ?> <?php $result = ExamStudent::findOne(['ExamId' => $id, 'StudentId' => Yii::$app->user->identity->UserID]); if ($result) { echo '<h3>Score: ' . $result->Score . '</h3>'; } else { echo '<h2>Questions</h2> <br />'; $i = 1; $questions = ExamQuestion::findAll(['ExamId' => $id]); $form = ActiveForm::begin(['method' => 'post', 'action' => 'index.php?r=exams%2Fsubmitexam']); foreach ($questions as $question) { echo '<p> #' . $i . '. ' . $question->Question . '</p>'; $choices = ExamQuestionChoices::findAll(['ExamQuestionId' => $question->ExamQuestionId]); foreach ($choices as $choice) { echo '<input type="radio" name="' . $question->ExamQuestionId . '" id="' . $choice->ExamQuestionChoicesId . '" value="' . $choice->ExamQuestionChoicesId . '" /><label for="' . $choice->ExamQuestionChoicesId . '"> ' . $choice->ChoiceDescription . '</label><br />'; } echo '<br /><br />'; $i++;
/* @var $this yii\web\View */ /* @var $model app\models\Exams */ $this->title = 'Test'; $this->params['breadcrumbs'][] = ['label' => 'Exams', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> <div class="exams-view"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php $user = Yii::$app->user->identity; $examstudent = new ExamStudent(); $examstudent->ExamId = $_POST['ExamId']; $examstudent->StudentId = $user->UserID; $sql = "SELECT MAX(ExamStudentId) AS ExamStudentId FROM exam_student"; $max_id = ExamStudent::findBySql($sql)->one(); $examstudent->ExamStudentId = $max_id->ExamStudentId + 1; $score = 0; foreach ($_POST as $name => $val) { if ($name != '_csrf' && $name != 'ExamId') { $choice = ExamQuestionChoices::findOne($val); if ($choice->IsRightChoice == 1) { $score++; } } } $examstudent->Score = $score;
public function actionSubmitexam() { $user = Yii::$app->user->identity; $examstudent = new ExamStudent(); $examstudent->ExamId = $_POST['ExamId']; $examstudent->StudentId = $user->UserID; $sql = "SELECT MAX(ExamStudentId) AS ExamStudentId FROM exam_student"; $max_id = ExamStudent::findBySql($sql)->one(); $examstudent->ExamStudentId = $max_id->ExamStudentId + 1; $score = 0; foreach (Yii::$app->request->post() as $name => $val) { if ($name != '_csrf' && $name != 'ExamId') { $choice = ExamQuestionChoices::findOne($val); if ($choice->IsRightChoice == 1) { $score++; } } } $examstudent->Score = $score; $examstudent->save(); foreach (Yii::$app->request->post() as $name => $val) { if ($name != '_csrf' && $name != 'ExamId') { $answer = new ExamStudentAnswer(); $answer->ExamQuestionChoiceId = $val; $answer->UserId = $user->UserID; $sql2 = "SELECT MAX(ExamStudentAnswerId) AS ExamStudentAnswerId FROM exam_student_answer"; $max_id2 = ExamStudentAnswer::findBySql($sql2)->one(); $answer->ExamStudentAnswerId = $max_id2->ExamStudentAnswerId + 1; $answer->save(); } } return $this->redirect(['/site/feed']); }