From 701e76e3017ab06f2138b88e0660704382d5cb2d Mon Sep 17 00:00:00 2001 From: danghieu1407 Date: Tue, 18 Mar 2025 15:08:06 +0700 Subject: [PATCH] MDL-66584 qtype_ddimageortext: Move JSON data to data-attribute --- .../ddimageortext/amd/build/question.min.js | Bin 21382 -> 21472 bytes .../amd/build/question.min.js.map | Bin 64452 -> 64628 bytes .../type/ddimageortext/amd/src/question.js | 4 +- question/type/ddimageortext/rendererbase.php | 33 ++++++++-------- .../ddimageortext/tests/walkthrough_test.php | 2 + .../type/ddmarker/amd/build/question.min.js | Bin 18274 -> 18372 bytes .../ddmarker/amd/build/question.min.js.map | Bin 54361 -> 54501 bytes question/type/ddmarker/amd/src/question.js | 5 ++- question/type/ddmarker/renderer.php | 36 ++++++++---------- .../type/ddmarker/tests/walkthrough_test.php | 25 +++++++----- 10 files changed, 54 insertions(+), 51 deletions(-) diff --git a/question/type/ddimageortext/amd/build/question.min.js b/question/type/ddimageortext/amd/build/question.min.js index 9c293a384c9efc760ae3e79a05015bcaf0b5942a..f5a4fd282c6ba6079d822371e419c975a8b69fa7 100644 GIT binary patch delta 114 zcmZo$&iG(CjzC5f4NsYRYC+DZy~DMk4ORrz_T#Y&obDTyVC#i=EFVDmil O((*N}H_vo=%Lf3u11h5c delta 27 jcmaE`oUv^=?6_)*YW@T`*Qn@BMC59$g;a zf4Vd8osvB!$i!ddxbD{CTfcgL=cC{MB(uy&3?M-=02wDi9s%SSDNs7) zvQ7a^T49ol$;z1+$zcr(@(%~0?XW5|KoK=G@Y$eEl_&s8IZ3Lxu2Ls1RS8r&tkmOj z+oU8b`jnCqj8d3tCW#85k|kh3LJI!R^|)s{7d2>6Ifn^Gfj#~QsK@}ytWPqfk}_Uy zD`It;+DYj^w8scbLX$!J1!awYIlMDa(Rk|UMyRZHFSMq0Z`_QcA4)UQNC^8=ilB#b z*Kee%uukdlxl-Oba$l@!B!EiRC=M#Uh@iJeSKT(1ETn}es}7aginJq6INPFg{QJ?2 ze>x&&k+pg%Y>k+BY*I@_F!M9BVMb}n4P zl@)vK)kM8)XR?inmv++@+dJ39&#imn&H3@o8|Q?zFeXZ0uXetDy#JsxwhNxg)B=Mj zq6PDa^GypD5pAYEQVi0_%4|9;KJ6uk8yuDiS)Q7ryZ8J~+kz4y)@YV=B!ets)*l5M z*$iuo<$dGzI)|Lp z7YB~WA%zUVFy+%b$o+_^a8{*2=#(Z9M)vq0q8vjku#+S{4h5NHJyeYAq9tJ;xLqgL z1{WI#Q)g$n_;YaEF!*u%!9fqJVL83I8md^3Kunk_85RgKh#3c>6Y4q@DyF)Vv1txI zS+_`FVRCMkYff#4s1cT6%^;y9fXNc+)XXcjr4!c0mtXHjA4c`i z%9Xqx@{9QI&*KA`RmA&GhZ_-PSu-zDedyov$vzaC zWv8y>lR(y_azy 'dropbackground img-fluid w-100']); - $output .= html_writer::div('', 'dropzones'); + // Note, the mobile app implementation of ddimageortext relies on extracting the + // blob of places data out of the rendered HTML, which makes it impossible + // to clean up this structure of otherwise unnecessary stuff. + $placeinfoforjsandmobileapp = []; + foreach ($question->places as $placeno => $place) { + $varname = $question->field($placeno); + [$fieldname, $html] = $this->hidden_field_for_qt_var($qa, $varname, null, + ['placeinput', 'place' . $placeno, 'group' . $place->group]); + $output .= $html; + $placeinfo = (object) (array) $place; + $placeinfo->fieldname = $fieldname; + $placeinfoforjsandmobileapp[$placeno] = $placeinfo; + } + + $output .= html_writer::div('', 'dropzones', ['data-place-info' => json_encode($placeinfoforjsandmobileapp)]); $output .= html_writer::end_div(); $output .= html_writer::start_div($draghomesclass); @@ -103,26 +117,11 @@ class qtype_ddtoimage_renderer_base extends qtype_with_combined_feedback_rendere $output .= $dragimagehomes; $output .= html_writer::end_div(); - // Note, the mobile app implementation of ddimageortext relies on extracting the - // blob of places data out of the rendered HTML, which makes it impossible - // to clean up this structure of otherwise unnecessary stuff. - $placeinfoforjsandmobileapp = []; - foreach ($question->places as $placeno => $place) { - $varname = $question->field($placeno); - [$fieldname, $html] = $this->hidden_field_for_qt_var($qa, $varname, null, - ['placeinput', 'place' . $placeno, 'group' . $place->group]); - $output .= $html; - $placeinfo = (object) (array) $place; - $placeinfo->fieldname = $fieldname; - $placeinfo->text = format_string($placeinfo->text); - $placeinfoforjsandmobileapp[$placeno] = $placeinfo; - } - $output .= html_writer::end_div(); $this->page->requires->string_for_js('blank', 'qtype_ddimageortext'); $this->page->requires->js_call_amd('qtype_ddimageortext/question', 'init', - [$qa->get_outer_question_div_unique_id(), $options->readonly, $placeinfoforjsandmobileapp]); + [$qa->get_outer_question_div_unique_id(), $options->readonly]); if ($qa->get_state() == question_state::$invalid) { $output .= html_writer::div($question->get_validation_error($qa->get_last_qt_data()), 'validationerror'); diff --git a/question/type/ddimageortext/tests/walkthrough_test.php b/question/type/ddimageortext/tests/walkthrough_test.php index fd98d4e5ab2..4404c1c92da 100644 --- a/question/type/ddimageortext/tests/walkthrough_test.php +++ b/question/type/ddimageortext/tests/walkthrough_test.php @@ -68,6 +68,8 @@ final class walkthrough_test extends \qbehaviour_walkthrough_test_base { $this->check_current_mark(null); $this->check_current_output( + new \question_pattern_expectation( + '~
get_contains_drag_image_home_expectation(1, 1, 1), $this->get_contains_drag_image_home_expectation(2, 2, 1), $this->get_contains_drag_image_home_expectation(3, 1, 2), diff --git a/question/type/ddmarker/amd/build/question.min.js b/question/type/ddmarker/amd/build/question.min.js index 0d5612c648076876dbec17d1a914917d26ea3a7d..6313546e6461c35d7cfa280770db6aee7284f8b0 100644 GIT binary patch delta 142 zcmaFV$9SZlal<~Fdd=$O{Ji24g|f`z%%q%Dm!kZFsQkRtVq33Ze?Pr~#G>L*&`~p&v$>m>;Lt>|I?Sp z{Lk}29@-e?=T8puhtE#L>iJrJxoCu|W@X7N8#Tvu$Boon(o;!SYsN&z)a{Z{wmhr0 zQEjG-qM=)6b;32~a=0D3S+>&ovQeGQ;Frt62Ny2#SNY?^nY5Lz8J4G`oe|>e{P)HX zfAQqp*5)bxar5l<%Z)9k`-rTQI6F*yJHi#lwy(Z%U0K#{GZF(xkQ6|wnjm)o(u`y& zup(t7PXS7_qzXu^5+(^mmK2FCM2HTclUe*_NuEv&V3UfSAR2(iNDW1ny$WyaES$-z zWEDSiG^g?(I}-yMV!@c?$9CP9{0UOPeN0-9NM$D1su7X~7G!>&+zpOZRN)D-)DDv- zzBH*7CklX)Pm+=}+2C(?FVFQpdQy(pw6vuZ#F&8LBfzTx-((nPXB;O%g07nZu|vqUHIN>8%T8F^3A>H+;i@^^YcUhxA*-YzwBW7 z)ew#jT#o0Qz|nk9bG%J0SM@8}mhZVWE%3$5X=+YQt2os_^YdCzaJ9Va<{mlObsTv; zj_sXs{53FvpPpYpZFdO2?w;NI{OslA-qW{JGgFy+j3fapk^#t6Epi{gVe+xe$OZ*g zV)aEl)w**gZm4&GCJc01*9Vit1#la<+nSwdT4bF9__V?m4^y=&Ck`;3HSv2(?_0x> z_UPEWMarVHOsh+BY?6e<49}=>Jm0>Q{;L|JV26zIk^=dZtN<%e-695nQA`mVz-EL4 zaI!2E%d$s3ldOsntJKGL?Q4BiY_-RA-|Wt<^s1#rA<}3p_!db6q&X=HP?3@>Kvoi! zm_iO8Bk|V$^hnJl5g~bv5)16xA&T4kW7`|FBcd)Sn#Te>DoI(C)p(~at^3DyLkeXL zwqeSx*=luLvX$l5Y1c&4B)&q;bvZ0B=2%1-&L~8bk4i}X9}o;3+z78K_LaGw@koy` zp+<#ROp-AG#g^ds;^69%_$DO%FX*wU0^3kKK2H%C`y!l`Z~3#3LN W?J>e2vsj|)_t90B;^<<=nex< diff --git a/question/type/ddmarker/amd/src/question.js b/question/type/ddmarker/amd/src/question.js index 6ff3d7df726..5d7dfe0d911 100644 --- a/question/type/ddmarker/amd/src/question.js +++ b/question/type/ddmarker/amd/src/question.js @@ -846,9 +846,10 @@ define([ * * @param {String} containerId the id of the div.que that contains this question. * @param {boolean} readOnly whether the question is read-only. - * @param {Object[]} visibleDropZones data on any drop zones to draw as part of the feedback. */ - init: function(containerId, readOnly, visibleDropZones) { + init: function(containerId, readOnly) { + const visibleDropZones = JSON.parse(document.querySelector('#' + containerId + ' .dropzones') + .dataset.visibledDropzones); questionManager.questions[containerId] = new DragDropMarkersQuestion(containerId, readOnly, visibleDropZones); if (!questionManager.eventHandlersInitialised) { diff --git a/question/type/ddmarker/renderer.php b/question/type/ddmarker/renderer.php index 48f4c298592..f54fd3b173f 100644 --- a/question/type/ddmarker/renderer.php +++ b/question/type/ddmarker/renderer.php @@ -60,7 +60,17 @@ class qtype_ddmarker_renderer extends qtype_ddtoimage_renderer_base { $output .= html_writer::img(self::get_url_for_image($qa, 'bgimage'), get_string('dropbackground', 'qtype_ddmarker'), ['class' => 'dropbackground img-fluid w-100']); - $output .= html_writer::div('', 'dropzones'); + $visibledropzones = []; + if ($question->showmisplaced && $qa->get_state()->is_finished()) { + $visibledropzones = $question->get_drop_zones_without_hit($response); + if (count($visibledropzones) !== 0) { + $wrongpartsstringspans = []; + foreach ($visibledropzones as $visibledropzone) { + $wrongpartsstringspans[] = html_writer::span($visibledropzone->markertext, 'wrongpart'); + } + } + } + $output .= html_writer::div('', 'dropzones', ['data-visibled-dropzones' => json_encode($visibledropzones)]); $output .= html_writer::div('', 'markertexts'); $output .= html_writer::end_div(); @@ -91,33 +101,19 @@ class qtype_ddmarker_renderer extends qtype_ddtoimage_renderer_base { $output .= html_writer::end_div(); $output .= html_writer::end_div(); - if ($question->showmisplaced && $qa->get_state()->is_finished()) { - $visibledropzones = $question->get_drop_zones_without_hit($response); - } else { - $visibledropzones = []; - } - if ($qa->get_state() == question_state::$invalid) { $output .= html_writer::div($question->get_validation_error($qa->get_last_qt_data()), 'validationerror'); } - if ($question->showmisplaced && $qa->get_state()->is_finished()) { - $wrongparts = $question->get_drop_zones_without_hit($response); - if (count($wrongparts) !== 0) { - $wrongpartsstringspans = []; - foreach ($wrongparts as $wrongpart) { - $wrongpartsstringspans[] = html_writer::span($wrongpart->markertext, 'wrongpart'); - } - $wrongpartsstring = join(', ', $wrongpartsstringspans); - $output .= html_writer::span(get_string('followingarewrongandhighlighted', 'qtype_ddmarker', $wrongpartsstring), - 'wrongparts'); - } + if (count($visibledropzones) !== 0) { + $wrongpartsstring = join(', ', $wrongpartsstringspans); + $output .= html_writer::span(get_string('followingarewrongandhighlighted', 'qtype_ddmarker', $wrongpartsstring), + 'wrongparts'); } $output .= html_writer::div($hiddenfields, 'ddform'); - $this->page->requires->js_call_amd('qtype_ddmarker/question', 'init', - [$qa->get_outer_question_div_unique_id(), $options->readonly, $visibledropzones]); + [$qa->get_outer_question_div_unique_id(), $options->readonly]); return $output; } diff --git a/question/type/ddmarker/tests/walkthrough_test.php b/question/type/ddmarker/tests/walkthrough_test.php index 38b8f29a989..89f3f6bad74 100644 --- a/question/type/ddmarker/tests/walkthrough_test.php +++ b/question/type/ddmarker/tests/walkthrough_test.php @@ -96,16 +96,18 @@ final class walkthrough_test extends \qbehaviour_walkthrough_test_base { $this->check_current_mark(null); $this->check_current_output( - $this->get_contains_draggable_marker_home_expectation(1, false), - $this->get_contains_draggable_marker_home_expectation(2, false), - $this->get_contains_draggable_marker_home_expectation(3, false), - $this->get_contains_hidden_expectation(1), - $this->get_contains_hidden_expectation(2), - $this->get_contains_hidden_expectation(3), - $this->get_contains_submit_button_expectation(true), - $this->get_does_not_contain_feedback_expectation(), - $this->get_tries_remaining_expectation(3), - $this->get_no_hint_visible_expectation()); + new \question_pattern_expectation( + '~
get_contains_draggable_marker_home_expectation(1, false), + $this->get_contains_draggable_marker_home_expectation(2, false), + $this->get_contains_draggable_marker_home_expectation(3, false), + $this->get_contains_hidden_expectation(1), + $this->get_contains_hidden_expectation(2), + $this->get_contains_hidden_expectation(3), + $this->get_contains_submit_button_expectation(true), + $this->get_does_not_contain_feedback_expectation(), + $this->get_tries_remaining_expectation(3), + $this->get_no_hint_visible_expectation()); $completelywrong = array('c1' => '0,250', 'c2' => '100,250', 'c3' => '150,250'); // Save the wrong answer. @@ -321,6 +323,7 @@ final class walkthrough_test extends \qbehaviour_walkthrough_test_base { // Create a drag-and-drop question. $dd = \test_question_maker::make_question('ddmarker'); $dd->shufflechoices = false; + $dd->showmisplaced = true; $this->start_attempt_at_question($dd, 'deferredfeedback', 3); // Check the initial state. @@ -358,6 +361,8 @@ final class walkthrough_test extends \qbehaviour_walkthrough_test_base { $this->check_current_state(question_state::$gradedpartial); $this->check_current_mark(2); $this->check_current_output( + new \question_pattern_expectation( + '~
get_contains_draggable_marker_home_expectation(1, false), $this->get_contains_draggable_marker_home_expectation(2, false), $this->get_contains_draggable_marker_home_expectation(3, false), -- 2.43.0