Merge branch 'MDL-51445_m30v1' of https://github.com/sbourget/moodle into MOODLE_30_S...
[moodle.git] / mod / quiz / review.php
blobeebab6b5460a944addf5f7fc2395dd33892d17a5
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(dirname(__FILE__) . '/../../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);
37 $url = new moodle_url('/mod/quiz/review.php', array('attempt'=>$attemptid));
38 if ($page !== 0) {
39 $url->param('page', $page);
40 } else if ($showall) {
41 $url->param('showall', $showall);
43 $PAGE->set_url($url);
45 $attemptobj = quiz_attempt::create($attemptid);
46 $page = $attemptobj->force_page_number_into_range($page);
48 // Now we can validate the params better, re-genrate the page URL.
49 if ($showall === null) {
50 $showall = $page == 0 && $attemptobj->get_default_show_all('review');
52 $PAGE->set_url($attemptobj->review_url(null, $page, $showall));
54 // Check login.
55 require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
56 $attemptobj->check_review_capability();
58 // Create an object to manage all the other (non-roles) access rules.
59 $accessmanager = $attemptobj->get_access_manager(time());
60 $accessmanager->setup_attempt_page($PAGE);
62 $options = $attemptobj->get_display_options(true);
64 // Check permissions - warning there is similar code in reviewquestion.php and
65 // quiz_attempt::check_file_access. If you change on, change them all.
66 if ($attemptobj->is_own_attempt()) {
67 if (!$attemptobj->is_finished()) {
68 redirect($attemptobj->attempt_url(null, $page));
70 } else if (!$options->attempt) {
71 $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'),
72 $attemptobj->cannot_review_message());
75 } else if (!$attemptobj->is_review_allowed()) {
76 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noreviewattempt');
79 // Load the questions and states needed by this page.
80 if ($showall) {
81 $questionids = $attemptobj->get_slots();
82 } else {
83 $questionids = $attemptobj->get_slots($page);
86 // Save the flag states, if they are being changed.
87 if ($options->flags == question_display_options::EDITABLE && optional_param('savingflags', false,
88 PARAM_BOOL)) {
89 require_sesskey();
90 $attemptobj->save_question_flags();
91 redirect($attemptobj->review_url(null, $page, $showall));
94 // Work out appropriate title and whether blocks should be shown.
95 if ($attemptobj->is_own_preview()) {
96 $strreviewtitle = get_string('reviewofpreview', 'quiz');
97 navigation_node::override_active_url($attemptobj->start_attempt_url());
99 } else {
100 $strreviewtitle = get_string('reviewofattempt', 'quiz', $attemptobj->get_attempt_number());
101 if (empty($attemptobj->get_quiz()->showblocks) && !$attemptobj->is_preview_user()) {
102 $PAGE->blocks->show_only_fake_blocks();
106 // Set up the page header.
107 $headtags = $attemptobj->get_html_head_contributions($page, $showall);
108 $PAGE->set_title($attemptobj->get_quiz_name());
109 $PAGE->set_heading($attemptobj->get_course()->fullname);
111 // Summary table start. ============================================================================
113 // Work out some time-related things.
114 $attempt = $attemptobj->get_attempt();
115 $quiz = $attemptobj->get_quiz();
116 $overtime = 0;
118 if ($attempt->state == quiz_attempt::FINISHED) {
119 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
120 if ($quiz->timelimit && $timetaken > ($quiz->timelimit + 60)) {
121 $overtime = $timetaken - $quiz->timelimit;
122 $overtime = format_time($overtime);
124 $timetaken = format_time($timetaken);
125 } else {
126 $timetaken = "-";
128 } else {
129 $timetaken = get_string('unfinished', 'quiz');
132 // Prepare summary informat about the whole attempt.
133 $summarydata = array();
134 if (!$attemptobj->get_quiz()->showuserpicture && $attemptobj->get_userid() != $USER->id) {
135 // If showuserpicture is true, the picture is shown elsewhere, so don't repeat it.
136 $student = $DB->get_record('user', array('id' => $attemptobj->get_userid()));
137 $usrepicture = new user_picture($student);
138 $usrepicture->courseid = $attemptobj->get_courseid();
139 $summarydata['user'] = array(
140 'title' => $usrepicture,
141 'content' => new action_link(new moodle_url('/user/view.php', array(
142 'id' => $student->id, 'course' => $attemptobj->get_courseid())),
143 fullname($student, true)),
147 if ($attemptobj->has_capability('mod/quiz:viewreports')) {
148 $attemptlist = $attemptobj->links_to_other_attempts($attemptobj->review_url(null, $page,
149 $showall));
150 if ($attemptlist) {
151 $summarydata['attemptlist'] = array(
152 'title' => get_string('attempts', 'quiz'),
153 'content' => $attemptlist,
158 // Timing information.
159 $summarydata['startedon'] = array(
160 'title' => get_string('startedon', 'quiz'),
161 'content' => userdate($attempt->timestart),
164 $summarydata['state'] = array(
165 'title' => get_string('attemptstate', 'quiz'),
166 'content' => quiz_attempt::state_name($attempt->state),
169 if ($attempt->state == quiz_attempt::FINISHED) {
170 $summarydata['completedon'] = array(
171 'title' => get_string('completedon', 'quiz'),
172 'content' => userdate($attempt->timefinish),
174 $summarydata['timetaken'] = array(
175 'title' => get_string('timetaken', 'quiz'),
176 'content' => $timetaken,
180 if (!empty($overtime)) {
181 $summarydata['overdue'] = array(
182 'title' => get_string('overdue', 'quiz'),
183 'content' => $overtime,
187 // Show marks (if the user is allowed to see marks at the moment).
188 $grade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
189 if ($options->marks >= question_display_options::MARK_AND_MAX && quiz_has_grades($quiz)) {
191 if ($attempt->state != quiz_attempt::FINISHED) {
192 // Cannot display grade.
194 } else if (is_null($grade)) {
195 $summarydata['grade'] = array(
196 'title' => get_string('grade', 'quiz'),
197 'content' => quiz_format_grade($quiz, $grade),
200 } else {
201 // Show raw marks only if they are different from the grade (like on the view page).
202 if ($quiz->grade != $quiz->sumgrades) {
203 $a = new stdClass();
204 $a->grade = quiz_format_grade($quiz, $attempt->sumgrades);
205 $a->maxgrade = quiz_format_grade($quiz, $quiz->sumgrades);
206 $summarydata['marks'] = array(
207 'title' => get_string('marks', 'quiz'),
208 'content' => get_string('outofshort', 'quiz', $a),
212 // Now the scaled grade.
213 $a = new stdClass();
214 $a->grade = html_writer::tag('b', quiz_format_grade($quiz, $grade));
215 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
216 if ($quiz->grade != 100) {
217 $a->percent = html_writer::tag('b', format_float(
218 $attempt->sumgrades * 100 / $quiz->sumgrades, 0));
219 $formattedgrade = get_string('outofpercent', 'quiz', $a);
220 } else {
221 $formattedgrade = get_string('outof', 'quiz', $a);
223 $summarydata['grade'] = array(
224 'title' => get_string('grade', 'quiz'),
225 'content' => $formattedgrade,
230 // Any additional summary data from the behaviour.
231 $summarydata = array_merge($summarydata, $attemptobj->get_additional_summary_data($options));
233 // Feedback if there is any, and the user is allowed to see it now.
234 $feedback = $attemptobj->get_overall_feedback($grade);
235 if ($options->overallfeedback && $feedback) {
236 $summarydata['feedback'] = array(
237 'title' => get_string('feedback', 'quiz'),
238 'content' => $feedback,
242 // Summary table end. ==============================================================================
244 if ($showall) {
245 $slots = $attemptobj->get_slots();
246 $lastpage = true;
247 } else {
248 $slots = $attemptobj->get_slots($page);
249 $lastpage = $attemptobj->is_last_page($page);
252 $output = $PAGE->get_renderer('mod_quiz');
254 // Arrange for the navigation to be displayed.
255 $navbc = $attemptobj->get_navigation_panel($output, 'quiz_review_nav_panel', $page, $showall);
256 $regions = $PAGE->blocks->get_regions();
257 $PAGE->blocks->add_fake_block($navbc, reset($regions));
259 echo $output->review_page($attemptobj, $slots, $page, $showall, $lastpage, $options, $summarydata);
261 // Trigger an event for this review.
262 $params = array(
263 'objectid' => $attemptobj->get_attemptid(),
264 'relateduserid' => $attemptobj->get_userid(),
265 'courseid' => $attemptobj->get_courseid(),
266 'context' => context_module::instance($attemptobj->get_cmid()),
267 'other' => array(
268 'quizid' => $attemptobj->get_quizid()
271 $event = \mod_quiz\event\attempt_reviewed::create($params);
272 $event->add_record_snapshot('quiz_attempts', $attemptobj->get_attempt());
273 $event->trigger();