MDL-79863 qtype_ordering: Qtype ordering should not allow duplicatue items (#40)

Thank you Mahmoud Kassaei for your continuing work to improve the Ordering question type
This commit is contained in:
Mahmoud Kassaei 2019-03-04 23:57:08 +00:00 committed by Mathew May
parent 356ad12ad4
commit 9d3998ecf5
3 changed files with 25 additions and 1 deletions

View File

@ -363,13 +363,24 @@ class qtype_ordering_edit_form extends question_edit_form {
$plugin = 'qtype_ordering';
$answercount = 0;
$list = [];
foreach ($data['answer'] as $answer) {
if (is_array($answer)) {
$answer = $answer['text'];
$answer = trim($answer['text']);
}
if (trim($answer) == '') {
continue; // Skip empty answer.
}
// Check on duplicates.
if ($answer && in_array($answer, $list)) {
$draggableitem = new stdClass();
$draggableitem->number = array_search($answer, $list) + 1;
$draggableitem->text = $answer;
$errors["answer[$answercount]"] = get_string('err_draggableitemsduplication', 'qtype_ordering', $draggableitem);
} else {
$list[] = trim($answer);
}
$answercount++;
}

View File

@ -35,6 +35,7 @@ $string['defaultanswerformat'] = 'Default answer format';
$string['defaultquestionname'] = 'Drag the following items into the correct order.';
$string['editingordering'] = 'Editing ordering question';
$string['err_draggableitemsduplication'] = 'Dupplication of draggable items are not allowed. The string "{$a->text}" is already used in Draggable item {$a->number}.';
$string['gradedetails'] = 'Grade details';
$string['gradingtype'] = 'Grading type';

View File

@ -35,3 +35,15 @@ Feature: Test editing an Ordering question
| Question name | Edited Ordering |
And I press "id_submitbutton"
Then I should see "Edited Ordering"
@javascript @_switch_window
Scenario: Editing an ordering question and making sure the form does not allow duplication of draggables
When I click on "Edit" "link" in the "Ordering for editing" "table_row"
And I set the following fields to these values:
| Draggable item 4 | Object |
And I press "id_submitbutton"
Then I should see "Dupplication of draggable items are not allowed. The string \"Object\" is already used in Draggable item 2."
Given I set the following fields to these values:
| Draggable item 4 | Dynamic |
And I press "id_submitbutton"
Then I should see "Ordering for editing"