From 5990158a6fc3d8f053b293a32fc427d3bfa2d2b4 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Mon, 23 May 2022 17:46:12 +0100 Subject: [PATCH] MDL-74752 qtypes gapselect & ddwtos: implement regrading hooks --- .../gapselect/lang/en/qtype_gapselect.php | 2 ++ question/type/gapselect/questionbase.php | 19 ++++++++++++ .../type/gapselect/tests/question_test.php | 30 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/question/type/gapselect/lang/en/qtype_gapselect.php b/question/type/gapselect/lang/en/qtype_gapselect.php index 81b74549728..9d3647adb1c 100644 --- a/question/type/gapselect/lang/en/qtype_gapselect.php +++ b/question/type/gapselect/lang/en/qtype_gapselect.php @@ -45,6 +45,8 @@ $string['privacy:metadata'] = 'Select missing words question type plugin allows $string['privacy:preference:defaultmark'] = 'The default mark set for a given question.'; $string['privacy:preference:penalty'] = 'The penalty for each incorrect try when questions are run using the \'Interactive with multiple tries\' or \'Adaptive mode\' behaviour.'; $string['privacy:preference:shuffleanswers'] = 'Whether the answers should be automatically shuffled.'; +$string['regradeissuenumchoiceschanged'] = 'The number of choices in group {$a} changed.'; +$string['regradeissuenumgroupsschanged'] = 'The number of groups of choices has changed.'; $string['shuffle'] = 'Shuffle'; $string['tagsnotallowed'] = '{$a->tag} is not allowed. (Only {$a->allowed} are permitted.)'; $string['tagsnotallowedatall'] = '{$a->tag} is not allowed. (No HTML is allowed here.)'; diff --git a/question/type/gapselect/questionbase.php b/question/type/gapselect/questionbase.php index 0a93eb4561f..10595d011c1 100644 --- a/question/type/gapselect/questionbase.php +++ b/question/type/gapselect/questionbase.php @@ -112,6 +112,25 @@ abstract class qtype_gapselect_question_base extends question_graded_automatical } } + public function validate_can_regrade_with_other_version(question_definition $otherversion): ?string { + $basemessage = parent::validate_can_regrade_with_other_version($otherversion); + if ($basemessage) { + return $basemessage; + } + + if (count($this->choices) != count($otherversion->choices)) { + return get_string('regradeissuenumgroupsschanged', 'qtype_gapselect'); + } + + foreach ($this->choices as $group => $choices) { + if (count($this->choices[$group]) != count($otherversion->choices[$group])) { + return get_string('regradeissuenumchoiceschanged', 'qtype_gapselect', $group); + } + } + + return null; + } + public function get_question_summary() { $question = $this->html_to_text($this->questiontext, $this->questiontextformat); $groups = array(); diff --git a/question/type/gapselect/tests/question_test.php b/question/type/gapselect/tests/question_test.php index 450e3008c01..5aef925f250 100644 --- a/question/type/gapselect/tests/question_test.php +++ b/question/type/gapselect/tests/question_test.php @@ -34,6 +34,8 @@ require_once($CFG->dirroot . '/question/type/gapselect/tests/helper.php'); * @package qtype_gapselect * @copyright 2012 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \qtype_gapselect_question_base + * @covers \qtype_gapselect_question */ class question_test extends \basic_testcase { @@ -258,4 +260,32 @@ class question_test extends \basic_testcase { $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions); $this->assertEquals(1, $options['shufflechoices']); } + + public function test_validate_can_regrade_with_other_version_ok() { + $question = \test_question_maker::make_question('gapselect'); + + $newquestion = clone($question); + + $this->assertNull($newquestion->validate_can_regrade_with_other_version($question)); + } + + public function test_validate_can_regrade_with_other_version_bad_groups() { + $question = \test_question_maker::make_question('gapselect'); + + $newquestion = clone($question); + unset($newquestion->choices[3]); + + $this->assertEquals(get_string('regradeissuenumgroupsschanged', 'qtype_gapselect'), + $newquestion->validate_can_regrade_with_other_version($question)); + } + + public function test_validate_can_regrade_with_other_version_bad_choices() { + $question = \test_question_maker::make_question('gapselect'); + + $newquestion = clone($question); + unset($newquestion->choices[2][2]); + + $this->assertEquals(get_string('regradeissuenumchoiceschanged', 'qtype_gapselect', 2), + $newquestion->validate_can_regrade_with_other_version($question)); + } }