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
26 use core_question\output\question_version_info
;
28 defined('MOODLE_INTERNAL') ||
die();
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.
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_question_renderer
extends plugin_renderer_base
{
40 public function get_page() {
45 * @deprecated since Moodle 4.0
47 public function question_preview_link() {
48 throw new coding_exception(__FUNCTION__
. '() has been removed.');
52 * Generate the display of a question in a particular state, and with certain
53 * display options. Normally you do not call this method directly. Intsead
54 * you call {@link question_usage_by_activity::render_question()} which will
55 * call this method with appropriate arguments.
57 * @param question_attempt $qa the question attempt to display.
58 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
60 * @param qtype_renderer $qtoutput the renderer to output the question type
62 * @param question_display_options $options controls what should and should not be displayed.
63 * @param string|null $number The question number to display. 'i' is a special
64 * value that gets displayed as Information. Null means no number is displayed.
65 * @return string HTML representation of the question.
67 public function question(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
68 qtype_renderer
$qtoutput, question_display_options
$options, $number) {
70 // If not already set, record the questionidentifier.
71 $options = clone($options);
72 if (!$options->has_question_identifier()) {
73 $options->questionidentifier
= $this->question_number_text($number);
77 $output .= html_writer
::start_tag('div', array(
78 'id' => $qa->get_outer_question_div_unique_id(),
79 'class' => implode(' ', array(
81 $qa->get_question(false)->get_type_name(),
82 $qa->get_behaviour_name(),
83 $qa->get_state_class($options->correctness
&& $qa->has_marks()),
87 $output .= html_writer
::tag('div',
88 $this->info($qa, $behaviouroutput, $qtoutput, $options, $number),
89 array('class' => 'info'));
91 $output .= html_writer
::start_tag('div', array('class' => 'content'));
93 $output .= html_writer
::tag('div',
94 $this->add_part_heading($qtoutput->formulation_heading(),
95 $this->formulation($qa, $behaviouroutput, $qtoutput, $options)),
96 array('class' => 'formulation clearfix'));
97 $output .= html_writer
::nonempty_tag('div',
98 $this->add_part_heading(get_string('feedback', 'question'),
99 $this->outcome($qa, $behaviouroutput, $qtoutput, $options)),
100 array('class' => 'outcome clearfix'));
101 $output .= html_writer
::nonempty_tag('div',
102 $this->add_part_heading(get_string('comments', 'question'),
103 $this->manual_comment($qa, $behaviouroutput, $qtoutput, $options)),
104 array('class' => 'comment clearfix'));
105 $output .= html_writer
::nonempty_tag('div',
106 $this->response_history($qa, $behaviouroutput, $qtoutput, $options),
107 array('class' => 'history clearfix border p-2'));
109 $output .= html_writer
::end_tag('div');
110 $output .= html_writer
::end_tag('div');
115 * Generate the information bit of the question display that contains the
116 * metadata like the question number, current state, and mark.
117 * @param question_attempt $qa the question attempt to display.
118 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
120 * @param qtype_renderer $qtoutput the renderer to output the question type
122 * @param question_display_options $options controls what should and should not be displayed.
123 * @param string|null $number The question number to display. 'i' is a special
124 * value that gets displayed as Information. Null means no number is displayed.
125 * @return HTML fragment.
127 protected function info(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
128 qtype_renderer
$qtoutput, question_display_options
$options, $number) {
130 $output .= $this->number($number);
131 $output .= $this->status($qa, $behaviouroutput, $options);
132 $output .= $this->mark_summary($qa, $behaviouroutput, $options);
133 $output .= $this->question_flag($qa, $options->flags
);
134 $output .= $this->edit_question_link($qa, $options);
135 if ($options->versioninfo
) {
136 $output .= $this->render(new question_version_info($qa->get_question(), true));
142 * Generate the display of the question number.
143 * @param string|null $number The question number to display. 'i' is a special
144 * value that gets displayed as Information. Null means no number is displayed.
145 * @return HTML fragment.
147 protected function number($number) {
148 if (trim($number ??
'') === '') {
151 if (trim($number) === 'i') {
152 $numbertext = get_string('information', 'question');
154 $numbertext = get_string('questionx', 'question',
155 html_writer
::tag('span', s($number), array('class' => 'qno')));
157 return html_writer
::tag('h3', $numbertext, array('class' => 'no'));
161 * Get the question number as a string.
163 * @param string|null $number e.g. '123' or 'i'. null or '' means do not display anything number-related.
164 * @return string e.g. 'Question 123' or 'Information' or ''.
166 protected function question_number_text(?
string $number): string {
167 $number = $number ??
'';
168 // Trim the question number of whitespace, including .
169 $trimmed = trim(html_entity_decode($number), " \n\r\t\v\x00\xC2\xA0");
170 if ($trimmed === '') {
173 if (trim($number) === 'i') {
174 return get_string('information', 'question');
176 return get_string('questionx', 'question', s($number));
181 * Add an invisible heading like 'question text', 'feebdack' at the top of
182 * a section's contents, but only if the section has some content.
183 * @param string $heading the heading to add.
184 * @param string $content the content of the section.
185 * @return string HTML fragment with the heading added.
187 protected function add_part_heading($heading, $content) {
189 $content = html_writer
::tag('h4', $heading, array('class' => 'accesshide')) . $content;
195 * Generate the display of the status line that gives the current state of
197 * @param question_attempt $qa the question attempt to display.
198 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
200 * @param question_display_options $options controls what should and should not be displayed.
201 * @return HTML fragment.
203 protected function status(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
204 question_display_options
$options) {
205 return html_writer
::tag('div', $qa->get_state_string($options->correctness
),
206 array('class' => 'state'));
210 * Generate the display of the marks for this question.
211 * @param question_attempt $qa the question attempt to display.
212 * @param qbehaviour_renderer $behaviouroutput the behaviour renderer, which can generate a custom display.
213 * @param question_display_options $options controls what should and should not be displayed.
214 * @return HTML fragment.
216 protected function mark_summary(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput, question_display_options
$options) {
217 return html_writer
::nonempty_tag('div',
218 $behaviouroutput->mark_summary($qa, $this, $options),
219 array('class' => 'grade'));
223 * Generate the display of the marks for this question.
224 * @param question_attempt $qa the question attempt to display.
225 * @param question_display_options $options controls what should and should not be displayed.
226 * @return HTML fragment.
228 public function standard_mark_summary(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput, question_display_options
$options) {
229 if (!$options->marks
) {
232 } else if ($qa->get_max_mark() == 0) {
233 return get_string('notgraded', 'question');
235 } else if ($options->marks
== question_display_options
::MAX_ONLY ||
236 is_null($qa->get_fraction())) {
237 return $behaviouroutput->marked_out_of_max($qa, $this, $options);
240 return $behaviouroutput->mark_out_of_max($qa, $this, $options);
245 * Generate the display of the available marks for this question.
246 * @param question_attempt $qa the question attempt to display.
247 * @param question_display_options $options controls what should and should not be displayed.
248 * @return HTML fragment.
250 public function standard_marked_out_of_max(question_attempt
$qa, question_display_options
$options) {
251 return get_string('markedoutofmax', 'question', $qa->format_max_mark($options->markdp
));
255 * Generate the display of the marks for this question out of the available marks.
256 * @param question_attempt $qa the question attempt to display.
257 * @param question_display_options $options controls what should and should not be displayed.
258 * @return HTML fragment.
260 public function standard_mark_out_of_max(question_attempt
$qa, question_display_options
$options) {
262 $a->mark
= $qa->format_mark($options->markdp
);
263 $a->max
= $qa->format_max_mark($options->markdp
);
264 return get_string('markoutofmax', 'question', $a);
268 * Render the question flag, assuming $flagsoption allows it.
270 * @param question_attempt $qa the question attempt to display.
271 * @param int $flagsoption the option that says whether flags should be displayed.
273 protected function question_flag(question_attempt
$qa, $flagsoption) {
274 $divattributes = array('class' => 'questionflag');
276 switch ($flagsoption) {
277 case question_display_options
::VISIBLE
:
278 $flagcontent = $this->get_flag_html($qa->is_flagged());
281 case question_display_options
::EDITABLE
:
282 $id = $qa->get_flag_field_name();
283 // The checkbox id must be different from any element name, because
284 // of a stupid IE bug:
285 // http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
286 $checkboxattributes = array(
287 'type' => 'checkbox',
288 'id' => $id . 'checkbox',
292 if ($qa->is_flagged()) {
293 $checkboxattributes['checked'] = 'checked';
295 $postdata = question_flags
::get_postdata($qa);
297 $flagcontent = html_writer
::empty_tag('input',
298 array('type' => 'hidden', 'name' => $id, 'value' => 0)) .
299 html_writer
::empty_tag('input',
300 array('type' => 'hidden', 'value' => $postdata, 'class' => 'questionflagpostdata')) .
301 html_writer
::empty_tag('input', $checkboxattributes) .
302 html_writer
::tag('label', $this->get_flag_html($qa->is_flagged(), $id . 'img'),
303 array('id' => $id . 'label', 'for' => $id . 'checkbox')) . "\n";
305 $divattributes = array(
306 'class' => 'questionflag editable',
315 return html_writer
::nonempty_tag('div', $flagcontent, $divattributes);
319 * Work out the actual img tag needed for the flag
321 * @param bool $flagged whether the question is currently flagged.
322 * @param string $id an id to be added as an attribute to the img (optional).
323 * @return string the img tag.
325 protected function get_flag_html($flagged, $id = '') {
328 $label = get_string('clickunflag', 'question');
330 $icon = 'i/unflagged';
331 $label = get_string('clickflag', 'question');
334 'src' => $this->image_url($icon),
336 'class' => 'questionflagimage',
339 $attributes['id'] = $id;
341 $img = html_writer
::empty_tag('img', $attributes);
342 $img .= html_writer
::span($label);
348 * Generate the display of the edit question link.
350 * @param question_attempt $qa The question attempt to display.
351 * @param question_display_options $options controls what should and should not be displayed.
354 protected function edit_question_link(question_attempt
$qa, question_display_options
$options) {
355 if (empty($options->editquestionparams
)) {
359 $params = $options->editquestionparams
;
360 if ($params['returnurl'] instanceof moodle_url
) {
361 $params['returnurl'] = $params['returnurl']->out_as_local_url(false);
363 $params['id'] = $qa->get_question_id();
364 $editurl = new moodle_url('/question/bank/editquestion/question.php', $params);
366 return html_writer
::tag('div', html_writer
::link(
367 $editurl, $this->pix_icon('t/edit', get_string('edit'), '', array('class' => 'iconsmall')) .
368 get_string('editquestion', 'question')),
369 array('class' => 'editquestion'));
373 * Generate the display of the formulation part of the question. This is the
374 * area that contains the quetsion text, and the controls for students to
375 * input their answers. Some question types also embed feedback, for
376 * example ticks and crosses, in this area.
378 * @param question_attempt $qa the question attempt to display.
379 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
381 * @param qtype_renderer $qtoutput the renderer to output the question type
383 * @param question_display_options $options controls what should and should not be displayed.
384 * @return HTML fragment.
386 protected function formulation(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
387 qtype_renderer
$qtoutput, question_display_options
$options) {
389 $output .= html_writer
::empty_tag('input', array(
391 'name' => $qa->get_control_field_name('sequencecheck'),
392 'value' => $qa->get_sequence_check_count()));
393 $output .= $qtoutput->formulation_and_controls($qa, $options);
394 if ($options->clearwrong
) {
395 $output .= $qtoutput->clear_wrong($qa);
397 $output .= html_writer
::nonempty_tag('div',
398 $behaviouroutput->controls($qa, $options), array('class' => 'im-controls'));
403 * Generate the display of the outcome part of the question. This is the
404 * area that contains the various forms of feedback.
406 * @param question_attempt $qa the question attempt to display.
407 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
409 * @param qtype_renderer $qtoutput the renderer to output the question type
411 * @param question_display_options $options controls what should and should not be displayed.
412 * @return HTML fragment.
414 protected function outcome(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
415 qtype_renderer
$qtoutput, question_display_options
$options) {
417 $output .= html_writer
::nonempty_tag('div',
418 $qtoutput->feedback($qa, $options), array('class' => 'feedback'));
419 $output .= html_writer
::nonempty_tag('div',
420 $behaviouroutput->feedback($qa, $options), array('class' => 'im-feedback'));
421 $output .= html_writer
::nonempty_tag('div',
422 $options->extrainfocontent
, array('class' => 'extra-feedback'));
426 protected function manual_comment(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
427 qtype_renderer
$qtoutput, question_display_options
$options) {
428 return $qtoutput->manual_comment($qa, $options) .
429 $behaviouroutput->manual_comment($qa, $options);
433 * Generate the display of the response history part of the question. This
434 * is the table showing all the steps the question has been through.
436 * @param question_attempt $qa the question attempt to display.
437 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
439 * @param qtype_renderer $qtoutput the renderer to output the question type
441 * @param question_display_options $options controls what should and should not be displayed.
442 * @return HTML fragment.
444 protected function response_history(question_attempt
$qa, qbehaviour_renderer
$behaviouroutput,
445 qtype_renderer
$qtoutput, question_display_options
$options) {
447 if (!$options->history
) {
451 $table = new html_table();
452 $table->head
= array (
453 get_string('step', 'question'),
455 get_string('action', 'question'),
456 get_string('state', 'question'),
458 if ($options->marks
>= question_display_options
::MARK_AND_MAX
) {
459 $table->head
[] = get_string('marks', 'question');
462 foreach ($qa->get_full_step_iterator() as $i => $step) {
466 if ($stepno == $qa->get_num_steps()) {
467 $rowclass = 'current';
468 } else if (!empty($options->questionreviewlink
)) {
469 $url = new moodle_url($options->questionreviewlink
,
470 array('slot' => $qa->get_slot(), 'step' => $i));
471 $stepno = $this->output
->action_link($url, $stepno,
472 new popup_action('click', $url, 'reviewquestion',
473 array('width' => 450, 'height' => 650)),
474 array('title' => get_string('reviewresponse', 'question')));
477 $restrictedqa = new question_attempt_with_restricted_history($qa, $i, null);
480 userdate($step->get_timecreated(), get_string('strftimedatetimeshortaccurate', 'core_langconfig')),
481 s($qa->summarise_action($step)) . $this->action_author($step, $options),
482 $restrictedqa->get_state_string($options->correctness
)];
484 if ($options->marks
>= question_display_options
::MARK_AND_MAX
) {
485 $row[] = $qa->format_fraction_as_mark($step->get_fraction(), $options->markdp
);
488 $table->rowclasses
[] = $rowclass;
489 $table->data
[] = $row;
492 return html_writer
::tag('h4', get_string('responsehistory', 'question'),
493 array('class' => 'responsehistoryheader')) .
494 $options->extrahistorycontent
.
495 html_writer
::tag('div', html_writer
::table($table, true),
496 array('class' => 'responsehistoryheader'));
500 * Action author's profile link.
502 * @param question_attempt_step $step The step.
503 * @param question_display_options $options The display options.
504 * @return string The link to user's profile.
506 protected function action_author(question_attempt_step
$step, question_display_options
$options): string {
507 if ($options->userinfoinhistory
&& $step->get_user_id() != $options->userinfoinhistory
) {
508 return html_writer
::link(
509 new moodle_url('/user/view.php', ['id' => $step->get_user_id(), 'course' => $this->page
->course
->id
]),
510 $step->get_user_fullname(), ['class' => 'd-table-cell']);