Merge branch 'MDL-28976' of git://github.com/timhunt/moodle
[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
a47a46f1
TH
764 if ($viewobj->markcolumn) {
765 if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX &&
766 $attempt->timefinish > 0) {
da59992e
DL
767 $row[] = quiz_format_grade($quiz, $attempt->sumgrades);
768 } else {
769 $row[] = '';
06eb64b8 770 }
da59992e 771 }
06eb64b8 772
da59992e
DL
773 // Ouside the if because we may be showing feedback but not grades.
774 $attemptgrade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
06eb64b8 775
da59992e
DL
776 if ($viewobj->gradecolumn) {
777 if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX &&
778 $attempt->timefinish > 0) {
779 $formattedgrade = quiz_format_grade($quiz, $attemptgrade);
780 // highlight the highest grade if appropriate
781 if ($viewobj->overallstats && !$attempt->preview
782 && $viewobj->numattempts > 1 && !is_null($viewobj->mygrade)
783 && $attemptgrade == $viewobj->mygrade
784 && $quiz->grademethod == QUIZ_GRADEHIGHEST) {
785 $table->rowclasses[$attempt->attempt] = 'bestrow';
06eb64b8 786 }
06eb64b8 787
da59992e
DL
788 $row[] = $formattedgrade;
789 } else {
790 $row[] = '';
06eb64b8 791 }
da59992e 792 }
06eb64b8 793
da59992e
DL
794 if ($viewobj->canreviewmine) {
795 $row[] = $viewobj->accessmanager->make_review_link($attempt,
796 $viewobj->canpreview, $attemptoptions);
797 }
798
799 if ($viewobj->feedbackcolumn && $attempt->timefinish > 0) {
800 if ($attemptoptions->overallfeedback) {
1b327dec 801 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context);
06eb64b8 802 } else {
da59992e 803 $row[] = '';
06eb64b8 804 }
da59992e
DL
805 }
806
807 if (isset($quiz->showtimetaken)) {
808 $row[] = $timetaken;
809 }
810
811 if ($attempt->preview) {
812 $table->data['preview'] = $row;
813 } else {
814 $table->data[$attempt->attempt] = $row;
815 }
816 } // End of loop over attempts.
817 $output .= html_writer::table($table);
818
06eb64b8
DL
819 return $output;
820 }
821
2c3e2b53
DL
822 /**
823 * Prints the students best score
824 *
825 * @param mod_quiz_view_object $viewobj
826 */
8e8bd3f9 827 public function view_best_score($viewobj) {
06eb64b8
DL
828 $output = '';
829 // Print information about the student's best score for this quiz if possible.
830 if (!$viewobj->moreattempts) {
831 $output .= $this->heading(get_string('nomoreattempts', 'quiz'));
832 }
833 return $output;
834 }
835
2c3e2b53
DL
836 /**
837 * Generates data pertaining to quiz results
838 *
839 * @param array $quiz Array containing quiz data
840 * @param int $context The page context ID
841 * @param int $cm The Course Module Id
842 * @param mod_quiz_view_object $viewobj
843 */
8e8bd3f9 844 public function view_result_info($quiz, $context, $cm, $viewobj) {
06eb64b8 845 $output = '';
da59992e
DL
846 if (!$viewobj->numattempts && !$viewobj->gradecolumn && is_null($viewobj->mygrade)) {
847 return $output;
848 }
849 $resultinfo = '';
850
851 if ($viewobj->overallstats) {
852 if ($viewobj->moreattempts) {
853 $a = new stdClass();
854 $a->method = quiz_get_grading_option_name($quiz->grademethod);
855 $a->mygrade = quiz_format_grade($quiz, $viewobj->mygrade);
856 $a->quizgrade = quiz_format_grade($quiz, $quiz->grade);
857 $resultinfo .= $this->heading(get_string('gradesofar', 'quiz', $a), 2, 'main');
858 } else {
859 $a = new stdClass();
860 $a->grade = quiz_format_grade($quiz, $viewobj->mygrade);
861 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
862 $a = get_string('outofshort', 'quiz', $a);
863 $resultinfo .= $this->heading(get_string('yourfinalgradeis', 'quiz', $a), 2,
864 'main');
06eb64b8 865 }
da59992e 866 }
cbe87543 867
da59992e 868 if ($viewobj->mygradeoverridden) {
7933ab8c 869
37015e0c 870 $resultinfo .= html_writer::tag('p', get_string('overriddennotice', 'grades'),
4e5b9e49 871 array('class' => 'overriddennotice'))."\n";
da59992e
DL
872 }
873 if ($viewobj->gradebookfeedback) {
874 $resultinfo .= $this->heading(get_string('comment', 'quiz'), 3, 'main');
875 $resultinfo .= '<p class="quizteacherfeedback">'.$viewobj->gradebookfeedback.
876 "</p>\n";
877 }
878 if ($viewobj->feedbackcolumn) {
879 $resultinfo .= $this->heading(get_string('overallfeedback', 'quiz'), 3, 'main');
37015e0c 880 $resultinfo .= html_writer::tag('p',
1b327dec 881 quiz_feedback_for_grade($viewobj->mygrade, $quiz, $context),
4e5b9e49 882 array('class' => 'quizgradefeedback'))."\n";
da59992e 883 }
cbe87543 884
da59992e
DL
885 if ($resultinfo) {
886 $output .= $this->box($resultinfo, 'generalbox', 'feedback');
06eb64b8
DL
887 }
888 return $output;
889 }
890
2c3e2b53
DL
891 /**
892 * Generates the view attempt button
893 *
894 * @param int $course The course ID
895 * @param array $quiz Array containging quiz date
896 * @param int $cm The Course Module ID
897 * @param int $context The page Context ID
898 * @param mod_quiz_view_object $viewobj
899 * @param string $buttontext
900 */
adf7fe21
TH
901 public function view_attempt_button($course, $quiz, $cm, $context, $viewobj,
902 $buttontext, $preventmessages) {
06eb64b8
DL
903 $output = '';
904 // Determine if we should be showing a start/continue attempt button,
905 // or a button to go back to the course page.
906 $output .= $this->box_start('quizattempt');
cbe87543 907
06eb64b8 908 // Now actually print the appropriate button.
a2ac2349
TH
909 if (!quiz_clean_layout($quiz->questions, true)) {
910 $output .= quiz_no_questions_message($quiz, $cm, $context);
911 }
912
adf7fe21
TH
913 if ($preventmessages) {
914 $output .= $this->access_messages($preventmessages);
915 }
916
06eb64b8 917 if ($buttontext) {
cbe87543
DL
918 $output .= $viewobj->accessmanager->print_start_attempt_button($viewobj->canpreview,
919 $buttontext, $viewobj->unfinished);
06eb64b8 920 } else if ($buttontext === '') {
cbe87543
DL
921 $output .= $this->single_button(new moodle_url('/course/view.php',
922 array('id' => $course->id)), get_string('backtocourse', 'quiz'), 'get',
923 array('class' => 'continuebutton'));
06eb64b8
DL
924 }
925 $output .= $this->box_end();
926
927 return $output;
928 }
da59992e
DL
929
930 /**
931 * Returns the same as {@link quiz_num_attempt_summary()} but wrapped in a link
932 * to the quiz reports.
933 *
934 * @param object $quiz the quiz object. Only $quiz->id is used at the moment.
fcd5e245
DL
935 * @param object $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid
936 * fields are used at the moment.
da59992e 937 * @param object $context the quiz context.
fcd5e245
DL
938 * @param bool $returnzero if false (default), when no attempts have been made '' is returned
939 * instead of 'Attempts: 0'.
940 * @param int $currentgroup if there is a concept of current group where this method is being
941 * called
da59992e
DL
942 * (e.g. a report) pass it in here. Default 0 which means no current group.
943 * @return string HTML fragment for the link.
944 */
8e8bd3f9 945 public function quiz_attempt_summary_link_to_reports($quiz, $cm, $context,
fcd5e245 946 $returnzero = false, $currentgroup = 0) {
da59992e
DL
947 global $CFG;
948 $summary = quiz_num_attempt_summary($quiz, $cm, $returnzero, $currentgroup);
949 if (!$summary) {
950 return '';
951 }
fcd5e245 952
da59992e
DL
953 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
954 $url = new moodle_url('/mod/quiz/report.php', array(
955 'id' => $cm->id, 'mode' => quiz_report_default_report($context)));
956 return html_writer::link($url, $summary);
957 }
b3782c71
TH
958}
959
b3782c71
TH
960class mod_quiz_links_to_other_attempts implements renderable {
961 /**
962 * @var array string attempt number => url, or null for the current attempt.
963 */
964 public $links = array();
3b7efd6d
DL
965}
966
967class mod_quiz_view_object {
56cef201
DL
968 /**
969 * @var array $attempt contains all the user's attempts at this quiz.
970 */
3b7efd6d 971 public $attempts;
56cef201
DL
972 /**
973 * @var object $accessmanager contains various access rules.
974 */
3b7efd6d 975 public $accessmanager;
56cef201
DL
976 /**
977 * @var int $canattempt determins capability for attempting a quiz.
978 */
3b7efd6d 979 public $canattempt;
56cef201
DL
980 /**
981 * @var int $canpreview determins capability for previewing a quiz.
982 */
3b7efd6d 983 public $canpreview;
56cef201
DL
984 /**
985 * @var int $canreviewmine determins capability for reviwing own quiz.
986 */
3b7efd6d 987 public $canreviewmine;
56cef201
DL
988 /**
989 * @var int $attemptcolumn contains the number of attempts done.
990 */
3b7efd6d 991 public $attemptcolumn;
56cef201
DL
992 /**
993 * @var int $gradecolumn contains the grades of any attempts.
994 */
3b7efd6d 995 public $gradecolumn;
56cef201
DL
996 /**
997 * @var int $markcolumn contains the marks of any attempt.
998 */
3b7efd6d 999 public $markcolumn;
56cef201
DL
1000 /**
1001 * @var int $overallstats contains all marks for any attempt.
1002 */
3b7efd6d 1003 public $overallstats;
56cef201
DL
1004 /**
1005 * @var string $feedbackcolumn contains any feedback for and attempt.
1006 */
3b7efd6d 1007 public $feedbackcolumn;
56cef201
DL
1008 /**
1009 * @var string $timenow contains a timestamp in string format.
1010 */
3b7efd6d 1011 public $timenow;
56cef201
DL
1012 /**
1013 * @var int $numattempts contains the total number of attempts.
1014 */
3b7efd6d 1015 public $numattempts;
56cef201
DL
1016 /**
1017 * @var int $mygrade contains the current users final grade for a quiz.
1018 */
3b7efd6d 1019 public $mygrade;
56cef201
DL
1020 /**
1021 * @var int $moreattempts total attempts left.
1022 */
3b7efd6d 1023 public $moreattempts;
56cef201
DL
1024 /**
1025 * @var int $mygradeoverridden contains an overriden grade.
1026 */
3b7efd6d 1027 public $mygradeoverridden;
56cef201
DL
1028 /**
1029 * @var string $gradebookfeedback contains any feedback for a gradebook.
1030 */
3b7efd6d 1031 public $gradebookfeedback;
56cef201
DL
1032 /**
1033 * @var int $unfinished contains 1 if an attempt is unfinished.
1034 */
3b7efd6d 1035 public $unfinished;
56cef201
DL
1036 /**
1037 * @var int $lastfinishedattempt contains a pointer to the last attempt in the attempts array.
1038 */
3b7efd6d 1039 public $lastfinishedattempt;
1b327dec 1040}