MDL-47494 ddwtos: Add lots of missing public/protected/private.
[moodle.git] / question / type / ddwtos / renderer.php
CommitLineData
61381e50
TH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
61381e50
TH
18/**
19 * Drag-and-drop words into sentences question renderer class.
20 *
3a4ac328
TH
21 * @package qtype
22 * @subpackage ddwtos
23 * @copyright 2010 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
25 */
26
3a4ac328 27
9d55f4a5
TH
28defined('MOODLE_INTERNAL') || die();
29
61381e50
TH
30require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
31
32
33/**
34 * Generates the output for drag-and-drop words into sentences questions.
35 *
3a4ac328
TH
36 * @copyright 2010 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
38 */
39class qtype_ddwtos_renderer extends qtype_elements_embedded_in_question_text_renderer {
40
41 protected function qtext_classname(){
42 return 'qtext ddwtos_questionid_for_javascript';
43 }
44
45 protected function post_qtext_elements(question_attempt $qa, question_display_options $options){
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
74 protected function embedded_element(question_attempt $qa, $place, question_display_options $options) {
75 $question = $qa->get_question();
76 $group = $question->places[$place];
77 $boxcontents = $this->dodgy_ie_fix('&#160;');
78
79 $value = $qa->get_last_qt_var($question->field($place));
80
81 $attributes = array(
82 'id' => $this->box_id($qa, 'p' . $place, $group),
83 'class' => 'slot group' . $group
84 );
85
86 if ($options->readonly) {
87 $attributes['class'] .= ' readonly';
88 } else {
89 $attributes['tabindex'] = '0';
90 }
91
92 $feedbackimage = '';
93 if ($options->correctness) {
94 $response = $qa->get_last_qt_data();
95 $fieldname = $question->field($place);
96 if (array_key_exists($fieldname, $response)) {
97 $fraction = (int) ($response[$fieldname] == $question->get_right_choice_for($place));
98 $attributes['class'] .= ' ' . $this->feedback_class($fraction);
99 $feedbackimage = $this->feedback_image($fraction);
100 }
101 }
102
103 return html_writer::tag('span', $boxcontents, $attributes) . ' ' . $feedbackimage;
104 }
105
106 protected function drag_boxes($qa, $group, $choices, question_display_options $options) {
107 $readonly = '';
108 if ($options->readonly) {
109 $readonly = ' readonly';
110 }
111
112 $boxes = '';
113 foreach ($choices as $key => $choice) {
114 //Bug 8632 - long text entry causes bug in drag and drop field in IE
115 $content = str_replace('-', '&#x2011;', $choice->text);
116 $content = $this->dodgy_ie_fix(str_replace(' ', '&#160;', $content));
117
118 $infinite = '';
119 if ($choice->isinfinite) {
120 $infinite = ' infinite';
121 }
122
123 $boxes .= html_writer::tag('span', $content, array(
124 'id' => $this->box_id($qa, $key, $choice->draggroup),
125 'class' => 'player group' . $choice->draggroup . $infinite . $readonly)) . ' ';
126 }
127
128 return html_writer::nonempty_tag('div', $boxes, array('class' => 'answertext'));
129 }
130
131
132 public function head_code(question_attempt $qa) {
5ac7176c
TH
133 $this->page->requires->yui2_lib('dom');
134 $this->page->requires->yui2_lib('event');
135 $this->page->requires->yui2_lib('dragdrop');
61381e50
TH
136 return parent::head_code($qa);
137 }
138
139 /**
140 * Actually, this question type abuses this method to always ouptut the
141 * hidden fields it needs.
142 */
143 public function clear_wrong(question_attempt $qa, $reallyclear = true) {
144 $question = $qa->get_question();
145 $response = $qa->get_last_qt_data();
146
147 if (!empty($response) && $reallyclear) {
148 $cleanresponse = $question->clear_wrong_from_response($response);
149 } else {
150 $cleanresponse = $response;
151 }
152
153 $output = '';
154 foreach ($question->places as $place => $group) {
155 $fieldname = $question->field($place);
156 if (array_key_exists($fieldname, $response)) {
157 $value = $response[$fieldname];
158 } else {
159 $value = '0';
160 }
161 if (array_key_exists($fieldname, $cleanresponse)) {
162 $cleanvalue = $cleanresponse[$fieldname];
163 } else {
164 $cleanvalue = '0';
165 }
166 if ($cleanvalue != $value) {
167 $output .= html_writer::empty_tag('input', array(
168 'type' => 'hidden',
169 'id' => $this->box_id($qa, 'p' . $place, $group) . '_hidden',
170 'value' => s($value))) .
171 html_writer::empty_tag('input', array(
172 'type' => 'hidden',
173 'name' => $qa->get_qt_field_name($fieldname),
174 'value' => s($cleanvalue)));
175 } else {
176 $output .= html_writer::empty_tag('input', array(
177 'type' => 'hidden',
178 'id' => $this->box_id($qa, 'p' . $place, $group) . '_hidden',
179 'name' => $qa->get_qt_field_name($fieldname),
180 'value' => s($value)));
181 }
182 }
183 return $output;
184 }
185
186}