Skip to content

Commit

Permalink
MDL-83541 qtypes: Define excluded hash fields and test hints
Browse files Browse the repository at this point in the history
  • Loading branch information
marxjohnson committed Feb 14, 2025
1 parent 1233d28 commit b8804de
Show file tree
Hide file tree
Showing 20 changed files with 321 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,46 @@ public function process_calculated_option($data) {
$newitemid = $DB->insert_record('question_calculated_options', $data);
}
}

#[\Override]
public static function convert_backup_to_questiondata(array $tags): \stdClass {
$questiondata = parent::convert_backup_to_questiondata($tags);
$qtype = $questiondata->qtype;
foreach ($tags["plugin_qtype_{$qtype}_question"]['calculated_records']['calculated_record'] as $record) {
foreach($questiondata->options->answers as &$answer) {
if ($answer->id == $record['answer']) {
$answer->tolerance = $record['tolerance'];
$answer->tolerancetype = $record['tolerancetype'];
$answer->correctanswerlength = $record['correctanswerlength'];
$answer->correctanswerformat = $record['correctanswerformat'];
continue 2;
}
}
}
if (isset($tags["plugin_qtype_{$qtype}_question"]['calculated_options'])) {
$questiondata->options = (object) array_merge(
(array) $questiondata->options,
$tags["plugin_qtype_{$qtype}_question"]['calculated_options']['calculated_option'][0],
);
}
return $questiondata;
}

#[\Override]
protected function define_excluded_identity_hash_fields(): array {
return [
// These option fields are present in the database, but are only used by calculatedmulti.
'/options/synchronize',
'/options/single',
'/options/shuffleanswers',
'/options/correctfeedback',
'/options/correctfeedbackformat',
'/options/partiallycorrectfeedback',
'/options/partiallycorrectfeedbackformat',
'/options/incorrectfeedback',
'/options/incorrectfeedbackformat',
'/options/answernumbering',
'/options/shownumcorrect',
];
}
}
16 changes: 15 additions & 1 deletion question/type/calculated/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function get_calculated_question_form_data_sum() {
$fromform->defaultmark = 1.0;
$fromform->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';

$fromform->unitrole = '3';
$fromform->unitrole = '0';
$fromform->unitpenalty = 0.1;
$fromform->unitgradingtypes = '1';
$fromform->unitsleft = '0';
Expand Down Expand Up @@ -187,6 +187,20 @@ public function get_calculated_question_form_data_sum() {

$fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;

$fromform->hint = [
[
'text' => 'Add',
'format' => FORMAT_HTML,
],
];

$fromform->unit = [
'x'
];
$fromform->multiplier = [
'1.0'
];

return $fromform;
}

Expand Down
5 changes: 4 additions & 1 deletion question/type/calculated/tests/question_type_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ public function test_load_question(): void {
$this->assertEquals($question->createdby, $questiondata->modifiedby);
$this->assertEquals('', $questiondata->idnumber);
$this->assertEquals($category->contextid, $questiondata->contextid);
$this->assertEquals([], $questiondata->hints);
$this->assertCount(1, $questiondata->hints);
$hint = array_pop($questiondata->hints);
$this->assertEquals('Add', $hint->hint);
$this->assertEquals(FORMAT_HTML, $hint->hintformat);

// Options.
$this->assertEquals($questiondata->id, $questiondata->options->question);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public function recode_legacy_state_answer($state) {
}
return $result ? $result : $answer;
}

#[\Override]
protected function define_excluded_identity_hash_fields(): array {
return [];
}
}
91 changes: 91 additions & 0 deletions question/type/calculatedmulti/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,95 @@ public function make_calculatedmulti_question_multiresponse() {

return $q;
}

/**
* Return the form data for a question with a single response.
*
* @return stdClass
*/
public function get_calculatedmulti_question_form_data_singleresponse(): stdClass {
question_bank::load_question_definition_classes('calculated');
$fromform = new stdClass();

$fromform->name = 'Simple sum';
$fromform->questiontext['text'] = 'What is {a} + {b}?';
$fromform->questiontext['format'] = FORMAT_HTML;
$fromform->defaultmark = 1.0;
$fromform->generalfeedback['text'] = 'Generalfeedback: {={a} + {b}} is the right answer.';
$fromform->generalfeedback['format'] = FORMAT_HTML;

$fromform->unitrole = '3';
$fromform->unitpenalty = 0.1;
$fromform->unitgradingtypes = '1';
$fromform->unitsleft = '0';
$fromform->nounits = 1;
$fromform->multiplier = [];
$fromform->multiplier[0] = '1.0';
$fromform->synchronize = 0;
$fromform->answernumbering = 0;
$fromform->shuffleanswers = 0;
$fromform->single = 1;
$fromform->correctfeedback['text'] = 'Very good';
$fromform->correctfeedback['format'] = FORMAT_HTML;
$fromform->partiallycorrectfeedback['text'] = 'Mostly good';
$fromform->partiallycorrectfeedback['format'] = FORMAT_HTML;
$fromform->incorrectfeedback['text'] = 'Completely Wrong';
$fromform->incorrectfeedback['format'] = FORMAT_HTML;
$fromform->shownumcorrect = 1;

$fromform->noanswers = 6;
$fromform->answer = [];
$fromform->answer[0]['text'] = '{a} + {b}';
$fromform->answer[0]['format'] = FORMAT_HTML;
$fromform->answer[1]['text'] = '{a} - {b}';
$fromform->answer[1]['format'] = FORMAT_HTML;
$fromform->answer[2]['text'] = '*';
$fromform->answer[2]['format'] = FORMAT_HTML;

$fromform->fraction = [];
$fromform->fraction[0] = '1.0';
$fromform->fraction[1] = '0.0';
$fromform->fraction[2] = '0.0';

$fromform->tolerance = [];
$fromform->tolerance[0] = 0.001;
$fromform->tolerance[1] = 0.001;
$fromform->tolerance[2] = 0;

$fromform->tolerancetype[0] = 1;
$fromform->tolerancetype[1] = 1;
$fromform->tolerancetype[2] = 1;

$fromform->correctanswerlength[0] = 2;
$fromform->correctanswerlength[1] = 2;
$fromform->correctanswerlength[2] = 2;

$fromform->correctanswerformat[0] = 1;
$fromform->correctanswerformat[1] = 1;
$fromform->correctanswerformat[2] = 1;

$fromform->feedback = [];
$fromform->feedback[0] = [];
$fromform->feedback[0]['format'] = FORMAT_HTML;
$fromform->feedback[0]['text'] = 'Very good.';

$fromform->feedback[1] = [];
$fromform->feedback[1]['format'] = FORMAT_HTML;
$fromform->feedback[1]['text'] = 'Add. not subtract!';

$fromform->feedback[2] = [];
$fromform->feedback[2]['format'] = FORMAT_HTML;
$fromform->feedback[2]['text'] = 'Completely wrong.';

$fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;

$fromform->hint = [
[
'text' => 'Add',
'format' => FORMAT_HTML,
],
];

return $fromform;
}
}
7 changes: 7 additions & 0 deletions question/type/calculatedsimple/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ public function get_calculatedsimple_question_form_data_sumwithvariants() {
$form->definition[19] = '1-0-b';
$form->definition[20] = '1-0-a';

$form->hint = [
[
'text' => 'Add',
'format' => FORMAT_HTML,
],
];

$form->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;

return $form;
Expand Down
7 changes: 6 additions & 1 deletion question/type/calculatedsimple/tests/question_type_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function test_question_saving_sumwithvariants(): void {
$actualquestiondata = end($actualquestionsdata);

foreach ($questiondata as $property => $value) {
if (!in_array($property, array('id', 'timemodified', 'timecreated', 'options', 'idnumber'))) {
if (!in_array($property, array('id', 'timemodified', 'timecreated', 'options', 'idnumber', 'hints'))) {
$this->assertEquals($value, $actualquestiondata->$property);
}
}
Expand All @@ -110,6 +110,11 @@ public function test_question_saving_sumwithvariants(): void {
}
}

$this->assertCount(1, $actualquestiondata->hints);
$hint = array_pop($actualquestiondata->hints);
$this->assertEquals($formdata->hint[0]['text'], $hint->hint);
$this->assertEquals($formdata->hint[0]['format'], $hint->hintformat);

$datasetloader = new qtype_calculated_dataset_loader($actualquestiondata->id);

$this->assertEquals(10, $datasetloader->get_number_of_items());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,29 @@ public static function define_decode_contents() {

return $contents;
}

#[\Override]
public static function convert_backup_to_questiondata(array $tags): \stdClass {
$questiondata = parent::convert_backup_to_questiondata($tags);
$questiondata->options->drags = array_map(
fn($drag) => (object) $drag,
$tags['plugin_qtype_ddimageortext_question']['drags']['drag'],
);
$questiondata->options->drops = array_map(
fn($drop) => (object) $drop,
$tags['plugin_qtype_ddimageortext_question']['drops']['drop'],
);
return $questiondata;
}

#[\Override]
protected function define_excluded_identity_hash_fields(): array {
return [
'/options/drags/id',
'/options/drags/questionid',
'/options/drops/id',
'/options/drops/questionid',

];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,28 @@ public static function define_decode_contents() {

return $contents;
}

#[\Override]
public static function convert_backup_to_questiondata(array $tags): \stdClass {
$questiondata = parent::convert_backup_to_questiondata($tags);
$questiondata->options->drags = array_map(
fn($drag) => (object) $drag,
$tags['plugin_qtype_ddmarker_question']['drags']['drag'],
);
$questiondata->options->drops = array_map(
fn($drop) => (object) $drop,
$tags['plugin_qtype_ddmarker_question']['drops']['drop'],
);
return $questiondata;
}

#[\Override]
protected function define_excluded_identity_hash_fields(): array {
return [
'/options/drags/id',
'/options/drags/questionid',
'/options/drops/id',
'/options/drops/questionid',
];
}
}
7 changes: 7 additions & 0 deletions question/type/ddwtos/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ public function get_ddwtos_question_form_data_fox() {
$fromform->penalty = 0.3333333;
$fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;

$fromform->hint = [
[
'text' => 'Fast',
'format' => FORMAT_HTML,
],
];

return $fromform;
}

Expand Down
7 changes: 7 additions & 0 deletions question/type/gapselect/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public function get_gapselect_question_form_data_missingchoiceno() {
$fromform->penalty = 0.3333333;
$fromform->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;

$fromform->hint = [
[
'text' => 'Cat',
'format' => FORMAT_HTML,
],
];

return $fromform;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,23 @@ public static function define_decode_contents() {

return $contents;
}

#[\Override]
public static function convert_backup_to_questiondata(array $tags): \stdClass {
$questiondata = parent::convert_backup_to_questiondata($tags);
$questiondata->options = (object) $tags["plugin_qtype_match_question"]['matchoptions'][0];
$questiondata->options->subquestions = array_map(
fn($match) => (object) $match,
$tags["plugin_qtype_match_question"]['matches']['match'],
);
return $questiondata;
}

#[\Override]
protected function define_excluded_identity_hash_fields(): array {
return [
'/options/subquestions/id',
'/options/subquestions/questionid',
];
}
}
7 changes: 7 additions & 0 deletions question/type/match/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ public function get_match_question_form_data_foursubq() {

$q->noanswers = 4;

$q->hint = [
[
'text' => 'Frog and newt are the same',
'format' => FORMAT_HTML,
],
];

return $q;
}

Expand Down
7 changes: 6 additions & 1 deletion question/type/match/tests/question_type_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function test_question_saving_foursubq(): void {

foreach ($questiondata as $property => $value) {
if (!in_array($property, ['id', 'timemodified', 'timecreated', 'options', 'stamp',
'versionid', 'questionbankentryid'])) {
'versionid', 'questionbankentryid', 'hints'])) {
if (!empty($actualquestiondata)) {
$this->assertEquals($value, $actualquestiondata->$property);
}
Expand All @@ -203,6 +203,11 @@ public function test_question_saving_foursubq(): void {
}
}

$this->assertCount(1, $actualquestiondata->hints);
$hint = array_pop($actualquestiondata->hints);
$this->assertEquals($formdata->hint[0]['text'], $hint->hint);
$this->assertEquals($formdata->hint[0]['format'], $hint->hintformat);

$this->assertObjectHasProperty('subquestions', $actualquestiondata->options);

$subqpropstoignore = array('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,19 @@ public function recode_legacy_state_answer($state) {
return implode(',', $resultarr);
}

#[\Override]
public function define_excluded_identity_hash_fields(): array {
return [
'/options/sequence',
'/options/question',
];
}

#[\Override]
public static function remove_excluded_question_data(stdClass $questiondata, array $excludefields = []): stdClass {
if (isset($questiondata->options->questions)) {
unset($questiondata->options->questions);
}
return parent::remove_excluded_question_data($questiondata, $excludefields);
}
}
Loading

0 comments on commit b8804de

Please sign in to comment.