diff --git a/src/Models/QuizAttempt.php b/src/Models/QuizAttempt.php index 893ad79..06c633b 100644 --- a/src/Models/QuizAttempt.php +++ b/src/Models/QuizAttempt.php @@ -77,7 +77,10 @@ public static function get_score_for_type_1_question(QuizAttempt $quizAttempt, Q $negative_marks = self::get_negative_marks_for_question($quiz, $quizQuestion); if (!empty($correct_answer)) { if (count($quizQuestionAnswers)) { - return $quizQuestionAnswers[0]->question_option_id == $correct_answer ? $quizQuestion->marks : -($negative_marks); // Return marks in case of correct answer else negative marks + if (is_array($quizQuestionAnswers)) { + return $quizQuestionAnswers[0]->question_option_id == $correct_answer ? $quizQuestion->marks : -($negative_marks); // Return marks in case of correct answer else negative marks + } + return $quizQuestionAnswers->first()->question_option_id == $correct_answer ? $quizQuestion->marks : -($negative_marks); // Return marks in case of correct answer else negative marks } return $quizQuestion->is_optional ? 0 : -$negative_marks; // If the question is optional, then the negative marks will be 0 } diff --git a/tests/Unit/QuizAttemptTest.php b/tests/Unit/QuizAttemptTest.php index 099dde7..02f481f 100644 --- a/tests/Unit/QuizAttemptTest.php +++ b/tests/Unit/QuizAttemptTest.php @@ -571,19 +571,57 @@ public function test_quiz_attempt_validate() $questionsWithOptions = [ [ 'question' => 'How many world wonders are there?', - 'answer' => 7, + 'options' => [ + [1, 7, true], + ], 'id' => 1, + 'question_type' => 3, ], [ 'question' => 'What is the biggest desert in the world?', - 'answer' => 'Sahara', + 'options' => [[2, 'Sahara', true]], 'id' => 2, + 'question_type' => 3, ], [ 'question' => 'What is the biggest bird?', - 'answer' => 'Ostrich', + 'options' => [[3, 'Ostrich', true]], 'id' => 3, + 'question_type' => 3, ], + [ + 'question' => 'Which One of these is not a continent?', + 'options' => [ + [4, 'US', true], [5, 'Asia', false], [6, 'Europe', false], [7, 'Australia', false], + ], + 'id' => 4, + 'question_type' => 1, + ], + [ + 'question' => 'Which of the following is a non metal that remains liquid at room temperature?', + 'options' => [ + [8, 'Phosphorous', false], [9, 'Bromine', true], [10, 'Chlorine', false], [11, 'Helium', false], + ], + 'id' => 5, + 'question_type' => 1, + ], + [ + 'question' => 'Select All The Mammals', + 'options' => [ + [12, 'cats', true], [13, 'Dogs', true], [14, 'apes', true], [15, 'None of the above', false], + ], + 'id' => 6, + 'question_type' => 2, + ], + [ + 'question' => 'Select All The Amphibians', + 'options' => [ + [16, 'frogs', true], [17, 'Dogs', false], [18, 'salamanders', true], [19, 'None of the above', false], + ], + 'id' => 7, + 'question_type' => 2, + ], + ]; $quiz = Quiz::factory()->make()->create([ 'name' => 'Sample Quiz', @@ -607,16 +645,19 @@ public function test_quiz_attempt_validate() foreach ($questionsWithOptions as $questionsWithOption) { Question::factory()->create([ 'name' => $questionsWithOption['question'], - 'question_type_id' => 3, + 'question_type_id' => $questionsWithOption['question_type'], 'is_active' => true, 'id' => $questionsWithOption['id'], ]); - QuestionOption::factory()->create([ - 'question_id' => $questionsWithOption['id'], - 'name' => $questionsWithOption['answer'], - 'is_correct' => true, - 'id' => $questionsWithOption['id'], - ]); + foreach ($questionsWithOption['options'] as $option) { + QuestionOption::factory()->create([ + 'question_id' => $questionsWithOption['id'], + 'name' => $option[1], + 'is_correct' => $option[2], + 'id' => $option[0], + ]); + } + QuizQuestion::factory()->create([ 'quiz_id' => $quiz->id, 'question_id' => $questionsWithOption['id'], @@ -625,67 +666,161 @@ public function test_quiz_attempt_validate() 'negative_marks' => 0.5, 'id' => $questionsWithOption['id'], ]); - QuizAttemptAnswer::create( - [ - 'quiz_attempt_id' => $quizAttemptOne->id, - 'quiz_question_id' => $questionsWithOption['id'], - 'question_option_id' => $questionsWithOption['id'], - 'answer' => $questionsWithOption['answer'], - ] - ); - if ($questionsWithOption['id'] != 3) { //Skipping the third question being attempted - QuizAttemptAnswer::create( - [ - 'quiz_attempt_id' => $quizAttemptTwo->id, - 'quiz_question_id' => $questionsWithOption['id'], - 'question_option_id' => $questionsWithOption['id'], - 'answer' => $questionsWithOption['answer'] . 's', - ] - ); + foreach ($questionsWithOption['options'] as $option) { + if ($option[2]) { + QuizAttemptAnswer::create( + [ + 'quiz_attempt_id' => $quizAttemptOne->id, + 'quiz_question_id' => $questionsWithOption['id'], + 'question_option_id' => $option[0], + 'answer' => $option[1], + ] + ); + if ($questionsWithOption['id'] != 3) { //Skipping the third question being attempted + QuizAttemptAnswer::create( + [ + 'quiz_attempt_id' => $quizAttemptTwo->id, + 'quiz_question_id' => $questionsWithOption['id'], + 'question_option_id' => $option[0], + 'answer' => $option[1] . 's', + ] + ); + } + + } + } } $this->assertEquals([ 1 => [ - 'score' => 1, - 'is_correct' => true, - 'correct_answer' => '7', + 'score' => 1.0, + "is_correct" => true, + 'correct_answer' => 7, 'user_answer' => 7, ], + 2 => [ - 'score' => 1, + 'score' => 1.0, 'is_correct' => true, 'correct_answer' => 'Sahara', 'user_answer' => 'Sahara', ], + 3 => [ - 'score' => 1, + 'score' => 1.0, 'is_correct' => true, 'correct_answer' => 'Ostrich', 'user_answer' => 'Ostrich', ], + + 4 => [ + + 'score' => 1, + 'is_correct' => true, + 'correct_answer' => 'US', + 'user_answer' => 'US', + ], + + 5 => [ + 'score' => 1, + 'is_correct' => true, + 'correct_answer' => 'Bromine', + 'user_answer' => 'Bromine', + ], + + 6 => [ + 'score' => 1, + 'is_correct' => true, + 'correct_answer' => [ + 0 => 'cats', + 1 => 'Dogs', + 2 => 'apes', + ], + + 'user_answer' => [ + 0 => 'cats', + 1 => 'Dogs', + 2 => 'apes', + ], + + ], + + 7 => [ + 'score' => 1, + 'is_correct' => true, + 'correct_answer' => [ + 0 => 'frogs', + 1 => 'salamanders', + ], + + 'user_answer' => [ + 0 => 'frogs', + 1 => 'salamanders', + ], + ], + ], $quizAttemptOne->validate(), 'Quiz Attempt With Correct Answers'); - $this->assertEquals([ - 1 => [ - 'score' => 0, + + $this->assertEquals(array( + 1 => array( + 'score' => -0, 'is_correct' => false, - 'correct_answer' => '7', + 'correct_answer' => 7, 'user_answer' => '7s', - ], - 2 => [ - 'score' => 0, + ), + 2 => array( + 'score' => -0, 'is_correct' => false, 'correct_answer' => 'Sahara', 'user_answer' => 'Saharas', - ], - 3 => [ - 'score' => 0, + ), + 3 => array( + 'score' => -0, 'is_correct' => false, 'correct_answer' => 'Ostrich', - 'user_answer' => null, - ], - ], $quizAttemptTwo->validate(), 'Quiz Attempt With Wrong Answers'); + 'user_answer' => '', + ), + 4 => array( + 'score' => 1, + 'is_correct' => true, + 'correct_answer' => 'US', + 'user_answer' => 'US', + ), + 5 => array( + 'score' => 1, + 'is_correct' => true, + 'correct_answer' => 'Bromine', + 'user_answer' => 'Bromine', + ), + 6 => array( + 'score' => 1, + 'is_correct' => true, + 'correct_answer' => array( + 0 => 'cats', + 1 => 'Dogs', + 2 => 'apes', + ), + 'user_answer' => array( + 0 => 'cats', + 1 => 'Dogs', + 2 => 'apes', + ), + ), + 7 => array( + 'score' => 1, + 'is_correct' => true, + 'correct_answer' => array( + 0 => 'frogs', + 1 => 'salamanders', + ), + 'user_answer' => array( + 0 => 'frogs', + 1 => 'salamanders', + ), + ), + ), $quizAttemptTwo->validate(), 'Quiz Attempt With Wrong Answers'); } }