MDL-47494 ddwtos: NOBUG added a yui3 module skeleton.
[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
c3e7201a
JP
44 public function formulation_and_controls(question_attempt $qa,
45 question_display_options $options) {
46 global $PAGE;
47 $result = parent::formulation_and_controls($qa, $options);
48 $topnode = 'div#q'.$qa->get_slot().' div.ddarea';
49 $params = array('topnode' => $topnode,
50 'readonly' => $options->readonly);
51
52 $PAGE->requires->yui_module('moodle-qtype_ddwtos-dd', 'M.qtype_ddwtos.init_question',
53 array($params));
54 return $result;
55 }
56
29929b5b
TH
57 protected function post_qtext_elements(question_attempt $qa,
58 question_display_options $options) {
61381e50
TH
59 $result = '';
60 $question = $qa->get_question();
61 $dragboxs = '';
62 foreach ($question->choices as $group => $choices) {
63 $dragboxs .= $this->drag_boxes($qa, $group,
64 $question->get_ordered_choices($group), $options);
65 }
66 $result .= html_writer::tag('div', $dragboxs,
67 array('class' => 'answercontainer'));
68 // We abuse the clear_wrong method to output the hidden form fields we
69 // want irrespective of whether we are actually clearing the wrong
70 // bits of the response.
71 if (!$options->clearwrong) {
72 $result .= $this->clear_wrong($qa, false);
73 }
74 return $result;
75 }
76
77 /**
78 * Modify the contents of a drag/drop box to fix some IE-related problem.
79 * Unfortunately I don't have more details than that.
80 * @param string $string the box contents.
81 * @return string the box contents modified.
82 */
83 protected function dodgy_ie_fix($string) {
84 return '<sub>&#160;</sub>' . $string . '<sup>&#160;</sup>';
85 }
86
29929b5b
TH
87 protected function embedded_element(question_attempt $qa, $place,
88 question_display_options $options) {
61381e50
TH
89 $question = $qa->get_question();
90 $group = $question->places[$place];
91 $boxcontents = $this->dodgy_ie_fix('&#160;');
92
93 $value = $qa->get_last_qt_var($question->field($place));
94
95 $attributes = array(
96 'id' => $this->box_id($qa, 'p' . $place, $group),
97 'class' => 'slot group' . $group
98 );
99
100 if ($options->readonly) {
101 $attributes['class'] .= ' readonly';
102 } else {
103 $attributes['tabindex'] = '0';
104 }
105
106 $feedbackimage = '';
107 if ($options->correctness) {
108 $response = $qa->get_last_qt_data();
109 $fieldname = $question->field($place);
110 if (array_key_exists($fieldname, $response)) {
29929b5b
TH
111 $fraction = (int) ($response[$fieldname] ==
112 $question->get_right_choice_for($place));
61381e50
TH
113 $attributes['class'] .= ' ' . $this->feedback_class($fraction);
114 $feedbackimage = $this->feedback_image($fraction);
115 }
116 }
117
118 return html_writer::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
119 }
120
121 protected function drag_boxes($qa, $group, $choices, question_display_options $options) {
122 $readonly = '';
123 if ($options->readonly) {
124 $readonly = ' readonly';
125 }
126
127 $boxes = '';
128 foreach ($choices as $key => $choice) {
129 //Bug 8632 - long text entry causes bug in drag and drop field in IE
130 $content = str_replace('-', '&#x2011;', $choice->text);
131 $content = $this->dodgy_ie_fix(str_replace(' ', '&#160;', $content));
132
133 $infinite = '';
134 if ($choice->isinfinite) {
135 $infinite = ' infinite';
136 }
137
138 $boxes .= html_writer::tag('span', $content, array(
139 'id' => $this->box_id($qa, $key, $choice->draggroup),
140 'class' => 'player group' . $choice->draggroup . $infinite . $readonly)) . ' ';
141 }
142
143 return html_writer::nonempty_tag('div', $boxes, array('class' => 'answertext'));
144 }
145
61381e50
TH
146 /**
147 * Actually, this question type abuses this method to always ouptut the
148 * hidden fields it needs.
149 */
150 public function clear_wrong(question_attempt $qa, $reallyclear = true) {
151 $question = $qa->get_question();
152 $response = $qa->get_last_qt_data();
153
154 if (!empty($response) && $reallyclear) {
155 $cleanresponse = $question->clear_wrong_from_response($response);
156 } else {
157 $cleanresponse = $response;
158 }
159
160 $output = '';
161 foreach ($question->places as $place => $group) {
162 $fieldname = $question->field($place);
163 if (array_key_exists($fieldname, $response)) {
164 $value = $response[$fieldname];
165 } else {
166 $value = '0';
167 }
168 if (array_key_exists($fieldname, $cleanresponse)) {
169 $cleanvalue = $cleanresponse[$fieldname];
170 } else {
171 $cleanvalue = '0';
172 }
173 if ($cleanvalue != $value) {
174 $output .= html_writer::empty_tag('input', array(
022ab315 175 'type' => 'text',
61381e50
TH
176 'id' => $this->box_id($qa, 'p' . $place, $group) . '_hidden',
177 'value' => s($value))) .
178 html_writer::empty_tag('input', array(
179 'type' => 'hidden',
180 'name' => $qa->get_qt_field_name($fieldname),
181 'value' => s($cleanvalue)));
182 } else {
183 $output .= html_writer::empty_tag('input', array(
022ab315 184 'type' => 'text',
61381e50
TH
185 'id' => $this->box_id($qa, 'p' . $place, $group) . '_hidden',
186 'name' => $qa->get_qt_field_name($fieldname),
187 'value' => s($value)));
188 }
189 }
190 return $output;
191 }
192
193}