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 * 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.
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));
39 $url->param('page', $page);
40 } else if ($showall) {
41 $url->param('showall', $showall);
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));
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);
65 if ($attemptobj->is_own_attempt()) {
66 if (!$attemptobj->is_finished()) {
67 redirect($attemptobj->attempt_url(null, $page));
69 } else if (!$options->attempt
) {
70 $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'),
71 $attemptobj->cannot_review_message());
74 } else if (!$attemptobj->is_review_allowed()) {
75 throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noreviewattempt');
78 // Load the questions and states needed by this page.
80 $questionids = $attemptobj->get_slots();
82 $questionids = $attemptobj->get_slots($page);
85 // Save the flag states, if they are being changed.
86 if ($options->flags
== question_display_options
::EDITABLE
&& optional_param('savingflags', false,
89 $attemptobj->save_question_flags();
90 redirect($attemptobj->review_url(null, $page, $showall));
93 // Work out appropriate title and whether blocks should be shown.
94 if ($attemptobj->is_preview_user() && $attemptobj->is_own_attempt()) {
95 $strreviewtitle = get_string('reviewofpreview', 'quiz');
96 navigation_node
::override_active_url($attemptobj->start_attempt_url());
99 $strreviewtitle = get_string('reviewofattempt', 'quiz', $attemptobj->get_attempt_number());
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->get_quiz_name());
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();
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);
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 $usrepicture = new user_picture($student);
137 $usrepicture->courseid
= $attemptobj->get_courseid();
138 $summarydata['user'] = array(
139 'title' => $usrepicture,
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,
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),
200 // Show raw marks only if they are different from the grade (like on the view page).
201 if ($quiz->grade
!= $quiz->sumgrades
) {
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.
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);
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. ==============================================================================
244 $slots = $attemptobj->get_slots();
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.
262 'objectid' => $attemptobj->get_attemptid(),
263 'relateduserid' => $attemptobj->get_userid(),
264 'courseid' => $attemptobj->get_courseid(),
265 'context' => context_module
::instance($attemptobj->get_cmid()),
267 'quizid' => $attemptobj->get_quizid()
270 $event = \mod_quiz\event\attempt_reviewed
::create($params);
271 $event->add_record_snapshot('quiz_attempts', $attemptobj->get_attempt());