MDL-79863 qtype_ordering: qtype_ordering improvements for compatability with AMD in Moodle >= 2.9. Many thanks to Jakob Ackermann (github.com/jacac)

This commit is contained in:
Gordon Bateson
2024-04-04 10:59:25 +08:00
committed by Mathew May
parent e72e1a6078
commit c40caa6a09
4 changed files with 22 additions and 12 deletions
+4
View File
@@ -51,6 +51,10 @@ class qtype_ordering extends question_type {
// Include "script.js" and/or "script.php" in the normal way.
parent::find_standard_scripts();
if (method_exists($PAGE->requires, 'js_call_amd')) {
return true; // Moodle >= 2.9
}
$version = '';
$minversion = '1.11.0'; // Moodle 2.7.
$search = '/jquery-([0-9.]+)(\.min)?\.js$/';
+14 -10
View File
@@ -56,7 +56,7 @@ class qtype_ordering_renderer extends qtype_with_combined_feedback_renderer {
* @return string HTML fragment.
*/
public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
global $CFG, $DB;
global $CFG, $DB, $PAGE;
$question = $qa->get_question();
$response = $qa->get_last_qt_data();
@@ -90,7 +90,12 @@ class qtype_ordering_renderer extends qtype_with_combined_feedback_renderer {
if ($options->readonly) {
// Items cannot be dragged in readonly mode.
} else if (method_exists($PAGE->requires, 'js_call_amd')) {
// Moodle >= 2.9
$params = array($sortableid, $responseid, $ablockid, $axis);
$PAGE->requires->js_call_amd('qtype_ordering/ordering', 'init', $params);
} else {
// Moodle <= 2.8
$script = "\n";
$script .= "//<![CDATA[\n";
$script .= "if (window.$) {\n";
@@ -100,15 +105,7 @@ class qtype_ordering_renderer extends qtype_with_combined_feedback_renderer {
$script .= " containment: '#$ablockid',\n";
$script .= " opacity: 0.6,\n";
$script .= " update: function(event, ui){\n";
$script .= " if (typeof($(this).sortable)=='function') {\n";
$script .= " var ItemsOrder = $(this).sortable('toArray');\n";
$script .= " } else {\n";
$script .= " // fix for Moodle 3.4, in which 'sortable' method disappears !!\n";
$script .= " var ItemsOrder = [];\n";
$script .= " $(this).children('li.sortableitem').each(function(){\n";
$script .= " ItemsOrder.push($(this).prop('id'));\n";
$script .= " });\n";
$script .= " }\n";
$script .= " var ItemsOrder = $(this).sortable('toArray');\n";
$script .= " $('#$responseid').val(ItemsOrder.toString());\n";
$script .= " }\n";
$script .= " });\n";
@@ -128,6 +125,10 @@ class qtype_ordering_renderer extends qtype_with_combined_feedback_renderer {
$printeditems = false;
if (count($currentresponse)) {
// initialize cache of answer md5keys
// this represents the initial position of the items
$md5keys = array();
// Set layout class.
$layoutclass = $question->get_ordering_layoutclass();
@@ -179,6 +180,9 @@ class qtype_ordering_renderer extends qtype_with_combined_feedback_renderer {
// Therefore we use the $answer's md5key for the "id".
$params = array('class' => $class, 'id' => $answer->md5key);
$result .= html_writer::tag('li', $img.$answertext, $params);
// cache this answer key
$md5keys[] = $question->answers[$answerid]->md5key;
}
}
+2
View File
@@ -30,6 +30,8 @@
.que.ordering ul.sortablelist li.sortableitem {
position : relative;
cursor : move;
touch-action : none;
-ms-touch-action : none;
}
.que.ordering ul.sortablelist li.horizontal {
float : left;
+2 -2
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 = 2017112366;
$plugin->release = '2017-11-23 (66)';
$plugin->version = 2018011667;
$plugin->release = '2018-01-16 (67)';