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