3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Drag-and-drop words into sentences question renderer class.
23 * @copyright 2010 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/question/type/gapselect/rendererbase.php');
34 * Generates the output for drag-and-drop words into sentences questions.
36 * @copyright 2010 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class qtype_ddwtos_renderer
extends qtype_elements_embedded_in_question_text_renderer
{
41 protected function qtext_classname() {
42 return 'qtext ddwtos_questionid_for_javascript';
45 protected function post_qtext_elements(question_attempt
$qa,
46 question_display_options
$options) {
48 $question = $qa->get_question();
50 foreach ($question->choices
as $group => $choices) {
51 $dragboxs .= $this->drag_boxes($qa, $group,
52 $question->get_ordered_choices($group), $options);
54 $result .= html_writer
::tag('div', $dragboxs,
55 array('class' => 'answercontainer'));
56 // We abuse the clear_wrong method to output the hidden form fields we
57 // want irrespective of whether we are actually clearing the wrong
58 // bits of the response.
59 if (!$options->clearwrong
) {
60 $result .= $this->clear_wrong($qa, false);
66 * Modify the contents of a drag/drop box to fix some IE-related problem.
67 * Unfortunately I don't have more details than that.
68 * @param string $string the box contents.
69 * @return string the box contents modified.
71 protected function dodgy_ie_fix($string) {
72 return '<sub> </sub>' . $string . '<sup> </sup>';
75 protected function embedded_element(question_attempt
$qa, $place,
76 question_display_options
$options) {
77 $question = $qa->get_question();
78 $group = $question->places
[$place];
79 $boxcontents = $this->dodgy_ie_fix(' ');
81 $value = $qa->get_last_qt_var($question->field($place));
84 'id' => $this->box_id($qa, 'p' . $place, $group),
85 'class' => 'slot group' . $group
88 if ($options->readonly
) {
89 $attributes['class'] .= ' readonly';
91 $attributes['tabindex'] = '0';
95 if ($options->correctness
) {
96 $response = $qa->get_last_qt_data();
97 $fieldname = $question->field($place);
98 if (array_key_exists($fieldname, $response)) {
99 $fraction = (int) ($response[$fieldname] ==
100 $question->get_right_choice_for($place));
101 $attributes['class'] .= ' ' . $this->feedback_class($fraction);
102 $feedbackimage = $this->feedback_image($fraction);
106 return html_writer
::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
109 protected function drag_boxes($qa, $group, $choices, question_display_options
$options) {
111 if ($options->readonly
) {
112 $readonly = ' readonly';
116 foreach ($choices as $key => $choice) {
117 //Bug 8632 - long text entry causes bug in drag and drop field in IE
118 $content = str_replace('-', '‑', $choice->text
);
119 $content = $this->dodgy_ie_fix(str_replace(' ', ' ', $content));
122 if ($choice->isinfinite
) {
123 $infinite = ' infinite';
126 $boxes .= html_writer
::tag('span', $content, array(
127 'id' => $this->box_id($qa, $key, $choice->draggroup
),
128 'class' => 'player group' . $choice->draggroup
. $infinite . $readonly)) . ' ';
131 return html_writer
::nonempty_tag('div', $boxes, array('class' => 'answertext'));
135 public function head_code(question_attempt
$qa) {
136 $this->page
->requires
->yui2_lib('dom');
137 $this->page
->requires
->yui2_lib('event');
138 $this->page
->requires
->yui2_lib('dragdrop');
139 return parent
::head_code($qa);
143 * Actually, this question type abuses this method to always ouptut the
144 * hidden fields it needs.
146 public function clear_wrong(question_attempt
$qa, $reallyclear = true) {
147 $question = $qa->get_question();
148 $response = $qa->get_last_qt_data();
150 if (!empty($response) && $reallyclear) {
151 $cleanresponse = $question->clear_wrong_from_response($response);
153 $cleanresponse = $response;
157 foreach ($question->places
as $place => $group) {
158 $fieldname = $question->field($place);
159 if (array_key_exists($fieldname, $response)) {
160 $value = $response[$fieldname];
164 if (array_key_exists($fieldname, $cleanresponse)) {
165 $cleanvalue = $cleanresponse[$fieldname];
169 if ($cleanvalue != $value) {
170 $output .= html_writer
::empty_tag('input', array(
172 'id' => $this->box_id($qa, 'p' . $place, $group) . '_hidden',
173 'value' => s($value))) .
174 html_writer
::empty_tag('input', array(
176 'name' => $qa->get_qt_field_name($fieldname),
177 'value' => s($cleanvalue)));
179 $output .= html_writer
::empty_tag('input', array(
181 'id' => $this->box_id($qa, 'p' . $place, $group) . '_hidden',
182 'name' => $qa->get_qt_field_name($fieldname),
183 'value' => s($value)));