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