Skip to content

Commit

Permalink
Matrix: Reimplement get_possible_responses() for stats #746912
Browse files Browse the repository at this point in the history
  • Loading branch information
mkassaei committed Dec 20, 2023
1 parent 916f198 commit a54722c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
40 changes: 19 additions & 21 deletions questiontype.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,29 +316,27 @@ 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 = [];
if ($questiondata->options->inputtype === 'single') {
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 / count($question->columns);
} else {
$fraction = 0;
}
$responses[$colid] = new question_possible_response($responseclass, $fraction);
}
$responses[null] = question_possible_response::no_response();
$subqs[$rowid - 1] = $responses;
}

$responses[null] = question_possible_response::no_response();
return [$questiondata->id => $responses];
return $subqs;
} 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;
return null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down
53 changes: 53 additions & 0 deletions tests/questiontype_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -70,6 +73,56 @@ 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 / 4),
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 / 4),
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),
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 / 4),
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);
$this->assertEquals(null, $this->qtype->get_possible_responses($q));
}

public function get_save_question_which() {
return [['animals_single'], ['oumatrix_multiple']];
}
Expand Down

0 comments on commit a54722c

Please sign in to comment.