So, I have created a quiz with the answer options a, b, c and I want to get an answer from that option and save it to the database. How to save in the database so that the answer from the first try of the quiz is different from the second trial answer from the quiz, so that I can get the answer and call in the answer to be calculate in score function of the quiz?
what i have tried :
in my controller
public function score($id){
$user_login = Auth::user()->id;
$answers = Answer::select('user_answer')->where('jenis_quiz_id','=',$id)->where('user_id','=',$user_login)->get();//get the answer
unset($a);
unset($b);
unset($c);
$a = 0;
$b = 0;
$c = 0;
if($answers->count()) {
foreach ($answers as $answer) {
switch ($answer->user_answer) {
case 1:
$a++;
break;
case 2:
$b++;
break;
case 3:
$c++;
break;
}
}
return $a+$b+$c;
}
public function getShowResultOfQuiz($id){
$categoryquiz= JenisQuiz::findOrFail($id);
$user= Auth::user()->id;
// $instansi = Auth::user()->instansi;
// $kelas = Auth::user()->kelas;
$score= $this->score($id);
$kelas = Auth::user()->kelas;
$instansi = Auth::user()->instansi;
History::create([
'user_id'=>$user,
'jenis_quiz_id'=>$id,
'score'=> $score,
'kelas' => $kelas,
'instansi' => $instansi
]);
// $time_taken = date("H:i:s", strtotime(Answer::whereJenisQuizId($id)->orderBy('id', 'desc')->first()->time_taken));
switch ($id) {
case '1':
return view('user.pages.quizresult',compact('score','categoryquiz'));
break;
case '2':
return view('user.pages.quizanxietyresult',compact('score','categoryquiz'));
break;
case '3':
return view('user.pages.quizdepresiresult',compact('score','categoryquiz'));
break;
}
}
}
problem : can't distinguish answers from the first try of quiz trial and second try of quiz (from same quiz form but i taken it twice, and how to make the difference between it.
My code result above is score from first try added to the second try,how to not added it??????