From c0e53d297fd299e203504f90ac02ac9a9f943cfd Mon Sep 17 00:00:00 2001 From: Khoa Nguyen Dang Date: Mon, 28 Mar 2022 09:57:04 +0700 Subject: [PATCH] MDL-73874 Questions: Add new validation for Drag and drop into text --- question/type/ddwtos/edit_ddwtos_form.php | 13 +++++++++++++ question/type/ddwtos/lang/en/qtype_ddwtos.php | 1 + question/type/ddwtos/tests/behat/add.feature | 17 +++++++++++++++++ question/type/ddwtos/tests/behat/edit.feature | 11 +++++++++++ question/type/gapselect/edit_form_base.php | 14 ++++++++++++-- 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/question/type/ddwtos/edit_ddwtos_form.php b/question/type/ddwtos/edit_ddwtos_form.php index 3cb00a003503f..aadb68dae110f 100644 --- a/question/type/ddwtos/edit_ddwtos_form.php +++ b/question/type/ddwtos/edit_ddwtos_form.php @@ -53,4 +53,17 @@ protected function choice_group($mform) { array('size' => 1, 'class' => 'tweakcss')); return $grouparray; } + + protected function extra_slot_validation(array $slots, array $choices): ?string { + foreach ($slots as $slot) { + if (count(array_keys($slots, $slot)) > 1) { + $choice = $choices[$slot - 1]; + if (!isset($choice['infinite']) || $choice['infinite'] != 1) { + return get_string('errorlimitedchoice', 'qtype_ddwtos', + html_writer::tag('b', $slot)); + } + } + } + return null; + } } diff --git a/question/type/ddwtos/lang/en/qtype_ddwtos.php b/question/type/ddwtos/lang/en/qtype_ddwtos.php index e25433ab2a2cc..13d0723a81ea6 100644 --- a/question/type/ddwtos/lang/en/qtype_ddwtos.php +++ b/question/type/ddwtos/lang/en/qtype_ddwtos.php @@ -26,6 +26,7 @@ $string['answer'] = 'Answer'; $string['blank'] = 'blank'; $string['correctansweris'] = 'The correct answer is: {$a}'; +$string['errorlimitedchoice'] = 'Choice [[{$a}]] has been used more than once without being set to "Unlimited". Please recheck this question.'; $string['infinite'] = 'Unlimited'; $string['pleaseputananswerineachbox'] = 'Please put an answer in each box.'; $string['pluginname'] = 'Drag and drop into text'; diff --git a/question/type/ddwtos/tests/behat/add.feature b/question/type/ddwtos/tests/behat/add.feature index a73b9c86d9bc2..d00b8a43a5cb9 100644 --- a/question/type/ddwtos/tests/behat/add.feature +++ b/question/type/ddwtos/tests/behat/add.feature @@ -39,3 +39,20 @@ Feature: Test creating a drag and drop into text question And the following fields match these values: | id_shuffleanswers | 1 | | Penalty for each incorrect try | 20% | + + Scenario: Cannot create a drag and drop into text question when making the unsolvable questions + When I am on the "Course 1" "core_question > course question bank" page logged in as teacher + And I add a "Drag and drop into text" question filling the form with: + | Question name | Drag and drop into text 001 | + | Question text | The [[1]] [[2]] on the [[1]]. | + | General feedback | The cat sat on the cat. | + | id_shuffleanswers | 1 | + | id_choices_0_answer | cat | + | id_choices_1_answer | goes through | + | id_choices_2_answer | mat | + | id_choices_3_answer | dog | + | id_choices_4_answer | table | + | Penalty for each incorrect try | 20% | + | Hint 1 | First hint | + | Hint 2 | Second hint | + Then I should see "Choice [[1]] has been used more than once without being set to \"Unlimited\". Please recheck this question." diff --git a/question/type/ddwtos/tests/behat/edit.feature b/question/type/ddwtos/tests/behat/edit.feature index eeb1dd21f7afc..fd531d49d93e2 100644 --- a/question/type/ddwtos/tests/behat/edit.feature +++ b/question/type/ddwtos/tests/behat/edit.feature @@ -31,3 +31,14 @@ Feature: Test editing a drag and drop into text questions | Question name | Edited question name | And I press "id_submitbutton" Then I should see "Edited question name" + + Scenario: Cannot update a drag and drop into text question to the unsolvable questions + When I am on the "Drag to text" "core_question > edit" page logged in as teacher + And I should see "Choice [[1]]" + And I should see "Choice [[2]]" + And I should see "Choice [[3]]" + And I set the following fields to these values: + | Question name | Edited question name | + | Question text | Choice [[1]] Choice [[2]] Choice [[1]] | + And I press "id_submitbutton" + Then I should see "Choice [[1]] has been used more than once without being set to \"Unlimited\". Please recheck this question." diff --git a/question/type/gapselect/edit_form_base.php b/question/type/gapselect/edit_form_base.php index 1ae7d121d1e1b..a539866a3e4f4 100644 --- a/question/type/gapselect/edit_form_base.php +++ b/question/type/gapselect/edit_form_base.php @@ -281,7 +281,6 @@ private function validate_slots($questiontext, $choices) { } $slots = $cleanedslots; - $found = false; foreach ($slots as $slot) { $found = false; foreach ($choices as $key => $choice) { @@ -299,10 +298,21 @@ private function validate_slots($questiontext, $choices) { html_writer::tag('b', $slot)); } } - return false; + return $this->extra_slot_validation($slots, $choices) ?? false; } public function qtype() { return ''; } + + /** + * Finds more errors in question slots. + * + * @param array $slots The question text + * @param array $choices Question choices + * @return string|null Error message or false if no errors + */ + protected function extra_slot_validation(array $slots, array $choices): ?string { + return null; + } }