$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++; } echo '<input type="hidden" name="ExamId" value="' . $model->ExamId . '">'; ?> <div class="form-group"> <?php
/** * Finds the ExamQuestion model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Posts the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ExamQuestion::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getExamQuestions() { return $this->hasMany(ExamQuestion::className(), ['ExamId' => 'ExamId']); }
public function actionAddquestions() { $curModel = null; foreach (Yii::$app->request->post() as $name => $val) { echo htmlspecialchars($name . ': ' . $val) . "<br />"; $model = null; if (substr($name, 0, 8) == 'question') { $model = new ExamQuestion(); $model->Question = $val; $model->ExamId = $_POST['ExamId']; $sql = "SELECT MAX(ExamQuestionId) AS ExamQuestionId FROM exam_question"; $max_id = ExamQuestion::findBySql($sql)->one(); $model->ExamQuestionId = $max_id->ExamQuestionId + 1; $model->save(); $curModel = $model; } else { if (substr($name, 0, 10) == 'choicedesc') { $model = new ExamQuestionChoices(); $model->ChoiceDescription = $val; $model->IsRightChoice = $_POST['isrightchoice' . substr($name, 10, 12)]; $model->ExamQuestionId = $curModel->ExamQuestionId; $sql = "SELECT MAX(ExamQuestionChoicesId) AS ExamQuestionChoicesId FROM exam_question_choices"; $max_id = ExamQuestionChoices::findBySql($sql)->one(); $model->ExamQuestionChoicesId = $max_id->ExamQuestionChoicesId + 1; $model->save(); } } } return $this->redirect(['/site/feed']); }