$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; $examstudent->save(); foreach ($_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($sql)->one(); $answer->ExamStudentAnswerId = $max_id->ExamStudentAnswerId + 1; $answer->save(); } } //return $this->redirect(['index.php?r=site%2Ffeed']); ?> </div>
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']); }