From 66a4a376ee58109e963de3ff7d9bfae549118d82 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Thu, 12 Jan 2017 09:56:09 +0800 Subject: [PATCH] MDL-57368 mod_feeback: fix drag and drop question reordering in boost --- mod/feedback/item/captcha/lib.php | 3 ++- mod/feedback/item/feedback_item_class.php | 6 +++++- mod/feedback/item/label/lib.php | 1 + mod/feedback/item/multichoice/lib.php | 4 ++++ mod/feedback/item/multichoicerated/lib.php | 2 ++ mod/feedback/yui/dragdrop/dragdrop.js | 14 +++++++++++--- 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/mod/feedback/item/captcha/lib.php b/mod/feedback/item/captcha/lib.php index 3e19008286b..fa39c782835 100644 --- a/mod/feedback/item/captcha/lib.php +++ b/mod/feedback/item/captcha/lib.php @@ -120,8 +120,9 @@ class feedback_item_captcha extends feedback_item_base { $inputname = $item->typ . '_' . $item->id; if ($form->get_mode() != mod_feedback_complete_form::MODE_COMPLETE) { + // Span to hold the element id. The id is used for drag and drop reordering. $form->add_form_element($item, - ['static', $inputname, $name], + ['static', $inputname, $name, html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])], false, false); } else { diff --git a/mod/feedback/item/feedback_item_class.php b/mod/feedback/item/feedback_item_class.php index ddb3c432354..92eb10a4265 100644 --- a/mod/feedback/item/feedback_item_class.php +++ b/mod/feedback/item/feedback_item_class.php @@ -294,7 +294,11 @@ class feedback_item_pagebreak extends feedback_item_base { */ public function complete_form_element($item, $form) { $form->add_form_element($item, - ['static', $item->typ.'_'.$item->id, '', '
']); + ['static', + $item->typ.'_'.$item->id, + '', + html_writer::empty_tag('hr', ['class' => 'feedback_pagebreak', 'id' => 'feedback_item_' . $item->id]) + ]); } /** diff --git a/mod/feedback/item/label/lib.php b/mod/feedback/item/label/lib.php index 4ea3ae45261..6fa83bf5472 100644 --- a/mod/feedback/item/label/lib.php +++ b/mod/feedback/item/label/lib.php @@ -194,6 +194,7 @@ class feedback_item_label extends feedback_item_base { $context->id, 'mod_feedback', $filearea, $item->id); $formatoptions = array('overflowdiv' => true, 'noclean' => true); $output = format_text($output, FORMAT_HTML, $formatoptions); + $output = html_writer::div($output, '', ['id' => 'feedback_item_' . $item->id]); $inputname = $item->typ . '_' . $item->id; diff --git a/mod/feedback/item/multichoice/lib.php b/mod/feedback/item/multichoice/lib.php index 8e47ca0feee..d3b628bc16a 100644 --- a/mod/feedback/item/multichoice/lib.php +++ b/mod/feedback/item/multichoice/lib.php @@ -342,6 +342,8 @@ class feedback_item_multichoice extends feedback_item_base { $objs[] = ['advcheckbox', $inputname.'['.$idx.']', '', $label, null, array(0, $idx)]; $form->set_element_type($inputname.'['.$idx.']', PARAM_INT); } + // Span to hold the element id. The id is used for drag and drop reordering. + $objs[] = ['static', '', '', html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])]; $element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class); if ($tmpvalue) { foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $tmpvalue) as $v) { @@ -357,6 +359,8 @@ class feedback_item_multichoice extends feedback_item_base { foreach ($options as $idx => $label) { $objs[] = ['radio', $inputname.'[0]', '', $label, $idx]; } + // Span to hold the element id. The id is used for drag and drop reordering. + $objs[] = ['static', '', '', html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])]; $element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class); $form->set_element_default($inputname.'[0]', $tmpvalue); $form->set_element_type($inputname.'[0]', PARAM_INT); diff --git a/mod/feedback/item/multichoicerated/lib.php b/mod/feedback/item/multichoicerated/lib.php index 5eb16e37b1d..8be2faa97e8 100644 --- a/mod/feedback/item/multichoicerated/lib.php +++ b/mod/feedback/item/multichoicerated/lib.php @@ -312,6 +312,8 @@ class feedback_item_multichoicerated extends feedback_item_base { foreach ($options as $idx => $label) { $objs[] = ['radio', $inputname, '', $label, $idx]; } + // Span to hold the element id. The id is used for drag and drop reordering. + $objs[] = ['static', '', '', html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])]; $separator = $info->horizontal ? ' ' : '
'; $class .= ' multichoicerated-' . ($info->horizontal ? 'horizontal' : 'vertical'); $el = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class); diff --git a/mod/feedback/yui/dragdrop/dragdrop.js b/mod/feedback/yui/dragdrop/dragdrop.js index 31f117fab7e..ac4af8ff6d7 100644 --- a/mod/feedback/yui/dragdrop/dragdrop.js +++ b/mod/feedback/yui/dragdrop/dragdrop.js @@ -167,12 +167,20 @@ YUI.add('moodle-mod_feedback-dragdrop', function(Y) { if (!drop.contains(drag)) { drop.appendChild(drag); } - myElements = ''; + var childElement; + var elementId; + var elements = []; drop.all(CSS.DRAGITEM).each(function(v) { - myElements = myElements + ',' + this.get_node_id(v.get('id')); + childElement = v.one('.felement').one('[id^="feedback_item_"]'); + if (childElement) { + elementId = this.get_node_id(childElement.get('id')); + if (elements.indexOf(elementId) == -1) { + elements.push(elementId); + } + } }, this); var spinner = M.util.add_spinner(Y, dragnode); - this.save_item_order(this.cmid, myElements, spinner); + this.save_item_order(this.cmid, elements.toString(), spinner); } },