Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / mod / quiz / grade.php
blob77b685859776297f2354cdd79f7b6ecbbd451089
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 is the entry page into the quiz UI. Displays information about the
19 * quiz to students and teachers, and lets students see their previous attempts.
21 * @package mod_quiz
22 * @category grade
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 use mod_quiz\quiz_attempt;
28 use mod_quiz\quiz_settings;
30 require_once(__DIR__ . '/../../config.php');
31 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
35 $id = required_param('id', PARAM_INT);
36 $userid = optional_param('userid', 0, PARAM_INT);
38 $quizobj = quiz_settings::create_for_cmid($id);
39 $quiz = $quizobj->get_quiz();
40 $cm = $quizobj->get_cm();
41 $course = $quizobj->get_course();
42 require_login($course, false, $cm);
44 $reportlist = quiz_report_list($quizobj->get_context());
45 if (empty($reportlist) || $userid == $USER->id) {
46 // If the user cannot see reports, or can see reports but is looking
47 // at their own grades, redirect them to the view.php page.
48 // (The looking at their own grades case is unlikely, since users who
49 // appear in the gradebook are unlikely to be able to see quiz reports,
50 // but it is possible.)
51 redirect(new moodle_url('/mod/quiz/view.php', ['id' => $cm->id]));
54 // Now we know the user is interested in reports. If they are interested in a
55 // specific other user, try to send them to the most appropriate attempt review page.
56 if ($userid) {
58 // Work out which attempt is most significant from a grading point of view.
59 $attempts = quiz_get_user_attempts($quiz->id, $userid, 'finished');
60 $attempt = null;
61 switch ($quiz->grademethod) {
62 case QUIZ_ATTEMPTFIRST:
63 $attempt = reset($attempts);
64 break;
66 case QUIZ_ATTEMPTLAST:
67 case QUIZ_GRADEAVERAGE:
68 $attempt = end($attempts);
69 break;
71 case QUIZ_GRADEHIGHEST:
72 $maxmark = 0;
73 foreach ($attempts as $at) {
74 // Operator >=, since we want to most recent relevant attempt.
75 if ((float) $at->sumgrades >= $maxmark) {
76 $maxmark = $at->sumgrades;
77 $attempt = $at;
80 break;
83 // If the user can review the relevant attempt, redirect to it.
84 if ($attempt) {
85 $attemptobj = new quiz_attempt($attempt, $quiz, $cm, $course, false);
86 if ($attemptobj->is_review_allowed()) {
87 $attemptobj->load_questions();
88 redirect($attemptobj->review_url());
92 // Otherwise, fall thorugh to the generic case.
95 // Send the user to the first report they can see.
96 redirect(new moodle_url('/mod/quiz/report.php', [
97 'id' => $cm->id, 'mode' => reset($reportlist)]));