Merge branch 'MDL-28976' of git://github.com/timhunt/moodle
[moodle.git] / mod / quiz / review.php
CommitLineData
83192608 1<?php
b2607ccc
TH
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
ee1fb969 17/**
f63a4ff2 18 * This page prints a review of a particular quiz attempt
19 *
b2607ccc
TH
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.
22 *
ba643847 23 * @package mod
b2607ccc 24 * @subpackage quiz
ba643847
TH
25 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f63a4ff2 27 */
29d5d0b4 28
ba643847 29
b2607ccc
TH
30require_once(dirname(__FILE__) . '/../../config.php');
31require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
33
34$attemptid = required_param('attempt', PARAM_INT);
35$page = optional_param('page', 0, PARAM_INT);
36$showall = optional_param('showall', 0, PARAM_BOOL);
37
38$url = new moodle_url('/mod/quiz/review.php', array('attempt'=>$attemptid));
39if ($page !== 0) {
40 $url->param('page', $page);
41}
42if ($showall !== 0) {
43 $url->param('showall', $showall);
44}
45$PAGE->set_url($url);
46
47$attemptobj = quiz_attempt::create($attemptid);
48
49// Check login.
50require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
51$attemptobj->check_review_capability();
52
53// Create an object to manage all the other (non-roles) access rules.
54$accessmanager = $attemptobj->get_access_manager(time());
55$options = $attemptobj->get_display_options(true);
56
8032cd79
TH
57// Check permissions.
58if ($attemptobj->is_own_attempt()) {
b2607ccc 59 if (!$attemptobj->is_finished()) {
45e5fbe1 60 redirect($attemptobj->attempt_url(null, $page));
8032cd79 61 } else if (!$options->attempt) {
b2607ccc
TH
62 $accessmanager->back_to_view_page($attemptobj->is_preview_user(),
63 $accessmanager->cannot_review_message($attemptobj->get_attempt_state()));
83192608 64 }
8032cd79
TH
65
66} else if (!$attemptobj->is_review_allowed()) {
67 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noreviewattempt');
b2607ccc
TH
68}
69
70// Load the questions and states needed by this page.
71if ($showall) {
72 $questionids = $attemptobj->get_slots();
73} else {
74 $questionids = $attemptobj->get_slots($page);
75}
76
77// Save the flag states, if they are being changed.
cbe87543
DL
78if ($options->flags == question_display_options::EDITABLE && optional_param('savingflags', false,
79 PARAM_BOOL)) {
b2607ccc
TH
80 require_sesskey();
81 $attemptobj->save_question_flags();
82 redirect($attemptobj->review_url(0, $page, $showall));
83}
84
85// Log this review.
86add_to_log($attemptobj->get_courseid(), 'quiz', 'review', 'review.php?attempt=' .
87 $attemptobj->get_attemptid(), $attemptobj->get_quizid(), $attemptobj->get_cmid());
88
89// Work out appropriate title and whether blocks should be shown
90if ($attemptobj->is_preview_user() && $attemptobj->is_own_attempt()) {
91 $strreviewtitle = get_string('reviewofpreview', 'quiz');
92 navigation_node::override_active_url($attemptobj->start_attempt_url());
93
94} else {
95 $strreviewtitle = get_string('reviewofattempt', 'quiz', $attemptobj->get_attempt_number());
96 if (empty($attemptobj->get_quiz()->showblocks) && !$attemptobj->is_preview_user()) {
97 $PAGE->blocks->show_only_fake_blocks();
f62040ed 98 }
b2607ccc
TH
99}
100
b3782c71 101// Set up the page header
b2607ccc
TH
102$headtags = $attemptobj->get_html_head_contributions($page, $showall);
103if ($accessmanager->securewindow_required($attemptobj->is_preview_user())) {
cbe87543
DL
104 $accessmanager->setup_secure_page($attemptobj->get_course()->shortname.': '.
105 format_string($attemptobj->get_quiz_name()), $headtags);
b3782c71 106} else if ($accessmanager->safebrowser_required($attemptobj->is_preview_user())) {
cbe87543
DL
107 $PAGE->set_title($attemptobj->get_course()->shortname . ': '.
108 format_string($attemptobj->get_quiz_name()));
b2607ccc
TH
109 $PAGE->set_heading($attemptobj->get_course()->fullname);
110 $PAGE->set_cacheable(false);
b2607ccc
TH
111} else {
112 $PAGE->navbar->add($strreviewtitle);
113 $PAGE->set_title(format_string($attemptobj->get_quiz_name()));
114 $PAGE->set_heading($attemptobj->get_course()->fullname);
b2607ccc
TH
115}
116
b2607ccc
TH
117// Summary table start ============================================================================
118
119// Work out some time-related things.
120$attempt = $attemptobj->get_attempt();
121$quiz = $attemptobj->get_quiz();
122$overtime = 0;
123
124if ($attempt->timefinish) {
125 if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
b3782c71 126 if ($quiz->timelimit && $timetaken > ($quiz->timelimit + 60)) {
b2607ccc
TH
127 $overtime = $timetaken - $quiz->timelimit;
128 $overtime = format_time($overtime);
56ed242b 129 }
b2607ccc 130 $timetaken = format_time($timetaken);
ee1fb969 131 } else {
b2607ccc 132 $timetaken = "-";
99a1bf3d 133 }
b2607ccc
TH
134} else {
135 $timetaken = get_string('unfinished', 'quiz');
136}
137
3a5da40b 138// Prepare summary informat about the whole attempt.
b3782c71 139$summarydata = array();
b2607ccc
TH
140if (!$attemptobj->get_quiz()->showuserpicture && $attemptobj->get_userid() != $USER->id) {
141 // If showuserpicture is true, the picture is shown elsewhere, so don't repeat it.
142 $student = $DB->get_record('user', array('id' => $attemptobj->get_userid()));
b3782c71
TH
143 $usrepicture = new user_picture($student);
144 $usrepicture->courseid = $attemptobj->get_courseid();
145 $summarydata['user'] = array(
146 'title' => $usrepicture,
147 'content' => new action_link(new moodle_url('/user/view.php', array(
148 'id' => $student->id, 'course' => $attemptobj->get_courseid())),
149 fullname($student, true)),
150 );
b2607ccc
TH
151}
152if ($attemptobj->has_capability('mod/quiz:viewreports')) {
cbe87543
DL
153 $attemptlist = $attemptobj->links_to_other_attempts($attemptobj->review_url(0, $page,
154 $showall));
b2607ccc 155 if ($attemptlist) {
b3782c71
TH
156 $summarydata['attemptlist'] = array(
157 'title' => get_string('attempts', 'quiz'),
158 'content' => $attemptlist,
159 );
29d5d0b4 160 }
b2607ccc
TH
161}
162
b3782c71
TH
163// Timing information.
164$summarydata['startedon'] = array(
165 'title' => get_string('startedon', 'quiz'),
166 'content' => userdate($attempt->timestart),
167);
b2607ccc 168
b3782c71
TH
169if ($attempt->timefinish) {
170 $summarydata['completedon'] = array(
171 'title' => get_string('completedon', 'quiz'),
172 'content' => userdate($attempt->timefinish),
173 );
174 $summarydata['timetaken'] = array(
175 'title' => get_string('timetaken', 'quiz'),
b2817f80 176 'content' => $timetaken,
b3782c71
TH
177 );
178}
b2607ccc 179
b3782c71
TH
180if (!empty($overtime)) {
181 $summarydata['overdue'] = array(
182 'title' => get_string('overdue', 'quiz'),
f57d75bd 183 'content' => format_time($overtime),
b3782c71
TH
184 );
185}
9c22b42e 186
b3782c71
TH
187// Show marks (if the user is allowed to see marks at the moment).
188$grade = quiz_rescale_grade($attempt->sumgrades, $quiz, false);
189if ($options->marks >= question_display_options::MARK_AND_MAX && quiz_has_grades($quiz)) {
4fc3d7e5 190
b3782c71
TH
191 if (!$attempt->timefinish) {
192 $summarydata['grade'] = array(
193 'title' => get_string('grade', 'quiz'),
194 'content' => get_string('attemptstillinprogress', 'quiz'),
195 );
196
197 } else if (is_null($grade)) {
198 $summarydata['grade'] = array(
199 'title' => get_string('grade', 'quiz'),
200 'content' => quiz_format_grade($quiz, $grade),
201 );
202
203 } else {
204 // Show raw marks only if they are different from the grade (like on the view page).
205 if ($quiz->grade != $quiz->sumgrades) {
f39cf78c 206 $a = new stdClass();
b3782c71
TH
207 $a->grade = quiz_format_grade($quiz, $attempt->sumgrades);
208 $a->maxgrade = quiz_format_grade($quiz, $quiz->sumgrades);
209 $summarydata['marks'] = array(
210 'title' => get_string('marks', 'quiz'),
211 'content' => get_string('outofshort', 'quiz', $a),
212 );
b2607ccc 213 }
b2607ccc 214
b3782c71
TH
215 // Now the scaled grade.
216 $a = new stdClass();
217 $a->grade = html_writer::tag('b', quiz_format_grade($quiz, $grade));
218 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
219 if ($quiz->grade != 100) {
220 $a->percent = html_writer::tag('b', format_float(
221 $attempt->sumgrades * 100 / $quiz->sumgrades, 0));
222 $formattedgrade = get_string('outofpercent', 'quiz', $a);
223 } else {
224 $formattedgrade = get_string('outof', 'quiz', $a);
225 }
226 $summarydata['grade'] = array(
227 'title' => get_string('grade', 'quiz'),
228 'content' => $formattedgrade,
229 );
f39cf78c 230 }
b2607ccc
TH
231}
232
b3782c71
TH
233// Feedback if there is any, and the user is allowed to see it now.
234$feedback = $attemptobj->get_overall_feedback($grade);
235if ($options->overallfeedback && $feedback) {
236 $summarydata['feedback'] = array(
237 'title' => get_string('feedback', 'quiz'),
238 'content' => $feedback,
239 );
b2607ccc
TH
240}
241
242// Summary table end ==============================================================================
243
b2607ccc 244if ($showall) {
b3782c71 245 $slots = $attemptobj->get_slots();
b2607ccc
TH
246 $lastpage = true;
247} else {
b3782c71 248 $slots = $attemptobj->get_slots($page);
b2607ccc
TH
249 $lastpage = $attemptobj->is_last_page($page);
250}
b2607ccc 251
b3782c71 252$output = $PAGE->get_renderer('mod_quiz');
b2607ccc 253
b3782c71
TH
254// Arrange for the navigation to be displayed.
255$navbc = $attemptobj->get_navigation_panel($output, 'quiz_review_nav_panel', $page, $showall);
256$firstregion = reset($PAGE->blocks->get_regions());
257$PAGE->blocks->add_fake_block($navbc, $firstregion);
258
259echo $output->review_page($attemptobj, $slots, $page, $showall, $lastpage, $options, $summarydata);