2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Renderers for outputting parts of the question engine.
21 * @subpackage questionengine
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
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.
35 * @copyright 2009 The Open University
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_question_renderer
extends plugin_renderer_base
{
39 public function get_page() {
44 * Render an icon, optionally with the word 'Preview' beside it, to preview
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.
52 public function question_preview_link($questionid, context
$context, $showlabel) {
55 $label = get_string('preview');
56 $attributes = array();
58 $alt = get_string('preview');
60 $attributes = array('title' => $alt);
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());
68 return $this->action_link($link, $image . $label, $action, $attributes);
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.
77 * @param question_attempt $qa the question attempt to display.
78 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
80 * @param qtype_renderer $qtoutput the renderer to output the question type
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.
87 public function question(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
88 qtype_renderer
$qtoutput, question_display_options
$options, $number) {
91 $output .= html_writer
::start_tag('div', array(
92 'id' => 'q' . $qa->get_slot(),
93 'class' => implode(' ', array(
95 $qa->get_question()->qtype
->name(),
96 $qa->get_behaviour_name(),
97 $qa->get_state_class($options->correctness
&& $qa->has_marks()),
101 $output .= html_writer
::tag('div',
102 $this->info($qa, $behaviouroutput, $qtoutput, $options, $number),
103 array('class' => 'info'));
105 $output .= html_writer
::start_tag('div', array('class' => 'content'));
107 $output .= html_writer
::tag('div',
108 $this->add_part_heading($qtoutput->formulation_heading(),
109 $this->formulation($qa, $behaviouroutput, $qtoutput, $options)),
110 array('class' => 'formulation clearfix'));
111 $output .= html_writer
::nonempty_tag('div',
112 $this->add_part_heading(get_string('feedback', 'question'),
113 $this->outcome($qa, $behaviouroutput, $qtoutput, $options)),
114 array('class' => 'outcome clearfix'));
115 $output .= html_writer
::nonempty_tag('div',
116 $this->add_part_heading(get_string('comments', 'question'),
117 $this->manual_comment($qa, $behaviouroutput, $qtoutput, $options)),
118 array('class' => 'comment clearfix'));
119 $output .= html_writer
::nonempty_tag('div',
120 $this->response_history($qa, $behaviouroutput, $qtoutput, $options),
121 array('class' => 'history clearfix'));
123 $output .= html_writer
::end_tag('div');
124 $output .= html_writer
::end_tag('div');
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
134 * @param qtype_renderer $qtoutput the renderer to output the question type
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.
141 protected function info(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
142 qtype_renderer
$qtoutput, question_display_options
$options, $number) {
144 $output .= $this->number($number);
145 $output .= $this->status($qa, $behaviouroutput, $options);
146 $output .= $this->mark_summary($qa, $behaviouroutput, $options);
147 $output .= $this->question_flag($qa, $options->flags
);
148 $output .= $this->edit_question_link($qa, $options);
153 * Generate the display of the question number.
154 * @param string|null $number The question number to display. 'i' is a special
155 * value that gets displayed as Information. Null means no number is displayed.
156 * @return HTML fragment.
158 protected function number($number) {
159 if (trim($number) === '') {
163 if (trim($number) === 'i') {
164 $numbertext = get_string('information', 'question');
166 $numbertext = get_string('questionx', 'question',
167 html_writer
::tag('span', $number, array('class' => 'qno')));
169 return html_writer
::tag('h3', $numbertext, array('class' => 'no'));
173 * Add an invisible heading like 'question text', 'feebdack' at the top of
174 * a section's contents, but only if the section has some content.
175 * @param string $heading the heading to add.
176 * @param string $content the content of the section.
177 * @return string HTML fragment with the heading added.
179 protected function add_part_heading($heading, $content) {
181 $content = html_writer
::tag('h4', $heading, array('class' => 'accesshide')) . $content;
187 * Generate the display of the status line that gives the current state of
189 * @param question_attempt $qa the question attempt to display.
190 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
192 * @param question_display_options $options controls what should and should not be displayed.
193 * @return HTML fragment.
195 protected function status(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
196 question_display_options
$options) {
197 return html_writer
::tag('div', $qa->get_state_string($options->correctness
),
198 array('class' => 'state'));
202 * Generate the display of the marks for this question.
203 * @param question_attempt $qa the question attempt to display.
204 * @param qbehaviour_renderer $behaviouroutput the behaviour renderer, which can generate a custom display.
205 * @param question_display_options $options controls what should and should not be displayed.
206 * @return HTML fragment.
208 protected function mark_summary(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput, question_display_options
$options) {
209 return html_writer
::nonempty_tag('div',
210 $behaviouroutput->mark_summary($qa, $this, $options),
211 array('class' => 'grade'));
215 * Generate the display of the marks for this question.
216 * @param question_attempt $qa the question attempt to display.
217 * @param question_display_options $options controls what should and should not be displayed.
218 * @return HTML fragment.
220 public function standard_mark_summary(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput, question_display_options
$options) {
221 if (!$options->marks
) {
224 } else if ($qa->get_max_mark() == 0) {
225 return get_string('notgraded', 'question');
227 } else if ($options->marks
== question_display_options
::MAX_ONLY ||
228 is_null($qa->get_fraction())) {
229 return $behaviouroutput->marked_out_of_max($qa, $this, $options);
232 return $behaviouroutput->mark_out_of_max($qa, $this, $options);
237 * Generate the display of the available marks for this question.
238 * @param question_attempt $qa the question attempt to display.
239 * @param question_display_options $options controls what should and should not be displayed.
240 * @return HTML fragment.
242 public function standard_marked_out_of_max(question_attempt
$qa, question_display_options
$options) {
243 return get_string('markedoutofmax', 'question', $qa->format_max_mark($options->markdp
));
247 * Generate the display of the marks for this question out of the available marks.
248 * @param question_attempt $qa the question attempt to display.
249 * @param question_display_options $options controls what should and should not be displayed.
250 * @return HTML fragment.
252 public function standard_mark_out_of_max(question_attempt
$qa, question_display_options
$options) {
254 $a->mark
= $qa->format_mark($options->markdp
);
255 $a->max
= $qa->format_max_mark($options->markdp
);
256 return get_string('markoutofmax', 'question', $a);
260 * Render the question flag, assuming $flagsoption allows it.
262 * @param question_attempt $qa the question attempt to display.
263 * @param int $flagsoption the option that says whether flags should be displayed.
265 protected function question_flag(question_attempt
$qa, $flagsoption) {
268 $divattributes = array('class' => 'questionflag');
270 switch ($flagsoption) {
271 case question_display_options
::VISIBLE
:
272 $flagcontent = $this->get_flag_html($qa->is_flagged());
275 case question_display_options
::EDITABLE
:
276 $id = $qa->get_flag_field_name();
277 // The checkbox id must be different from any element name, because
278 // of a stupid IE bug:
279 // http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
280 $checkboxattributes = array(
281 'type' => 'checkbox',
282 'id' => $id . 'checkbox',
286 if ($qa->is_flagged()) {
287 $checkboxattributes['checked'] = 'checked';
289 $postdata = question_flags
::get_postdata($qa);
291 $flagcontent = html_writer
::empty_tag('input',
292 array('type' => 'hidden', 'name' => $id, 'value' => 0)) .
293 html_writer
::empty_tag('input', $checkboxattributes) .
294 html_writer
::empty_tag('input',
295 array('type' => 'hidden', 'value' => $postdata, 'class' => 'questionflagpostdata')) .
296 html_writer
::tag('label', $this->get_flag_html($qa->is_flagged(), $id . 'img'),
297 array('id' => $id . 'label', 'for' => $id . 'checkbox')) . "\n";
299 $divattributes = array(
300 'class' => 'questionflag editable',
301 'aria-atomic' => 'true',
302 'aria-relevant' => 'text',
303 'aria-live' => 'assertive',
312 return html_writer
::nonempty_tag('div', $flagcontent, $divattributes);
316 * Work out the actual img tag needed for the flag
318 * @param bool $flagged whether the question is currently flagged.
319 * @param string $id an id to be added as an attribute to the img (optional).
320 * @return string the img tag.
322 protected function get_flag_html($flagged, $id = '') {
325 $alt = get_string('flagged', 'question');
327 $icon = 'i/unflagged';
328 $alt = get_string('notflagged', 'question');
331 'src' => $this->image_url($icon),
335 $attributes['id'] = $id;
337 $img = html_writer
::empty_tag('img', $attributes);
339 $img .= ' ' . get_string('flagged', 'question');
344 protected function edit_question_link(question_attempt
$qa,
345 question_display_options
$options) {
348 if (empty($options->editquestionparams
)) {
352 $params = $options->editquestionparams
;
353 if ($params['returnurl'] instanceof moodle_url
) {
354 $params['returnurl'] = $params['returnurl']->out_as_local_url(false);
356 $params['id'] = $qa->get_question()->id
;
357 $editurl = new moodle_url('/question/question.php', $params);
359 return html_writer
::tag('div', html_writer
::link(
360 $editurl, $this->pix_icon('t/edit', get_string('edit'), '', array('class' => 'iconsmall')) .
361 get_string('editquestion', 'question')),
362 array('class' => 'editquestion'));
366 * Generate the display of the formulation part of the question. This is the
367 * area that contains the quetsion text, and the controls for students to
368 * input their answers. Some question types also embed feedback, for
369 * example ticks and crosses, in this area.
371 * @param question_attempt $qa the question attempt to display.
372 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
374 * @param qtype_renderer $qtoutput the renderer to output the question type
376 * @param question_display_options $options controls what should and should not be displayed.
377 * @return HTML fragment.
379 protected function formulation(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
380 qtype_renderer
$qtoutput, question_display_options
$options) {
382 $output .= html_writer
::empty_tag('input', array(
384 'name' => $qa->get_control_field_name('sequencecheck'),
385 'value' => $qa->get_sequence_check_count()));
386 $output .= $qtoutput->formulation_and_controls($qa, $options);
387 if ($options->clearwrong
) {
388 $output .= $qtoutput->clear_wrong($qa);
390 $output .= html_writer
::nonempty_tag('div',
391 $behaviouroutput->controls($qa, $options), array('class' => 'im-controls'));
396 * Generate the display of the outcome part of the question. This is the
397 * area that contains the various forms of feedback.
399 * @param question_attempt $qa the question attempt to display.
400 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
402 * @param qtype_renderer $qtoutput the renderer to output the question type
404 * @param question_display_options $options controls what should and should not be displayed.
405 * @return HTML fragment.
407 protected function outcome(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
408 qtype_renderer
$qtoutput, question_display_options
$options) {
410 $output .= html_writer
::nonempty_tag('div',
411 $qtoutput->feedback($qa, $options), array('class' => 'feedback'));
412 $output .= html_writer
::nonempty_tag('div',
413 $behaviouroutput->feedback($qa, $options), array('class' => 'im-feedback'));
414 $output .= html_writer
::nonempty_tag('div',
415 $options->extrainfocontent
, array('class' => 'extra-feedback'));
419 protected function manual_comment(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
420 qtype_renderer
$qtoutput, question_display_options
$options) {
421 return $qtoutput->manual_comment($qa, $options) .
422 $behaviouroutput->manual_comment($qa, $options);
426 * Generate the display of the response history part of the question. This
427 * is the table showing all the steps the question has been through.
429 * @param question_attempt $qa the question attempt to display.
430 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
432 * @param qtype_renderer $qtoutput the renderer to output the question type
434 * @param question_display_options $options controls what should and should not be displayed.
435 * @return HTML fragment.
437 protected function response_history(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
438 qtype_renderer
$qtoutput, question_display_options
$options) {
440 if (!$options->history
) {
444 $table = new html_table();
445 $table->head
= array (
446 get_string('step', 'question'),
448 get_string('action', 'question'),
449 get_string('state', 'question'),
451 if ($options->marks
>= question_display_options
::MARK_AND_MAX
) {
452 $table->head
[] = get_string('marks', 'question');
455 foreach ($qa->get_full_step_iterator() as $i => $step) {
459 if ($stepno == $qa->get_num_steps()) {
460 $rowclass = 'current';
461 } else if (!empty($options->questionreviewlink
)) {
462 $url = new moodle_url($options->questionreviewlink
,
463 array('slot' => $qa->get_slot(), 'step' => $i));
464 $stepno = $this->output
->action_link($url, $stepno,
465 new popup_action('click', $url, 'reviewquestion',
466 array('width' => 450, 'height' => 650)),
467 array('title' => get_string('reviewresponse', 'question')));
470 $restrictedqa = new question_attempt_with_restricted_history($qa, $i, null);
472 $user = new stdClass();
473 $user->id
= $step->get_user_id();
476 userdate($step->get_timecreated(), get_string('strftimedatetimeshort')),
477 s($qa->summarise_action($step)),
478 $restrictedqa->get_state_string($options->correctness
),
481 if ($options->marks
>= question_display_options
::MARK_AND_MAX
) {
482 $row[] = $qa->format_fraction_as_mark($step->get_fraction(), $options->markdp
);
485 $table->rowclasses
[] = $rowclass;
486 $table->data
[] = $row;
489 return html_writer
::tag('h4', get_string('responsehistory', 'question'),
490 array('class' => 'responsehistoryheader')) .
491 $options->extrahistorycontent
.
492 html_writer
::tag('div', html_writer
::table($table, true),
493 array('class' => 'responsehistoryheader'));