diff --git a/question.php b/question.php index 691451f..9e22f90 100644 --- a/question.php +++ b/question.php @@ -287,7 +287,7 @@ public function classify_response(array $response): array { $classifiedresponse = []; foreach ($this->roworder as $key => $rownumber) { $row = $this->rows[$rownumber]; - $partname = format_string($row->name); + $partname = shorten_text($row->number . '. ' . format_string($row->name), 100); if (!array_key_exists($this->field($key), $response)) { $classifiedresponse[$partname] = question_classified_response::no_response(); continue; @@ -479,11 +479,12 @@ public function classify_response(array $response) { foreach ($this->columns as $column) { if ($this->is_choice_selected($response, $rowkey, $column->number)) { - $classifiedresponse[$rowname . ': ' . format_string($column->name)] = + $classifiedresponse[shorten_text($row->number . '. ' . format_string($rowname), 50) . + shorten_text(': ' . format_string($column->name), 50)] = new question_classified_response( 1, get_string('selected', 'qtype_oumatrix'), - array_key_exists($column->number, $row->correctanswers) / count($row->correctanswers), + (int) array_key_exists($column->number, $row->correctanswers) / count($row->correctanswers), ); } } diff --git a/questiontype.php b/questiontype.php index 3094bc5..f784067 100644 --- a/questiontype.php +++ b/questiontype.php @@ -351,7 +351,7 @@ protected function get_possible_responses_single(stdClass $questiondata): array ); } $responses[null] = question_possible_response::no_response(); - $parts[format_string($row->name)] = $responses; + $parts[shorten_text($row->number . '. ' . format_string($row->name), 100)] = $responses; } return $parts; } @@ -368,10 +368,11 @@ protected function get_possible_responses_multiple(stdClass $questiondata): arra $rowname = format_string($row->name); $correctanswer = explode(',', $row->correctanswers); foreach ($questiondata->columns as $column) { - $parts[$rowname . ': ' . format_string($column->name)] = [ + $parts[shorten_text($row->number . '. ' . format_string($rowname), 50) . + shorten_text(': ' . format_string($column->name), 50)] = [ 1 => new question_possible_response( get_string('selected', 'qtype_oumatrix'), - in_array($column->number, $correctanswer) / count($correctanswer), + (int) in_array($column->number, $correctanswer) / count($correctanswer), ), ]; } diff --git a/tests/question_multiple_test.php b/tests/question_multiple_test.php index 476dd74..bb15c1d 100644 --- a/tests/question_multiple_test.php +++ b/tests/question_multiple_test.php @@ -116,13 +116,13 @@ public function test_classify_response_multiple(): void { 'Vegetables' => [2 => 'Carrot', 4 => 'Asparagus', 7 => 'Potato'], 'Fats' => [5 => 'Olive oil']]); $this->assertEquals([ - "Proteins: Chicken breast" => new question_classified_response(1, 'Selected', 1 / 3), - "Proteins: Salmon fillet" => new question_classified_response(1, 'Selected', 1 / 3), - "Proteins: Steak" => new question_classified_response(1, 'Selected', 1 / 3), - "Vegetables: Carrot" => new question_classified_response(1, 'Selected', 1 / 3), - "Vegetables: Asparagus" => new question_classified_response(1, 'Selected', 1 / 3), - "Vegetables: Potato" => new question_classified_response(1, 'Selected', 1 / 3), - "Fats: Olive oil" => new question_classified_response(1, 'Selected', 1), + "1. Proteins: Chicken breast" => new question_classified_response(1, 'Selected', 1 / 3), + "1. Proteins: Salmon fillet" => new question_classified_response(1, 'Selected', 1 / 3), + "1. Proteins: Steak" => new question_classified_response(1, 'Selected', 1 / 3), + "2. Vegetables: Carrot" => new question_classified_response(1, 'Selected', 1 / 3), + "2. Vegetables: Asparagus" => new question_classified_response(1, 'Selected', 1 / 3), + "2. Vegetables: Potato" => new question_classified_response(1, 'Selected', 1 / 3), + "3. Fats: Olive oil" => new question_classified_response(1, 'Selected', 1), ], $question->classify_response($response)); // Test a partial response. @@ -131,11 +131,11 @@ public function test_classify_response_multiple(): void { 'Vegetables' => [2 => 'Carrot', 1 => 'Chicken breast'], 'Fats' => [5 => 'Olive oil']]); $this->assertEquals([ - "Proteins: Chicken breast" => new question_classified_response(1, 'Selected', 1 / 3), - "Proteins: Asparagus" => new question_classified_response(1, 'Selected', 0), - "Vegetables: Chicken breast" => new question_classified_response(1, 'Selected', 0), - "Vegetables: Carrot" => new question_classified_response(1, 'Selected', 1 / 3), - "Fats: Olive oil" => new question_classified_response(1, 'Selected', 1), + "1. Proteins: Chicken breast" => new question_classified_response(1, 'Selected', 1 / 3), + "1. Proteins: Asparagus" => new question_classified_response(1, 'Selected', 0), + "2. Vegetables: Chicken breast" => new question_classified_response(1, 'Selected', 0), + "2. Vegetables: Carrot" => new question_classified_response(1, 'Selected', 1 / 3), + "3. Fats: Olive oil" => new question_classified_response(1, 'Selected', 1), ], $question->classify_response($response)); } diff --git a/tests/question_single_test.php b/tests/question_single_test.php index e0d1835..d6565e9 100644 --- a/tests/question_single_test.php +++ b/tests/question_single_test.php @@ -78,40 +78,40 @@ public function test_classify_response_single(): void { $response = $question->prepare_simulated_post_data( ['Bee' => 'Insects', 'Salmon' => 'Fish', 'Seagull' => 'Birds', 'Dog' => 'Mammals']); $this->assertEquals([ - 'Bee' => new question_classified_response(1, 'Insects', 1), - 'Salmon' => new question_classified_response(2, 'Fish', 1), - 'Seagull' => new question_classified_response(3, 'Birds', 1), - 'Dog' => new question_classified_response(4, 'Mammals', 1), + '1. Bee' => new question_classified_response(1, 'Insects', 1), + '2. Salmon' => new question_classified_response(2, 'Fish', 1), + '3. Seagull' => new question_classified_response(3, 'Birds', 1), + '4. Dog' => new question_classified_response(4, '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([ - 'Bee' => new question_classified_response(1, 'Insects', 1), - 'Salmon' => new question_classified_response(3, 'Birds', 0), - 'Seagull' => new question_classified_response(3, 'Birds', 1), - 'Dog' => new question_classified_response(4, 'Mammals', 1), + '1. Bee' => new question_classified_response(1, 'Insects', 1), + '2. Salmon' => new question_classified_response(3, 'Birds', 0), + '3. Seagull' => new question_classified_response(3, 'Birds', 1), + '4. Dog' => new question_classified_response(4, '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([ - 'Bee' => new question_classified_response(1, 'Insects', 1), - 'Salmon' => new question_classified_response(3, 'Birds', 0), - 'Seagull' => new question_classified_response(3, 'Birds', 1), - 'Dog' => new question_classified_response(1, 'Insects', 0), + '1. Bee' => new question_classified_response(1, 'Insects', 1), + '2. Salmon' => new question_classified_response(3, 'Birds', 0), + '3. Seagull' => new question_classified_response(3, 'Birds', 1), + '4. Dog' => new question_classified_response(1, '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([ - 'Bee' => new question_classified_response(1, 'Insects', 1), - 'Salmon' => question_classified_response::no_response(), - 'Seagull' => new question_classified_response(3, 'Birds', 1), - 'Dog' => new question_classified_response(1, 'Insects', 0), + '1. Bee' => new question_classified_response(1, 'Insects', 1), + '2. Salmon' => question_classified_response::no_response(), + '3. Seagull' => new question_classified_response(3, 'Birds', 1), + '4. Dog' => new question_classified_response(1, 'Insects', 0), ], $question->classify_response($response)); } diff --git a/tests/questiontype_test.php b/tests/questiontype_test.php index d0c288e..bb30a85 100644 --- a/tests/questiontype_test.php +++ b/tests/questiontype_test.php @@ -84,28 +84,28 @@ public function test_get_possible_responses_single(): void { $q = question_bank::load_question_data($createdquestion->id); $expected = [ - 'Bee' => [ + '1. Bee' => [ 1 => new question_possible_response('Insects', 1), 2 => new question_possible_response('Fish', 0), 3 => new question_possible_response('Birds', 0), 4 => new question_possible_response('Mammals', 0), null => question_possible_response::no_response(), ], - 'Salmon' => [ + '2. Salmon' => [ 1 => new question_possible_response('Insects', 0), 2 => new question_possible_response('Fish', 1), 3 => new question_possible_response('Birds', 0), 4 => new question_possible_response('Mammals', 0), null => question_possible_response::no_response(), ], - 'Seagull' => [ + '3. Seagull' => [ 1 => new question_possible_response('Insects', 0), 2 => new question_possible_response('Fish', 0), 3 => new question_possible_response('Birds', 1), 4 => new question_possible_response('Mammals', 0), null => question_possible_response::no_response(), ], - 'Dog' => [ + '4. Dog' => [ 1 => new question_possible_response('Insects', 0), 2 => new question_possible_response('Fish', 0), 3 => new question_possible_response('Birds', 0), @@ -124,27 +124,27 @@ public function test_get_possible_responses_multiple(): void { ['category' => $category->id, 'name' => 'Test question']); $q = question_bank::load_question_data($createdquestion->id); $expected = [ - 'Proteins: Chicken breast' => [1 => new question_possible_response('Selected', 1 / 3)], - 'Proteins: Carrot' => [1 => new question_possible_response('Selected', 0)], - 'Proteins: Salmon fillet' => [1 => new question_possible_response('Selected', 1 / 3)], - 'Proteins: Asparagus' => [1 => new question_possible_response('Selected', 0)], - 'Proteins: Olive oil' => [1 => new question_possible_response('Selected', 0)], - 'Proteins: Steak' => [1 => new question_possible_response('Selected', 1 / 3)], - 'Proteins: Potato' => [1 => new question_possible_response('Selected', 0)], - 'Vegetables: Chicken breast' => [1 => new question_possible_response('Selected', 0)], - 'Vegetables: Carrot' => [1 => new question_possible_response('Selected', 1 / 3)], - 'Vegetables: Salmon fillet' => [1 => new question_possible_response('Selected', 0)], - 'Vegetables: Asparagus' => [1 => new question_possible_response('Selected', 1 / 3)], - 'Vegetables: Olive oil' => [1 => new question_possible_response('Selected', 0)], - 'Vegetables: Steak' => [1 => new question_possible_response('Selected', 0)], - 'Vegetables: Potato' => [1 => new question_possible_response('Selected', 1 / 3)], - 'Fats: Chicken breast' => [1 => new question_possible_response('Selected', 0)], - 'Fats: Carrot' => [1 => new question_possible_response('Selected', 0)], - 'Fats: Salmon fillet' => [1 => new question_possible_response('Selected', 0)], - 'Fats: Asparagus' => [1 => new question_possible_response('Selected', 0)], - 'Fats: Olive oil' => [1 => new question_possible_response('Selected', 1)], - 'Fats: Steak' => [1 => new question_possible_response('Selected', 0)], - 'Fats: Potato' => [1 => new question_possible_response('Selected', 0)], + '1. Proteins: Chicken breast' => [1 => new question_possible_response('Selected', 1 / 3)], + '1. Proteins: Carrot' => [1 => new question_possible_response('Selected', 0)], + '1. Proteins: Salmon fillet' => [1 => new question_possible_response('Selected', 1 / 3)], + '1. Proteins: Asparagus' => [1 => new question_possible_response('Selected', 0)], + '1. Proteins: Olive oil' => [1 => new question_possible_response('Selected', 0)], + '1. Proteins: Steak' => [1 => new question_possible_response('Selected', 1 / 3)], + '1. Proteins: Potato' => [1 => new question_possible_response('Selected', 0)], + '2. Vegetables: Chicken breast' => [1 => new question_possible_response('Selected', 0)], + '2. Vegetables: Carrot' => [1 => new question_possible_response('Selected', 1 / 3)], + '2. Vegetables: Salmon fillet' => [1 => new question_possible_response('Selected', 0)], + '2. Vegetables: Asparagus' => [1 => new question_possible_response('Selected', 1 / 3)], + '2. Vegetables: Olive oil' => [1 => new question_possible_response('Selected', 0)], + '2. Vegetables: Steak' => [1 => new question_possible_response('Selected', 0)], + '2. Vegetables: Potato' => [1 => new question_possible_response('Selected', 1 / 3)], + '3. Fats: Chicken breast' => [1 => new question_possible_response('Selected', 0)], + '3. Fats: Carrot' => [1 => new question_possible_response('Selected', 0)], + '3. Fats: Salmon fillet' => [1 => new question_possible_response('Selected', 0)], + '3. Fats: Asparagus' => [1 => new question_possible_response('Selected', 0)], + '3. Fats: Olive oil' => [1 => new question_possible_response('Selected', 1)], + '3. Fats: Steak' => [1 => new question_possible_response('Selected', 0)], + '3. Fats: Potato' => [1 => new question_possible_response('Selected', 0)], ]; $this->assertEquals($expected, $this->qtype->get_possible_responses($q)); }