Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Models/QuizAttempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
225 changes: 180 additions & 45 deletions tests/Unit/QuizAttemptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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'],
Expand All @@ -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');

}
}