MDL-28976 quiz was passing 0 to ->attempt_url when it should have been null.
[moodle.git] / mod / quiz / renderer.php
CommitLineData
b3782c71
TH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Defines the renderer for the quiz module.
19 *
20 * @package mod
21 * @subpackage quiz
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27/**
28 * The renderer for the quiz module.
29 *
78a61cd6 30 * @copyright 2011 The Open University
b3782c71
TH
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
33class mod_quiz_renderer extends plugin_renderer_base {
2c3e2b53
DL
34 /**
35 * Builds the review page
36 *
37 * @param quiz_attempt $attemptobj an instance of quiz_attempt.
38 * @param array $slots an array of intgers relating to questions.
39 * @param int $page the current page number
40 * @param bool $showall whether to show entire attempt on one page.
41 * @param bool $lastpage if true the current page is the last page.
42 * @param mod_quiz_display_options $displayoptions instance of mod_quiz_display_options.
43 * @param array $summarydata contains all table data
44 * @return $output containing html data.
45 */
b3782c71 46 public function review_page(quiz_attempt $attemptobj, $slots, $page, $showall,
9f335e76
DL
47 $lastpage, mod_quiz_display_options $displayoptions,
48 $summarydata) {
b3782c71
TH
49
50 $output = '';
51 $output .= $this->header();
52 $output .= $this->review_summary_table($summarydata, $page);
53 $output .= $this->review_form($page, $showall, $displayoptions,
06eb64b8 54 $this->questions($attemptobj, true, $slots, $page, $showall, $displayoptions),
8c4deb80 55 $attemptobj);
b3782c71
TH
56
57 $output .= $this->review_next_navigation($attemptobj, $page, $lastpage);
58 $output .= $this->footer();
59 return $output;
60 }
61
3a5da40b
TH
62 /**
63 * Renders the review question pop-up.
64 *
65 * @param quiz_attempt $attemptobj an instance of quiz_attempt.
66 * @param int $slot which question to display.
67 * @param int $seq which step of the question attempt to show. null = latest.
68 * @param mod_quiz_display_options $displayoptions instance of mod_quiz_display_options.
69 * @param array $summarydata contains all table data
70 * @return $output containing html data.
71 */
72 public function review_question_page(quiz_attempt $attemptobj, $slot, $seq,
73 mod_quiz_display_options $displayoptions, $summarydata) {
74
75 $output = '';
76 $output .= $this->header();
77 $output .= $this->review_summary_table($summarydata, 0);
78
79 if (!is_null($seq)) {
da729916 80 $output .= $attemptobj->render_question_at_step($slot, $seq, true);
3a5da40b 81 } else {
da729916 82 $output .= $attemptobj->render_question($slot, true);
3a5da40b
TH
83 }
84
85 $output .= $this->close_window_button();
86 $output .= $this->footer();
87 return $output;
88 }
89
90 /**
91 * Renders the review question pop-up.
92 *
93 * @param string $message Why the review is not allowed.
94 * @return string html to output.
95 */
96 public function review_question_not_allowed($message) {
97 $output = '';
98 $output .= $this->header();
99 $output .= $this->notification($message);
100 $output .= $this->close_window_button();
101 $output .= $this->footer();
102 return $output;
103 }
104
2c3e2b53
DL
105 /**
106 * Filters the summarydata array.
107 *
108 * @param array $summarydata contains row data for table
109 * @param int $page the current page number
110 * @return $summarydata containing filtered row data
111 */
b3782c71
TH
112 protected function filter_summary_table($summarydata, $page) {
113 if ($page == 0) {
114 return $summarydata;
115 }
116
117 // Only show some of summary table on subsequent pages.
118 foreach ($summarydata as $key => $rowdata) {
119 if (!in_array($key, array('user', 'attemptlist'))) {
120 unset($summarydata[$key]);
121 }
122 }
123
124 return $summarydata;
125 }
126
2c3e2b53
DL
127 /**
128 * Outputs the table containing data from summary data array
129 *
130 * @param array $summarydata contains row data for table
131 * @param int $page contains the current page number
132 */
b3782c71 133 public function review_summary_table($summarydata, $page) {
3a5da40b 134 $summarydata = $this->filter_summary_table($summarydata, $page);
b3782c71
TH
135 if (empty($summarydata)) {
136 return '';
137 }
138
139 $output = '';
140 $output .= html_writer::start_tag('table', array(
141 'class' => 'generaltable generalbox quizreviewsummary'));
142 $output .= html_writer::start_tag('tbody');
143 foreach ($summarydata as $rowdata) {
144 if ($rowdata['title'] instanceof renderable) {
145 $title = $this->render($rowdata['title']);
146 } else {
147 $title = $rowdata['title'];
148 }
149
150 if ($rowdata['content'] instanceof renderable) {
151 $content = $this->render($rowdata['content']);
152 } else {
153 $content = $rowdata['content'];
154 }
155
156 $output .= html_writer::tag('tr',
157 html_writer::tag('th', $title, array('class' => 'cell', 'scope' => 'row')) .
9f335e76 158 html_writer::tag('td', $content, array('class' => 'cell'))
b3782c71
TH
159 );
160 }
161
162 $output .= html_writer::end_tag('tbody');
163 $output .= html_writer::end_tag('table');
164 return $output;
165 }
166
2c3e2b53
DL
167 /**
168 * Renders each question
169 *
170 * @param quiz_attempt $attemptobj instance of quiz_attempt
171 * @param bool $reviewing
172 * @param array $slots array of intgers relating to questions
173 * @param int $page current page number
174 * @param bool $showall if true shows attempt on single page
175 * @param mod_quiz_display_options $displayoptions instance of mod_quiz_display_options
176 */
b3782c71 177 public function questions(quiz_attempt $attemptobj, $reviewing, $slots, $page, $showall,
bcd42560 178 mod_quiz_display_options $displayoptions) {
b3782c71
TH
179 $output = '';
180 foreach ($slots as $slot) {
181 $output .= $attemptobj->render_question($slot, $reviewing,
182 $attemptobj->review_url($slot, $page, $showall));
183 }
184 return $output;
185 }
186
2c3e2b53
DL
187 /**
188 * Renders the main bit of the review page.
189 *
190 * @param array $summarydata contain row data for table
191 * @param int $page current page number
192 * @param mod_quiz_display_options $displayoptions instance of mod_quiz_display_options
193 * @param $content contains each question
194 * @param quiz_attempt $attemptobj instance of quiz_attempt
195 * @param bool $showall if true display attempt on one page
196 */
8c4deb80 197 public function review_form($page, $showall, $displayoptions, $content, $attemptobj) {
b3782c71
TH
198 if ($displayoptions->flags != question_display_options::EDITABLE) {
199 return $content;
200 }
201
9f335e76
DL
202 $this->page->requires->js_init_call('M.mod_quiz.init_review_form', null, false,
203 quiz_get_js_module());
b3782c71 204
b3782c71 205 $output = '';
bcd42560
DL
206 $output .= html_writer::start_tag('form', array('action' => $attemptobj->review_url(0,
207 $page, $showall), 'method' => 'post', 'class' => 'questionflagsaveform'));
208 $output .= html_writer::start_tag('div');
2c3e2b53 209 $output .= $content;
bcd42560
DL
210 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey',
211 'value' => sesskey()));
212 $output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
213 $output .= html_writer::empty_tag('input', array('type' => 'submit',
214 'class' => 'questionflagsavebutton', 'name' => 'savingflags',
215 'value' => get_string('saveflags', 'question')));
06eb64b8
DL
216 $output .= html_writer::end_tag('div');
217 $output .= html_writer::end_tag('div');
218 $output .= html_writer::end_tag('form');
b3782c71
TH
219
220 return $output;
221 }
222
2c3e2b53
DL
223 /**
224 * Returns either a liink or button
225 *
226 * @param $url contains a url for the review link
227 */
b3782c71 228 public function finish_review_link($url) {
9f335e76
DL
229 if ($this->page->pagelayout == 'popup') {
230 // In a 'secure' popup window.
231 $this->page->requires->js_init_call('M.mod_quiz.secure_window.init_close_button',
232 array($url), quiz_get_js_module());
233 return html_writer::empty_tag('input', array('type' => 'button',
234 'value' => get_string('finishreview', 'quiz'),
235 'id' => 'secureclosebutton'));
236 } else {
237 return html_writer::link($url, get_string('finishreview', 'quiz'));
238 }
b3782c71
TH
239 }
240
2c3e2b53
DL
241 /**
242 * Creates a next page arrow or the finishing link
243 *
244 * @param quiz_attempt $attemptobj instance of quiz_attempt
245 * @param int $page the current page
246 * @param bool $lastpage if true current page is the last page
247 */
b3782c71
TH
248 public function review_next_navigation(quiz_attempt $attemptobj, $page, $lastpage) {
249 if ($lastpage) {
250 $nav = $this->finish_review_link($attemptobj->view_url());
251 } else {
252 $nav = link_arrow_right(get_string('next'), $attemptobj->review_url(0, $page + 1));
253 }
254 return html_writer::tag('div', $nav, array('class' => 'submitbtns'));
255 }
256
257 /**
258 * Return the HTML of the quiz timer.
259 * @return string HTML content.
260 */
261 public function countdown_timer() {
262 return html_writer::tag('div', get_string('timeleft', 'quiz') .
263 html_writer::tag('span', '', array('id' => 'quiz-time-left')),
264 array('id' => 'quiz-timer'));
265 }
266
2c3e2b53
DL
267 /**
268 * Create a preview link
269 *
270 * @param $url contains a url to the given page
271 */
b3782c71
TH
272 public function restart_preview_button($url) {
273 return $this->single_button($url, get_string('startnewpreview', 'quiz'));
274 }
275
2c3e2b53
DL
276 /**
277 * Outputs the navigation block panel
278 *
279 * @param quiz_nav_panel_base $panel instance of quiz_nav_panel_base
280 */
b3782c71
TH
281 public function navigation_panel(quiz_nav_panel_base $panel) {
282
283 $output = '';
284 $userpicture = $panel->user_picture();
285 if ($userpicture) {
286 $output .= html_writer::tag('div', $this->render($userpicture),
287 array('id' => 'user-picture', 'class' => 'clearfix'));
288 }
289 $output .= $panel->render_before_button_bits($this);
290
e28bbd89 291 $output .= html_writer::start_tag('div', array('class' => 'qn_buttons'));
b3782c71
TH
292 foreach ($panel->get_question_buttons() as $button) {
293 $output .= $this->render($button);
294 }
295 $output .= html_writer::end_tag('div');
296
297 $output .= html_writer::tag('div', $panel->render_end_bits($this),
298 array('class' => 'othernav'));
299
9f335e76
DL
300 $this->page->requires->js_init_call('M.mod_quiz.nav.init', null, false,
301 quiz_get_js_module());
b3782c71
TH
302
303 return $output;
304 }
305
2c3e2b53
DL
306 /**
307 * Returns the quizzes navigation button
308 *
309 * @param quiz_nav_question_button $button
310 */
b3782c71
TH
311 protected function render_quiz_nav_question_button(quiz_nav_question_button $button) {
312 $classes = array('qnbutton', $button->stateclass);
313 $attributes = array();
314
315 if ($button->currentpage) {
316 $classes[] = 'thispage';
317 $attributes[] = get_string('onthispage', 'quiz');
318 }
319
320 $attributes[] = $button->statestring;
321
322 // Flagged?
323 if ($button->flagged) {
324 $classes[] = 'flagged';
325 $flaglabel = get_string('flagged', 'question');
326 } else {
327 $flaglabel = '';
328 }
329 $attributes[] = html_writer::tag('span', $flaglabel, array('class' => 'flagstate'));
330
331 if (is_numeric($button->number)) {
332 $qnostring = 'questionnonav';
333 } else {
334 $qnostring = 'questionnonavinfo';
335 }
336
337 $a = new stdClass();
338 $a->number = $button->number;
339 $a->attributes = implode(' ', $attributes);
340
341 return html_writer::link($button->url,
342 html_writer::tag('span', '', array('class' => 'thispageholder')) .
343 html_writer::tag('span', '', array('class' => 'trafficlight')) .
344 get_string($qnostring, 'quiz', $a),
345 array('class' => implode(' ', $classes), 'id' => $button->id,
346 'title' => $button->statestring));
347 }
348
2c3e2b53
DL
349 /**
350 * outputs the link the other attempts.
351 *
352 * @param mod_quiz_links_to_other_attempts $links
353 */
b3782c71 354 protected function render_mod_quiz_links_to_other_attempts(
bcd42560 355 mod_quiz_links_to_other_attempts $links) {
b3782c71
TH
356 $attemptlinks = array();
357 foreach ($links->links as $attempt => $url) {
358 if ($url) {
359 $attemptlinks[] = html_writer::link($url, $attempt);
360 } else {
361 $attemptlinks[] = html_writer::tag('strong', $attempt);
362 }
363 }
364 return implode(', ', $attemptlinks);
365 }
9f335e76 366
2c3e2b53 367 /**
606e07d5 368 * Attempt Page
2c3e2b53
DL
369 *
370 * @param quiz_attempt $attemptobj Instance of quiz_attempt
371 * @param int $page Current page number
372 * @param quiz_access_manager $accessmanager Instance of quiz_access_manager
373 * @param array $messages An array of messages
374 * @param array $slots Contains an array of integers that relate to questions
375 * @param int $id The ID of an attempt
376 * @param int $nextpage The number of the next page
606e07d5 377 */
9f335e76 378 public function attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id,
bcd42560 379 $nextpage) {
606e07d5 380 $output = '';
bcd42560 381 $output .= $this->quiz_notices($messages);
9f335e76 382 $output .= $this->attempt_form($attemptobj, $page, $slots, $id, $nextpage);
606e07d5
DL
383 return $output;
384 }
9f335e76 385
2c3e2b53
DL
386 /**
387 * Returns any notices.
388 *
389 * @param array $messages
390 */
8e8bd3f9 391 public function quiz_notices($messages) {
bcd42560
DL
392 if (!$messages) {
393 return '';
606e07d5 394 }
bcd42560 395 return $this->box($this->heading(get_string('accessnoticesheader', 'quiz'), 3) .
adf7fe21 396 $this->access_messages($messages), 'quizaccessnotices');
606e07d5 397 }
9f335e76 398
2c3e2b53
DL
399 /**
400 * Ouputs the form for making an attempt
401 *
402 * @param quiz_attempt $attemptobj
403 * @param int $page Current page number
404 * @param array $slots Array of integers relating to questions
405 * @param int $id ID of the attempt
406 * @param int $nextpage Next page number
407 */
8e8bd3f9 408 public function attempt_form($attemptobj, $page, $slots, $id, $nextpage) {
9f335e76 409 $output = '';
bcd42560
DL
410
411 //Start Form
63be47d9 412 $output .= html_writer::start_tag('form',
13040e14 413 array('action' => $attemptobj->processattempt_url(), 'method' => 'post',
63be47d9
DL
414 'enctype' => 'multipart/form-data', 'accept-charset' => 'utf-8',
415 'id' => 'responseform'));
bcd42560 416 $output .= html_writer::start_tag('div');
9f335e76 417
606e07d5
DL
418 // Print all the questions
419 foreach ($slots as $slot) {
45e5fbe1
TH
420 $output .= $attemptobj->render_question($slot, false,
421 $attemptobj->attempt_url($slot, $page));
606e07d5 422 }
9f335e76 423
bcd42560 424 $output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
63be47d9
DL
425 $output .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'next',
426 'value' => get_string('next')));
bcd42560 427 $output .= html_writer::end_tag('div');
9f335e76 428
606e07d5 429 // Some hidden fields to trach what is going on.
63be47d9
DL
430 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'attempt',
431 'value' => $attemptobj->get_attemptid()));
432 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'thispage',
433 'value' => $page, 'id' => 'followingpage'));
434 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'nextpage',
435 'value' => $nextpage));
436 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'timeup',
437 'value' => '0', 'id' => 'timeup'));
438 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey',
439 'value' => sesskey()));
440 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'scrollpos',
441 'value' => '', 'id' => 'scrollpos'));
9f335e76 442
606e07d5
DL
443 // Add a hidden field with questionids. Do this at the end of the form, so
444 // if you navigate before the form has finished loading, it does not wipe all
445 // the student's answers.
63be47d9
DL
446 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'slots',
447 'value' => implode(',', $slots)));
448
bcd42560
DL
449 //Finish form
450 $output .= html_writer::end_tag('div');
451 $output .= html_writer::end_tag('form');
9f335e76
DL
452
453 return $output;
606e07d5 454 }
cbe87543 455
13040e14
DL
456 /**
457 * Print each message in an array, surrounded by &lt;p>, &lt;/p> tags.
458 *
459 * @param array $messages the array of message strings.
460 * @param bool $return if true, return a string, instead of outputting.
461 *
462 * @return mixed, if $return is true, return the string that would have been output, otherwise
463 * return null.
464 */
465 public function access_messages($messages) {
466 $output = '';
467 foreach ($messages as $message) {
468 $output .= html_writer::tag('p', $message) . "\n";
469 }
470 return $output;
471 }
9f335e76 472
63be47d9
DL
473 /*
474 * Summary Page
475 */
2c3e2b53
DL
476 /**
477 * Create the summary page
478 *
479 * @param quiz_attempt $attemptobj
480 * @param mod_quiz_display_options $displayoptions
481 */
63be47d9
DL
482 public function summary_page($attemptobj, $displayoptions) {
483 $output = '';
484 $output .= $this->summary_table($attemptobj, $displayoptions);
13040e14 485 $output .= $this->summary_page_controls($attemptobj);
63be47d9
DL
486 return $output;
487 }
488
2c3e2b53
DL
489 /**
490 * Generates the table of summarydata
491 *
492 * @param quiz_attempt $attemptobj
493 * @param mod_quiz_display_options $displayoptions
494 */
8e8bd3f9 495 public function summary_table($attemptobj, $displayoptions) {
63be47d9
DL
496 // Prepare the summary table header
497 $table = new html_table();
498 $table->attributes['class'] = 'generaltable quizsummaryofattempt boxaligncenter';
499 $table->head = array(get_string('question', 'quiz'), get_string('status', 'quiz'));
500 $table->align = array('left', 'left');
501 $table->size = array('', '');
502 $markscolumn = $displayoptions->marks >= question_display_options::MARK_AND_MAX;
503 if ($markscolumn) {
504 $table->head[] = get_string('marks', 'quiz');
505 $table->align[] = 'left';
506 $table->size[] = '';
507 }
508 $table->data = array();
509
510 // Get the summary info for each question.
511 $slots = $attemptobj->get_slots();
512 foreach ($slots as $slot) {
513 if (!$attemptobj->is_real_question($slot)) {
514 continue;
515 }
516 $flag = '';
517 if ($attemptobj->is_question_flagged($slot)) {
518 $flag = html_writer::empty_tag('img', array('src' => $this->pix_url('i/flagged'),
519 'alt' => get_string('flagged', 'question'), 'class' => 'questionflag'));
520 }
2178cd3a
TH
521 $row = array(html_writer::link($attemptobj->attempt_url($slot),
522 $attemptobj->get_question_number($slot) . $flag),
523 $attemptobj->get_question_status($slot, $displayoptions->correctness));
63be47d9
DL
524 if ($markscolumn) {
525 $row[] = $attemptobj->get_question_mark($slot);
526 }
527 $table->data[] = $row;
97cdc1de
TH
528 $table->rowclasses[] = $attemptobj->get_question_state_class(
529 $slot, $displayoptions->correctness);
63be47d9
DL
530 }
531
532 // Print the summary table.
533 $output = html_writer::table($table);
534
535 return $output;
536 }
537
2c3e2b53
DL
538 /**
539 * Creates any controls a the page should have.
540 *
541 * @param quiz_attempt $attemptobj
542 */
8e8bd3f9 543 public function summary_page_controls($attemptobj) {
63be47d9
DL
544 $output = '';
545 // countdown timer
da59992e 546 $output .= $this->countdown_timer();
63be47d9
DL
547
548 // Finish attempt button.
63be47d9
DL
549 $options = array(
550 'attempt' => $attemptobj->get_attemptid(),
551 'finishattempt' => 1,
552 'timeup' => 0,
553 'slots' => '',
554 'sesskey' => sesskey(),
555 );
556
557 $button = new single_button(
558 new moodle_url($attemptobj->processattempt_url(), $options),
559 get_string('submitallandfinish', 'quiz'));
560 $button->id = 'responseform';
6c7da461
TH
561 $button->add_action(new confirm_action(get_string('confirmclose', 'quiz'), null,
562 get_string('submitallandfinish', 'quiz')));
63be47d9 563
6c7da461
TH
564 $output .= $this->container($this->container($this->render($button),
565 'controls'), 'submitbtns mdl-align');
63be47d9
DL
566
567 return $output;
568 }
cbe87543 569
06eb64b8
DL
570 /*
571 * View Page
572 */
2c3e2b53
DL
573 /**
574 * Generates the view page
575 *
576 * @param int $course The id of the course
577 * @param array $quiz Array conting quiz data
578 * @param int $cm Course Module ID
579 * @param int $context The page context ID
adf7fe21 580 * @param array $infomessages information about this quiz
2c3e2b53 581 * @param mod_quiz_view_object $viewobj
adf7fe21
TH
582 * @param string $buttontext text for the start/continue attempt button, if
583 * it should be shown.
584 * @param array $infomessages further information about why the student cannot
585 * attempt this quiz now, if appicable this quiz
2c3e2b53 586 */
adf7fe21
TH
587 public function view_page($course, $quiz, $cm, $context, $infomessages, $viewobj,
588 $buttontext, $preventmessages) {
06eb64b8 589 $output = '';
adf7fe21 590 $output .= $this->view_information($course, $quiz, $cm, $context, $infomessages);
06eb64b8
DL
591 $output .= $this->view_table($quiz, $context, $viewobj);
592 $output .= $this->view_best_score($viewobj);
593 $output .= $this->view_result_info($quiz, $context, $cm, $viewobj);
adf7fe21
TH
594 $output .= $this->view_attempt_button($course, $quiz, $cm, $context, $viewobj,
595 $buttontext, $preventmessages);
06eb64b8
DL
596 return $output;
597 }
37015e0c 598
2c3e2b53
DL
599 /**
600 * Outputs an error message for any guests accessing the quiz
601 *
602 * @param int $course The course ID
603 * @param array $quiz Array contingin quiz data
604 * @param int $cm Course Module ID
605 * @param int $context The page contect ID
606 * @param array $messages Array containing any messages
607 */
37015e0c 608 public function view_page_guest($course, $quiz, $cm, $context, $messages) {
88c7e2a9
DL
609 $output = '';
610 $output .= $this->view_information($course, $quiz, $cm, $context, $messages);
37015e0c
DL
611 $guestno = html_writer::tag('p', get_string('guestsno', 'quiz'));
612 $liketologin = html_writer::tag('p', get_string('liketologin'));
4e5b9e49 613 $output .= $this->confirm($guestno."\n\n".$liketologin."\n", get_login_url(),
37015e0c 614 get_referer(false));
88c7e2a9
DL
615 return $output;
616 }
37015e0c 617
2c3e2b53
DL
618 /**
619 * Outputs and error message for anyone who is not enrolle don the course
620 *
621 * @param int $course The course ID
622 * @param array $quiz Array contingin quiz data
623 * @param int $cm Course Module ID
624 * @param int $context The page contect ID
625 * @param array $messages Array containing any messages
626 */
37015e0c 627 public function view_page_notenrolled($course, $quiz, $cm, $context, $messages) {
88c7e2a9
DL
628 global $CFG;
629 $output = '';
630 $output .= $this->view_information($course, $quiz, $cm, $context, $messages);
37015e0c
DL
631 $youneedtoenrol = html_writer::tag('p', get_string('youneedtoenrol', 'quiz'));
632 $button = html_writer::tag('p',
633 $this->continue_button($CFG->wwwroot . '/course/view.php?id=' . $course->id));
4e5b9e49 634 $output .= $this->box($youneedtoenrol."\n\n".$button."\n", 'generalbox', 'notice');
88c7e2a9
DL
635 return $output;
636 }
cbe87543 637
2c3e2b53
DL
638 /**
639 * Output the page information
640 *
641 * @param int $course The course ID
642 * @param array $quiz Array contingin quiz data
643 * @param int $cm Course Module ID
644 * @param int $context The page contect ID
645 * @param array $messages Array containing any messages
646 */
8e8bd3f9 647 public function view_information($course, $quiz, $cm, $context, $messages) {
06eb64b8
DL
648 global $CFG;
649 $output = '';
650 // Print quiz name and description
651 $output .= $this->heading(format_string($quiz->name));
652 if (trim(strip_tags($quiz->intro))) {
cbe87543
DL
653 $output .= $this->box(format_module_intro('quiz', $quiz, $cm->id), 'generalbox',
654 'intro');
06eb64b8
DL
655 }
656
adf7fe21 657 $output .= $this->box($this->access_messages($messages), 'quizinfo');
06eb64b8
DL
658
659 // Show number of attempts summary to those who can view reports.
660 if (has_capability('mod/quiz:viewreports', $context)) {
fcd5e245
DL
661 if ($strattemptnum = $this->quiz_attempt_summary_link_to_reports($quiz, $cm,
662 $context)) {
37015e0c
DL
663 $output .= html_writer::tag('div', $strattemptnum,
664 array('class' => 'quizattemptcounts'));
06eb64b8
DL
665 }
666 }
06eb64b8
DL
667 return $output;
668 }
669
0de43939
DL
670 /**
671 * Generates the table heading.
672 */
673 public function view_table_heading() {
674 return $this->heading(get_string('summaryofattempts', 'quiz'));
675 }
676
2c3e2b53
DL
677 /**
678 * Generates the table of data
679 *
680 * @param array $quiz Array contining quiz data
681 * @param int $context The page context ID
682 * @param mod_quiz_view_object $viewobj
683 */
8e8bd3f9 684 public function view_table($quiz, $context, $viewobj) {
06eb64b8 685 $output = '';
da59992e
DL
686 if (!$viewobj->attempts) {
687 return $output;
688 }
0de43939 689 $output .= $this->view_table_heading();
da59992e
DL
690
691 // Prepare table header
692 $table = new html_table();
693 $table->attributes['class'] = 'generaltable quizattemptsummary';
694 $table->head = array();
695 $table->align = array();
696 $table->size = array();
697 if ($viewobj->attemptcolumn) {
698 $table->head[] = get_string('attemptnumber', 'quiz');
699 $table->align[] = 'center';
700 $table->size[] = '';
701 }
702 $table->head[] = get_string('timecompleted', 'quiz');
703 $table->align[] = 'left';
704 $table->size[] = '';
705 if ($viewobj->markcolumn) {
706 $table->head[] = get_string('marks', 'quiz') . ' / ' .
707 quiz_format_grade($quiz, $quiz->sumgrades);
708 $table->align[] = 'center';
709 $table->size[] = '';
710 }
711 if ($viewobj->gradecolumn) {
712 $table->head[] = get_string('grade') . ' / ' .
713 quiz_format_grade($quiz, $quiz->grade);
714 $table->align[] = 'center';
715 $table->size[] = '';
716 }
717 if ($viewobj->canreviewmine) {
718 $table->head[] = get_string('review', 'quiz');
719 $table->align[] = 'center';
720 $table->size[] = '';
721 }
722 if ($viewobj->feedbackcolumn) {
723 $table->head[] = get_string('feedback', 'quiz');
06eb64b8
DL
724 $table->align[] = 'left';
725 $table->size[] = '';
da59992e
DL
726 }
727 if (isset($quiz->showtimetaken)) {
728 $table->head[] = get_string('timetaken', 'quiz');
729 $table->align[] = 'left';
730 $table->size[] = '';
731 }
06eb64b8 732
da59992e
DL
733 // One row for each attempt
734 foreach ($viewobj->attempts as $attempt) {
735 $attemptoptions = quiz_get_review_options($quiz, $attempt, $context);
736 $row = array();
06eb64b8 737
da59992e
DL
738 // Add the attempt number, making it a link, if appropriate.
739 if ($viewobj->attemptcolumn) {
740 if ($attempt->preview) {
741 $row[] = get_string('preview', 'quiz');
06eb64b8 742 } else {
da59992e 743 $row[] = $attempt->attempt;
06eb64b8 744 }
da59992e 745 }
06eb64b8 746
da59992e
DL
747 // prepare strings for time taken and date completed
748 $timetaken = '';
749 $datecompleted = '';
750 if ($attempt->timefinish > 0) {
751 // attempt has finished
752 $timetaken = format_time($attempt->timefinish - $attempt->timestart);
753 $datecompleted = userdate($attempt->timefinish);
754 } else if (!$quiz->timeclose || $viewobj->timenow < $quiz->timeclose) {
755 // The attempt is still in progress.
756 $timetaken = format_time($viewobj->timenow - $attempt->timestart);
757 $datecompleted = get_string('inprogress', 'quiz');
758 } else {
759 $timetaken = format_time($quiz->timeclose - $attempt->timestart);
760 $datecompleted = userdate($quiz->timeclose);
761 }
762 $row[] = $datecompleted;
06eb64b8 763
da59992e
DL
764 if ($viewobj->markcolumn && $attempt->timefinish > 0) {
765 if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX) {
766 $row[] = quiz_format_grade($quiz, $attempt->sumgrades);
767 } else {
768 $row[] = '';
06eb64b8 769 }
da59992e 770 }
06eb64b8 771
da59992e
DL
772 // Ouside the if because we may be showing feedback but not grades.
773 $attemptgrade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
06eb64b8 774
da59992e
DL
775 if ($viewobj->gradecolumn) {
776 if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX &&
777 $attempt->timefinish > 0) {
778 $formattedgrade = quiz_format_grade($quiz, $attemptgrade);
779 // highlight the highest grade if appropriate
780 if ($viewobj->overallstats && !$attempt->preview
781 && $viewobj->numattempts > 1 && !is_null($viewobj->mygrade)
782 && $attemptgrade == $viewobj->mygrade
783 && $quiz->grademethod == QUIZ_GRADEHIGHEST) {
784 $table->rowclasses[$attempt->attempt] = 'bestrow';
06eb64b8 785 }
06eb64b8 786
da59992e
DL
787 $row[] = $formattedgrade;
788 } else {
789 $row[] = '';
06eb64b8 790 }
da59992e 791 }
06eb64b8 792
da59992e
DL
793 if ($viewobj->canreviewmine) {
794 $row[] = $viewobj->accessmanager->make_review_link($attempt,
795 $viewobj->canpreview, $attemptoptions);
796 }
797
798 if ($viewobj->feedbackcolumn && $attempt->timefinish > 0) {
799 if ($attemptoptions->overallfeedback) {
1b327dec 800 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context);
06eb64b8 801 } else {
da59992e 802 $row[] = '';
06eb64b8 803 }
da59992e
DL
804 }
805
806 if (isset($quiz->showtimetaken)) {
807 $row[] = $timetaken;
808 }
809
810 if ($attempt->preview) {
811 $table->data['preview'] = $row;
812 } else {
813 $table->data[$attempt->attempt] = $row;
814 }
815 } // End of loop over attempts.
816 $output .= html_writer::table($table);
817
06eb64b8
DL
818 return $output;
819 }
820
2c3e2b53
DL
821 /**
822 * Prints the students best score
823 *
824 * @param mod_quiz_view_object $viewobj
825 */
8e8bd3f9 826 public function view_best_score($viewobj) {
06eb64b8
DL
827 $output = '';
828 // Print information about the student's best score for this quiz if possible.
829 if (!$viewobj->moreattempts) {
830 $output .= $this->heading(get_string('nomoreattempts', 'quiz'));
831 }
832 return $output;
833 }
834
2c3e2b53
DL
835 /**
836 * Generates data pertaining to quiz results
837 *
838 * @param array $quiz Array containing quiz data
839 * @param int $context The page context ID
840 * @param int $cm The Course Module Id
841 * @param mod_quiz_view_object $viewobj
842 */
8e8bd3f9 843 public function view_result_info($quiz, $context, $cm, $viewobj) {
06eb64b8 844 $output = '';
da59992e
DL
845 if (!$viewobj->numattempts && !$viewobj->gradecolumn && is_null($viewobj->mygrade)) {
846 return $output;
847 }
848 $resultinfo = '';
849
850 if ($viewobj->overallstats) {
851 if ($viewobj->moreattempts) {
852 $a = new stdClass();
853 $a->method = quiz_get_grading_option_name($quiz->grademethod);
854 $a->mygrade = quiz_format_grade($quiz, $viewobj->mygrade);
855 $a->quizgrade = quiz_format_grade($quiz, $quiz->grade);
856 $resultinfo .= $this->heading(get_string('gradesofar', 'quiz', $a), 2, 'main');
857 } else {
858 $a = new stdClass();
859 $a->grade = quiz_format_grade($quiz, $viewobj->mygrade);
860 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
861 $a = get_string('outofshort', 'quiz', $a);
862 $resultinfo .= $this->heading(get_string('yourfinalgradeis', 'quiz', $a), 2,
863 'main');
06eb64b8 864 }
da59992e 865 }
cbe87543 866
da59992e 867 if ($viewobj->mygradeoverridden) {
7933ab8c 868
37015e0c 869 $resultinfo .= html_writer::tag('p', get_string('overriddennotice', 'grades'),
4e5b9e49 870 array('class' => 'overriddennotice'))."\n";
da59992e
DL
871 }
872 if ($viewobj->gradebookfeedback) {
873 $resultinfo .= $this->heading(get_string('comment', 'quiz'), 3, 'main');
874 $resultinfo .= '<p class="quizteacherfeedback">'.$viewobj->gradebookfeedback.
875 "</p>\n";
876 }
877 if ($viewobj->feedbackcolumn) {
878 $resultinfo .= $this->heading(get_string('overallfeedback', 'quiz'), 3, 'main');
37015e0c 879 $resultinfo .= html_writer::tag('p',
1b327dec 880 quiz_feedback_for_grade($viewobj->mygrade, $quiz, $context),
4e5b9e49 881 array('class' => 'quizgradefeedback'))."\n";
da59992e 882 }
cbe87543 883
da59992e
DL
884 if ($resultinfo) {
885 $output .= $this->box($resultinfo, 'generalbox', 'feedback');
06eb64b8
DL
886 }
887 return $output;
888 }
889
2c3e2b53
DL
890 /**
891 * Generates the view attempt button
892 *
893 * @param int $course The course ID
894 * @param array $quiz Array containging quiz date
895 * @param int $cm The Course Module ID
896 * @param int $context The page Context ID
897 * @param mod_quiz_view_object $viewobj
898 * @param string $buttontext
899 */
adf7fe21
TH
900 public function view_attempt_button($course, $quiz, $cm, $context, $viewobj,
901 $buttontext, $preventmessages) {
06eb64b8
DL
902 $output = '';
903 // Determine if we should be showing a start/continue attempt button,
904 // or a button to go back to the course page.
905 $output .= $this->box_start('quizattempt');
cbe87543 906
06eb64b8 907 // Now actually print the appropriate button.
a2ac2349
TH
908 if (!quiz_clean_layout($quiz->questions, true)) {
909 $output .= quiz_no_questions_message($quiz, $cm, $context);
910 }
911
adf7fe21
TH
912 if ($preventmessages) {
913 $output .= $this->access_messages($preventmessages);
914 }
915
06eb64b8 916 if ($buttontext) {
cbe87543
DL
917 $output .= $viewobj->accessmanager->print_start_attempt_button($viewobj->canpreview,
918 $buttontext, $viewobj->unfinished);
06eb64b8 919 } else if ($buttontext === '') {
cbe87543
DL
920 $output .= $this->single_button(new moodle_url('/course/view.php',
921 array('id' => $course->id)), get_string('backtocourse', 'quiz'), 'get',
922 array('class' => 'continuebutton'));
06eb64b8
DL
923 }
924 $output .= $this->box_end();
925
926 return $output;
927 }
da59992e
DL
928
929 /**
930 * Returns the same as {@link quiz_num_attempt_summary()} but wrapped in a link
931 * to the quiz reports.
932 *
933 * @param object $quiz the quiz object. Only $quiz->id is used at the moment.
fcd5e245
DL
934 * @param object $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid
935 * fields are used at the moment.
da59992e 936 * @param object $context the quiz context.
fcd5e245
DL
937 * @param bool $returnzero if false (default), when no attempts have been made '' is returned
938 * instead of 'Attempts: 0'.
939 * @param int $currentgroup if there is a concept of current group where this method is being
940 * called
da59992e
DL
941 * (e.g. a report) pass it in here. Default 0 which means no current group.
942 * @return string HTML fragment for the link.
943 */
8e8bd3f9 944 public function quiz_attempt_summary_link_to_reports($quiz, $cm, $context,
fcd5e245 945 $returnzero = false, $currentgroup = 0) {
da59992e
DL
946 global $CFG;
947 $summary = quiz_num_attempt_summary($quiz, $cm, $returnzero, $currentgroup);
948 if (!$summary) {
949 return '';
950 }
fcd5e245 951
da59992e
DL
952 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
953 $url = new moodle_url('/mod/quiz/report.php', array(
954 'id' => $cm->id, 'mode' => quiz_report_default_report($context)));
955 return html_writer::link($url, $summary);
956 }
b3782c71
TH
957}
958
b3782c71
TH
959class mod_quiz_links_to_other_attempts implements renderable {
960 /**
961 * @var array string attempt number => url, or null for the current attempt.
962 */
963 public $links = array();
3b7efd6d
DL
964}
965
966class mod_quiz_view_object {
56cef201
DL
967 /**
968 * @var array $attempt contains all the user's attempts at this quiz.
969 */
3b7efd6d 970 public $attempts;
56cef201
DL
971 /**
972 * @var object $accessmanager contains various access rules.
973 */
3b7efd6d 974 public $accessmanager;
56cef201
DL
975 /**
976 * @var int $canattempt determins capability for attempting a quiz.
977 */
3b7efd6d 978 public $canattempt;
56cef201
DL
979 /**
980 * @var int $canpreview determins capability for previewing a quiz.
981 */
3b7efd6d 982 public $canpreview;
56cef201
DL
983 /**
984 * @var int $canreviewmine determins capability for reviwing own quiz.
985 */
3b7efd6d 986 public $canreviewmine;
56cef201
DL
987 /**
988 * @var int $attemptcolumn contains the number of attempts done.
989 */
3b7efd6d 990 public $attemptcolumn;
56cef201
DL
991 /**
992 * @var int $gradecolumn contains the grades of any attempts.
993 */
3b7efd6d 994 public $gradecolumn;
56cef201
DL
995 /**
996 * @var int $markcolumn contains the marks of any attempt.
997 */
3b7efd6d 998 public $markcolumn;
56cef201
DL
999 /**
1000 * @var int $overallstats contains all marks for any attempt.
1001 */
3b7efd6d 1002 public $overallstats;
56cef201
DL
1003 /**
1004 * @var string $feedbackcolumn contains any feedback for and attempt.
1005 */
3b7efd6d 1006 public $feedbackcolumn;
56cef201
DL
1007 /**
1008 * @var string $timenow contains a timestamp in string format.
1009 */
3b7efd6d 1010 public $timenow;
56cef201
DL
1011 /**
1012 * @var int $numattempts contains the total number of attempts.
1013 */
3b7efd6d 1014 public $numattempts;
56cef201
DL
1015 /**
1016 * @var int $mygrade contains the current users final grade for a quiz.
1017 */
3b7efd6d 1018 public $mygrade;
56cef201
DL
1019 /**
1020 * @var int $moreattempts total attempts left.
1021 */
3b7efd6d 1022 public $moreattempts;
56cef201
DL
1023 /**
1024 * @var int $mygradeoverridden contains an overriden grade.
1025 */
3b7efd6d 1026 public $mygradeoverridden;
56cef201
DL
1027 /**
1028 * @var string $gradebookfeedback contains any feedback for a gradebook.
1029 */
3b7efd6d 1030 public $gradebookfeedback;
56cef201
DL
1031 /**
1032 * @var int $unfinished contains 1 if an attempt is unfinished.
1033 */
3b7efd6d 1034 public $unfinished;
56cef201
DL
1035 /**
1036 * @var int $lastfinishedattempt contains a pointer to the last attempt in the attempts array.
1037 */
3b7efd6d 1038 public $lastfinishedattempt;
1b327dec 1039}