Skip to content

Commit

Permalink
MDL-73874 Questions: Add new validation for Drag and drop into text
Browse files Browse the repository at this point in the history
  • Loading branch information
Khoa Nguyen Dang committed Apr 7, 2022
1 parent efda17d commit c0e53d2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
13 changes: 13 additions & 0 deletions question/type/ddwtos/edit_ddwtos_form.php
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions question/type/ddwtos/lang/en/qtype_ddwtos.php
Expand Up @@ -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';
Expand Down
17 changes: 17 additions & 0 deletions question/type/ddwtos/tests/behat/add.feature
Expand Up @@ -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."
11 changes: 11 additions & 0 deletions question/type/ddwtos/tests/behat/edit.feature
Expand Up @@ -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."
14 changes: 12 additions & 2 deletions question/type/gapselect/edit_form_base.php
Expand Up @@ -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) {
Expand All @@ -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;
}
}

0 comments on commit c0e53d2

Please sign in to comment.