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 7, 2025
1 parent 76e84cf commit ccc7cb0
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,22 @@ public function process_calculated_option($data) {
$newitemid = $DB->insert_record('question_calculated_options', $data);
}
}

#[\Override]
protected function define_excluded_fields(): array {
return [
'answer',
'synchronize', // These option fields are present in the database, but are only used by calculatedmulti.
'single',
'shuffleanswers',
'correctfeedback',
'correctfeedbackformat',
'partiallycorrectfeedback',
'partiallycorrectfeedbackformat',
'incorrectfeedback',
'incorrectfeedbackformat',
'answernumbering',
'shownumcorrect',
];
}
}
7 changes: 7 additions & 0 deletions question/type/calculated/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ 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,
],
];

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($syscontext->id, $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_fields(): array {
return ['answer'];
}
}
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
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
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,16 @@ public function recode_legacy_state_answer($state) {
return implode(',', $resultarr);
}

#[\Override]
public function define_excluded_fields(): array {
return ['sequence'];
}

#[\Override]
public static function reduce_question_data(array $questiondata, array $excludedfields): array {
if (isset($questiondata['options']['questions'])) {
unset($questiondata['options']['questions']);
}
return parent::reduce_question_data($questiondata, $excludedfields);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ public function process_numerical($data) {
$newitemid = $DB->insert_record('question_numerical', $data);
}
}

#[\Override]
public function define_excluded_fields(): array {
return ['answer'];
}
}
7 changes: 7 additions & 0 deletions question/type/numerical/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ public function get_numerical_question_form_data_pi() {
$form->qtype = 'numerical';
$form->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;

$form->hint = [
[
'text' => 'Just over 3',
'format' => FORMAT_HTML,
],
];

return $form;
}

Expand Down
7 changes: 6 additions & 1 deletion question/type/shortanswer/tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ public function get_shortanswer_question_form_data_frogtoad() {
array('text' => 'That is a bad answer.', 'format' => FORMAT_HTML),
);
$form->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;

$form->hint = [
[
'text' => 'Rhymes with dog',
'format' => FORMAT_HTML,
],
];
return $form;
}

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

foreach ($questiondata as $property => $value) {
if (!in_array($property, array('id', 'timemodified', 'timecreated', 'options'))) {
if (!in_array($property, array('id', 'timemodified', 'timecreated', 'options', 'hints'))) {
$this->assertEquals($value, $actualquestiondata->$property);
}
}
Expand All @@ -140,6 +140,11 @@ public function test_question_saving_frogtoad(): 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);
}

public function test_question_saving_trims_answers(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ public function recode_legacy_state_answer($state) {
}
return $result;
}

#[\Override]
public function define_excluded_fields(): array {
return ['trueanswer', 'falseanswer'];
}
}

0 comments on commit ccc7cb0

Please sign in to comment.