MDL-79863 qtype_ordering: qtype/ordering fix Github issue #35: Clicking Add to add items more than once without saving the question in between has incorrect and unwanted behavior

This commit is contained in:
Gordon Bateson 2018-12-15 23:36:33 +09:00 committed by Mathew May
parent 2917f5a895
commit 33b360975b
3 changed files with 58 additions and 63 deletions

View File

@ -136,13 +136,8 @@ class qtype_ordering_edit_form extends question_edit_form {
$this->add_repeat_elements($mform, $name, $elements, $options);
$repeats = $this->get_answer_repeats($this->question);
if (optional_param('addanswers', 0, PARAM_RAW)) {
$repeats += optional_param('addanswerscount', 0, PARAM_INT);
}
// Adjust HTML editor and removal buttons.
$this->adjust_html_editors($mform, $name, $repeats);
$this->adjust_html_editors($mform, $name);
// Adding feedback fields (=Combined feedback).
if (method_exists($this, 'add_combined_feedback_fields')) {
@ -219,7 +214,7 @@ class qtype_ordering_edit_form extends question_edit_form {
* @param string $name
* @param int $repeats
*/
protected function adjust_html_editors($mform, $name, $repeats) {
protected function adjust_html_editors($mform, $name) {
// Cache the number of formats supported
// by the preferred editor for each format.
@ -233,50 +228,50 @@ class qtype_ordering_edit_form extends question_edit_form {
$defaultanswerformat = get_config('qtype_ordering', 'defaultanswerformat');
for ($i = 0; $i < $repeats; $i++) {
$i = 0;
while (($editor = $name."[$i]") && $mform->elementExists($editor)) {
$editor = $mform->getElement($editor);
$editor = $name . '[' . $i . ']';
if ($mform->elementExists($editor)) {
$editor = $mform->getElement($editor);
if (isset($ids[$i])) {
$id = $ids[$i];
} else {
$id = 0;
}
// The old/new name of the button to remove the HTML editor
// old : the name of the button when added by repeat_elements
// new : the simplified name of the button to satisfy "no_submit_button_pressed()" in lib/formslib.php.
$oldname = $name.'removeeditor['.$i.']';
$newname = $name.'removeeditor_'.$i;
// Remove HTML editor, if necessary.
if (optional_param($newname, 0, PARAM_RAW)) {
$format = $this->reset_editor_format($editor, FORMAT_MOODLE);
$_POST['answer'][$i]['format'] = $format; // Overwrite incoming data.
} else if ($id) {
$format = $this->question->options->answers[$id]->answerformat;
} else {
$format = $this->reset_editor_format($editor, $defaultanswerformat);
}
// Check we have a submit button - it should always be there !!
if ($mform->elementExists($oldname)) {
if (! isset($count[$format])) {
$editor = editors_get_preferred_editor($format);
$count[$format] = $editor->get_supported_formats();
$count[$format] = count($count[$format]);
}
if ($count[$format] > 1) {
$mform->removeElement($oldname);
} else {
$submit = $mform->getElement($oldname);
$submit->setName($newname);
}
$mform->registerNoSubmitButton($newname);
}
if (isset($ids[$i])) {
$id = $ids[$i];
} else {
$id = 0;
}
// The old/new name of the button to remove the HTML editor
// old : the name of the button when added by repeat_elements
// new : the simplified name of the button to satisfy "no_submit_button_pressed()" in lib/formslib.php.
$oldname = $name.'removeeditor['.$i.']';
$newname = $name.'removeeditor_'.$i;
// Remove HTML editor, if necessary.
if (optional_param($newname, 0, PARAM_RAW)) {
$format = $this->reset_editor_format($editor, FORMAT_MOODLE);
$_POST['answer'][$i]['format'] = $format; // Overwrite incoming data.
} else if ($id) {
$format = $this->question->options->answers[$id]->answerformat;
} else {
$format = $this->reset_editor_format($editor, $defaultanswerformat);
}
// Check we have a submit button - it should always be there !!
if ($mform->elementExists($oldname)) {
if (! isset($count[$format])) {
$editor = editors_get_preferred_editor($format);
$count[$format] = $editor->get_supported_formats();
$count[$format] = count($count[$format]);
}
if ($count[$format] > 1) {
$mform->removeElement($oldname);
} else {
$submit = $mform->getElement($oldname);
$submit->setName($newname);
}
$mform->registerNoSubmitButton($newname);
}
// increment the answer index
$i++;
}
}

View File

@ -692,7 +692,7 @@ class qtype_ordering_question extends question_graded_automatically {
/**
* Get all ordered subsets in the positions array
*
* @param array $positions
* @param array $positions maps an item's current position to its correct position
* @param boolean $contiguous TRUE if searching only for contiguous subsets; otherwise FALSE
*
* @return array of ordered subsets from within the $positions array
@ -702,10 +702,10 @@ class qtype_ordering_question extends question_graded_automatically {
// Var $subsets is the collection of all subsets within $positions.
$subsets = array();
// loop through the $current values at each position
foreach ($positions as $i => $current) {
// loop through the values at each position
foreach ($positions as $p => $value) {
// is $current a "new" value that cannot be added to any $subsets found so far
// is $value a "new" value that cannot be added to any $subsets found so far
$isnew = true;
// an array of new and saved subsets to be added to $subsets
@ -720,30 +720,30 @@ class qtype_ordering_question extends question_graded_automatically {
switch (true) {
case ($current == ($end + 1)):
// for a contiguous value, we simply append $i to the subset
case ($value == ($end + 1)):
// for a contiguous value, we simply append $p to the subset
$isnew = false;
$subsets[$s][] = $i;
$subsets[$s][] = $p;
break;
case $contiguous:
// if the $contiguous flag is set, we ignore non-contiguous values
break;
case ($current > $end):
case ($value > $end):
// for a non-contiguous value, we save the subset so far,
// because a value between $end and $current may be found later,
// and then append $i to the subset
// because a value between $end and $value may be found later,
// and then append $p to the subset
$isnew = false;
$new[] = $subset;
$subsets[$s][] = $i;
$subsets[$s][] = $p;
break;
}
}
// if this is a "new" value, add it as a new subset
if ($isnew) {
$new[] = array($i);
$new[] = array($p);
}
// append any "new" subsets that were found during this iteration

View File

@ -29,5 +29,5 @@ $plugin->cron = 0;
$plugin->component = 'qtype_ordering';
$plugin->maturity = MATURITY_STABLE;
$plugin->requires = 2010112400; // Moodle 2.0
$plugin->version = 2018121277;
$plugin->release = '2018-12-12 (77)';
$plugin->version = 2018121578;
$plugin->release = '2018-12-15 (78)';