diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index 339f0f9..2b491db 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -46,7 +46,7 @@ class provider implements * @param collection $collection The initialised collection to add items to. * @return collection A listing of user data stored through this system. */ - public static function get_metadata(collection $collection) : collection { + public static function get_metadata(collection $collection): collection { $collection->add_user_preference('qtype_oumatrix_defaultmark', 'privacy:preference:defaultmark'); $collection->add_user_preference('qtype_oumatrix_penalty', 'privacy:preference:penalty'); $collection->add_user_preference('qtype_oumatrix_inputtype', 'privacy:preference:inputtype'); diff --git a/question.php b/question.php index cd13208..3b23e13 100644 --- a/question.php +++ b/question.php @@ -89,6 +89,7 @@ public function apply_attempt_state(question_attempt_step $step) { /** * Returns the roworder of the question being displayed. + * * @param question_attempt $qa * @return array|null */ @@ -182,6 +183,36 @@ public function has_specific_feedback(): bool { } return false; } + + /** + * Return a colun object. + * + * @param int $number + * @return object column|null + */ + protected function get_column_by_number(int $number): ?qtype_oumatrix\column { + foreach ($this->columns as $column) { + if ($column->number == $number) { + return $column; + } + } + return null; + } + + /** + * Return a row object. + * + * @param int $rowid + * @return row|null + */ + protected function get_row_by_id(int $rowid): ?qtype_oumatrix\row { + foreach ($this->rows as $row) { + if ($row->id == $rowid) { + return $row; + } + } + return null; + } } /** @@ -269,6 +300,56 @@ public function is_complete_response(array $response): bool { return true; } + public function classify_response(array $response): array { + $selectedchoices = []; + foreach ($this->roworder as $key => $rowid) { + $fieldname = $this->field($key); + if (array_key_exists($fieldname, $response) && $response[$fieldname]) { + $selectedchoices[$rowid] = $response[$fieldname]; + } else { + $selectedchoices[$rowid] = 0; + } + } + + $choices = []; + foreach ($selectedchoices as $rowid => $colnumber) { + if ($selectedchoices[$rowid] === 0) { + $choices[$rowid] = question_classified_response::no_response(); + continue; + } + $column = $this->get_column_by_number($colnumber); + if (in_array($colnumber, array_keys($this->rows[$rowid]->correctanswers))) { + $fraction = 1; + } else { + $fraction = 0; + } + $choices[$rowid] = new question_classified_response($column->id, $column->name, $fraction); + } + return $choices; + } + + public function prepare_simulated_post_data($simulatedresponse): array { + $postdata = []; + $subquestions = array_keys($simulatedresponse); + $answers = array_values($simulatedresponse); + + foreach ($this->roworder as $key => $rowid) { + $row = $this->rows[$rowid]; + if ($row->name !== $subquestions[$key]) { + continue; + } + if ($key === ($row->number - 1) && $row->name === $subquestions[$key]) { + foreach ($this->columns as $colid => $column) { + if ($column->name !== $answers[$key]) { + continue; + } + $postdata[$this->field($key)] = $column->number; + } + } + } + return $postdata; + } + public function grade_response(array $response): array { // Retrieve the number of right responses and the total number of responses. [$numrightparts, $total] = $this->get_num_parts_right($response); @@ -388,6 +469,51 @@ public function is_complete_response(array $response): bool { return true; } + public function classify_response(array $response) { + $selectedchoices = []; + foreach ($response as $responsekey => $responsevalue) { + preg_match('/([\d+])_([\d+])/', $responsekey, $matches); + $rowid = $this->roworder[$matches[1]]; + $colnumber = $matches[2]; + $column = $this->get_column_by_number($colnumber); + $row = $this->get_row_by_id($rowid); + $rowcorrectanswers = $this->rows[$rowid]->correctanswers; + if ($responsevalue && in_array($colnumber, array_keys($rowcorrectanswers))) { + $fraction = 1 / count($rowcorrectanswers); + } else { + $fraction = 0; + } + $colid = $column->id; + $selectedchoices["$rowid-$colid"] = new question_classified_response( + $column->id, $row->name . ': ' . $column->name, $fraction); + } + return $selectedchoices; + } + + public function prepare_simulated_post_data($simulatedresponse): array { + $postdata = []; + $subquestions = array_keys($simulatedresponse); + $answers = array_values($simulatedresponse); + foreach ($this->roworder as $key => $rowid) { + $row = $this->rows[$rowid]; + $rowanswers = $answers[$key]; + if ($key === ($row->number - 1) && $row->name === $subquestions[$key]) { + foreach ($this->columns as $colid => $column) { + // Set the field to '0' initially. + $postdata[$this->field($key, $column->number)] = '0'; + foreach ($rowanswers as $colnumber => $colname) { + if ($row->name === $subquestions[$key] && + $column->number === $colnumber && $column->name === $colname) { + // Set the field to '1' if it has been ticked.. + $postdata[$this->field($key, $column->number)] = '1'; + } + } + } + } + } + return $postdata; + } + public function grade_response(array $response): array { // Retrieve the number of right responses and the total number of responses. if ($this->grademethod == 'allnone') { diff --git a/questiontype.php b/questiontype.php index 0af0711..27973bc 100644 --- a/questiontype.php +++ b/questiontype.php @@ -316,30 +316,24 @@ public function get_num_correct_choices(stdClass $questiondata): int { } public function get_possible_responses($questiondata) { - if ($questiondata->options->single) { - $responses = []; - - // TODO: Sort out this funtion to work with rows and columns, etc. - foreach ($questiondata->options->answers as $aid => $answer) { - $responses[$aid] = new question_possible_response( - question_utils::to_plain_text($answer->answer, $answer->answerformat), - $answer->fraction); + $question = $this->make_question_instance($questiondata); + $this->initialise_question_instance($question, $questiondata); + $subqs = []; + $responses = []; + foreach ($question->rows as $rowid => $row) { + foreach ($question->columns as $colid => $col) { + $responseclass = $question->html_to_text($row->name . ': ' . $col->name, FORMAT_PLAIN); + if (in_array($col->number, array_keys($row->correctanswers))) { + $fraction = 1; + } else { + $fraction = 0; + } + $responses[$colid] = new question_possible_response($responseclass, $fraction); } - $responses[null] = question_possible_response::no_response(); - return [$questiondata->id => $responses]; - } else { - $parts = []; - - foreach ($questiondata->options->answers as $aid => $answer) { - $parts[$aid] = [ - $aid => new question_possible_response(question_utils::to_plain_text( - $answer->answer, $answer->answerformat), $answer->fraction), - ]; - } - - return $parts; + $subqs[$rowid - 1] = $responses; } + return $subqs; } public function import_from_xml($data, $question, qformat_xml $format, $extra = null) { diff --git a/tests/helper.php b/tests/helper.php index 0d13274..1ba26b1 100644 --- a/tests/helper.php +++ b/tests/helper.php @@ -88,7 +88,7 @@ public function get_oumatrix_question_data_animals_single(): stdClass { 'name' => 'Birds', ], 14 => (object) [ - 'id' => 13, + 'id' => 14, 'number' => 4, 'name' => 'Mammals', ], diff --git a/tests/question_multiple_test.php b/tests/question_multiple_test.php index dab1c8d..190e3d1 100644 --- a/tests/question_multiple_test.php +++ b/tests/question_multiple_test.php @@ -18,6 +18,7 @@ use question_attempt_step; use question_state; +use question_classified_response; defined('MOODLE_INTERNAL') || die(); @@ -100,6 +101,112 @@ public function test_is_gradable_response(): void { $this->assertTrue($question->is_gradable_response($response), $question->is_complete_response($response)); } + public function test_classify_response_multiple(): void { + $this->resetAfterTest(); + $question = \test_question_maker::make_question('oumatrix', 'food_multiple'); + $question->shuffleanswers = 0; + $question->start_attempt(new question_attempt_step(), 1); + + $response = $question->prepare_simulated_post_data([ + 'Proteins' => [1 => 'Chicken breast', 3 => 'Salmon fillet', 6 => 'Steak'], + 'Vegetables' => [2 => 'Carrot', 4 => 'Asparagus', 7 => 'Potato'], + 'Fats' => [5 => 'Olive oil']]); + $this->assertEquals([ + "21-21" => new question_classified_response(21, 'Proteins: Chicken breast', 1 / 3), + "21-22" => new question_classified_response(22, 'Proteins: Carrot', 0), + "21-23" => new question_classified_response(23, 'Proteins: Salmon fillet', 1 / 3), + "21-24" => new question_classified_response(24, 'Proteins: Asparagus', 0), + "21-25" => new question_classified_response(25, 'Proteins: Olive oil', 0), + "21-26" => new question_classified_response(26, 'Proteins: Steak', 1 / 3), + "21-27" => new question_classified_response(27, 'Proteins: Potato', 0), + + "22-21" => new question_classified_response(21, 'Vegetables: Chicken breast', 0), + "22-22" => new question_classified_response(22, 'Vegetables: Carrot', 1 / 3), + "22-23" => new question_classified_response(23, 'Vegetables: Salmon fillet', 0), + "22-24" => new question_classified_response(24, 'Vegetables: Asparagus', 1 / 3), + "22-25" => new question_classified_response(25, 'Vegetables: Olive oil', 0), + "22-26" => new question_classified_response(26, 'Vegetables: Steak', 0), + "22-27" => new question_classified_response(27, 'Vegetables: Potato', 1 / 3), + + "23-21" => new question_classified_response(21, 'Fats: Chicken breast', 0), + "23-22" => new question_classified_response(22, 'Fats: Carrot', 0), + "23-23" => new question_classified_response(23, 'Fats: Salmon fillet', 0), + "23-24" => new question_classified_response(24, 'Fats: Asparagus', 0), + "23-25" => new question_classified_response(25, 'Fats: Olive oil', 1), + "23-26" => new question_classified_response(26, 'Fats: Steak', 0), + "23-27" => new question_classified_response(27, 'Fats: Potato', 0), + + ], $question->classify_response($response)); + + $response = $question->prepare_simulated_post_data([ + 'Proteins' => [1 => 'Chicken breast', 3 => 'Salmon fillet'], + 'Vegetables' => [2 => 'Carrot', 7 => 'Potato'], + 'Fats' => [5 => 'Olive oil']]); + $this->assertEquals([ + "21-21" => new question_classified_response(21, 'Proteins: Chicken breast', 1 / 3), + "21-22" => new question_classified_response(22, 'Proteins: Carrot', 0), + "21-23" => new question_classified_response(23, 'Proteins: Salmon fillet', 1 / 3), + "21-24" => new question_classified_response(24, 'Proteins: Asparagus', 0), + "21-25" => new question_classified_response(25, 'Proteins: Olive oil', 0), + "21-26" => new question_classified_response(26, 'Proteins: Steak', 0), + "21-27" => new question_classified_response(27, 'Proteins: Potato', 0), + + "22-21" => new question_classified_response(21, 'Vegetables: Chicken breast', 0), + "22-22" => new question_classified_response(22, 'Vegetables: Carrot', 1 / 3), + "22-23" => new question_classified_response(23, 'Vegetables: Salmon fillet', 0), + "22-24" => new question_classified_response(24, 'Vegetables: Asparagus', 0), + "22-25" => new question_classified_response(25, 'Vegetables: Olive oil', 0), + "22-26" => new question_classified_response(26, 'Vegetables: Steak', 0), + "22-27" => new question_classified_response(27, 'Vegetables: Potato', 1 / 3), + + "23-21" => new question_classified_response(21, 'Fats: Chicken breast', 0), + "23-22" => new question_classified_response(22, 'Fats: Carrot', 0), + "23-23" => new question_classified_response(23, 'Fats: Salmon fillet', 0), + "23-24" => new question_classified_response(24, 'Fats: Asparagus', 0), + "23-25" => new question_classified_response(25, 'Fats: Olive oil', 1), + "23-26" => new question_classified_response(26, 'Fats: Steak', 0), + "23-27" => new question_classified_response(27, 'Fats: Potato', 0), + ], $question->classify_response($response)); + } + + public function test_prepare_simulated_post_data_multiple(): void { + $this->resetAfterTest(); + $question = \test_question_maker::make_question('oumatrix', 'food_multiple'); + $question->shuffleanswers = 0; + $question->start_attempt(new question_attempt_step(), 1); + + $response = ['Proteins' => [1 => 'Chicken breast', 3 => 'Salmon fillet', 6 => 'Steak'], + 'Vegetables' => [2 => 'Carrot', 4 => 'Asparagus', 7 => 'Potato'], 'Fats' => [5 => 'Olive oil']]; + + $expected = [ + 'rowanswers0_1' => '1', + 'rowanswers0_2' => '0', + 'rowanswers0_3' => '1', + 'rowanswers0_4' => '0', + 'rowanswers0_5' => '0', + 'rowanswers0_6' => '1', + 'rowanswers0_7' => '0', + + 'rowanswers1_1' => '0', + 'rowanswers1_2' => '1', + 'rowanswers1_3' => '0', + 'rowanswers1_4' => '1', + 'rowanswers1_5' => '0', + 'rowanswers1_6' => '0', + 'rowanswers1_7' => '1', + + 'rowanswers2_1' => '0', + 'rowanswers2_2' => '0', + 'rowanswers2_3' => '0', + 'rowanswers2_4' => '0', + 'rowanswers2_5' => '1', + 'rowanswers2_6' => '0', + 'rowanswers2_7' => '0', + ]; + $this->assertEquals($expected, $question->prepare_simulated_post_data($response)); + } + + public function test_is_same_response(): void { $question = \test_question_maker::make_question('oumatrix', 'food_multiple'); $question->start_attempt(new question_attempt_step(), 1); diff --git a/tests/question_single_test.php b/tests/question_single_test.php index 1b8325d..078f06d 100644 --- a/tests/question_single_test.php +++ b/tests/question_single_test.php @@ -17,6 +17,7 @@ namespace qtype_oumatrix; use question_attempt_step; use question_state; +use question_classified_response; defined('MOODLE_INTERNAL') || die(); @@ -60,6 +61,72 @@ public function test_is_gradable_response(): void { $this->assertEquals($question->is_gradable_response($response), $question->is_complete_response($response)); } + public function test_classify_response_single(): void { + $this->resetAfterTest(); + $question = \test_question_maker::make_question('oumatrix', 'animals_single'); + $question->shuffleanswers = 0; + $question->start_attempt(new question_attempt_step(), 1); + + // All sub-questions are answered correctly. + $response = $question->prepare_simulated_post_data( + ['Bee' => 'Insects', 'Salmon' => 'Fish', 'Seagull' => 'Birds', 'Dog' => 'Mammals']); + $this->assertEquals([ + 11 => new question_classified_response(11, 'Insects', 1), + 12 => new question_classified_response(12, 'Fish', 1), + 13 => new question_classified_response(13, 'Birds', 1), + 14 => new question_classified_response(14, 'Mammals', 1), + ], $question->classify_response($response)); + + // Three sub-questions are answered correctly and one incorrectly. + $response = $question->prepare_simulated_post_data( + ['Bee' => 'Insects', 'Salmon' => 'Birds', 'Seagull' => 'Birds', 'Dog' => 'Mammals']); + $this->assertEquals([ + 11 => new question_classified_response(11, 'Insects', 1), + 12 => new question_classified_response(13, 'Birds', 0), + 13 => new question_classified_response(13, 'Birds', 1), + 14 => new question_classified_response(14, 'Mammals', 1), + ], $question->classify_response($response)); + + // Two sub-questions are answered correctly and two incorrectly. + $response = $question->prepare_simulated_post_data( + ['Bee' => 'Insects', 'Salmon' => 'Birds', 'Seagull' => 'Birds', 'Dog' => 'Insects']); + $this->assertEquals([ + 11 => new question_classified_response(11, 'Insects', 1), + 12 => new question_classified_response(13, 'Birds', 0), + 13 => new question_classified_response(13, 'Birds', 1), + 14 => new question_classified_response(11, 'Insects', 0), + ], $question->classify_response($response)); + + // Two sub-questions are answered correctly, one incorrectly, and the second sub-question is not answered. + $response = $question->prepare_simulated_post_data( + ['Bee' => 'Insects', 'Salmon' => '', 'Seagull' => 'Birds', 'Dog' => 'Insects']); + $this->assertEquals([ + 11 => new question_classified_response(11, 'Insects', 1), + 12 => question_classified_response::no_response(), + 13 => new question_classified_response(13, 'Birds', 1), + 14 => new question_classified_response(11, 'Insects', 0), + ], $question->classify_response($response)); + } + + public function test_prepare_simulated_post_data_single(): void { + $this->resetAfterTest(); + $question = \test_question_maker::make_question('oumatrix', 'animals_single'); + $question->shuffleanswers = 0; + $question->start_attempt(new question_attempt_step(), 1); + + $response = ['Bee' => 'Insects', 'Salmon' => 'Fish', 'Seagull' => 'Birds', 'Dog' => 'Mammals']; + $expected = ['rowanswers0' => 1, 'rowanswers1' => 2, 'rowanswers2' => 3, 'rowanswers3' => 4]; + $this->assertEquals($expected, $question->prepare_simulated_post_data($response)); + + $response = ['Bee' => 'Insects', 'Salmon' => 'Birds', 'Seagull' => 'Birds', 'Dog' => 'Mammals']; + $expected = ['rowanswers0' => 1, 'rowanswers1' => 3, 'rowanswers2' => 3, 'rowanswers3' => 4]; + $this->assertEquals($expected, $question->prepare_simulated_post_data($response)); + + $response = ['Bee' => 'Insects', 'Salmon' => 'Birds', 'Seagull' => 'Birds', 'Dog' => 'Insects']; + $expected = ['rowanswers0' => 1, 'rowanswers1' => 3, 'rowanswers2' => 3, 'rowanswers3' => 1]; + $this->assertEquals($expected, $question->prepare_simulated_post_data($response)); + } + public function test_is_same_response(): void { $question = \test_question_maker::make_question('oumatrix'); $question->start_attempt(new question_attempt_step(), 1); diff --git a/tests/questiontype_test.php b/tests/questiontype_test.php index 1634e89..050b3b1 100644 --- a/tests/questiontype_test.php +++ b/tests/questiontype_test.php @@ -19,10 +19,13 @@ use qtype_oumatrix; use qtype_oumatrix_edit_form; use qtype_oumatrix_test_helper; +use question_bank; +use question_possible_response; defined('MOODLE_INTERNAL') || die(); global $CFG; + require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); require_once($CFG->dirroot . '/question/type/oumatrix/tests/helper.php'); require_once($CFG->dirroot . '/question/type/oumatrix/questiontype.php'); @@ -70,6 +73,88 @@ public function test_get_random_guess_score_broken_question(): void { $this->assertNull($this->qtype->get_random_guess_score($q)); } + public function test_get_possible_responses_single(): void { + $this->resetAfterTest(); + $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $category = $generator->create_question_category([]); + $createdquestion = $generator->create_question('oumatrix', 'animals_single', + ['category' => $category->id, 'name' => 'Test question']); + $q = question_bank::load_question_data($createdquestion->id); + $expected = [ + 0 => [ + 1 => new question_possible_response('Bee: Insects', 1), + 2 => new question_possible_response('Bee: Fish', 0), + 3 => new question_possible_response('Bee: Birds', 0), + 4 => new question_possible_response('Bee: Mammals', 0), + null => question_possible_response::no_response(), + ], + 1 => [ + 1 => new question_possible_response('Salmon: Insects', 0), + 2 => new question_possible_response('Salmon: Fish', 1), + 3 => new question_possible_response('Salmon: Birds', 0), + 4 => new question_possible_response('Salmon: Mammals', 0), + null => question_possible_response::no_response(), + ], + 2 => [ + 1 => new question_possible_response('Seagull: Insects', 0), + 2 => new question_possible_response('Seagull: Fish', 0), + 3 => new question_possible_response('Seagull: Birds', 1), + 4 => new question_possible_response('Seagull: Mammals', 0), + null => question_possible_response::no_response(), + ], + 3 => [ + 1 => new question_possible_response('Dog: Insects', 0), + 2 => new question_possible_response('Dog: Fish', 0), + 3 => new question_possible_response('Dog: Birds', 0), + 4 => new question_possible_response('Dog: Mammals', 1), + null => question_possible_response::no_response(), + ], + ]; + $this->assertEquals($expected, $this->qtype->get_possible_responses($q)); + } + + public function test_get_possible_responses_multiple(): void { + $this->resetAfterTest(); + $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); + $category = $generator->create_question_category([]); + $createdquestion = $generator->create_question('oumatrix', 'food_multiple', + ['category' => $category->id, 'name' => 'Test question']); + $q = question_bank::load_question_data($createdquestion->id); + $expected = [ + 0 => [ + 1 => new question_possible_response('Proteins: Chicken breast', 1), + 2 => new question_possible_response('Proteins: Carrot', 0), + 3 => new question_possible_response('Proteins: Salmon fillet', 1), + 4 => new question_possible_response('Proteins: Asparagus', 0), + 5 => new question_possible_response('Proteins: Olive oil', 0), + 6 => new question_possible_response('Proteins: Steak', 1), + 7 => new question_possible_response('Proteins: Potato', 0), + null => question_possible_response::no_response(), + ], + 1 => [ + 1 => new question_possible_response('Vegetables: Chicken breast', 0), + 2 => new question_possible_response('Vegetables: Carrot', 1), + 3 => new question_possible_response('Vegetables: Salmon fillet', 0), + 4 => new question_possible_response('Vegetables: Asparagus', 1), + 5 => new question_possible_response('Vegetables: Olive oil', 0), + 6 => new question_possible_response('Vegetables: Steak', 0), + 7 => new question_possible_response('Vegetables: Potato', 1), + null => question_possible_response::no_response(), + ], + 2 => [ + 1 => new question_possible_response('Fats: Chicken breast', 0), + 2 => new question_possible_response('Fats: Carrot', 0), + 3 => new question_possible_response('Fats: Salmon fillet', 0), + 4 => new question_possible_response('Fats: Asparagus', 0), + 5 => new question_possible_response('Fats: Olive oil', 1), + 6 => new question_possible_response('Fats: Steak', 0), + 7 => new question_possible_response('Fats: Potato', 0), + null => question_possible_response::no_response(), + ], + ]; + $this->assertEquals($expected, $this->qtype->get_possible_responses($q)); + } + public function get_save_question_which() { return [['animals_single'], ['oumatrix_multiple']]; }