Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public function quizCheckAnswer($quiz_id, Request $request) {
$userdata = Cektoken::cek($request->token);
$data = [];
//Cek Quiz
$quiz = \App\Models\Quiz::find($quiz_id);
//Quiz tidak ditemukan
if (! $quiz) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Quiz tidak valid',404]);
}
//Asumsikan user belum pernah mengikuti kuis
$didParticipated = false;
$didFinished = false;
$currentScore = 0;
//Pertanyaan aktif adalah pertanyaan quiz yang pertama
$currentQuestion = \App\Models\Question::where('quiz_id', $quiz->id)->first()->id;
//Apakah user pernah mengikuti quiz ini?
$myResult = \App\Models\QuizResult::where('user_id', $userdata['data']['id'])->where('quiz_id', $quiz_id)->first();
//User pernah mengikuti
if (count($myResult) > 0) {
$didParticipated = true;
//Apakah user telah menjawab semua pertanyaan yang ada?
if ($myResult->is_finished == 'y') {
$didFinished = true;
$currentScore = $myResult->result;
//Tampilkan pesan dan hasil yang telah dicapai
$data['my-score'] = $currentScore;
return response()->json(['status' => false, 'data' => $data, 'message' => 'Anda sudah menyelesaikan quiz ini',404]);
}else{
$didFinished = false;
//User pernah mengikuti tapi belum menjawab semua pertanyaan
//Definisikan Hasil terakhir user
$currentScore = $myResult->result;
//Pertanyaan yang seharusnya dijawab oleh user
$currentQuestion = $myResult->current_question;
//Result adalah record yang sudah ada
$result = $myResult;
}
}else{
//User belum pernah mengikuti quiz ini
//Definisikan Quiz Result
$result = new \App\Models\QuizResult();
}
//User tidak mengirimkan data yang benar
if (!is_array($request->question_id)) {
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Anda mengirimkan jawaban yang tidak . Harap kirim response dalam bentuk array untuk masing-masing question \'question_id\'[id] => \'answer\''], 400);
}
//Check the answer
foreach ($request->question_id as $k => $v) {
//
if ($didParticipated) {
$currentQuestion = $myResult->current_question+1;
}else{
$currentQuestion = \App\Models\Question::where('id', $k)->first()->id;
}
if ($currentQuestion !== $k){
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Pertanyaan tidak valid. Anda mengirimkan jawaban yang tidak . Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400);
}
//Answer Correction
$question = \App\Models\Question::where('id', $currentQuestion)->first();
if ($question->correct_key == $request->question_id[$k]){
//Correct
$currentScore++;
}
$nextQuestion = \App\Models\Question::where('quiz_id', $quiz->id)->where('id', '>', $k)->first();
}
//Save the result
$result->quiz_id = $quiz->id;
$result->user_id = $userdata['data']['id'];
$result->question_count = \App\Models\Question::where('quiz_id', $quiz->id)->count();
$result->result = $currentScore;
if (count($nextQuestion) > 0) {
$data['next_question']['id'] = $nextQuestion->id;
$data['next_question']['type'] = $nextQuestion->type;
$data['next_question']['text'] = $nextQuestion->text;
if ($nextQuestion->type == 'pg') {
$data['next_question']['option_1'] = $nextQuestion->pil_a;
$data['next_question']['option_2'] = $nextQuestion->pil_b;
$data['next_question']['option_3'] = $nextQuestion->pil_c;
$data['next_question']['option_4'] = $nextQuestion->pil_d;
$data['next_question']['option_5'] = $nextQuestion->pil_e;
$data['next_question']['option_6'] = $nextQuestion->pil_f;
}else{
$data['next_question']['option_1'] = $nextQuestion->pil_a;
$data['next_question']['option_2'] = $nextQuestion->pil_b;
}
$result->is_finished = 'n';
}else{
$result->is_finished = 'y';
}
$result->current_question = $currentQuestion+1;
$result->save();
return response()->json(['status' => true, 'data' => $data, 'message' => ''], 200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.