MDL-63277 qtype_ddwtos: Re-implement old YUI JS in AMD
[moodle.git] / question / type / ddwtos / renderer.php
CommitLineData
61381e50 1<?php
61381e50
TH
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
61381e50
TH
17/**
18 * Drag-and-drop words into sentences question renderer class.
19 *
8bc1d28b
EM
20 * @package qtype_ddwtos
21 * @copyright 2010 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
23 */
24
3a4ac328 25
9d55f4a5
TH
26defined('MOODLE_INTERNAL') || die();
27
61381e50
TH
28require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
29
30
31/**
32 * Generates the output for drag-and-drop words into sentences questions.
33 *
3a4ac328
TH
34 * @copyright 2010 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
36 */
37class qtype_ddwtos_renderer extends qtype_elements_embedded_in_question_text_renderer {
38
29929b5b 39 protected function qtext_classname() {
61381e50
TH
40 return 'qtext ddwtos_questionid_for_javascript';
41 }
42
c3e7201a
JP
43 public function formulation_and_controls(question_attempt $qa,
44 question_display_options $options) {
45 global $PAGE;
8edf95a2 46
c3e7201a 47 $result = parent::formulation_and_controls($qa, $options);
8edf95a2 48
54a5be53
JP
49 $inputids = array();
50 $question = $qa->get_question();
51 foreach ($question->places as $placeno => $place) {
52 $inputids[$placeno] = $this->box_id($qa, $question->field($placeno));
53 }
8edf95a2
TH
54 $params = array(
55 'inputids' => $inputids,
56 'topnode' => 'div.que.ddwtos#q' . $qa->get_slot(),
57 'readonly' => $options->readonly
58 );
68943aff 59 $PAGE->requires->js_call_amd('qtype_ddwtos/ddwtos', 'init', array($params));
c3e7201a
JP
60 return $result;
61 }
62
29929b5b
TH
63 protected function post_qtext_elements(question_attempt $qa,
64 question_display_options $options) {
61381e50
TH
65 $result = '';
66 $question = $qa->get_question();
8edf95a2 67
61381e50
TH
68 $dragboxs = '';
69 foreach ($question->choices as $group => $choices) {
70 $dragboxs .= $this->drag_boxes($qa, $group,
71 $question->get_ordered_choices($group), $options);
72 }
8edf95a2 73
54a5be53
JP
74 $classes = array('answercontainer');
75 if (!$options->readonly) {
76 $classes[] = 'notreadonly';
77 } else {
78 $classes[] = 'readonly';
79 }
80 $result .= html_writer::tag('div', $dragboxs, array('class' => implode(' ', $classes)));
81
82 $classes = array('drags');
83 if (!$options->readonly) {
84 $classes[] = 'notreadonly';
85 } else {
86 $classes[] = 'readonly';
87 }
88 $result .= html_writer::tag('div', '', array('class' => implode(' ', $classes)));
8edf95a2 89
61381e50
TH
90 // We abuse the clear_wrong method to output the hidden form fields we
91 // want irrespective of whether we are actually clearing the wrong
92 // bits of the response.
93 if (!$options->clearwrong) {
94 $result .= $this->clear_wrong($qa, false);
95 }
96 return $result;
97 }
98
29929b5b
TH
99 protected function embedded_element(question_attempt $qa, $place,
100 question_display_options $options) {
61381e50
TH
101 $question = $qa->get_question();
102 $group = $question->places[$place];
b5cbcc4d
JB
103 $boxcontents = '&#160;' . html_writer::tag('span',
104 get_string('blank', 'qtype_ddwtos'), array('class' => 'accesshide'));
61381e50
TH
105
106 $value = $qa->get_last_qt_var($question->field($place));
107
108 $attributes = array(
8edf95a2 109 'class' => 'place' . $place . ' drop group' . $group
61381e50
TH
110 );
111
112 if ($options->readonly) {
113 $attributes['class'] .= ' readonly';
114 } else {
115 $attributes['tabindex'] = '0';
116 }
117
118 $feedbackimage = '';
119 if ($options->correctness) {
120 $response = $qa->get_last_qt_data();
121 $fieldname = $question->field($place);
122 if (array_key_exists($fieldname, $response)) {
29929b5b
TH
123 $fraction = (int) ($response[$fieldname] ==
124 $question->get_right_choice_for($place));
61381e50
TH
125 $feedbackimage = $this->feedback_image($fraction);
126 }
127 }
128
129 return html_writer::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
130 }
131
132 protected function drag_boxes($qa, $group, $choices, question_display_options $options) {
61381e50
TH
133 $boxes = '';
134 foreach ($choices as $key => $choice) {
a5290cd8 135 // Bug 8632: long text entry causes bug in drag and drop field in IE.
61381e50 136 $content = str_replace('-', '&#x2011;', $choice->text);
54a5be53 137 $content = str_replace(' ', '&#160;', $content);
61381e50
TH
138
139 $infinite = '';
efff972a 140 if ($choice->infinite) {
61381e50
TH
141 $infinite = ' infinite';
142 }
143
144 $boxes .= html_writer::tag('span', $content, array(
8edf95a2
TH
145 'class' => 'draghome choice' . $key . ' group' .
146 $choice->draggroup . $infinite)) . ' ';
61381e50
TH
147 }
148
54a5be53 149 return html_writer::nonempty_tag('div', $boxes,
8edf95a2 150 array('class' => 'draggrouphomes' . $choice->draggroup));
61381e50
TH
151 }
152
61381e50
TH
153 /**
154 * Actually, this question type abuses this method to always ouptut the
155 * hidden fields it needs.
156 */
157 public function clear_wrong(question_attempt $qa, $reallyclear = true) {
158 $question = $qa->get_question();
159 $response = $qa->get_last_qt_data();
160
161 if (!empty($response) && $reallyclear) {
162 $cleanresponse = $question->clear_wrong_from_response($response);
163 } else {
164 $cleanresponse = $response;
165 }
166
167 $output = '';
168 foreach ($question->places as $place => $group) {
169 $fieldname = $question->field($place);
170 if (array_key_exists($fieldname, $response)) {
171 $value = $response[$fieldname];
172 } else {
173 $value = '0';
174 }
175 if (array_key_exists($fieldname, $cleanresponse)) {
176 $cleanvalue = $cleanresponse[$fieldname];
177 } else {
178 $cleanvalue = '0';
179 }
180 if ($cleanvalue != $value) {
181 $output .= html_writer::empty_tag('input', array(
20dc02a4 182 'type' => 'hidden',
54a5be53 183 'id' => $this->box_id($qa, 'p' . $place),
61381e50
TH
184 'value' => s($value))) .
185 html_writer::empty_tag('input', array(
186 'type' => 'hidden',
187 'name' => $qa->get_qt_field_name($fieldname),
188 'value' => s($cleanvalue)));
189 } else {
190 $output .= html_writer::empty_tag('input', array(
20dc02a4 191 'type' => 'hidden',
54a5be53 192 'id' => $this->box_id($qa, 'p' . $place),
61381e50
TH
193 'name' => $qa->get_qt_field_name($fieldname),
194 'value' => s($value)));
195 }
196 }
197 return $output;
198 }
199
200}