Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 401 and 402] [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  require_once(__DIR__ . '/../../config.php');
  32  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  33  
  34  // Remember the current time as the time any responses were submitted
  35  // (so as to make sure students don't get penalized for slow processing on this page).
  36  $timenow = time();
  37  
  38  // Get submitted parameters.
  39  $attemptid     = required_param('attempt',  PARAM_INT);
  40  $thispage      = optional_param('thispage', 0, PARAM_INT);
  41  $nextpage      = optional_param('nextpage', 0, PARAM_INT);
  42  $previous      = optional_param('previous',      false, PARAM_BOOL);
  43  $next          = optional_param('next',          false, PARAM_BOOL);
  44  $finishattempt = optional_param('finishattempt', false, PARAM_BOOL);
  45  $timeup        = optional_param('timeup',        0,      PARAM_BOOL); // True if form was submitted by timer.
  46  $scrollpos     = optional_param('scrollpos',     '',     PARAM_RAW);
  47  $cmid          = optional_param('cmid', null, PARAM_INT);
  48  
  49  $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
  50  
  51  // Set $nexturl now.
  52  if ($next) {
  53      $page = $nextpage;
  54  } else if ($previous && $thispage > 0) {
  55      $page = $thispage - 1;
  56  } else {
  57      $page = $thispage;
  58  }
  59  if ($page == -1) {
  60      $nexturl = $attemptobj->summary_url();
  61  } else {
  62      $nexturl = $attemptobj->attempt_url(null, $page);
  63      if ($scrollpos !== '') {
  64          $nexturl->param('scrollpos', $scrollpos);
  65      }
  66  }
  67  
  68  // Check login.
  69  require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
  70  require_sesskey();
  71  
  72  // Check that this attempt belongs to this user.
  73  if ($attemptobj->get_userid() != $USER->id) {
  74      throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
  75  }
  76  
  77  // Check capabilities.
  78  if (!$attemptobj->is_preview_user()) {
  79      $attemptobj->require_capability('mod/quiz:attempt');
  80  }
  81  
  82  // If the attempt is already closed, send them to the review page.
  83  if ($attemptobj->is_finished()) {
  84      throw new moodle_quiz_exception($attemptobj->get_quizobj(),
  85              'attemptalreadyclosed', null, $attemptobj->review_url());
  86  }
  87  
  88  // If this page cannot be accessed, notify user and send them to the correct page.
  89  if (!$finishattempt && !$attemptobj->check_page_access($thispage)) {
  90      throw new moodle_exception('submissionoutofsequencefriendlymessage', 'question',
  91              $attemptobj->attempt_url(null, $attemptobj->get_currentpage()));
  92  }
  93  
  94  // Process the attempt, getting the new status for the attempt.
  95  $status = $attemptobj->process_attempt($timenow, $finishattempt, $timeup, $thispage);
  96  
  97  if ($status == quiz_attempt::OVERDUE) {
  98      redirect($attemptobj->summary_url());
  99  } else if ($status == quiz_attempt::IN_PROGRESS) {
 100      redirect($nexturl);
 101  } else {
 102      // Attempt abandoned or finished.
 103      redirect($attemptobj->review_url());
 104  }