Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
/mod/quiz/ -> grade.php (source)

Differences Between: [Versions 400 and 402] [Versions 400 and 403]

   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.
  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  
  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.
  20   *
  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
  25   */
  26  
  27  
  28  require_once(__DIR__ . '/../../config.php');
  29  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  30  require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
  31  
  32  
  33  $id = required_param('id', PARAM_INT);
  34  $userid = optional_param('userid', 0, PARAM_INT);
  35  
  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);
  40  
  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)));
  49  }
  50  
  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) {
  54  
  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;
  62  
  63          case QUIZ_ATTEMPTLAST:
  64          case QUIZ_GRADEAVERAGE:
  65              $attempt = end($attempts);
  66              break;
  67  
  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;
  75                  }
  76              }
  77              break;
  78      }
  79  
  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              $attemptobj->load_questions();
  85              redirect($attemptobj->review_url());
  86          }
  87      }
  88  
  89      // Otherwise, fall thorugh to the generic case.
  90  }
  91  
  92  // Send the user to the first report they can see.
  93  redirect(new moodle_url('/mod/quiz/report.php', array(
  94          'id' => $cm->id, 'mode' => reset($reportlist))));