From c32eedee364ec8f3affc8a1217521c23d59db35b Mon Sep 17 00:00:00 2001 From: mkassaei Date: Wed, 15 Nov 2023 15:18:05 +0000 Subject: [PATCH] Matrix: Add the get_num_correct_choices function #739047 --- question.php | 13 +++++++++++++ tests/question_multiple_test.php | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/question.php b/question.php index 8bbd2a7..1bb5717 100644 --- a/question.php +++ b/question.php @@ -462,4 +462,17 @@ public function get_num_selected_choices(array $response): int { } return $numselected; } + + /** + * Returns the count of correct answers for the question. + * + * @return int the number of choices that are correct. + */ + public function get_num_correct_choices(): int { + $numcorrect = 0; + foreach ($this->rows as $row) { + $numcorrect += count($row->correctanswers); + } + return $numcorrect; + } } diff --git a/tests/question_multiple_test.php b/tests/question_multiple_test.php index c02bb7c..dab1c8d 100644 --- a/tests/question_multiple_test.php +++ b/tests/question_multiple_test.php @@ -354,4 +354,11 @@ public function test_get_num_parts_grade_partial(): void { // Two responsess are given and both are correct (Second and third row have not been answered). $this->assertEquals([2, 7], $question->get_num_parts_grade_partial(['rowanswers0_1' => '1', 'rowanswers0_3' => '1'])); } + + public function test_get_num_correct_choices(): void { + $question = \test_question_maker::make_question('oumatrix', 'food_multiple'); + + // Correct number of choices are expected. + $this->assertEquals(7, $question->get_num_correct_choices()); + } }