2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Defines the renderer for the quiz module.
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * The renderer for the quiz module.
30 * @copyright 2011 The Open University
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class mod_quiz_renderer extends plugin_renderer_base {
35 * Builds the review page
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.
46 public function review_page(quiz_attempt $attemptobj, $slots, $page, $showall,
47 $lastpage, mod_quiz_display_options $displayoptions,
51 $output .= $this->header();
52 $output .= $this->review_summary_table($summarydata, $page);
53 $output .= $this->review_form($page, $showall, $displayoptions,
54 $this->questions($attemptobj, true, $slots, $page, $showall, $displayoptions),
57 $output .= $this->review_next_navigation($attemptobj, $page, $lastpage);
58 $output .= $this->footer();
63 * Renders the review question pop-up.
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.
72 public function review_question_page(quiz_attempt $attemptobj, $slot, $seq,
73 mod_quiz_display_options $displayoptions, $summarydata) {
76 $output .= $this->header();
77 $output .= $this->review_summary_table($summarydata, 0);
80 $output .= $attemptobj->render_question_at_step($slot, $seq, true);
82 $output .= $attemptobj->render_question($slot, true);
85 $output .= $this->close_window_button();
86 $output .= $this->footer();
91 * Renders the review question pop-up.
93 * @param string $message Why the review is not allowed.
94 * @return string html to output.
96 public function review_question_not_allowed($message) {
98 $output .= $this->header();
99 $output .= $this->notification($message);
100 $output .= $this->close_window_button();
101 $output .= $this->footer();
106 * Filters the summarydata array.
108 * @param array $summarydata contains row data for table
109 * @param int $page the current page number
110 * @return $summarydata containing filtered row data
112 protected function filter_summary_table($summarydata, $page) {
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]);
128 * Outputs the table containing data from summary data array
130 * @param array $summarydata contains row data for table
131 * @param int $page contains the current page number
133 public function review_summary_table($summarydata, $page) {
134 $summarydata = $this->filter_summary_table($summarydata, $page);
135 if (empty($summarydata)) {
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']);
147 $title = $rowdata['title'];
150 if ($rowdata['content'] instanceof renderable) {
151 $content = $this->render($rowdata['content']);
153 $content = $rowdata['content'];
156 $output .= html_writer::tag('tr',
157 html_writer::tag('th', $title, array('class' => 'cell', 'scope' => 'row')) .
158 html_writer::tag('td', $content, array('class' => 'cell'))
162 $output .= html_writer::end_tag('tbody');
163 $output .= html_writer::end_tag('table');
168 * Renders each question
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
177 public function questions(quiz_attempt $attemptobj, $reviewing, $slots, $page, $showall,
178 mod_quiz_display_options $displayoptions) {
180 foreach ($slots as $slot) {
181 $output .= $attemptobj->render_question($slot, $reviewing,
182 $attemptobj->review_url($slot, $page, $showall));
188 * Renders the main bit of the review page.
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
197 public function review_form($page, $showall, $displayoptions, $content, $attemptobj) {
198 if ($displayoptions->flags != question_display_options::EDITABLE) {
202 $this->page->requires->js_init_call('M.mod_quiz.init_review_form', null, false,
203 quiz_get_js_module());
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');
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')));
216 $output .= html_writer::end_tag('div');
217 $output .= html_writer::end_tag('div');
218 $output .= html_writer::end_tag('form');
224 * Returns either a liink or button
226 * @param $url contains a url for the review link
228 public function finish_review_link($url) {
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'));
237 return html_writer::link($url, get_string('finishreview', 'quiz'));
242 * Creates a next page arrow or the finishing link
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
248 public function review_next_navigation(quiz_attempt $attemptobj, $page, $lastpage) {
250 $nav = $this->finish_review_link($attemptobj->view_url());
252 $nav = link_arrow_right(get_string('next'), $attemptobj->review_url(0, $page + 1));
254 return html_writer::tag('div', $nav, array('class' => 'submitbtns'));
258 * Return the HTML of the quiz timer.
259 * @return string HTML content.
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'));
268 * Create a preview link
270 * @param $url contains a url to the given page
272 public function restart_preview_button($url) {
273 return $this->single_button($url, get_string('startnewpreview', 'quiz'));
277 * Outputs the navigation block panel
279 * @param quiz_nav_panel_base $panel instance of quiz_nav_panel_base
281 public function navigation_panel(quiz_nav_panel_base $panel) {
284 $userpicture = $panel->user_picture();
286 $output .= html_writer::tag('div', $this->render($userpicture),
287 array('id' => 'user-picture', 'class' => 'clearfix'));
289 $output .= $panel->render_before_button_bits($this);
291 $output .= html_writer::start_tag('div', array('class' => 'qn_buttons'));
292 foreach ($panel->get_question_buttons() as $button) {
293 $output .= $this->render($button);
295 $output .= html_writer::end_tag('div');
297 $output .= html_writer::tag('div', $panel->render_end_bits($this),
298 array('class' => 'othernav'));
300 $this->page->requires->js_init_call('M.mod_quiz.nav.init', null, false,
301 quiz_get_js_module());
307 * Returns the quizzes navigation button
309 * @param quiz_nav_question_button $button
311 protected function render_quiz_nav_question_button(quiz_nav_question_button $button) {
312 $classes = array('qnbutton', $button->stateclass);
313 $attributes = array();
315 if ($button->currentpage) {
316 $classes[] = 'thispage';
317 $attributes[] = get_string('onthispage', 'quiz');
320 $attributes[] = $button->statestring;
323 if ($button->flagged) {
324 $classes[] = 'flagged';
325 $flaglabel = get_string('flagged', 'question');
329 $attributes[] = html_writer::tag('span', $flaglabel, array('class' => 'flagstate'));
331 if (is_numeric($button->number)) {
332 $qnostring = 'questionnonav';
334 $qnostring = 'questionnonavinfo';
338 $a->number = $button->number;
339 $a->attributes = implode(' ', $attributes);
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));
350 * outputs the link the other attempts.
352 * @param mod_quiz_links_to_other_attempts $links
354 protected function render_mod_quiz_links_to_other_attempts(
355 mod_quiz_links_to_other_attempts $links) {
356 $attemptlinks = array();
357 foreach ($links->links as $attempt => $url) {
359 $attemptlinks[] = html_writer::link($url, $attempt);
361 $attemptlinks[] = html_writer::tag('strong', $attempt);
364 return implode(', ', $attemptlinks);
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
378 public function attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id,
381 $output .= $this->quiz_notices($messages);
382 $output .= $this->attempt_form($attemptobj, $page, $slots, $id, $nextpage);
387 * Returns any notices.
389 * @param array $messages
391 public function quiz_notices($messages) {
395 return $this->box($this->heading(get_string('accessnoticesheader', 'quiz'), 3) .
396 $this->access_messages($messages), 'quizaccessnotices');
400 * Ouputs the form for making an attempt
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
408 public function attempt_form($attemptobj, $page, $slots, $id, $nextpage) {
412 $output .= html_writer::start_tag('form',
413 array('action' => $attemptobj->processattempt_url(), 'method' => 'post',
414 'enctype' => 'multipart/form-data', 'accept-charset' => 'utf-8',
415 'id' => 'responseform'));
416 $output .= html_writer::start_tag('div');
418 // Print all the questions
419 foreach ($slots as $slot) {
420 $output .= $attemptobj->render_question($slot, false, $attemptobj->attempt_url($id,
424 $output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
425 $output .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'next',
426 'value' => get_string('next')));
427 $output .= html_writer::end_tag('div');
429 // Some hidden fields to trach what is going on.
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'));
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.
446 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'slots',
447 'value' => implode(',', $slots)));
450 $output .= html_writer::end_tag('div');
451 $output .= html_writer::end_tag('form');
457 * Print each message in an array, surrounded by <p>, </p> tags.
459 * @param array $messages the array of message strings.
460 * @param bool $return if true, return a string, instead of outputting.
462 * @return mixed, if $return is true, return the string that would have been output, otherwise
465 public function access_messages($messages) {
467 foreach ($messages as $message) {
468 $output .= html_writer::tag('p', $message) . "\n";
477 * Create the summary page
479 * @param quiz_attempt $attemptobj
480 * @param mod_quiz_display_options $displayoptions
482 public function summary_page($attemptobj, $displayoptions) {
484 $output .= $this->summary_table($attemptobj, $displayoptions);
485 $output .= $this->summary_page_controls($attemptobj);
490 * Generates the table of summarydata
492 * @param quiz_attempt $attemptobj
493 * @param mod_quiz_display_options $displayoptions
495 public function summary_table($attemptobj, $displayoptions) {
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;
504 $table->head[] = get_string('marks', 'quiz');
505 $table->align[] = 'left';
508 $table->data = array();
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)) {
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'));
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));
525 $row[] = $attemptobj->get_question_mark($slot);
527 $table->data[] = $row;
528 $table->rowclasses[] = $attemptobj->get_question_state_class(
529 $slot, $displayoptions->correctness);
532 // Print the summary table.
533 $output = html_writer::table($table);
539 * Creates any controls a the page should have.
541 * @param quiz_attempt $attemptobj
543 public function summary_page_controls($attemptobj) {
546 $output .= $this->countdown_timer();
548 // Finish attempt button.
550 'attempt' => $attemptobj->get_attemptid(),
551 'finishattempt' => 1,
554 'sesskey' => sesskey(),
557 $button = new single_button(
558 new moodle_url($attemptobj->processattempt_url(), $options),
559 get_string('submitallandfinish', 'quiz'));
560 $button->id = 'responseform';
561 $button->add_action(new confirm_action(get_string('confirmclose', 'quiz'), null,
562 get_string('submitallandfinish', 'quiz')));
564 $output .= $this->container($this->container($this->render($button),
565 'controls'), 'submitbtns mdl-align');
574 * Generates the view page
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
580 * @param array $infomessages information about this quiz
581 * @param mod_quiz_view_object $viewobj
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
587 public function view_page($course, $quiz, $cm, $context, $infomessages, $viewobj,
588 $buttontext, $preventmessages) {
590 $output .= $this->view_information($course, $quiz, $cm, $context, $infomessages);
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);
594 $output .= $this->view_attempt_button($course, $quiz, $cm, $context, $viewobj,
595 $buttontext, $preventmessages);
600 * Outputs an error message for any guests accessing the quiz
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
608 public function view_page_guest($course, $quiz, $cm, $context, $messages) {
610 $output .= $this->view_information($course, $quiz, $cm, $context, $messages);
611 $guestno = html_writer::tag('p', get_string('guestsno', 'quiz'));
612 $liketologin = html_writer::tag('p', get_string('liketologin'));
613 $output .= $this->confirm($guestno."\n\n".$liketologin."\n", get_login_url(),
619 * Outputs and error message for anyone who is not enrolle don the course
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
627 public function view_page_notenrolled($course, $quiz, $cm, $context, $messages) {
630 $output .= $this->view_information($course, $quiz, $cm, $context, $messages);
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));
634 $output .= $this->box($youneedtoenrol."\n\n".$button."\n", 'generalbox', 'notice');
639 * Output the page information
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
647 public function view_information($course, $quiz, $cm, $context, $messages) {
650 // Print quiz name and description
651 $output .= $this->heading(format_string($quiz->name));
652 if (trim(strip_tags($quiz->intro))) {
653 $output .= $this->box(format_module_intro('quiz', $quiz, $cm->id), 'generalbox',
657 $output .= $this->box($this->access_messages($messages), 'quizinfo');
659 // Show number of attempts summary to those who can view reports.
660 if (has_capability('mod/quiz:viewreports', $context)) {
661 if ($strattemptnum = $this->quiz_attempt_summary_link_to_reports($quiz, $cm,
663 $output .= html_writer::tag('div', $strattemptnum,
664 array('class' => 'quizattemptcounts'));
671 * Generates the table heading.
673 public function view_table_heading() {
674 return $this->heading(get_string('summaryofattempts', 'quiz'));
678 * Generates the table of data
680 * @param array $quiz Array contining quiz data
681 * @param int $context The page context ID
682 * @param mod_quiz_view_object $viewobj
684 public function view_table($quiz, $context, $viewobj) {
686 if (!$viewobj->attempts) {
689 $output .= $this->view_table_heading();
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';
702 $table->head[] = get_string('timecompleted', 'quiz');
703 $table->align[] = 'left';
705 if ($viewobj->markcolumn) {
706 $table->head[] = get_string('marks', 'quiz') . ' / ' .
707 quiz_format_grade($quiz, $quiz->sumgrades);
708 $table->align[] = 'center';
711 if ($viewobj->gradecolumn) {
712 $table->head[] = get_string('grade') . ' / ' .
713 quiz_format_grade($quiz, $quiz->grade);
714 $table->align[] = 'center';
717 if ($viewobj->canreviewmine) {
718 $table->head[] = get_string('review', 'quiz');
719 $table->align[] = 'center';
722 if ($viewobj->feedbackcolumn) {
723 $table->head[] = get_string('feedback', 'quiz');
724 $table->align[] = 'left';
727 if (isset($quiz->showtimetaken)) {
728 $table->head[] = get_string('timetaken', 'quiz');
729 $table->align[] = 'left';
733 // One row for each attempt
734 foreach ($viewobj->attempts as $attempt) {
735 $attemptoptions = quiz_get_review_options($quiz, $attempt, $context);
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');
743 $row[] = $attempt->attempt;
747 // prepare strings for time taken and date completed
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');
759 $timetaken = format_time($quiz->timeclose - $attempt->timestart);
760 $datecompleted = userdate($quiz->timeclose);
762 $row[] = $datecompleted;
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);
772 // Ouside the if because we may be showing feedback but not grades.
773 $attemptgrade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
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';
787 $row[] = $formattedgrade;
793 if ($viewobj->canreviewmine) {
794 $row[] = $viewobj->accessmanager->make_review_link($attempt,
795 $viewobj->canpreview, $attemptoptions);
798 if ($viewobj->feedbackcolumn && $attempt->timefinish > 0) {
799 if ($attemptoptions->overallfeedback) {
800 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context);
806 if (isset($quiz->showtimetaken)) {
810 if ($attempt->preview) {
811 $table->data['preview'] = $row;
813 $table->data[$attempt->attempt] = $row;
815 } // End of loop over attempts.
816 $output .= html_writer::table($table);
822 * Prints the students best score
824 * @param mod_quiz_view_object $viewobj
826 public function view_best_score($viewobj) {
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'));
836 * Generates data pertaining to quiz results
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
843 public function view_result_info($quiz, $context, $cm, $viewobj) {
845 if (!$viewobj->numattempts && !$viewobj->gradecolumn && is_null($viewobj->mygrade)) {
850 if ($viewobj->overallstats) {
851 if ($viewobj->moreattempts) {
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');
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,
867 if ($viewobj->mygradeoverridden) {
869 $resultinfo .= html_writer::tag('p', get_string('overriddennotice', 'grades'),
870 array('class' => 'overriddennotice'))."\n";
872 if ($viewobj->gradebookfeedback) {
873 $resultinfo .= $this->heading(get_string('comment', 'quiz'), 3, 'main');
874 $resultinfo .= '<p class="quizteacherfeedback">'.$viewobj->gradebookfeedback.
877 if ($viewobj->feedbackcolumn) {
878 $resultinfo .= $this->heading(get_string('overallfeedback', 'quiz'), 3, 'main');
879 $resultinfo .= html_writer::tag('p',
880 quiz_feedback_for_grade($viewobj->mygrade, $quiz, $context),
881 array('class' => 'quizgradefeedback'))."\n";
885 $output .= $this->box($resultinfo, 'generalbox', 'feedback');
891 * Generates the view attempt button
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
900 public function view_attempt_button($course, $quiz, $cm, $context, $viewobj,
901 $buttontext, $preventmessages) {
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');
907 // Now actually print the appropriate button.
908 if (!quiz_clean_layout($quiz->questions, true)) {
909 $output .= quiz_no_questions_message($quiz, $cm, $context);
912 if ($preventmessages) {
913 $output .= $this->access_messages($preventmessages);
917 $output .= $viewobj->accessmanager->print_start_attempt_button($viewobj->canpreview,
918 $buttontext, $viewobj->unfinished);
919 } else if ($buttontext === '') {
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'));
924 $output .= $this->box_end();
930 * Returns the same as {@link quiz_num_attempt_summary()} but wrapped in a link
931 * to the quiz reports.
933 * @param object $quiz the quiz object. Only $quiz->id is used at the moment.
934 * @param object $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid
935 * fields are used at the moment.
936 * @param object $context the quiz context.
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
941 * (e.g. a report) pass it in here. Default 0 which means no current group.
942 * @return string HTML fragment for the link.
944 public function quiz_attempt_summary_link_to_reports($quiz, $cm, $context,
945 $returnzero = false, $currentgroup = 0) {
947 $summary = quiz_num_attempt_summary($quiz, $cm, $returnzero, $currentgroup);
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);
959 class mod_quiz_links_to_other_attempts implements renderable {
961 * @var array string attempt number => url, or null for the current attempt.
963 public $links = array();
966 class mod_quiz_view_object {
968 * @var array $attempt contains all the user's attempts at this quiz.
972 * @var object $accessmanager contains various access rules.
974 public $accessmanager;
976 * @var int $canattempt determins capability for attempting a quiz.
980 * @var int $canpreview determins capability for previewing a quiz.
984 * @var int $canreviewmine determins capability for reviwing own quiz.
986 public $canreviewmine;
988 * @var int $attemptcolumn contains the number of attempts done.
990 public $attemptcolumn;
992 * @var int $gradecolumn contains the grades of any attempts.
996 * @var int $markcolumn contains the marks of any attempt.
1000 * @var int $overallstats contains all marks for any attempt.
1002 public $overallstats;
1004 * @var string $feedbackcolumn contains any feedback for and attempt.
1006 public $feedbackcolumn;
1008 * @var string $timenow contains a timestamp in string format.
1012 * @var int $numattempts contains the total number of attempts.
1014 public $numattempts;
1016 * @var int $mygrade contains the current users final grade for a quiz.
1020 * @var int $moreattempts total attempts left.
1022 public $moreattempts;
1024 * @var int $mygradeoverridden contains an overriden grade.
1026 public $mygradeoverridden;
1028 * @var string $gradebookfeedback contains any feedback for a gradebook.
1030 public $gradebookfeedback;
1032 * @var int $unfinished contains 1 if an attempt is unfinished.
1036 * @var int $lastfinishedattempt contains a pointer to the last attempt in the attempts array.
1038 public $lastfinishedattempt;