Merge branch 'MDL-70041-310' of git://github.com/mihailges/moodle into MOODLE_310_STABLE
[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 *
8bc1d28b
EM
20 * @package qtype_ddwtos
21 * @copyright 2010 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
23 */
24
3a4ac328 25
9d55f4a5
TH
26defined('MOODLE_INTERNAL') || die();
27
61381e50
TH
28require_once($CFG->dirroot . '/question/type/gapselect/rendererbase.php');
29
30
31/**
32 * Generates the output for drag-and-drop words into sentences questions.
33 *
3a4ac328
TH
34 * @copyright 2010 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
36 */
37class qtype_ddwtos_renderer extends qtype_elements_embedded_in_question_text_renderer {
38
c3e7201a
JP
39 public function formulation_and_controls(question_attempt $qa,
40 question_display_options $options) {
8edf95a2 41
c3e7201a 42 $result = parent::formulation_and_controls($qa, $options);
8edf95a2 43
1dcd0d34 44 $this->page->requires->js_call_amd('qtype_ddwtos/ddwtos', 'init',
f1ffd134 45 [$qa->get_outer_question_div_unique_id(), $options->readonly]);
c3e7201a
JP
46 return $result;
47 }
48
29929b5b
TH
49 protected function post_qtext_elements(question_attempt $qa,
50 question_display_options $options) {
61381e50
TH
51 $result = '';
52 $question = $qa->get_question();
8edf95a2 53
61381e50
TH
54 $dragboxs = '';
55 foreach ($question->choices as $group => $choices) {
56 $dragboxs .= $this->drag_boxes($qa, $group,
57 $question->get_ordered_choices($group), $options);
58 }
8edf95a2 59
54a5be53 60 $classes = array('answercontainer');
c28d0aa1 61 if ($options->readonly) {
54a5be53
JP
62 $classes[] = 'readonly';
63 }
64 $result .= html_writer::tag('div', $dragboxs, array('class' => implode(' ', $classes)));
65
61381e50
TH
66 // We abuse the clear_wrong method to output the hidden form fields we
67 // want irrespective of whether we are actually clearing the wrong
68 // bits of the response.
69 if (!$options->clearwrong) {
70 $result .= $this->clear_wrong($qa, false);
71 }
72 return $result;
73 }
74
29929b5b
TH
75 protected function embedded_element(question_attempt $qa, $place,
76 question_display_options $options) {
61381e50
TH
77 $question = $qa->get_question();
78 $group = $question->places[$place];
b5cbcc4d
JB
79 $boxcontents = '&#160;' . html_writer::tag('span',
80 get_string('blank', 'qtype_ddwtos'), array('class' => 'accesshide'));
61381e50
TH
81
82 $value = $qa->get_last_qt_var($question->field($place));
83
84 $attributes = array(
f389e916 85 'class' => 'place' . $place . ' drop active group' . $group
61381e50
TH
86 );
87
88 if ($options->readonly) {
89 $attributes['class'] .= ' readonly';
90 } else {
91 $attributes['tabindex'] = '0';
92 }
93
94 $feedbackimage = '';
95 if ($options->correctness) {
96 $response = $qa->get_last_qt_data();
97 $fieldname = $question->field($place);
98 if (array_key_exists($fieldname, $response)) {
29929b5b
TH
99 $fraction = (int) ($response[$fieldname] ==
100 $question->get_right_choice_for($place));
61381e50
TH
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) {
61381e50
TH
109 $boxes = '';
110 foreach ($choices as $key => $choice) {
a5290cd8 111 // Bug 8632: long text entry causes bug in drag and drop field in IE.
61381e50 112 $content = str_replace('-', '&#x2011;', $choice->text);
54a5be53 113 $content = str_replace(' ', '&#160;', $content);
61381e50
TH
114
115 $infinite = '';
efff972a 116 if ($choice->infinite) {
61381e50
TH
117 $infinite = ' infinite';
118 }
119
120 $boxes .= html_writer::tag('span', $content, array(
8edf95a2
TH
121 'class' => 'draghome choice' . $key . ' group' .
122 $choice->draggroup . $infinite)) . ' ';
61381e50
TH
123 }
124
54a5be53 125 return html_writer::nonempty_tag('div', $boxes,
8edf95a2 126 array('class' => 'draggrouphomes' . $choice->draggroup));
61381e50
TH
127 }
128
61381e50 129 /**
c28d0aa1 130 * Actually, this question type abuses this method to always output the
61381e50 131 * hidden fields it needs.
c28d0aa1
TH
132 *
133 * @param question_attempt $qa the question attempt.
134 * @param bool $reallyclear whether we are really clearing the responses, or just outputting them.
135 * @return string HTML to output.
61381e50
TH
136 */
137 public function clear_wrong(question_attempt $qa, $reallyclear = true) {
138 $question = $qa->get_question();
139 $response = $qa->get_last_qt_data();
140
141 if (!empty($response) && $reallyclear) {
142 $cleanresponse = $question->clear_wrong_from_response($response);
143 } else {
144 $cleanresponse = $response;
145 }
146
147 $output = '';
148 foreach ($question->places as $place => $group) {
149 $fieldname = $question->field($place);
150 if (array_key_exists($fieldname, $response)) {
c28d0aa1 151 $value = (string) $response[$fieldname];
61381e50
TH
152 } else {
153 $value = '0';
154 }
155 if (array_key_exists($fieldname, $cleanresponse)) {
c28d0aa1 156 $cleanvalue = (string) $cleanresponse[$fieldname];
61381e50
TH
157 } else {
158 $cleanvalue = '0';
159 }
c28d0aa1
TH
160 if ($cleanvalue === $value) {
161 // Normal case: just one hidden input, to store the
162 // current value and be the value submitted.
61381e50 163 $output .= html_writer::empty_tag('input', array(
20dc02a4 164 'type' => 'hidden',
54a5be53 165 'id' => $this->box_id($qa, 'p' . $place),
c28d0aa1 166 'class' => 'placeinput place' . $place . ' group' . $group,
61381e50 167 'name' => $qa->get_qt_field_name($fieldname),
c28d0aa1 168 'value' => s($value)));
61381e50 169 } else {
c28d0aa1
TH
170 // The case, which only happens when the question is read-only, where
171 // we want to show the drag item in a given place (first hidden input),
172 // but when submitted, we want it to go to a different place (second input).
61381e50 173 $output .= html_writer::empty_tag('input', array(
20dc02a4 174 'type' => 'hidden',
54a5be53 175 'id' => $this->box_id($qa, 'p' . $place),
c28d0aa1
TH
176 'class' => 'placeinput place' . $place . ' group' . $group,
177 'value' => s($value))) .
178 html_writer::empty_tag('input', array(
179 'type' => 'hidden',
61381e50 180 'name' => $qa->get_qt_field_name($fieldname),
c28d0aa1 181 'value' => s($cleanvalue)));
61381e50
TH
182 }
183 }
184 return $output;
185 }
186
187}