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