MDL-47494 ddwtos: NOBUG fix for ie7 not correctly calculating height of span with...
[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);
54a5be53
JP
48 $topnode = 'div.que.ddwtos#q'.$qa->get_slot();
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 }
54 $params = array('inputids' => $inputids,
55 'topnode' => $topnode,
c3e7201a
JP
56 'readonly' => $options->readonly);
57
58 $PAGE->requires->yui_module('moodle-qtype_ddwtos-dd', 'M.qtype_ddwtos.init_question',
59 array($params));
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();
67 $dragboxs = '';
68 foreach ($question->choices as $group => $choices) {
69 $dragboxs .= $this->drag_boxes($qa, $group,
70 $question->get_ordered_choices($group), $options);
71 }
54a5be53
JP
72 $classes = array('answercontainer');
73 if (!$options->readonly) {
74 $classes[] = 'notreadonly';
75 } else {
76 $classes[] = 'readonly';
77 }
78 $result .= html_writer::tag('div', $dragboxs, array('class' => implode(' ', $classes)));
79
80 $classes = array('drags');
81 if (!$options->readonly) {
82 $classes[] = 'notreadonly';
83 } else {
84 $classes[] = 'readonly';
85 }
86 $result .= html_writer::tag('div', '', array('class' => implode(' ', $classes)));
61381e50
TH
87 // We abuse the clear_wrong method to output the hidden form fields we
88 // want irrespective of whether we are actually clearing the wrong
89 // bits of the response.
90 if (!$options->clearwrong) {
91 $result .= $this->clear_wrong($qa, false);
92 }
93 return $result;
94 }
95
29929b5b
TH
96 protected function embedded_element(question_attempt $qa, $place,
97 question_display_options $options) {
61381e50
TH
98 $question = $qa->get_question();
99 $group = $question->places[$place];
54a5be53 100 $boxcontents = '&#160;';
61381e50
TH
101
102 $value = $qa->get_last_qt_var($question->field($place));
103
104 $attributes = array(
54a5be53 105 'class' => 'place'.$place.' drop group' . $group
61381e50
TH
106 );
107
108 if ($options->readonly) {
109 $attributes['class'] .= ' readonly';
110 } else {
111 $attributes['tabindex'] = '0';
112 }
113
114 $feedbackimage = '';
115 if ($options->correctness) {
116 $response = $qa->get_last_qt_data();
117 $fieldname = $question->field($place);
118 if (array_key_exists($fieldname, $response)) {
29929b5b
TH
119 $fraction = (int) ($response[$fieldname] ==
120 $question->get_right_choice_for($place));
61381e50
TH
121 $feedbackimage = $this->feedback_image($fraction);
122 }
123 }
124
125 return html_writer::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
126 }
127
128 protected function drag_boxes($qa, $group, $choices, question_display_options $options) {
61381e50
TH
129 $boxes = '';
130 foreach ($choices as $key => $choice) {
131 //Bug 8632 - long text entry causes bug in drag and drop field in IE
132 $content = str_replace('-', '&#x2011;', $choice->text);
54a5be53 133 $content = str_replace(' ', '&#160;', $content);
61381e50
TH
134
135 $infinite = '';
136 if ($choice->isinfinite) {
137 $infinite = ' infinite';
138 }
139
140 $boxes .= html_writer::tag('span', $content, array(
54a5be53 141 'class' => 'draghome choice'.$key.' group' . $choice->draggroup . $infinite)) . ' ';
61381e50
TH
142 }
143
54a5be53
JP
144 return html_writer::nonempty_tag('div', $boxes,
145 array('class' => 'draggrouphomes'. $choice->draggroup));
61381e50
TH
146 }
147
61381e50
TH
148 /**
149 * Actually, this question type abuses this method to always ouptut the
150 * hidden fields it needs.
151 */
152 public function clear_wrong(question_attempt $qa, $reallyclear = true) {
153 $question = $qa->get_question();
154 $response = $qa->get_last_qt_data();
155
156 if (!empty($response) && $reallyclear) {
157 $cleanresponse = $question->clear_wrong_from_response($response);
158 } else {
159 $cleanresponse = $response;
160 }
161
162 $output = '';
163 foreach ($question->places as $place => $group) {
164 $fieldname = $question->field($place);
165 if (array_key_exists($fieldname, $response)) {
166 $value = $response[$fieldname];
167 } else {
168 $value = '0';
169 }
170 if (array_key_exists($fieldname, $cleanresponse)) {
171 $cleanvalue = $cleanresponse[$fieldname];
172 } else {
173 $cleanvalue = '0';
174 }
175 if ($cleanvalue != $value) {
176 $output .= html_writer::empty_tag('input', array(
20dc02a4 177 'type' => 'hidden',
54a5be53 178 'id' => $this->box_id($qa, 'p' . $place),
61381e50
TH
179 'value' => s($value))) .
180 html_writer::empty_tag('input', array(
181 'type' => 'hidden',
182 'name' => $qa->get_qt_field_name($fieldname),
183 'value' => s($cleanvalue)));
184 } else {
185 $output .= html_writer::empty_tag('input', array(
20dc02a4 186 'type' => 'hidden',
54a5be53 187 'id' => $this->box_id($qa, 'p' . $place),
61381e50
TH
188 'name' => $qa->get_qt_field_name($fieldname),
189 'value' => s($value)));
190 }
191 }
192 return $output;
193 }
194
195}