public function update(Quiz $quiz, Question $question, QuestionRequest $request) { if ($request['correct_answer_id']) { $ca = CorrectAnswer::whereQuestionId($question->id)->first(); if ($ca) { $ca->update(['option_id' => $request['correct_answer_id']]); } else { CorrectAnswer::create(['question_id' => $question->id, 'option_id' => $request['correct_answer_id']]); } } $question->update($request->all()); }
public function store(Quiz $quiz, Request $request) { $student_id = Auth::user()->student()->first()->id; if (Score::whereQuizIdAndStudentId($quiz->id, $student_id)) { return "e"; } $points = 0; foreach ($request->all() as $r) { $ca = CorrectAnswer::whereQuestionId($r['question_id'])->first()->option_id; if ($ca == $r['option_id']) { $points++; } if (!Answer::whereStudentIdAndQuestionId($student_id, $r['question_id'])->first()) { Answer::create(['student_id' => $student_id, 'question_id' => $r['question_id'], 'option_id' => $r['option_id']]); } } $score = Score::create(['score' => $points, 'time' => time() - strtotime($quiz->date_time), 'quiz_id' => $quiz->id, 'student_id' => $student_id]); DB::table('quiz_student')->insert(['student_id' => $student_id, 'quiz_id' => $quiz->id]); return $score; }