MDL-52427 questions: all parts of a Q should be clearfix
[moodle.git] / question / engine / renderer.php
CommitLineData
d1b7e03d 1<?php
d1b7e03d
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
d1b7e03d
TH
17/**
18 * Renderers for outputting parts of the question engine.
19 *
017bc1d9 20 * @package moodlecore
d1b7e03d 21 * @subpackage questionengine
017bc1d9
TH
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d
TH
24 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
d1b7e03d
TH
30/**
31 * This renderer controls the overall output of questions. It works with a
32 * {@link qbehaviour_renderer} and a {@link qtype_renderer} to output the
33 * type-specific bits. The main entry point is the {@link question()} method.
34 *
017bc1d9
TH
35 * @copyright 2009 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d1b7e03d 37 */
729c372b 38class core_question_renderer extends plugin_renderer_base {
2daffca5
TH
39 public function get_page() {
40 return $this->page;
41 }
d1b7e03d 42
8011be18
TH
43 /**
44 * Render an icon, optionally with the word 'Preview' beside it, to preview
45 * a given question.
46 * @param int $questionid the id of the question to be previewed.
47 * @param context $context the context in which the preview is happening.
48 * Must be a course or category context.
49 * @param bool $showlabel if true, show the word 'Preview' after the icon.
50 * If false, just show the icon.
51 */
52 public function question_preview_link($questionid, context $context, $showlabel) {
53 if ($showlabel) {
54 $alt = '';
55 $label = ' ' . get_string('preview');
56 $attributes = array();
57 } else {
58 $alt = get_string('preview');
59 $label = '';
60 $attributes = array('title' => $alt);
61 }
62
63 $image = $this->pix_icon('t/preview', $alt, '', array('class' => 'iconsmall'));
64 $link = question_preview_url($questionid, null, null, null, null, $context);
65 $action = new popup_action('click', $link, 'questionpreview',
66 question_preview_popup_params());
67
68 return $this->action_link($link, $image . $label, $action, $attributes);
69 }
70
d1b7e03d
TH
71 /**
72 * Generate the display of a question in a particular state, and with certain
73 * display options. Normally you do not call this method directly. Intsead
74 * you call {@link question_usage_by_activity::render_question()} which will
75 * call this method with appropriate arguments.
76 *
77 * @param question_attempt $qa the question attempt to display.
78 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
79 * specific parts.
80 * @param qtype_renderer $qtoutput the renderer to output the question type
81 * specific parts.
82 * @param question_display_options $options controls what should and should not be displayed.
83 * @param string|null $number The question number to display. 'i' is a special
84 * value that gets displayed as Information. Null means no number is displayed.
85 * @return string HTML representation of the question.
86 */
87 public function question(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
88 qtype_renderer $qtoutput, question_display_options $options, $number) {
89
90 $output = '';
91 $output .= html_writer::start_tag('div', array(
92 'id' => 'q' . $qa->get_slot(),
25d9a6a6
TH
93 'class' => implode(' ', array(
94 'que',
95 $qa->get_question()->qtype->name(),
96 $qa->get_behaviour_name(),
97 $qa->get_state_class($options->correctness && $qa->has_marks()),
98 ))
d1b7e03d
TH
99 ));
100
101 $output .= html_writer::tag('div',
102 $this->info($qa, $behaviouroutput, $qtoutput, $options, $number),
103 array('class' => 'info'));
104
105 $output .= html_writer::start_tag('div', array('class' => 'content'));
106
107 $output .= html_writer::tag('div',
7042a252
TH
108 $this->add_part_heading($qtoutput->formulation_heading(),
109 $this->formulation($qa, $behaviouroutput, $qtoutput, $options)),
d392b13a 110 array('class' => 'formulation clearfix'));
d1b7e03d
TH
111 $output .= html_writer::nonempty_tag('div',
112 $this->add_part_heading(get_string('feedback', 'question'),
7042a252 113 $this->outcome($qa, $behaviouroutput, $qtoutput, $options)),
d392b13a 114 array('class' => 'outcome clearfix'));
d1b7e03d
TH
115 $output .= html_writer::nonempty_tag('div',
116 $this->add_part_heading(get_string('comments', 'question'),
7042a252 117 $this->manual_comment($qa, $behaviouroutput, $qtoutput, $options)),
d392b13a 118 array('class' => 'comment clearfix'));
d1b7e03d
TH
119 $output .= html_writer::nonempty_tag('div',
120 $this->response_history($qa, $behaviouroutput, $qtoutput, $options),
d392b13a 121 array('class' => 'history clearfix'));
d1b7e03d
TH
122
123 $output .= html_writer::end_tag('div');
124 $output .= html_writer::end_tag('div');
125 return $output;
126 }
127
128 /**
129 * Generate the information bit of the question display that contains the
130 * metadata like the question number, current state, and mark.
131 * @param question_attempt $qa the question attempt to display.
132 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
133 * specific parts.
134 * @param qtype_renderer $qtoutput the renderer to output the question type
135 * specific parts.
136 * @param question_display_options $options controls what should and should not be displayed.
137 * @param string|null $number The question number to display. 'i' is a special
138 * value that gets displayed as Information. Null means no number is displayed.
139 * @return HTML fragment.
140 */
141 protected function info(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
142 qtype_renderer $qtoutput, question_display_options $options, $number) {
143 $output = '';
144 $output .= $this->number($number);
145 $output .= $this->status($qa, $behaviouroutput, $options);
02d3e4d5 146 $output .= $this->mark_summary($qa, $behaviouroutput, $options);
5e63b335 147 $output .= $options->extrainfocontent;
d1b7e03d 148 $output .= $this->question_flag($qa, $options->flags);
da729916 149 $output .= $this->edit_question_link($qa, $options);
d1b7e03d
TH
150 return $output;
151 }
152
153 /**
154 * Generate the display of the question number.
155 * @param string|null $number The question number to display. 'i' is a special
156 * value that gets displayed as Information. Null means no number is displayed.
157 * @return HTML fragment.
158 */
159 protected function number($number) {
160 $numbertext = '';
161 if (is_numeric($number)) {
162 $numbertext = get_string('questionx', 'question',
163 html_writer::tag('span', $number, array('class' => 'qno')));
164 } else if ($number == 'i') {
165 $numbertext = get_string('information', 'question');
166 }
167 if (!$numbertext) {
168 return '';
169 }
c544ee92 170 return html_writer::tag('h3', $numbertext, array('class' => 'no'));
d1b7e03d
TH
171 }
172
173 /**
174 * Add an invisible heading like 'question text', 'feebdack' at the top of
175 * a section's contents, but only if the section has some content.
176 * @param string $heading the heading to add.
177 * @param string $content the content of the section.
178 * @return string HTML fragment with the heading added.
179 */
180 protected function add_part_heading($heading, $content) {
181 if ($content) {
c544ee92 182 $content = html_writer::tag('h4', $heading, array('class' => 'accesshide')) . $content;
d1b7e03d
TH
183 }
184 return $content;
185 }
186
187 /**
188 * Generate the display of the status line that gives the current state of
189 * the question.
190 * @param question_attempt $qa the question attempt to display.
191 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
192 * specific parts.
193 * @param question_display_options $options controls what should and should not be displayed.
194 * @return HTML fragment.
195 */
9c197f44
TH
196 protected function status(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
197 question_display_options $options) {
d1b7e03d
TH
198 return html_writer::tag('div', $qa->get_state_string($options->correctness),
199 array('class' => 'state'));
200 }
201
202 /**
203 * Generate the display of the marks for this question.
204 * @param question_attempt $qa the question attempt to display.
02d3e4d5 205 * @param qbehaviour_renderer $behaviouroutput the behaviour renderer, which can generate a custom display.
d1b7e03d
TH
206 * @param question_display_options $options controls what should and should not be displayed.
207 * @return HTML fragment.
208 */
02d3e4d5
TH
209 protected function mark_summary(question_attempt $qa, qbehaviour_renderer $behaviouroutput, question_display_options $options) {
210 return html_writer::nonempty_tag('div',
211 $behaviouroutput->mark_summary($qa, $this, $options),
212 array('class' => 'grade'));
213 }
214
215 /**
216 * Generate the display of the marks for this question.
217 * @param question_attempt $qa the question attempt to display.
218 * @param question_display_options $options controls what should and should not be displayed.
219 * @return HTML fragment.
220 */
221 public function standard_mark_summary(question_attempt $qa, qbehaviour_renderer $behaviouroutput, question_display_options $options) {
d1b7e03d
TH
222 if (!$options->marks) {
223 return '';
d1b7e03d 224
02d3e4d5
TH
225 } else if ($qa->get_max_mark() == 0) {
226 return get_string('notgraded', 'question');
d1b7e03d
TH
227
228 } else if ($options->marks == question_display_options::MAX_ONLY ||
229 is_null($qa->get_fraction())) {
02d3e4d5 230 return $behaviouroutput->marked_out_of_max($qa, $this, $options);
d1b7e03d
TH
231
232 } else {
02d3e4d5 233 return $behaviouroutput->mark_out_of_max($qa, $this, $options);
d1b7e03d 234 }
02d3e4d5
TH
235 }
236
237 /**
238 * Generate the display of the available marks for this question.
239 * @param question_attempt $qa the question attempt to display.
240 * @param question_display_options $options controls what should and should not be displayed.
241 * @return HTML fragment.
242 */
243 public function standard_marked_out_of_max(question_attempt $qa, question_display_options $options) {
244 return get_string('markedoutofmax', 'question', $qa->format_max_mark($options->markdp));
245 }
d1b7e03d 246
02d3e4d5
TH
247 /**
248 * Generate the display of the marks for this question out of the available marks.
249 * @param question_attempt $qa the question attempt to display.
250 * @param question_display_options $options controls what should and should not be displayed.
251 * @return HTML fragment.
252 */
253 public function standard_mark_out_of_max(question_attempt $qa, question_display_options $options) {
254 $a = new stdClass();
255 $a->mark = $qa->format_mark($options->markdp);
256 $a->max = $qa->format_max_mark($options->markdp);
257 return get_string('markoutofmax', 'question', $a);
d1b7e03d
TH
258 }
259
260 /**
261 * Render the question flag, assuming $flagsoption allows it.
262 *
263 * @param question_attempt $qa the question attempt to display.
f7970e3c 264 * @param int $flagsoption the option that says whether flags should be displayed.
d1b7e03d
TH
265 */
266 protected function question_flag(question_attempt $qa, $flagsoption) {
267 global $CFG;
f1b3d3e0
TH
268
269 $divattributes = array('class' => 'questionflag');
270
d1b7e03d
TH
271 switch ($flagsoption) {
272 case question_display_options::VISIBLE:
273 $flagcontent = $this->get_flag_html($qa->is_flagged());
274 break;
f1b3d3e0 275
d1b7e03d
TH
276 case question_display_options::EDITABLE:
277 $id = $qa->get_flag_field_name();
9c197f44
TH
278 // The checkbox id must be different from any element name, because
279 // of a stupid IE bug:
280 // http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
f1b3d3e0
TH
281 $checkboxattributes = array(
282 'type' => 'checkbox',
283 'id' => $id . 'checkbox',
284 'name' => $id,
285 'value' => 1,
286 );
287 if ($qa->is_flagged()) {
288 $checkboxattributes['checked'] = 'checked';
289 }
290 $postdata = question_flags::get_postdata($qa);
291
292 $flagcontent = html_writer::empty_tag('input',
293 array('type' => 'hidden', 'name' => $id, 'value' => 0)) .
294 html_writer::empty_tag('input', $checkboxattributes) .
295 html_writer::empty_tag('input',
296 array('type' => 'hidden', 'value' => $postdata, 'class' => 'questionflagpostdata')) .
297 html_writer::tag('label', $this->get_flag_html($qa->is_flagged(), $id . 'img'),
298 array('id' => $id . 'label', 'for' => $id . 'checkbox')) . "\n";
299
300 $divattributes = array(
301 'class' => 'questionflag editable',
302 'aria-atomic' => 'true',
303 'aria-relevant' => 'text',
304 'aria-live' => 'assertive',
305 );
306
d1b7e03d 307 break;
f1b3d3e0 308
d1b7e03d
TH
309 default:
310 $flagcontent = '';
311 }
f1b3d3e0
TH
312
313 return html_writer::nonempty_tag('div', $flagcontent, $divattributes);
d1b7e03d
TH
314 }
315
316 /**
317 * Work out the actual img tag needed for the flag
318 *
f7970e3c 319 * @param bool $flagged whether the question is currently flagged.
d1b7e03d
TH
320 * @param string $id an id to be added as an attribute to the img (optional).
321 * @return string the img tag.
322 */
323 protected function get_flag_html($flagged, $id = '') {
22c97c64
TH
324 if ($flagged) {
325 $icon = 'i/flagged';
326 $alt = get_string('flagged', 'question');
327 } else {
328 $icon = 'i/unflagged';
329 $alt = get_string('notflagged', 'question');
330 }
331 $attributes = array(
332 'src' => $this->pix_url($icon),
333 'alt' => $alt,
334 );
d1b7e03d 335 if ($id) {
22c97c64 336 $attributes['id'] = $id;
d1b7e03d 337 }
22c97c64 338 $img = html_writer::empty_tag('img', $attributes);
d1b7e03d 339 if ($flagged) {
22c97c64 340 $img .= ' ' . get_string('flagged', 'question');
d1b7e03d 341 }
22c97c64 342 return $img;
d1b7e03d
TH
343 }
344
da729916
TH
345 protected function edit_question_link(question_attempt $qa,
346 question_display_options $options) {
347 global $CFG;
348
349 if (empty($options->editquestionparams)) {
350 return '';
351 }
352
353 $params = $options->editquestionparams;
354 if ($params['returnurl'] instanceof moodle_url) {
a530d4a9 355 $params['returnurl'] = $params['returnurl']->out_as_local_url(false);
da729916
TH
356 }
357 $params['id'] = $qa->get_question()->id;
358 $editurl = new moodle_url('/question/question.php', $params);
359
360 return html_writer::tag('div', html_writer::link(
c3847443 361 $editurl, $this->pix_icon('t/edit', get_string('edit'), '', array('class' => 'iconsmall')) .
da729916
TH
362 get_string('editquestion', 'question')),
363 array('class' => 'editquestion'));
364 }
365
d1b7e03d
TH
366 /**
367 * Generate the display of the formulation part of the question. This is the
368 * area that contains the quetsion text, and the controls for students to
369 * input their answers. Some question types also embed feedback, for
370 * example ticks and crosses, in this area.
371 *
372 * @param question_attempt $qa the question attempt to display.
373 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
374 * specific parts.
375 * @param qtype_renderer $qtoutput the renderer to output the question type
376 * specific parts.
377 * @param question_display_options $options controls what should and should not be displayed.
378 * @return HTML fragment.
379 */
380 protected function formulation(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
381 qtype_renderer $qtoutput, question_display_options $options) {
382 $output = '';
383 $output .= html_writer::empty_tag('input', array(
384 'type' => 'hidden',
385 'name' => $qa->get_control_field_name('sequencecheck'),
c7fbfe46 386 'value' => $qa->get_sequence_check_count()));
d1b7e03d
TH
387 $output .= $qtoutput->formulation_and_controls($qa, $options);
388 if ($options->clearwrong) {
389 $output .= $qtoutput->clear_wrong($qa);
390 }
391 $output .= html_writer::nonempty_tag('div',
392 $behaviouroutput->controls($qa, $options), array('class' => 'im-controls'));
393 return $output;
394 }
395
396 /**
397 * Generate the display of the outcome part of the question. This is the
398 * area that contains the various forms of feedback.
399 *
400 * @param question_attempt $qa the question attempt to display.
401 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
402 * specific parts.
403 * @param qtype_renderer $qtoutput the renderer to output the question type
404 * specific parts.
405 * @param question_display_options $options controls what should and should not be displayed.
406 * @return HTML fragment.
407 */
408 protected function outcome(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
409 qtype_renderer $qtoutput, question_display_options $options) {
410 $output = '';
411 $output .= html_writer::nonempty_tag('div',
412 $qtoutput->feedback($qa, $options), array('class' => 'feedback'));
413 $output .= html_writer::nonempty_tag('div',
414 $behaviouroutput->feedback($qa, $options), array('class' => 'im-feedback'));
415 return $output;
416 }
417
783af252
TH
418 protected function manual_comment(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
419 qtype_renderer $qtoutput, question_display_options $options) {
420 return $qtoutput->manual_comment($qa, $options) .
421 $behaviouroutput->manual_comment($qa, $options);
422 }
423
d1b7e03d
TH
424 /**
425 * Generate the display of the response history part of the question. This
426 * is the table showing all the steps the question has been through.
427 *
428 * @param question_attempt $qa the question attempt to display.
429 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
430 * specific parts.
431 * @param qtype_renderer $qtoutput the renderer to output the question type
432 * specific parts.
433 * @param question_display_options $options controls what should and should not be displayed.
434 * @return HTML fragment.
435 */
436 protected function response_history(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
437 qtype_renderer $qtoutput, question_display_options $options) {
438
439 if (!$options->history) {
440 return '';
441 }
442
068b4594 443 $table = new html_table();
d1b7e03d
TH
444 $table->head = array (
445 get_string('step', 'question'),
446 get_string('time'),
447 get_string('action', 'question'),
448 get_string('state', 'question'),
449 );
450 if ($options->marks >= question_display_options::MARK_AND_MAX) {
451 $table->head[] = get_string('marks', 'question');
452 }
453
454 foreach ($qa->get_full_step_iterator() as $i => $step) {
455 $stepno = $i + 1;
d47d12d0 456
d1b7e03d
TH
457 $rowclass = '';
458 if ($stepno == $qa->get_num_steps()) {
459 $rowclass = 'current';
460 } else if (!empty($options->questionreviewlink)) {
9c197f44
TH
461 $url = new moodle_url($options->questionreviewlink,
462 array('slot' => $qa->get_slot(), 'step' => $i));
729c372b 463 $stepno = $this->output->action_link($url, $stepno,
9c197f44
TH
464 new popup_action('click', $url, 'reviewquestion',
465 array('width' => 450, 'height' => 650)),
5e8a85aa 466 array('title' => get_string('reviewresponse', 'question')));
d1b7e03d 467 }
d47d12d0
TH
468
469 $restrictedqa = new question_attempt_with_restricted_history($qa, $i, null);
470
0ff4bd08 471 $user = new stdClass();
d1b7e03d
TH
472 $user->id = $step->get_user_id();
473 $row = array(
474 $stepno,
475 userdate($step->get_timecreated(), get_string('strftimedatetimeshort')),
476 s($qa->summarise_action($step)),
d47d12d0 477 $restrictedqa->get_state_string($options->correctness),
d1b7e03d
TH
478 );
479
480 if ($options->marks >= question_display_options::MARK_AND_MAX) {
481 $row[] = $qa->format_fraction_as_mark($step->get_fraction(), $options->markdp);
482 }
483
068b4594 484 $table->rowclasses[] = $rowclass;
d1b7e03d
TH
485 $table->data[] = $row;
486 }
487
c544ee92 488 return html_writer::tag('h4', get_string('responsehistory', 'question'),
5e63b335
TH
489 array('class' => 'responsehistoryheader')) .
490 $options->extrahistorycontent .
491 html_writer::tag('div', html_writer::table($table, true),
492 array('class' => 'responsehistoryheader'));
d1b7e03d
TH
493 }
494
495}