如果“结果”达到2,我试图将考试标记为“ is_complete”。当用户在完成测试并计算结果后提交表格时,我想这样做。两个变量都在同一表中。是否可以通过PHP或我需要使用Java脚本。
这是我尝试制定代码的方式。
在模型上
public function answers()
{
return $this->hasMany('App\ExamResultsAnswers');
}
public function passed()
{
$instance = new ExamResult;
$instance->result < 2;
$var = ExamResult::where('result', '>=', 2)->get();
$var_is_greater_than_two = ($var >= 2 ? true : false);
$condition = new ExamResult;
$condition->is_complete ='1';
if ($this->compare($instance, $condition)) {
return $instance->$column == 1;
}
}
在控制器上
public function exam($course_id, Request $request)
{
$course = Course::where('id', $course_id)->firstOrFail();
$answers = [];
$exam_score = 0;
foreach ($request->get('question') as $question_id => $answer_id) {
$question = ExamQuestion::find($question_id);
$correct_answer = ExamOption::where('exam_question_id', $question_id)
->where('id', $answer_id)
->where('is_correct', 1)->count() > 0;
$answers[] = [
'exam_question_id' => $question_id,
'exam_option_id' => $answer_id,
'corect' => $correct_answer
];
if ($correct_answer) {
$exam_score += $question->score;
}
}
$exam_result = ExamResult::create([
'exam_id' => $course->exam->id,
'employee_id' => \Auth::id(),
'result' => $exam_score,
]);
$exam_result->answers()->createMany($answers);
$exam_result->passed();
return redirect()->route('learn.show', [$course, $request])->with('message', 'Test score: ' . $exam_score);
}
控制器应该执行以下操作
答案 0 :(得分:4)
使用以下代码将考试标记为已完成:
$qbProjects->andWhere($qbProjects->expr()->in('p.categories', [1,2,3]));
让我知道我是否误解了要求。
另一个优化的解决方案
$exam_result = ExamResult::create([
'exam_id' => $course->exam->id,
'employee_id' => \Auth::id(),
'result' => $exam_score,
]);
$exam_result->answers()->createMany($answers);
if($exam_result->result > 2) {
$exam_result->is_complete = 1;
$exam_result->save();
}