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),
55 $attemptobj, $showall);
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($summarydata, $page, $displayoptions, $content, $attemptobj,
199 if ($displayoptions->flags != question_display_options::EDITABLE) {
203 $this->page->requires->js_init_call('M.mod_quiz.init_review_form', null, false,
204 quiz_get_js_module());
207 $output .= html_writer::start_tag('form', array('action' => $attemptobj->review_url(0,
208 $page, $showall), 'method' => 'post', 'class' => 'questionflagsaveform'));
209 $output .= html_writer::start_tag('div');
211 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey',
212 'value' => sesskey()));
213 $output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
214 $output .= html_writer::empty_tag('input', array('type' => 'submit',
215 'class' => 'questionflagsavebutton', 'name' => 'savingflags',
216 'value' => get_string('saveflags', 'question')));
217 $output .= html_writer::end_tag('div');
218 $output .= html_writer::end_tag('div');
219 $output .= html_writer::end_tag('form');
225 * Returns either a liink or button
227 * @param $url contains a url for the review link
229 public function finish_review_link($url) {
230 if ($this->page->pagelayout == 'popup') {
231 // In a 'secure' popup window.
232 $this->page->requires->js_init_call('M.mod_quiz.secure_window.init_close_button',
233 array($url), quiz_get_js_module());
234 return html_writer::empty_tag('input', array('type' => 'button',
235 'value' => get_string('finishreview', 'quiz'),
236 'id' => 'secureclosebutton'));
238 return html_writer::link($url, get_string('finishreview', 'quiz'));
243 * Creates a next page arrow or the finishing link
245 * @param quiz_attempt $attemptobj instance of quiz_attempt
246 * @param int $page the current page
247 * @param bool $lastpage if true current page is the last page
249 public function review_next_navigation(quiz_attempt $attemptobj, $page, $lastpage) {
251 $nav = $this->finish_review_link($attemptobj->view_url());
253 $nav = link_arrow_right(get_string('next'), $attemptobj->review_url(0, $page + 1));
255 return html_writer::tag('div', $nav, array('class' => 'submitbtns'));
259 * Return the HTML of the quiz timer.
260 * @return string HTML content.
262 public function countdown_timer() {
263 return html_writer::tag('div', get_string('timeleft', 'quiz') .
264 html_writer::tag('span', '', array('id' => 'quiz-time-left')),
265 array('id' => 'quiz-timer'));
269 * Create a preview link
271 * @param $url contains a url to the given page
273 public function restart_preview_button($url) {
274 return $this->single_button($url, get_string('startnewpreview', 'quiz'));
278 * Outputs the navigation block panel
280 * @param quiz_nav_panel_base $panel instance of quiz_nav_panel_base
282 public function navigation_panel(quiz_nav_panel_base $panel) {
285 $userpicture = $panel->user_picture();
287 $output .= html_writer::tag('div', $this->render($userpicture),
288 array('id' => 'user-picture', 'class' => 'clearfix'));
290 $output .= $panel->render_before_button_bits($this);
292 $output .= html_writer::start_tag('div', array('class' => 'qn_buttons'));
293 foreach ($panel->get_question_buttons() as $button) {
294 $output .= $this->render($button);
296 $output .= html_writer::end_tag('div');
298 $output .= html_writer::tag('div', $panel->render_end_bits($this),
299 array('class' => 'othernav'));
301 $this->page->requires->js_init_call('M.mod_quiz.nav.init', null, false,
302 quiz_get_js_module());
308 * Returns the quizzes navigation button
310 * @param quiz_nav_question_button $button
312 protected function render_quiz_nav_question_button(quiz_nav_question_button $button) {
313 $classes = array('qnbutton', $button->stateclass);
314 $attributes = array();
316 if ($button->currentpage) {
317 $classes[] = 'thispage';
318 $attributes[] = get_string('onthispage', 'quiz');
321 $attributes[] = $button->statestring;
324 if ($button->flagged) {
325 $classes[] = 'flagged';
326 $flaglabel = get_string('flagged', 'question');
330 $attributes[] = html_writer::tag('span', $flaglabel, array('class' => 'flagstate'));
332 if (is_numeric($button->number)) {
333 $qnostring = 'questionnonav';
335 $qnostring = 'questionnonavinfo';
339 $a->number = $button->number;
340 $a->attributes = implode(' ', $attributes);
342 return html_writer::link($button->url,
343 html_writer::tag('span', '', array('class' => 'thispageholder')) .
344 html_writer::tag('span', '', array('class' => 'trafficlight')) .
345 get_string($qnostring, 'quiz', $a),
346 array('class' => implode(' ', $classes), 'id' => $button->id,
347 'title' => $button->statestring));
351 * outputs the link the other attempts.
353 * @param mod_quiz_links_to_other_attempts $links
355 protected function render_mod_quiz_links_to_other_attempts(
356 mod_quiz_links_to_other_attempts $links) {
357 $attemptlinks = array();
358 foreach ($links->links as $attempt => $url) {
360 $attemptlinks[] = html_writer::link($url, $attempt);
362 $attemptlinks[] = html_writer::tag('strong', $attempt);
365 return implode(', ', $attemptlinks);
371 * @param quiz_attempt $attemptobj Instance of quiz_attempt
372 * @param int $page Current page number
373 * @param quiz_access_manager $accessmanager Instance of quiz_access_manager
374 * @param array $messages An array of messages
375 * @param array $slots Contains an array of integers that relate to questions
376 * @param int $id The ID of an attempt
377 * @param int $nextpage The number of the next page
379 public function attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id,
382 $output .= $this->quiz_notices($messages);
383 $output .= $this->attempt_form($attemptobj, $page, $slots, $id, $nextpage);
388 * Returns any notices.
390 * @param array $messages
392 public function quiz_notices($messages) {
396 return $this->box($this->heading(get_string('accessnoticesheader', 'quiz'), 3) .
397 $this->access_messages($messages), 'quizaccessnotices');
401 * Ouputs the form for making an attempt
403 * @param quiz_attempt $attemptobj
404 * @param int $page Current page number
405 * @param array $slots Array of integers relating to questions
406 * @param int $id ID of the attempt
407 * @param int $nextpage Next page number
409 public function attempt_form($attemptobj, $page, $slots, $id, $nextpage) {
413 $output .= html_writer::start_tag('form',
414 array('action' => $attemptobj->processattempt_url(), 'method' => 'post',
415 'enctype' => 'multipart/form-data', 'accept-charset' => 'utf-8',
416 'id' => 'responseform'));
417 $output .= html_writer::start_tag('div');
419 // Print all the questions
420 foreach ($slots as $slot) {
421 $output .= $attemptobj->render_question($slot, false, $attemptobj->attempt_url($id,
425 $output .= html_writer::start_tag('div', array('class' => 'submitbtns'));
426 $output .= html_writer::empty_tag('input', array('type' => 'submit', 'name' => 'next',
427 'value' => get_string('next')));
428 $output .= html_writer::end_tag('div');
430 // Some hidden fields to trach what is going on.
431 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'attempt',
432 'value' => $attemptobj->get_attemptid()));
433 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'thispage',
434 'value' => $page, 'id' => 'followingpage'));
435 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'nextpage',
436 'value' => $nextpage));
437 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'timeup',
438 'value' => '0', 'id' => 'timeup'));
439 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey',
440 'value' => sesskey()));
441 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'scrollpos',
442 'value' => '', 'id' => 'scrollpos'));
444 // Add a hidden field with questionids. Do this at the end of the form, so
445 // if you navigate before the form has finished loading, it does not wipe all
446 // the student's answers.
447 $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'slots',
448 'value' => implode(',', $slots)));
451 $output .= html_writer::end_tag('div');
452 $output .= html_writer::end_tag('form');
458 * Print each message in an array, surrounded by <p>, </p> tags.
460 * @param array $messages the array of message strings.
461 * @param bool $return if true, return a string, instead of outputting.
463 * @return mixed, if $return is true, return the string that would have been output, otherwise
466 public function access_messages($messages) {
468 foreach ($messages as $message) {
469 $output .= html_writer::tag('p', $message) . "\n";
478 * Create the summary page
480 * @param quiz_attempt $attemptobj
481 * @param mod_quiz_display_options $displayoptions
483 public function summary_page($attemptobj, $displayoptions) {
485 $output .= $this->summary_table($attemptobj, $displayoptions);
486 $output .= $this->summary_page_controls($attemptobj);
491 * Generates the table of summarydata
493 * @param quiz_attempt $attemptobj
494 * @param mod_quiz_display_options $displayoptions
496 public function summary_table($attemptobj, $displayoptions) {
497 // Prepare the summary table header
498 $table = new html_table();
499 $table->attributes['class'] = 'generaltable quizsummaryofattempt boxaligncenter';
500 $table->head = array(get_string('question', 'quiz'), get_string('status', 'quiz'));
501 $table->align = array('left', 'left');
502 $table->size = array('', '');
503 $markscolumn = $displayoptions->marks >= question_display_options::MARK_AND_MAX;
505 $table->head[] = get_string('marks', 'quiz');
506 $table->align[] = 'left';
509 $table->data = array();
511 // Get the summary info for each question.
512 $slots = $attemptobj->get_slots();
513 foreach ($slots as $slot) {
514 if (!$attemptobj->is_real_question($slot)) {
518 if ($attemptobj->is_question_flagged($slot)) {
519 $flag = html_writer::empty_tag('img', array('src' => $this->pix_url('i/flagged'),
520 'alt' => get_string('flagged', 'question'), 'class' => 'questionflag'));
522 $row = array(html_writer::link($attemptobj->attempt_url($slot),
523 $attemptobj->get_question_number($slot) . $flag),
524 $attemptobj->get_question_status($slot, $displayoptions->correctness));
526 $row[] = $attemptobj->get_question_mark($slot);
528 $table->data[] = $row;
529 $table->rowclasses[] = $attemptobj->get_question_state_class(
530 $slot, $displayoptions->correctness);
533 // Print the summary table.
534 $output = html_writer::table($table);
540 * Creates any controls a the page should have.
542 * @param quiz_attempt $attemptobj
544 public function summary_page_controls($attemptobj) {
547 $output .= $this->countdown_timer();
549 // Finish attempt button.
551 'attempt' => $attemptobj->get_attemptid(),
552 'finishattempt' => 1,
555 'sesskey' => sesskey(),
558 $button = new single_button(
559 new moodle_url($attemptobj->processattempt_url(), $options),
560 get_string('submitallandfinish', 'quiz'));
561 $button->id = 'responseform';
562 $button->add_action(new confirm_action(get_string('confirmclose', 'quiz'), null,
563 get_string('submitallandfinish', 'quiz')));
565 $output .= $this->container($this->container($this->render($button),
566 'controls'), 'submitbtns mdl-align');
575 * Generates the view page
577 * @param int $course The id of the course
578 * @param array $quiz Array conting quiz data
579 * @param int $cm Course Module ID
580 * @param int $context The page context ID
581 * @param array $infomessages information about this quiz
582 * @param mod_quiz_view_object $viewobj
583 * @param string $buttontext text for the start/continue attempt button, if
584 * it should be shown.
585 * @param array $infomessages further information about why the student cannot
586 * attempt this quiz now, if appicable this quiz
588 public function view_page($course, $quiz, $cm, $context, $infomessages, $viewobj,
589 $buttontext, $preventmessages) {
591 $output .= $this->view_information($course, $quiz, $cm, $context, $infomessages);
592 $output .= $this->view_table($quiz, $context, $viewobj);
593 $output .= $this->view_best_score($viewobj);
594 $output .= $this->view_result_info($quiz, $context, $cm, $viewobj);
595 $output .= $this->view_attempt_button($course, $quiz, $cm, $context, $viewobj,
596 $buttontext, $preventmessages);
601 * Outputs an error message for any guests accessing the quiz
603 * @param int $course The course ID
604 * @param array $quiz Array contingin quiz data
605 * @param int $cm Course Module ID
606 * @param int $context The page contect ID
607 * @param array $messages Array containing any messages
609 public function view_page_guest($course, $quiz, $cm, $context, $messages) {
611 $output .= $this->view_information($course, $quiz, $cm, $context, $messages);
612 $guestno = html_writer::tag('p', get_string('guestsno', 'quiz'));
613 $liketologin = html_writer::tag('p', get_string('liketologin'));
614 $output .= $this->confirm($guestno.'\n\n'.$liketologin.'\n', get_login_url(),
620 * Outputs and error message for anyone who is not enrolle don the course
622 * @param int $course The course ID
623 * @param array $quiz Array contingin quiz data
624 * @param int $cm Course Module ID
625 * @param int $context The page contect ID
626 * @param array $messages Array containing any messages
628 public function view_page_notenrolled($course, $quiz, $cm, $context, $messages) {
631 $output .= $this->view_information($course, $quiz, $cm, $context, $messages);
632 $youneedtoenrol = html_writer::tag('p', get_string('youneedtoenrol', 'quiz'));
633 $button = html_writer::tag('p',
634 $this->continue_button($CFG->wwwroot . '/course/view.php?id=' . $course->id));
635 $output .= $this->box($youneedtoenrol.'\n\n'.$button.'\n', 'generalbox', 'notice');
640 * Output the page information
642 * @param int $course The course ID
643 * @param array $quiz Array contingin quiz data
644 * @param int $cm Course Module ID
645 * @param int $context The page contect ID
646 * @param array $messages Array containing any messages
648 public function view_information($course, $quiz, $cm, $context, $messages) {
651 // Print quiz name and description
652 $output .= $this->heading(format_string($quiz->name));
653 if (trim(strip_tags($quiz->intro))) {
654 $output .= $this->box(format_module_intro('quiz', $quiz, $cm->id), 'generalbox',
658 $output .= $this->box($this->access_messages($messages), 'quizinfo');
660 // Show number of attempts summary to those who can view reports.
661 if (has_capability('mod/quiz:viewreports', $context)) {
662 if ($strattemptnum = $this->quiz_attempt_summary_link_to_reports($quiz, $cm,
664 $output .= html_writer::tag('div', $strattemptnum,
665 array('class' => 'quizattemptcounts'));
672 * Generates the table heading.
674 public function view_table_heading() {
675 return $this->heading(get_string('summaryofattempts', 'quiz'));
679 * Generates the table of data
681 * @param array $quiz Array contining quiz data
682 * @param int $context The page context ID
683 * @param mod_quiz_view_object $viewobj
685 public function view_table($quiz, $context, $viewobj) {
687 if (!$viewobj->attempts) {
690 $output .= $this->view_table_heading();
692 // Prepare table header
693 $table = new html_table();
694 $table->attributes['class'] = 'generaltable quizattemptsummary';
695 $table->head = array();
696 $table->align = array();
697 $table->size = array();
698 if ($viewobj->attemptcolumn) {
699 $table->head[] = get_string('attemptnumber', 'quiz');
700 $table->align[] = 'center';
703 $table->head[] = get_string('timecompleted', 'quiz');
704 $table->align[] = 'left';
706 if ($viewobj->markcolumn) {
707 $table->head[] = get_string('marks', 'quiz') . ' / ' .
708 quiz_format_grade($quiz, $quiz->sumgrades);
709 $table->align[] = 'center';
712 if ($viewobj->gradecolumn) {
713 $table->head[] = get_string('grade') . ' / ' .
714 quiz_format_grade($quiz, $quiz->grade);
715 $table->align[] = 'center';
718 if ($viewobj->canreviewmine) {
719 $table->head[] = get_string('review', 'quiz');
720 $table->align[] = 'center';
723 if ($viewobj->feedbackcolumn) {
724 $table->head[] = get_string('feedback', 'quiz');
725 $table->align[] = 'left';
728 if (isset($quiz->showtimetaken)) {
729 $table->head[] = get_string('timetaken', 'quiz');
730 $table->align[] = 'left';
734 // One row for each attempt
735 foreach ($viewobj->attempts as $attempt) {
736 $attemptoptions = quiz_get_review_options($quiz, $attempt, $context);
739 // Add the attempt number, making it a link, if appropriate.
740 if ($viewobj->attemptcolumn) {
741 if ($attempt->preview) {
742 $row[] = get_string('preview', 'quiz');
744 $row[] = $attempt->attempt;
748 // prepare strings for time taken and date completed
751 if ($attempt->timefinish > 0) {
752 // attempt has finished
753 $timetaken = format_time($attempt->timefinish - $attempt->timestart);
754 $datecompleted = userdate($attempt->timefinish);
755 } else if (!$quiz->timeclose || $viewobj->timenow < $quiz->timeclose) {
756 // The attempt is still in progress.
757 $timetaken = format_time($viewobj->timenow - $attempt->timestart);
758 $datecompleted = get_string('inprogress', 'quiz');
760 $timetaken = format_time($quiz->timeclose - $attempt->timestart);
761 $datecompleted = userdate($quiz->timeclose);
763 $row[] = $datecompleted;
765 if ($viewobj->markcolumn && $attempt->timefinish > 0) {
766 if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX) {
767 $row[] = quiz_format_grade($quiz, $attempt->sumgrades);
773 // Ouside the if because we may be showing feedback but not grades.
774 $attemptgrade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
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';
788 $row[] = $formattedgrade;
794 if ($viewobj->canreviewmine) {
795 $row[] = $viewobj->accessmanager->make_review_link($attempt,
796 $viewobj->canpreview, $attemptoptions);
799 if ($viewobj->feedbackcolumn && $attempt->timefinish > 0) {
800 if ($attemptoptions->overallfeedback) {
801 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context);
807 if (isset($quiz->showtimetaken)) {
811 if ($attempt->preview) {
812 $table->data['preview'] = $row;
814 $table->data[$attempt->attempt] = $row;
816 } // End of loop over attempts.
817 $output .= html_writer::table($table);
823 * Prints the students best score
825 * @param mod_quiz_view_object $viewobj
827 public function view_best_score($viewobj) {
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'));
837 * Generates data pertaining to quiz results
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
844 public function view_result_info($quiz, $context, $cm, $viewobj) {
846 if (!$viewobj->numattempts && !$viewobj->gradecolumn && is_null($viewobj->mygrade)) {
851 if ($viewobj->overallstats) {
852 if ($viewobj->moreattempts) {
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');
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,
868 if ($viewobj->mygradeoverridden) {
870 $resultinfo .= html_writer::tag('p', get_string('overriddennotice', 'grades'),
871 array('class' => 'overriddennotice')).'\n';
873 if ($viewobj->gradebookfeedback) {
874 $resultinfo .= $this->heading(get_string('comment', 'quiz'), 3, 'main');
875 $resultinfo .= '<p class="quizteacherfeedback">'.$viewobj->gradebookfeedback.
878 if ($viewobj->feedbackcolumn) {
879 $resultinfo .= $this->heading(get_string('overallfeedback', 'quiz'), 3, 'main');
880 $resultinfo .= html_writer::tag('p',
881 quiz_feedback_for_grade($viewobj->mygrade, $quiz, $context),
882 array('class' => 'quizgradefeedback')).'\n';
886 $output .= $this->box($resultinfo, 'generalbox', 'feedback');
892 * Generates the view attempt button
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
901 public function view_attempt_button($course, $quiz, $cm, $context, $viewobj,
902 $buttontext, $preventmessages) {
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');
908 // Now actually print the appropriate button.
909 if (!quiz_clean_layout($quiz->questions, true)) {
910 $output .= quiz_no_questions_message($quiz, $cm, $context);
913 if ($preventmessages) {
914 $output .= $this->access_messages($preventmessages);
918 $output .= $viewobj->accessmanager->print_start_attempt_button($viewobj->canpreview,
919 $buttontext, $viewobj->unfinished);
920 } else if ($buttontext === '') {
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'));
925 $output .= $this->box_end();
931 * Returns the same as {@link quiz_num_attempt_summary()} but wrapped in a link
932 * to the quiz reports.
934 * @param object $quiz the quiz object. Only $quiz->id is used at the moment.
935 * @param object $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid
936 * fields are used at the moment.
937 * @param object $context the quiz context.
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
942 * (e.g. a report) pass it in here. Default 0 which means no current group.
943 * @return string HTML fragment for the link.
945 public function quiz_attempt_summary_link_to_reports($quiz, $cm, $context,
946 $returnzero = false, $currentgroup = 0) {
948 $summary = quiz_num_attempt_summary($quiz, $cm, $returnzero, $currentgroup);
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);
960 class mod_quiz_links_to_other_attempts implements renderable {
962 * @var array string attempt number => url, or null for the current attempt.
964 public $links = array();
967 class mod_quiz_view_object {
969 * @var array $attempt contains all the user's attempts at this quiz.
973 * @var object $accessmanager contains various access rules.
975 public $accessmanager;
977 * @var int $canattempt determins capability for attempting a quiz.
981 * @var int $canpreview determins capability for previewing a quiz.
985 * @var int $canreviewmine determins capability for reviwing own quiz.
987 public $canreviewmine;
989 * @var int $attemptcolumn contains the number of attempts done.
991 public $attemptcolumn;
993 * @var int $gradecolumn contains the grades of any attempts.
997 * @var int $markcolumn contains the marks of any attempt.
1001 * @var int $overallstats contains all marks for any attempt.
1003 public $overallstats;
1005 * @var string $feedbackcolumn contains any feedback for and attempt.
1007 public $feedbackcolumn;
1009 * @var string $timenow contains a timestamp in string format.
1013 * @var int $numattempts contains the total number of attempts.
1015 public $numattempts;
1017 * @var int $mygrade contains the current users final grade for a quiz.
1021 * @var int $moreattempts total attempts left.
1023 public $moreattempts;
1025 * @var int $mygradeoverridden contains an overriden grade.
1027 public $mygradeoverridden;
1029 * @var string $gradebookfeedback contains any feedback for a gradebook.
1031 public $gradebookfeedback;
1033 * @var int $unfinished contains 1 if an attempt is unfinished.
1037 * @var int $lastfinishedattempt contains a pointer to the last attempt in the attempts array.
1039 public $lastfinishedattempt;