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