Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OUMatrix qtype: Limit possible/classify responses subqid size #839371 #8

Merged
merged 1 commit into from
Nov 12, 2024
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
7 changes: 4 additions & 3 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
}
}
Expand Down
7 changes: 4 additions & 3 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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),
),
];
}
Expand Down
24 changes: 12 additions & 12 deletions tests/question_multiple_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
}

Expand Down
32 changes: 16 additions & 16 deletions tests/question_single_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
50 changes: 25 additions & 25 deletions tests/questiontype_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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));
}
Expand Down
Loading