MDL-20636 Add @package and GPL boiler-plate to files in /question.
[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 */
729c372b 37class core_question_renderer extends plugin_renderer_base {
d1b7e03d
TH
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 {
0ff4bd08 181 $a = new stdClass();
d1b7e03d
TH
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 = '') {
d1b7e03d
TH
234 if ($id) {
235 $id = 'id="' . $id . '" ';
236 }
237 if ($flagged) {
238 $img = 'flagged.png';
239 } else {
240 $img = 'unflagged.png';
241 }
ec3d4ef5 242 return '<img ' . $id . 'src="' . $this->pix_url('/i/' . $img) .
d1b7e03d
TH
243 '" alt="' . get_string('flagthisquestion', 'question') . '" />';
244 }
245
246 /**
247 * Generate the display of the formulation part of the question. This is the
248 * area that contains the quetsion text, and the controls for students to
249 * input their answers. Some question types also embed feedback, for
250 * example ticks and crosses, in this area.
251 *
252 * @param question_attempt $qa the question attempt to display.
253 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
254 * specific parts.
255 * @param qtype_renderer $qtoutput the renderer to output the question type
256 * specific parts.
257 * @param question_display_options $options controls what should and should not be displayed.
258 * @return HTML fragment.
259 */
260 protected function formulation(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
261 qtype_renderer $qtoutput, question_display_options $options) {
262 $output = '';
263 $output .= html_writer::empty_tag('input', array(
264 'type' => 'hidden',
265 'name' => $qa->get_control_field_name('sequencecheck'),
266 'value' => $qa->get_num_steps()));
267 $output .= $qtoutput->formulation_and_controls($qa, $options);
268 if ($options->clearwrong) {
269 $output .= $qtoutput->clear_wrong($qa);
270 }
271 $output .= html_writer::nonempty_tag('div',
272 $behaviouroutput->controls($qa, $options), array('class' => 'im-controls'));
273 return $output;
274 }
275
276 /**
277 * Generate the display of the outcome part of the question. This is the
278 * area that contains the various forms of feedback.
279 *
280 * @param question_attempt $qa the question attempt to display.
281 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
282 * specific parts.
283 * @param qtype_renderer $qtoutput the renderer to output the question type
284 * specific parts.
285 * @param question_display_options $options controls what should and should not be displayed.
286 * @return HTML fragment.
287 */
288 protected function outcome(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
289 qtype_renderer $qtoutput, question_display_options $options) {
290 $output = '';
291 $output .= html_writer::nonempty_tag('div',
292 $qtoutput->feedback($qa, $options), array('class' => 'feedback'));
293 $output .= html_writer::nonempty_tag('div',
294 $behaviouroutput->feedback($qa, $options), array('class' => 'im-feedback'));
295 return $output;
296 }
297
298 /**
299 * Generate the display of the response history part of the question. This
300 * is the table showing all the steps the question has been through.
301 *
302 * @param question_attempt $qa the question attempt to display.
303 * @param qbehaviour_renderer $behaviouroutput the renderer to output the behaviour
304 * specific parts.
305 * @param qtype_renderer $qtoutput the renderer to output the question type
306 * specific parts.
307 * @param question_display_options $options controls what should and should not be displayed.
308 * @return HTML fragment.
309 */
310 protected function response_history(question_attempt $qa, qbehaviour_renderer $behaviouroutput,
311 qtype_renderer $qtoutput, question_display_options $options) {
312
313 if (!$options->history) {
314 return '';
315 }
316
068b4594 317 $table = new html_table();
d1b7e03d
TH
318 $table->head = array (
319 get_string('step', 'question'),
320 get_string('time'),
321 get_string('action', 'question'),
322 get_string('state', 'question'),
323 );
324 if ($options->marks >= question_display_options::MARK_AND_MAX) {
325 $table->head[] = get_string('marks', 'question');
326 }
327
328 foreach ($qa->get_full_step_iterator() as $i => $step) {
329 $stepno = $i + 1;
330 $rowclass = '';
331 if ($stepno == $qa->get_num_steps()) {
332 $rowclass = 'current';
333 } else if (!empty($options->questionreviewlink)) {
729c372b
TH
334 $url = new moodle_url($options->questionreviewlink, array('slot' => $qa->get_slot(), 'step' => $i));
335 $stepno = $this->output->action_link($url, $stepno,
336 new popup_action('click', $url, 'reviewquestion', array('width' => 450, 'height' => 650)),
337 array('title' => get_string('reviewresponse', 'quiz')));
d1b7e03d 338 }
0ff4bd08 339 $user = new stdClass();
d1b7e03d
TH
340 $user->id = $step->get_user_id();
341 $row = array(
342 $stepno,
343 userdate($step->get_timecreated(), get_string('strftimedatetimeshort')),
344 s($qa->summarise_action($step)),
345 $step->get_state()->default_string(true),
346 );
347
348 if ($options->marks >= question_display_options::MARK_AND_MAX) {
349 $row[] = $qa->format_fraction_as_mark($step->get_fraction(), $options->markdp);
350 }
351
068b4594 352 $table->rowclasses[] = $rowclass;
d1b7e03d
TH
353 $table->data[] = $row;
354 }
355
356 return html_writer::tag('h3', get_string('responsehistory', 'question'),
357 array('class' => 'responsehistoryheader')) . html_writer::tag('div',
068b4594 358 html_writer::table($table, true), array('class' => 'responsehistoryheader'));
d1b7e03d
TH
359 }
360
361}