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 311 and 401] [Versions 39 and 401] [Versions 400 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 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  require_once(__DIR__ . '/../../config.php');
  30  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  31  
  32  // Get submitted parameters.
  33  $id = required_param('cmid', PARAM_INT); // Course module id
  34  $forcenew = optional_param('forcenew', false, PARAM_BOOL); // Used to force a new preview
  35  $page = optional_param('page', -1, PARAM_INT); // Page to jump to in the attempt.
  36  
  37  if (!$cm = get_coursemodule_from_id('quiz', $id)) {
  38      throw new \moodle_exception('invalidcoursemodule');
  39  }
  40  if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
  41      throw new \moodle_exception("coursemisconf");
  42  }
  43  
  44  $quizobj = quiz::create($cm->instance, $USER->id);
  45  // This script should only ever be posted to, so set page URL to the view page.
  46  $PAGE->set_url($quizobj->view_url());
  47  // During quiz attempts, the browser back/forwards buttons should force a reload.
  48  $PAGE->set_cacheable(false);
  49  
  50  // Check login and sesskey.
  51  require_login($quizobj->get_course(), false, $quizobj->get_cm());
  52  require_sesskey();
  53  $PAGE->set_heading($quizobj->get_course()->fullname);
  54  
  55  // If no questions have been set up yet redirect to edit.php or display an error.
  56  if (!$quizobj->has_questions()) {
  57      if ($quizobj->has_capability('mod/quiz:manage')) {
  58          redirect($quizobj->edit_url());
  59      } else {
  60          throw new \moodle_exception('cannotstartnoquestions', 'quiz', $quizobj->view_url());
  61      }
  62  }
  63  
  64  // Create an object to manage all the other (non-roles) access rules.
  65  $timenow = time();
  66  $accessmanager = $quizobj->get_access_manager($timenow);
  67  
  68  // Validate permissions for creating a new attempt and start a new preview attempt if required.
  69  list($currentattemptid, $attemptnumber, $lastattempt, $messages, $page) =
  70      quiz_validate_new_attempt($quizobj, $accessmanager, $forcenew, $page, true);
  71  
  72  // Check access.
  73  if (!$quizobj->is_preview_user() && $messages) {
  74      $output = $PAGE->get_renderer('mod_quiz');
  75      throw new \moodle_exception('attempterror', 'quiz', $quizobj->view_url(),
  76              $output->access_messages($messages));
  77  }
  78  
  79  if ($accessmanager->is_preflight_check_required($currentattemptid)) {
  80      // Need to do some checks before allowing the user to continue.
  81      $mform = $accessmanager->get_preflight_check_form(
  82              $quizobj->start_attempt_url($page), $currentattemptid);
  83  
  84      if ($mform->is_cancelled()) {
  85          $accessmanager->back_to_view_page($PAGE->get_renderer('mod_quiz'));
  86  
  87      } else if (!$mform->get_data()) {
  88  
  89          // Form not submitted successfully, re-display it and stop.
  90          $PAGE->set_url($quizobj->start_attempt_url($page));
  91          $PAGE->set_title($quizobj->get_quiz_name());
  92          $accessmanager->setup_attempt_page($PAGE);
  93          $output = $PAGE->get_renderer('mod_quiz');
  94          if (empty($quizobj->get_quiz()->showblocks)) {
  95              $PAGE->blocks->show_only_fake_blocks();
  96          }
  97  
  98          echo $output->start_attempt_page($quizobj, $mform);
  99          die();
 100      }
 101  
 102      // Pre-flight check passed.
 103      $accessmanager->notify_preflight_check_passed($currentattemptid);
 104  }
 105  if ($currentattemptid) {
 106      if ($lastattempt->state == quiz_attempt::OVERDUE) {
 107          redirect($quizobj->summary_url($lastattempt->id));
 108      } else {
 109          redirect($quizobj->attempt_url($currentattemptid, $page));
 110      }
 111  }
 112  
 113  $attempt = quiz_prepare_and_start_new_attempt($quizobj, $attemptnumber, $lastattempt);
 114  
 115  // Redirect to the attempt page.
 116  redirect($quizobj->attempt_url($attempt->id, $page));