Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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 script deals with starting a new attempt at a quiz.
  19   *
  20   * Normally, it will end up redirecting to attempt.php - unless a password form is displayed.
  21   *
  22   * This code used to be at the top of attempt.php, if you are looking for CVS history.
  23   *
  24   * @package   mod_quiz
  25   * @copyright 2009 The Open University
  26   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  use mod_quiz\quiz_attempt;
  30  use mod_quiz\quiz_settings;
  31  
  32  require_once(__DIR__ . '/../../config.php');
  33  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  34  
  35  // Get submitted parameters.
  36  $id = required_param('cmid', PARAM_INT); // Course module id
  37  $forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
  38  $page = optional_param('page', -1, PARAM_INT); // Page to jump to in the attempt.
  39  
  40  $quizobj = quiz_settings::create_for_cmid($id, $USER->id);
  41  
  42  // This script should only ever be posted to, so set page URL to the view page.
  43  $PAGE->set_url($quizobj->view_url());
  44  // During quiz attempts, the browser back/forwards buttons should force a reload.
  45  $PAGE->set_cacheable(false);
  46  
  47  // Check login and sesskey.
  48  require_login($quizobj->get_course(), false, $quizobj->get_cm());
  49  require_sesskey();
  50  $PAGE->set_heading($quizobj->get_course()->fullname);
  51  
  52  // If no questions have been set up yet redirect to edit.php or display an error.
  53  if (!$quizobj->has_questions()) {
  54      if ($quizobj->has_capability('mod/quiz:manage')) {
  55          redirect($quizobj->edit_url());
  56      } else {
  57          throw new \moodle_exception('cannotstartnoquestions', 'quiz', $quizobj->view_url());
  58      }
  59  }
  60  
  61  // Create an object to manage all the other (non-roles) access rules.
  62  $timenow = time();
  63  $accessmanager = $quizobj->get_access_manager($timenow);
  64  
  65  // Validate permissions for creating a new attempt and start a new preview attempt if required.
  66  list($currentattemptid, $attemptnumber, $lastattempt, $messages, $page) =
  67      quiz_validate_new_attempt($quizobj, $accessmanager, $forcenew, $page, true);
  68  
  69  // Check access.
  70  if (!$quizobj->is_preview_user() && $messages) {
  71      $output = $PAGE->get_renderer('mod_quiz');
  72      throw new \moodle_exception('attempterror', 'quiz', $quizobj->view_url(),
  73              $output->access_messages($messages));
  74  }
  75  
  76  if ($accessmanager->is_preflight_check_required($currentattemptid)) {
  77      // Need to do some checks before allowing the user to continue.
  78      $mform = $accessmanager->get_preflight_check_form(
  79              $quizobj->start_attempt_url($page), $currentattemptid);
  80  
  81      if ($mform->is_cancelled()) {
  82          $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'));
  83  
  84      } else if (!$mform->get_data()) {
  85  
  86          // Form not submitted successfully, re-display it and stop.
  87          $PAGE->set_url($quizobj->start_attempt_url($page));
  88          $PAGE->set_title($quizobj->get_quiz_name());
  89          $accessmanager->setup_attempt_page($PAGE);
  90          $output = $PAGE->get_renderer('mod_quiz');
  91          if (empty($quizobj->get_quiz()->showblocks)) {
  92              $PAGE->blocks->show_only_fake_blocks();
  93          }
  94  
  95          echo $output->start_attempt_page($quizobj, $mform);
  96          die();
  97      }
  98  
  99      // Pre-flight check passed.
 100      $accessmanager->notify_preflight_check_passed($currentattemptid);
 101  }
 102  if ($currentattemptid) {
 103      if ($lastattempt->state == quiz_attempt::OVERDUE) {
 104          redirect($quizobj->summary_url($lastattempt->id));
 105      } else {
 106          redirect($quizobj->attempt_url($currentattemptid, $page));
 107      }
 108  }
 109  
 110  $attempt = quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $lastattempt);
 111  
 112  // Redirect to the attempt page.
 113  redirect($quizobj->attempt_url($attempt->id, $page));