Merge branch 'MDL-49187-27' of https://github.com/spvickers/moodle into MOODLE_27_STABLE
[moodle.git] / mod / quiz / grade.php
blobdd0c9f8d9499faa9b693676b13169f27e095d3d2
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
28 require_once(dirname(__FILE__) . '/../../config.php');
29 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
30 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
33 $id = required_param('id', PARAM_INT);
34 $userid = optional_param('userid', 0, PARAM_INT);
36 $cm = get_coursemodule_from_id('quiz', $id, 0, false, MUST_EXIST);
37 $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
38 $quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST);
39 require_login($course, false, $cm);
41 $reportlist = quiz_report_list(context_module::instance($cm->id));
42 if (empty($reportlist) || $userid == $USER->id) {
43 // If the user cannot see reports, or can see reports but is looking
44 // at their own grades, redirect them to the view.php page.
45 // (The looking at their own grades case is unlikely, since users who
46 // appear in the gradebook are unlikely to be able to see quiz reports,
47 // but it is possible.)
48 redirect(new moodle_url('/mod/quiz/view.php', array('id' => $cm->id)));
51 // Now we know the user is interested in reports. If they are interested in a
52 // specific other user, try to send them to the most appropriate attempt review page.
53 if ($userid) {
55 // Work out which attempt is most significant from a grading point of view.
56 $attempts = quiz_get_user_attempts($quiz->id, $userid, 'finished');
57 $attempt = null;
58 switch ($quiz->grademethod) {
59 case QUIZ_ATTEMPTFIRST:
60 $attempt = reset($attempts);
61 break;
63 case QUIZ_ATTEMPTLAST:
64 case QUIZ_GRADEAVERAGE:
65 $attempt = end($attempts);
66 break;
68 case QUIZ_GRADEHIGHEST:
69 $maxmark = 0;
70 foreach ($attempts as $at) {
71 // Operator >=, since we want to most recent relevant attempt.
72 if ((float) $at->sumgrades >= $maxmark) {
73 $maxmark = $at->sumgrades;
74 $attempt = $at;
77 break;
80 // If the user can review the relevant attempt, redirect to it.
81 if ($attempt) {
82 $attemptobj = new quiz_attempt($attempt, $quiz, $cm, $course, false);
83 if ($attemptobj->is_review_allowed()) {
84 redirect($attemptobj->review_url());
88 // Otherwise, fall thorugh to the generic case.
91 // Send the user to the first report they can see.
92 redirect(new moodle_url('/mod/quiz/report.php', array(
93 'id' => $cm->id, 'mode' => reset($reportlist))));