MDL-20636 Remove all references to global $QTYPES.
[moodle.git] / question / engine / renderer.php
CommitLineData
d1b7e03d
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
d1b7e03d
TH
18/**
19 * Renderers for outputting parts of the question engine.
20 *
017bc1d9 21 * @package moodlecore
d1b7e03d 22 * @subpackage questionengine
017bc1d9
TH
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
25 */
26
27
a17b297d
TH
28defined('MOODLE_INTERNAL') || die();
29
30
d1b7e03d
TH
31/**
32 * This renderer controls the overall output of questions. It works with a
33 * {@link qbehaviour_renderer} and a {@link qtype_renderer} to output the
34 * type-specific bits. The main entry point is the {@link question()} method.
35 *
017bc1d9
TH
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d 38 */
729c372b 39class core_question_renderer extends plugin_renderer_base {
d1b7e03d
TH
40
41 /**
42 * Generate the display of a question in a particular state, and with certain
43 * display options. Normally you do not call this method directly. Intsead
44 * you call {@link question_usage_by_activity::render_question()} which will
45 * call this method with appropriate arguments.
46 *
47 * @param question_attempt $qa the question attempt to display.
48 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
49 * specific parts.
50 * @param qtype_renderer $qtoutput the renderer to output the question type
51 * specific parts.
52 * @param question_display_options $options controls what should and should not be displayed.
53 * @param string|null $number The question number to display. 'i' is a special
54 * value that gets displayed as Information. Null means no number is displayed.
55 * @return string HTML representation of the question.
56 */
57 public function question(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
58 qtype_renderer $qtoutput, question_display_options $options, $number) {
59
60 $output = '';
61 $output .= html_writer::start_tag('div', array(
62 'id' => 'q' . $qa->get_slot(),
63 'class' => 'que ' . $qa->get_question()->qtype->name() . ' ' .
64 $qa->get_behaviour_name(),
65 ));
66
67 $output .= html_writer::tag('div',
68 $this->info($qa, $behaviouroutput, $qtoutput, $options, $number),
69 array('class' => 'info'));
70
71 $output .= html_writer::start_tag('div', array('class' => 'content'));
72
73 $output .= html_writer::tag('div',
74 $this->add_part_heading(get_string('questiontext', 'question'),
75 $this->formulation($qa, $behaviouroutput, $qtoutput, $options)),
76 array('class' => 'formulation'));
77 $output .= html_writer::nonempty_tag('div',
78 $this->add_part_heading(get_string('feedback', 'question'),
79 $this->outcome($qa, $behaviouroutput, $qtoutput, $options)),
80 array('class' => 'outcome'));
81 $output .= html_writer::nonempty_tag('div',
82 $this->add_part_heading(get_string('comments', 'question'),
83 $behaviouroutput->manual_comment($qa, $options)), array('class' => 'comment'));
84 $output .= html_writer::nonempty_tag('div',
85 $this->response_history($qa, $behaviouroutput, $qtoutput, $options),
86 array('class' => 'history'));
87
88 $output .= html_writer::end_tag('div');
89 $output .= html_writer::end_tag('div');
90 return $output;
91 }
92
93 /**
94 * Generate the information bit of the question display that contains the
95 * metadata like the question number, current state, and mark.
96 * @param question_attempt $qa the question attempt to display.
97 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
98 * specific parts.
99 * @param qtype_renderer $qtoutput the renderer to output the question type
100 * specific parts.
101 * @param question_display_options $options controls what should and should not be displayed.
102 * @param string|null $number The question number to display. 'i' is a special
103 * value that gets displayed as Information. Null means no number is displayed.
104 * @return HTML fragment.
105 */
106 protected function info(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
107 qtype_renderer $qtoutput, question_display_options $options, $number) {
108 $output = '';
109 $output .= $this->number($number);
110 $output .= $this->status($qa, $behaviouroutput, $options);
111 $output .= $this->mark_summary($qa, $options);
112 $output .= $this->question_flag($qa, $options->flags);
113 return $output;
114 }
115
116 /**
117 * Generate the display of the question number.
118 * @param string|null $number The question number to display. 'i' is a special
119 * value that gets displayed as Information. Null means no number is displayed.
120 * @return HTML fragment.
121 */
122 protected function number($number) {
123 $numbertext = '';
124 if (is_numeric($number)) {
125 $numbertext = get_string('questionx', 'question',
126 html_writer::tag('span', $number, array('class' => 'qno')));
127 } else if ($number == 'i') {
128 $numbertext = get_string('information', 'question');
129 }
130 if (!$numbertext) {
131 return '';
132 }
133 return html_writer::tag('h2', $numbertext, array('class' => 'no'));
134 }
135
136 /**
137 * Add an invisible heading like 'question text', 'feebdack' at the top of
138 * a section's contents, but only if the section has some content.
139 * @param string $heading the heading to add.
140 * @param string $content the content of the section.
141 * @return string HTML fragment with the heading added.
142 */
143 protected function add_part_heading($heading, $content) {
144 if ($content) {
145 $content = html_writer::tag('h3', $heading, array('class' => 'accesshide')) . $content;
146 }
147 return $content;
148 }
149
150 /**
151 * Generate the display of the status line that gives the current state of
152 * the question.
153 * @param question_attempt $qa the question attempt to display.
154 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
155 * specific parts.
156 * @param question_display_options $options controls what should and should not be displayed.
157 * @return HTML fragment.
158 */
159 protected function status(question_attempt $qa, qbehaviour_renderer $behaviouroutput, question_display_options $options) {
160 return html_writer::tag('div', $qa->get_state_string($options->correctness),
161 array('class' => 'state'));
162 }
163
164 /**
165 * Generate the display of the marks for this question.
166 * @param question_attempt $qa the question attempt to display.
167 * @param question_display_options $options controls what should and should not be displayed.
168 * @return HTML fragment.
169 */
170 protected function mark_summary(question_attempt $qa, question_display_options $options) {
171 if (!$options->marks) {
172 return '';
173 }
174
175 if ($qa->get_max_mark() == 0) {
176 $summary = get_string('notgraded', 'question');
177
178 } else if ($options->marks == question_display_options::MAX_ONLY ||
179 is_null($qa->get_fraction())) {
180 $summary = get_string('markedoutofmax', 'question', $qa->format_max_mark($options->markdp));
181
182 } else {
0ff4bd08 183 $a = new stdClass();
d1b7e03d
TH
184 $a->mark = $qa->format_mark($options->markdp);
185 $a->max = $qa->format_max_mark($options->markdp);
186 $summary = get_string('markoutofmax', 'question', $a);
187 }
188
189 return html_writer::tag('div', $summary, array('class' => 'grade'));
190 }
191
192 /**
193 * Render the question flag, assuming $flagsoption allows it.
194 *
195 * @param question_attempt $qa the question attempt to display.
f7970e3c 196 * @param int $flagsoption the option that says whether flags should be displayed.
d1b7e03d
TH
197 */
198 protected function question_flag(question_attempt $qa, $flagsoption) {
199 global $CFG;
200 switch ($flagsoption) {
201 case question_display_options::VISIBLE:
202 $flagcontent = $this->get_flag_html($qa->is_flagged());
203 break;
204 case question_display_options::EDITABLE:
205 $id = $qa->get_flag_field_name();
206 if ($qa->is_flagged()) {
207 $checked = 'checked="checked" ';
208 } else {
209 $checked = '';
210 }
211 $postdata = question_flags::get_postdata($qa);
212 // The checkbox id must be different from any element name, becuase
213 // of a stupid IE bug: http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
214 $flagcontent = '<input type="hidden" name="' . $id . '" value="0" />' .
215 '<input type="checkbox" id="' . $id . 'checkbox" name="' . $id . '" value="1" ' . $checked . ' />' .
06f8ed54 216 '<input type="hidden" value="' . s($postdata) . '" class="questionflagpostdata" />' .
d1b7e03d 217 '<label id="' . $id . 'label" for="' . $id . '">' . $this->get_flag_html(
f29aeb5a 218 $qa->is_flagged(), $id . 'img') . '</label>' . "\n";
d1b7e03d
TH
219 break;
220 default:
221 $flagcontent = '';
222 }
223 if ($flagcontent) {
224 return '<div class="questionflag">' . $flagcontent . "</div>\n";
225 }
226 }
227
228 /**
229 * Work out the actual img tag needed for the flag
230 *
f7970e3c 231 * @param bool $flagged whether the question is currently flagged.
d1b7e03d
TH
232 * @param string $id an id to be added as an attribute to the img (optional).
233 * @return string the img tag.
234 */
235 protected function get_flag_html($flagged, $id = '') {
d1b7e03d
TH
236 if ($id) {
237 $id = 'id="' . $id . '" ';
238 }
239 if ($flagged) {
240 $img = 'flagged.png';
241 } else {
242 $img = 'unflagged.png';
243 }
ec3d4ef5 244 return '<img ' . $id . 'src="' . $this->pix_url('/i/' . $img) .
d1b7e03d
TH
245 '" alt="' . get_string('flagthisquestion', 'question') . '" />';
246 }
247
248 /**
249 * Generate the display of the formulation part of the question. This is the
250 * area that contains the quetsion text, and the controls for students to
251 * input their answers. Some question types also embed feedback, for
252 * example ticks and crosses, in this area.
253 *
254 * @param question_attempt $qa the question attempt to display.
255 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
256 * specific parts.
257 * @param qtype_renderer $qtoutput the renderer to output the question type
258 * specific parts.
259 * @param question_display_options $options controls what should and should not be displayed.
260 * @return HTML fragment.
261 */
262 protected function formulation(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
263 qtype_renderer $qtoutput, question_display_options $options) {
264 $output = '';
265 $output .= html_writer::empty_tag('input', array(
266 'type' => 'hidden',
267 'name' => $qa->get_control_field_name('sequencecheck'),
268 'value' => $qa->get_num_steps()));
269 $output .= $qtoutput->formulation_and_controls($qa, $options);
270 if ($options->clearwrong) {
271 $output .= $qtoutput->clear_wrong($qa);
272 }
273 $output .= html_writer::nonempty_tag('div',
274 $behaviouroutput->controls($qa, $options), array('class' => 'im-controls'));
275 return $output;
276 }
277
278 /**
279 * Generate the display of the outcome part of the question. This is the
280 * area that contains the various forms of feedback.
281 *
282 * @param question_attempt $qa the question attempt to display.
283 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
284 * specific parts.
285 * @param qtype_renderer $qtoutput the renderer to output the question type
286 * specific parts.
287 * @param question_display_options $options controls what should and should not be displayed.
288 * @return HTML fragment.
289 */
290 protected function outcome(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
291 qtype_renderer $qtoutput, question_display_options $options) {
292 $output = '';
293 $output .= html_writer::nonempty_tag('div',
294 $qtoutput->feedback($qa, $options), array('class' => 'feedback'));
295 $output .= html_writer::nonempty_tag('div',
296 $behaviouroutput->feedback($qa, $options), array('class' => 'im-feedback'));
297 return $output;
298 }
299
300 /**
301 * Generate the display of the response history part of the question. This
302 * is the table showing all the steps the question has been through.
303 *
304 * @param question_attempt $qa the question attempt to display.
305 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
306 * specific parts.
307 * @param qtype_renderer $qtoutput the renderer to output the question type
308 * specific parts.
309 * @param question_display_options $options controls what should and should not be displayed.
310 * @return HTML fragment.
311 */
312 protected function response_history(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
313 qtype_renderer $qtoutput, question_display_options $options) {
314
315 if (!$options->history) {
316 return '';
317 }
318
068b4594 319 $table = new html_table();
d1b7e03d
TH
320 $table->head = array (
321 get_string('step', 'question'),
322 get_string('time'),
323 get_string('action', 'question'),
324 get_string('state', 'question'),
325 );
326 if ($options->marks >= question_display_options::MARK_AND_MAX) {
327 $table->head[] = get_string('marks', 'question');
328 }
329
330 foreach ($qa->get_full_step_iterator() as $i => $step) {
331 $stepno = $i + 1;
332 $rowclass = '';
333 if ($stepno == $qa->get_num_steps()) {
334 $rowclass = 'current';
335 } else if (!empty($options->questionreviewlink)) {
729c372b
TH
336 $url = new moodle_url($options->questionreviewlink, array('slot' => $qa->get_slot(), 'step' => $i));
337 $stepno = $this->output->action_link($url, $stepno,
338 new popup_action('click', $url, 'reviewquestion', array('width' => 450, 'height' => 650)),
5e8a85aa 339 array('title' => get_string('reviewresponse', 'question')));
d1b7e03d 340 }
0ff4bd08 341 $user = new stdClass();
d1b7e03d
TH
342 $user->id = $step->get_user_id();
343 $row = array(
344 $stepno,
345 userdate($step->get_timecreated(), get_string('strftimedatetimeshort')),
346 s($qa->summarise_action($step)),
347 $step->get_state()->default_string(true),
348 );
349
350 if ($options->marks >= question_display_options::MARK_AND_MAX) {
351 $row[] = $qa->format_fraction_as_mark($step->get_fraction(), $options->markdp);
352 }
353
068b4594 354 $table->rowclasses[] = $rowclass;
d1b7e03d
TH
355 $table->data[] = $row;
356 }
357
358 return html_writer::tag('h3', get_string('responsehistory', 'question'),
359 array('class' => 'responsehistoryheader')) . html_writer::tag('div',
068b4594 360 html_writer::table($table, true), array('class' => 'responsehistoryheader'));
d1b7e03d
TH
361 }
362
363}