Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  // Process the attempt, getting the new status for the attempt.
  89  $status = $attemptobj->process_attempt($timenow, $finishattempt, $timeup, $thispage);
  90  
  91  if ($status == quiz_attempt::OVERDUE) {
  92      redirect($attemptobj->summary_url());
  93  } else if ($status == quiz_attempt::IN_PROGRESS) {
  94      redirect($nexturl);
  95  } else {
  96      // Attempt abandoned or finished.
  97      redirect($attemptobj->review_url());
  98  }