Merge branch 'MDL-76207-39' of https://github.com/andrewnicols/moodle into MOODLE_39_...
[moodle.git] / mod / quiz / review.php
blob6773b91081c1f23a2aa95d43841383b8d905e53d
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.
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/>.
17 /**
18 * This page prints a review of a particular quiz attempt
20 * It is used either by the student whose attempts this is, after the attempt,
21 * or by a teacher reviewing another's attempt during or afterwards.
23 * @package mod_quiz
24 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once(__DIR__ . '/../../config.php');
30 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
31 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
33 $attemptid = required_param('attempt', PARAM_INT);
34 $page = optional_param('page', 0, PARAM_INT);
35 $showall = optional_param('showall', null, PARAM_BOOL);
36 $cmid = optional_param('cmid', null, PARAM_INT);
38 $url = new moodle_url('/mod/quiz/review.php', array('attempt'=>$attemptid));
39 if ($page !== 0) {
40 $url->param('page', $page);
41 } else if ($showall) {
42 $url->param('showall', $showall);
44 $PAGE->set_url($url);
46 $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
47 $page = $attemptobj->force_page_number_into_range($page);
49 // Now we can validate the params better, re-genrate the page URL.
50 if ($showall === null) {
51 $showall = $page == 0 && $attemptobj->get_default_show_all('review');
53 $PAGE->set_url($attemptobj->review_url(null, $page, $showall));
55 // Check login.
56 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
57 $attemptobj->check_review_capability();
59 // Create an object to manage all the other (non-roles) access rules.
60 $accessmanager = $attemptobj->get_access_manager(time());
61 $accessmanager->setup_attempt_page($PAGE);
63 $options = $attemptobj->get_display_options(true);
65 // Check permissions - warning there is similar code in reviewquestion.php and
66 // quiz_attempt::check_file_access. If you change on, change them all.
67 if ($attemptobj->is_own_attempt()) {
68 if (!$attemptobj->is_finished()) {
69 redirect($attemptobj->attempt_url(null, $page));
71 } else if (!$options->attempt) {
72 $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'),
73 $attemptobj->cannot_review_message());
76 } else if (!$attemptobj->is_review_allowed()) {
77 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noreviewattempt');
80 // Load the questions and states needed by this page.
81 if ($showall) {
82 $questionids = $attemptobj->get_slots();
83 } else {
84 $questionids = $attemptobj->get_slots($page);
87 // Save the flag states, if they are being changed.
88 if ($options->flags == question_display_options::EDITABLE && optional_param('savingflags', false,
89 PARAM_BOOL)) {
90 require_sesskey();
91 $attemptobj->save_question_flags();
92 redirect($attemptobj->review_url(null, $page, $showall));
95 // Work out appropriate title and whether blocks should be shown.
96 if ($attemptobj->is_own_preview()) {
97 navigation_node::override_active_url($attemptobj->start_attempt_url());
99 } else {
100 if (empty($attemptobj->get_quiz()->showblocks) && !$attemptobj->is_preview_user()) {
101 $PAGE->blocks->show_only_fake_blocks();
105 // Set up the page header.
106 $headtags = $attemptobj->get_html_head_contributions($page, $showall);
107 $PAGE->set_title($attemptobj->review_page_title($page, $showall));
108 $PAGE->set_heading($attemptobj->get_course()->fullname);
110 // Summary table start. ============================================================================
112 // Work out some time-related things.
113 $attempt = $attemptobj->get_attempt();
114 $quiz = $attemptobj->get_quiz();
115 $overtime = 0;
117 if ($attempt->state == quiz_attempt::FINISHED) {
118 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
119 if ($quiz->timelimit && $timetaken > ($quiz->timelimit + 60)) {
120 $overtime = $timetaken - $quiz->timelimit;
121 $overtime = format_time($overtime);
123 $timetaken = format_time($timetaken);
124 } else {
125 $timetaken = "-";
127 } else {
128 $timetaken = get_string('unfinished', 'quiz');
131 // Prepare summary informat about the whole attempt.
132 $summarydata = array();
133 if (!$attemptobj->get_quiz()->showuserpicture && $attemptobj->get_userid() != $USER->id) {
134 // If showuserpicture is true, the picture is shown elsewhere, so don't repeat it.
135 $student = $DB->get_record('user', array('id' => $attemptobj->get_userid()));
136 $userpicture = new user_picture($student);
137 $userpicture->courseid = $attemptobj->get_courseid();
138 $summarydata['user'] = array(
139 'title' => $userpicture,
140 'content' => new action_link(new moodle_url('/user/view.php', array(
141 'id' => $student->id, 'course' => $attemptobj->get_courseid())),
142 fullname($student, true)),
146 if ($attemptobj->has_capability('mod/quiz:viewreports')) {
147 $attemptlist = $attemptobj->links_to_other_attempts($attemptobj->review_url(null, $page,
148 $showall));
149 if ($attemptlist) {
150 $summarydata['attemptlist'] = array(
151 'title' => get_string('attempts', 'quiz'),
152 'content' => $attemptlist,
157 // Timing information.
158 $summarydata['startedon'] = array(
159 'title' => get_string('startedon', 'quiz'),
160 'content' => userdate($attempt->timestart),
163 $summarydata['state'] = array(
164 'title' => get_string('attemptstate', 'quiz'),
165 'content' => quiz_attempt::state_name($attempt->state),
168 if ($attempt->state == quiz_attempt::FINISHED) {
169 $summarydata['completedon'] = array(
170 'title' => get_string('completedon', 'quiz'),
171 'content' => userdate($attempt->timefinish),
173 $summarydata['timetaken'] = array(
174 'title' => get_string('timetaken', 'quiz'),
175 'content' => $timetaken,
179 if (!empty($overtime)) {
180 $summarydata['overdue'] = array(
181 'title' => get_string('overdue', 'quiz'),
182 'content' => $overtime,
186 // Show marks (if the user is allowed to see marks at the moment).
187 $grade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
188 if ($options->marks >= question_display_options::MARK_AND_MAX && quiz_has_grades($quiz)) {
190 if ($attempt->state != quiz_attempt::FINISHED) {
191 // Cannot display grade.
193 } else if (is_null($grade)) {
194 $summarydata['grade'] = array(
195 'title' => get_string('grade', 'quiz'),
196 'content' => quiz_format_grade($quiz, $grade),
199 } else {
200 // Show raw marks only if they are different from the grade (like on the view page).
201 if ($quiz->grade != $quiz->sumgrades) {
202 $a = new stdClass();
203 $a->grade = quiz_format_grade($quiz, $attempt->sumgrades);
204 $a->maxgrade = quiz_format_grade($quiz, $quiz->sumgrades);
205 $summarydata['marks'] = array(
206 'title' => get_string('marks', 'quiz'),
207 'content' => get_string('outofshort', 'quiz', $a),
211 // Now the scaled grade.
212 $a = new stdClass();
213 $a->grade = html_writer::tag('b', quiz_format_grade($quiz, $grade));
214 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
215 if ($quiz->grade != 100) {
216 $a->percent = html_writer::tag('b', format_float(
217 $attempt->sumgrades * 100 / $quiz->sumgrades, 0));
218 $formattedgrade = get_string('outofpercent', 'quiz', $a);
219 } else {
220 $formattedgrade = get_string('outof', 'quiz', $a);
222 $summarydata['grade'] = array(
223 'title' => get_string('grade', 'quiz'),
224 'content' => $formattedgrade,
229 // Any additional summary data from the behaviour.
230 $summarydata = array_merge($summarydata, $attemptobj->get_additional_summary_data($options));
232 // Feedback if there is any, and the user is allowed to see it now.
233 $feedback = $attemptobj->get_overall_feedback($grade);
234 if ($options->overallfeedback && $feedback) {
235 $summarydata['feedback'] = array(
236 'title' => get_string('feedback', 'quiz'),
237 'content' => $feedback,
241 // Summary table end. ==============================================================================
243 if ($showall) {
244 $slots = $attemptobj->get_slots();
245 $lastpage = true;
246 } else {
247 $slots = $attemptobj->get_slots($page);
248 $lastpage = $attemptobj->is_last_page($page);
251 $output = $PAGE->get_renderer('mod_quiz');
253 // Arrange for the navigation to be displayed.
254 $navbc = $attemptobj->get_navigation_panel($output, 'quiz_review_nav_panel', $page, $showall);
255 $regions = $PAGE->blocks->get_regions();
256 $PAGE->blocks->add_fake_block($navbc, reset($regions));
258 echo $output->review_page($attemptobj, $slots, $page, $showall, $lastpage, $options, $summarydata);
260 // Trigger an event for this review.
261 $attemptobj->fire_attempt_reviewed_event();