MDL-47494 ddwtos: NOBUG making hidden inputs visible for easy debugging during develo...
[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 *
3a4ac328
TH
20 * @package qtype
21 * @subpackage ddwtos
22 * @copyright 2010 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
24 */
25
3a4ac328 26
9d55f4a5
TH
27defined('MOODLE_INTERNAL') || die();
28
61381e50
TH
29require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
30
31
32/**
33 * Generates the output for drag-and-drop words into sentences questions.
34 *
3a4ac328
TH
35 * @copyright 2010 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
37 */
38class qtype_ddwtos_renderer extends qtype_elements_embedded_in_question_text_renderer {
39
29929b5b 40 protected function qtext_classname() {
61381e50
TH
41 return 'qtext ddwtos_questionid_for_javascript';
42 }
43
29929b5b
TH
44 protected function post_qtext_elements(question_attempt $qa,
45 question_display_options $options) {
61381e50
TH
46 $result = '';
47 $question = $qa->get_question();
48 $dragboxs = '';
49 foreach ($question->choices as $group => $choices) {
50 $dragboxs .= $this->drag_boxes($qa, $group,
51 $question->get_ordered_choices($group), $options);
52 }
53 $result .= html_writer::tag('div', $dragboxs,
54 array('class' => 'answercontainer'));
55 // We abuse the clear_wrong method to output the hidden form fields we
56 // want irrespective of whether we are actually clearing the wrong
57 // bits of the response.
58 if (!$options->clearwrong) {
59 $result .= $this->clear_wrong($qa, false);
60 }
61 return $result;
62 }
63
64 /**
65 * Modify the contents of a drag/drop box to fix some IE-related problem.
66 * Unfortunately I don't have more details than that.
67 * @param string $string the box contents.
68 * @return string the box contents modified.
69 */
70 protected function dodgy_ie_fix($string) {
71 return '<sub>&#160;</sub>' . $string . '<sup>&#160;</sup>';
72 }
73
29929b5b
TH
74 protected function embedded_element(question_attempt $qa, $place,
75 question_display_options $options) {
61381e50
TH
76 $question = $qa->get_question();
77 $group = $question->places[$place];
78 $boxcontents = $this->dodgy_ie_fix('&#160;');
79
80 $value = $qa->get_last_qt_var($question->field($place));
81
82 $attributes = array(
83 'id' => $this->box_id($qa, 'p' . $place, $group),
84 'class' => 'slot group' . $group
85 );
86
87 if ($options->readonly) {
88 $attributes['class'] .= ' readonly';
89 } else {
90 $attributes['tabindex'] = '0';
91 }
92
93 $feedbackimage = '';
94 if ($options->correctness) {
95 $response = $qa->get_last_qt_data();
96 $fieldname = $question->field($place);
97 if (array_key_exists($fieldname, $response)) {
29929b5b
TH
98 $fraction = (int) ($response[$fieldname] ==
99 $question->get_right_choice_for($place));
61381e50
TH
100 $attributes['class'] .= ' ' . $this->feedback_class($fraction);
101 $feedbackimage = $this->feedback_image($fraction);
102 }
103 }
104
105 return html_writer::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
106 }
107
108 protected function drag_boxes($qa, $group, $choices, question_display_options $options) {
109 $readonly = '';
110 if ($options->readonly) {
111 $readonly = ' readonly';
112 }
113
114 $boxes = '';
115 foreach ($choices as $key => $choice) {
116 //Bug 8632 - long text entry causes bug in drag and drop field in IE
117 $content = str_replace('-', '&#x2011;', $choice->text);
118 $content = $this->dodgy_ie_fix(str_replace(' ', '&#160;', $content));
119
120 $infinite = '';
121 if ($choice->isinfinite) {
122 $infinite = ' infinite';
123 }
124
125 $boxes .= html_writer::tag('span', $content, array(
126 'id' => $this->box_id($qa, $key, $choice->draggroup),
127 'class' => 'player group' . $choice->draggroup . $infinite . $readonly)) . ' ';
128 }
129
130 return html_writer::nonempty_tag('div', $boxes, array('class' => 'answertext'));
131 }
132
133
134 public function head_code(question_attempt $qa) {
5ac7176c
TH
135 $this->page->requires->yui2_lib('dom');
136 $this->page->requires->yui2_lib('event');
137 $this->page->requires->yui2_lib('dragdrop');
61381e50
TH
138 return parent::head_code($qa);
139 }
140
141 /**
142 * Actually, this question type abuses this method to always ouptut the
143 * hidden fields it needs.
144 */
145 public function clear_wrong(question_attempt $qa, $reallyclear = true) {
146 $question = $qa->get_question();
147 $response = $qa->get_last_qt_data();
148
149 if (!empty($response) && $reallyclear) {
150 $cleanresponse = $question->clear_wrong_from_response($response);
151 } else {
152 $cleanresponse = $response;
153 }
154
155 $output = '';
156 foreach ($question->places as $place => $group) {
157 $fieldname = $question->field($place);
158 if (array_key_exists($fieldname, $response)) {
159 $value = $response[$fieldname];
160 } else {
161 $value = '0';
162 }
163 if (array_key_exists($fieldname, $cleanresponse)) {
164 $cleanvalue = $cleanresponse[$fieldname];
165 } else {
166 $cleanvalue = '0';
167 }
168 if ($cleanvalue != $value) {
169 $output .= html_writer::empty_tag('input', array(
022ab315 170 'type' => 'text',
61381e50
TH
171 'id' => $this->box_id($qa, 'p' . $place, $group) . '_hidden',
172 'value' => s($value))) .
173 html_writer::empty_tag('input', array(
174 'type' => 'hidden',
175 'name' => $qa->get_qt_field_name($fieldname),
176 'value' => s($cleanvalue)));
177 } else {
178 $output .= html_writer::empty_tag('input', array(
022ab315 179 'type' => 'text',
61381e50
TH
180 'id' => $this->box_id($qa, 'p' . $place, $group) . '_hidden',
181 'name' => $qa->get_qt_field_name($fieldname),
182 'value' => s($value)));
183 }
184 }
185 return $output;
186 }
187
188}