Merge branch 'MDL-70248-310' of https://github.com/HuongNV13/moodle into MOODLE_310_S...
[moodle.git] / question / type / ddmarker / renderer.php
CommitLineData
b3878dbd
JP
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
2a71bf53 18 * Drag-and-drop markers question renderer class.
b3878dbd 19 *
8bc1d28b
EM
20 * @package qtype_ddmarker
21 * @copyright 2012 The Open University
22 * @author Jamie Pratt <me@jamiep.org>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b3878dbd
JP
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
73f356a4 29require_once($CFG->dirroot . '/question/type/rendererbase.php');
dda954a2 30require_once($CFG->dirroot . '/question/type/ddimageortext/rendererbase.php');
b3878dbd
JP
31
32
33/**
2a71bf53 34 * Generates the output for drag-and-drop markers questions.
b3878dbd
JP
35 *
36 * @copyright 2010 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
dda954a2 39class qtype_ddmarker_renderer extends qtype_ddtoimage_renderer_base {
ddbea1ae
JP
40 public function formulation_and_controls(question_attempt $qa,
41 question_display_options $options) {
b3878dbd 42
ddbea1ae
JP
43 $question = $qa->get_question();
44 $response = $qa->get_last_qt_data();
d2fa9e79 45 $componentname = $question->qtype->plugin_name();
ddbea1ae
JP
46
47 $questiontext = $question->format_questiontext($qa);
48
d2fa9e79
HN
49 $dropareaclass = 'droparea';
50 $draghomesclass = 'draghomes';
51 if ($options->readonly) {
52 $dropareaclass .= ' readonly';
53 $draghomesclass .= ' readonly';
54 }
55
56 $output = html_writer::div($questiontext, 'qtext');
ddbea1ae 57
d2fa9e79
HN
58 $output .= html_writer::start_div('ddarea');
59 $output .= html_writer::start_div($dropareaclass);
60 $output .= html_writer::img(self::get_url_for_image($qa, 'bgimage'), get_string('dropbackground', 'qtype_ddmarker'),
49f1803c 61 ['class' => 'dropbackground img-fluid w-100']);
ddbea1ae 62
d2fa9e79
HN
63 $output .= html_writer::div('', 'dropzones');
64 $output .= html_writer::div('', 'markertexts');
612607d6 65
d2fa9e79
HN
66 $output .= html_writer::end_div();
67 $output .= html_writer::start_div($draghomesclass);
ddbea1ae 68
ddbea1ae 69 $orderedgroup = $question->get_ordered_choices(1);
a98db2a9
JP
70 $hiddenfields = '';
71 foreach ($orderedgroup as $choiceno => $drag) {
d2fa9e79
HN
72 $classes = ['marker', 'choice' . $choiceno];
73 $attr = [];
a98db2a9 74 if ($drag->infinite) {
ddbea1ae 75 $classes[] = 'infinite';
e1f5f601 76 } else {
d2fa9e79 77 $classes[] = 'dragno' . $drag->noofdrags;
ddbea1ae 78 }
d2fa9e79
HN
79 if (!$options->readonly) {
80 $attr['tabindex'] = 0;
81 }
82 $dragoutput = html_writer::start_span(join(' ', $classes), $attr);
83 $targeticonhtml = $this->output->image_icon('crosshairs', '', $componentname, ['class' => 'target']);
84 $markertext = html_writer::span($drag->text, 'markertext');
85 $dragoutput .= $targeticonhtml . $markertext;
86 $dragoutput .= html_writer::end_span();
87 $output .= $dragoutput;
e1f5f601 88 $hiddenfields .= $this->hidden_field_choice($qa, $choiceno, $drag->infinite, $drag->noofdrags);
ddbea1ae 89 }
ddbea1ae 90
d2fa9e79
HN
91 $output .= html_writer::end_div();
92 $output .= html_writer::end_div();
e47c7eae 93
43964253 94 if ($question->showmisplaced && $qa->get_state()->is_finished()) {
e47c7eae
JP
95 $visibledropzones = $question->get_drop_zones_without_hit($response);
96 } else {
d2fa9e79 97 $visibledropzones = [];
e47c7eae
JP
98 }
99
ddbea1ae 100 if ($qa->get_state() == question_state::$invalid) {
d2fa9e79 101 $output .= html_writer::div($question->get_validation_error($qa->get_last_qt_data()), 'validationerror');
ddbea1ae 102 }
43964253
JP
103
104 if ($question->showmisplaced && $qa->get_state()->is_finished()) {
70c434ee 105 $wrongparts = $question->get_drop_zones_without_hit($response);
c28563d2 106 if (count($wrongparts) !== 0) {
d2fa9e79 107 $wrongpartsstringspans = [];
5263d6d7 108 foreach ($wrongparts as $wrongpart) {
d2fa9e79 109 $wrongpartsstringspans[] = html_writer::span($wrongpart->markertext, 'wrongpart');
c28563d2 110 }
7401a914 111 $wrongpartsstring = join(', ', $wrongpartsstringspans);
d2fa9e79
HN
112 $output .= html_writer::span(get_string('followingarewrongandhighlighted', 'qtype_ddmarker', $wrongpartsstring),
113 'wrongparts');
e47c7eae 114 }
e47c7eae 115 }
70c434ee 116
d2fa9e79
HN
117 $output .= html_writer::div($hiddenfields, 'ddform');
118
119 $this->page->requires->js_call_amd('qtype_ddmarker/question', 'init',
120 [$qa->get_outer_question_div_unique_id(), $options->readonly, $visibledropzones]);
121
ddbea1ae
JP
122 return $output;
123 }
d2fa9e79 124
e1f5f601 125 protected function hidden_field_choice(question_attempt $qa, $choiceno, $infinite, $noofdrags, $value = null) {
a98db2a9 126 $varname = 'c'.$choiceno;
e1f5f601 127 $classes = array('choices', 'choice'.$choiceno, 'noofdrags'.$noofdrags);
10cbf24a
JP
128 if ($infinite) {
129 $classes[] = 'infinite';
130 }
5263d6d7 131 list(, $html) = $this->hidden_field_for_qt_var($qa, $varname, null, $classes);
a98db2a9
JP
132 return $html;
133 }
70c434ee 134
5263d6d7 135 protected function hint(question_attempt $qa, question_hint $hint) {
6f6d6e2f 136 $output = '';
70c434ee
JP
137 $question = $qa->get_question();
138 $response = $qa->get_last_qt_data();
139 if ($hint->statewhichincorrect) {
f76c454b
JP
140 $wrongdrags = $question->get_wrong_drags($response);
141 $wrongparts = array();
5263d6d7 142 foreach ($wrongdrags as $wrongdrag) {
f76c454b
JP
143 $wrongparts[] = html_writer::nonempty_tag('span',
144 $wrongdrag, array('class' => 'wrongpart'));
70c434ee 145 }
c9cf5af4 146 $output .= html_writer::nonempty_tag('div',
f76c454b 147 get_string('followingarewrong', 'qtype_ddmarker', join(', ', $wrongparts)),
70c434ee
JP
148 array('class' => 'wrongparts'));
149 }
c9cf5af4
JP
150 $output .= parent::hint($qa, $hint);
151 return $output;
70c434ee 152 }
e1f5f601 153}