Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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 deals with processing responses during an attempt at a quiz.
  19   *
  20   * People will normally arrive here from a form submission on attempt.php or
  21   * summary.php, and once the responses are processed, they will be redirected to
  22   * attempt.php or summary.php.
  23   *
  24   * This code used to be near the top of attempt.php, if you are looking for CVS history.
  25   *
  26   * @package   mod_quiz
  27   * @copyright 2009 Tim Hunt
  28   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  
  31  use mod_quiz\quiz_attempt;
  32  
  33  require_once(__DIR__ . '/../../config.php');
  34  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  35  
  36  // Remember the current time as the time any responses were submitted
  37  // (so as to make sure students don't get penalized for slow processing on this page).
  38  $timenow = time();
  39  
  40  // Get submitted parameters.
  41  $attemptid     = required_param('attempt',  PARAM_INT);
  42  $thispage      = optional_param('thispage', 0, PARAM_INT);
  43  $nextpage      = optional_param('nextpage', 0, PARAM_INT);
  44  $previous      = optional_param('previous',      false, PARAM_BOOL);
  45  $next          = optional_param('next',          false, PARAM_BOOL);
  46  $finishattempt = optional_param('finishattempt', false, PARAM_BOOL);
  47  $timeup        = optional_param('timeup',        0,      PARAM_BOOL); // True if form was submitted by timer.
  48  $mdlscrollto   = optional_param('mdlscrollto', '', PARAM_RAW);
  49  $cmid          = optional_param('cmid', null, PARAM_INT);
  50  
  51  $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
  52  
  53  // Set $nexturl now.
  54  if ($next) {
  55      $page = $nextpage;
  56  } else if ($previous && $thispage > 0) {
  57      $page = $thispage - 1;
  58  } else {
  59      $page = $thispage;
  60  }
  61  if ($page == -1) {
  62      $nexturl = $attemptobj->summary_url();
  63  } else {
  64      $nexturl = $attemptobj->attempt_url(null, $page);
  65      if ($mdlscrollto !== '') {
  66          $nexturl->param('mdlscrollto', $mdlscrollto);
  67      }
  68  }
  69  
  70  // Check login.
  71  require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
  72  require_sesskey();
  73  
  74  // Check that this attempt belongs to this user.
  75  if ($attemptobj->get_userid() != $USER->id) {
  76      throw new moodle_exception('notyourattempt', 'quiz', $attemptobj->view_url());
  77  }
  78  
  79  // Check capabilities.
  80  if (!$attemptobj->is_preview_user()) {
  81      $attemptobj->require_capability('mod/quiz:attempt');
  82  }
  83  
  84  // If the attempt is already closed, send them to the review page.
  85  if ($attemptobj->is_finished()) {
  86      throw new moodle_exception('attemptalreadyclosed', 'quiz', $attemptobj->view_url());
  87  }
  88  
  89  // If this page cannot be accessed, notify user and send them to the correct page.
  90  if (!$finishattempt && !$attemptobj->check_page_access($thispage)) {
  91      throw new moodle_exception('submissionoutofsequencefriendlymessage', 'question',
  92              $attemptobj->attempt_url(null, $attemptobj->get_currentpage()));
  93  }
  94  
  95  // Process the attempt, getting the new status for the attempt.
  96  $status = $attemptobj->process_attempt($timenow, $finishattempt, $timeup, $thispage);
  97  
  98  if ($status == quiz_attempt::OVERDUE) {
  99      redirect($attemptobj->summary_url());
 100  } else if ($status == quiz_attempt::IN_PROGRESS) {
 101      redirect($nexturl);
 102  } else {
 103      // Attempt abandoned or finished.
 104      redirect($attemptobj->review_url());
 105  }