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 displays a particular page of a quiz attempt that is in progress.
  19   *
  20   * @package   mod_quiz
  21   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use mod_quiz\output\navigation_panel_attempt;
  26  use mod_quiz\output\renderer;
  27  use mod_quiz\quiz_attempt;
  28  
  29  require_once(__DIR__ . '/../../config.php');
  30  require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  31  
  32  // Look for old-style URLs, such as may be in the logs, and redirect them to startattemtp.php.
  33  if ($id = optional_param('id', 0, PARAM_INT)) {
  34      redirect($CFG->wwwroot . '/mod/quiz/startattempt.php?cmid=' . $id . '&sesskey=' . sesskey());
  35  } else if ($qid = optional_param('q', 0, PARAM_INT)) {
  36      if (!$cm = get_coursemodule_from_instance('quiz', $qid)) {
  37          throw new \moodle_exception('invalidquizid', 'quiz');
  38      }
  39      redirect(new moodle_url('/mod/quiz/startattempt.php',
  40              ['cmid' => $cm->id, 'sesskey' => sesskey()]));
  41  }
  42  
  43  // Get submitted parameters.
  44  $attemptid = required_param('attempt', PARAM_INT);
  45  $page = optional_param('page', 0, PARAM_INT);
  46  $cmid = optional_param('cmid', null, PARAM_INT);
  47  
  48  $attemptobj = quiz_create_attempt_handling_errors($attemptid, $cmid);
  49  $page = $attemptobj->force_page_number_into_range($page);
  50  $PAGE->set_url($attemptobj->attempt_url(null, $page));
  51  // During quiz attempts, the browser back/forwards buttons should force a reload.
  52  $PAGE->set_cacheable(false);
  53  
  54  $PAGE->set_secondary_active_tab("modulepage");
  55  
  56  // Check login.
  57  require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
  58  
  59  // Check that this attempt belongs to this user.
  60  if ($attemptobj->get_userid() != $USER->id) {
  61      if ($attemptobj->has_capability('mod/quiz:viewreports')) {
  62          redirect($attemptobj->review_url(null, $page));
  63      } else {
  64          throw new moodle_exception('notyourattempt', 'quiz', $attemptobj->view_url());
  65      }
  66  }
  67  
  68  // Check capabilities and block settings.
  69  if (!$attemptobj->is_preview_user()) {
  70      $attemptobj->require_capability('mod/quiz:attempt');
  71      if (empty($attemptobj->get_quiz()->showblocks)) {
  72          $PAGE->blocks->show_only_fake_blocks();
  73      }
  74  
  75  } else {
  76      navigation_node::override_active_url($attemptobj->start_attempt_url());
  77  }
  78  
  79  // If the attempt is already closed, send them to the review page.
  80  if ($attemptobj->is_finished()) {
  81      redirect($attemptobj->review_url(null, $page));
  82  } else if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
  83      redirect($attemptobj->summary_url());
  84  }
  85  
  86  // Check the access rules.
  87  $accessmanager = $attemptobj->get_access_manager(time());
  88  $accessmanager->setup_attempt_page($PAGE);
  89  /** @var renderer $output */
  90  $output = $PAGE->get_renderer('mod_quiz');
  91  $messages = $accessmanager->prevent_access();
  92  if (!$attemptobj->is_preview_user() && $messages) {
  93      throw new \moodle_exception('attempterror', 'quiz', $attemptobj->view_url(),
  94              $output->access_messages($messages));
  95  }
  96  if ($accessmanager->is_preflight_check_required($attemptobj->get_attemptid())) {
  97      redirect($attemptobj->start_attempt_url(null, $page));
  98  }
  99  
 100  // Set up auto-save if required.
 101  $autosaveperiod = get_config('quiz', 'autosaveperiod');
 102  if ($autosaveperiod) {
 103      $PAGE->requires->yui_module('moodle-mod_quiz-autosave',
 104              'M.mod_quiz.autosave.init', [$autosaveperiod]);
 105  }
 106  
 107  // Log this page view.
 108  $attemptobj->fire_attempt_viewed_event();
 109  
 110  // Get the list of questions needed by this page.
 111  $slots = $attemptobj->get_slots($page);
 112  
 113  // Check.
 114  if (empty($slots)) {
 115      throw new moodle_exception('noquestionsfound', 'quiz', $attemptobj->view_url());
 116  }
 117  
 118  // Update attempt page, redirecting the user if $page is not valid.
 119  if (!$attemptobj->set_currentpage($page)) {
 120      redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
 121  }
 122  
 123  // Initialise the JavaScript.
 124  $headtags = $attemptobj->get_html_head_contributions($page);
 125  $PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
 126  \core\session\manager::keepalive(); // Try to prevent sessions expiring during quiz attempts.
 127  
 128  // Arrange for the navigation to be displayed in the first region on the page.
 129  $navbc = $attemptobj->get_navigation_panel($output, navigation_panel_attempt::class, $page);
 130  $regions = $PAGE->blocks->get_regions();
 131  $PAGE->blocks->add_fake_block($navbc, reset($regions));
 132  
 133  $headtags = $attemptobj->get_html_head_contributions($page);
 134  $PAGE->set_title($attemptobj->attempt_page_title($page));
 135  $PAGE->add_body_class('limitedwidth');
 136  $PAGE->set_heading($attemptobj->get_course()->fullname);
 137  $PAGE->activityheader->disable();
 138  if ($attemptobj->is_last_page($page)) {
 139      $nextpage = -1;
 140  } else {
 141      $nextpage = $page + 1;
 142  }
 143  
 144  echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);