[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/ -> attempt.php (source)

   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          print_error('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  
  43  $attemptobj = quiz_attempt::create($attemptid);
  44  $page = $attemptobj->force_page_number_into_range($page);
  45  $PAGE->set_url($attemptobj->attempt_url(null, $page));
  46  
  47  // Check login.
  48  require_login($attemptobj->get_course(), false, $attemptobj->get_cm());
  49  
  50  // Check that this attempt belongs to this user.
  51  if ($attemptobj->get_userid() != $USER->id) {
  52      if ($attemptobj->has_capability('mod/quiz:viewreports')) {
  53          redirect($attemptobj->review_url(null, $page));
  54      } else {
  55          throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'notyourattempt');
  56      }
  57  }
  58  
  59  // Check capabilities and block settings.
  60  if (!$attemptobj->is_preview_user()) {
  61      $attemptobj->require_capability('mod/quiz:attempt');
  62      if (empty($attemptobj->get_quiz()->showblocks)) {
  63          $PAGE->blocks->show_only_fake_blocks();
  64      }
  65  
  66  } else {
  67      navigation_node::override_active_url($attemptobj->start_attempt_url());
  68  }
  69  
  70  // If the attempt is already closed, send them to the review page.
  71  if ($attemptobj->is_finished()) {
  72      redirect($attemptobj->review_url(null, $page));
  73  } else if ($attemptobj->get_state() == quiz_attempt::OVERDUE) {
  74      redirect($attemptobj->summary_url());
  75  }
  76  
  77  // Check the access rules.
  78  $accessmanager = $attemptobj->get_access_manager(time());
  79  $accessmanager->setup_attempt_page($PAGE);
  80  $output = $PAGE->get_renderer('mod_quiz');
  81  $messages = $accessmanager->prevent_access();
  82  if (!$attemptobj->is_preview_user() && $messages) {
  83      print_error('attempterror', 'quiz', $attemptobj->view_url(),
  84              $output->access_messages($messages));
  85  }
  86  if ($accessmanager->is_preflight_check_required($attemptobj->get_attemptid())) {
  87      redirect($attemptobj->start_attempt_url(null, $page));
  88  }
  89  
  90  // Set up auto-save if required.
  91  $autosaveperiod = get_config('quiz', 'autosaveperiod');
  92  if ($autosaveperiod) {
  93      $PAGE->requires->yui_module('moodle-mod_quiz-autosave',
  94              'M.mod_quiz.autosave.init', array($autosaveperiod));
  95  }
  96  
  97  // Log this page view.
  98  $attemptobj->fire_attempt_viewed_event();
  99  
 100  // Get the list of questions needed by this page.
 101  $slots = $attemptobj->get_slots($page);
 102  
 103  // Check.
 104  if (empty($slots)) {
 105      throw new moodle_quiz_exception($attemptobj->get_quizobj(), 'noquestionsfound');
 106  }
 107  
 108  // Update attempt page, redirecting the user if $page is not valid.
 109  if (!$attemptobj->set_currentpage($page)) {
 110      redirect($attemptobj->start_attempt_url(null, $attemptobj->get_currentpage()));
 111  }
 112  
 113  // Initialise the JavaScript.
 114  $headtags = $attemptobj->get_html_head_contributions($page);
 115  $PAGE->requires->js_init_call('M.mod_quiz.init_attempt_form', null, false, quiz_get_js_module());
 116  
 117  // Arrange for the navigation to be displayed in the first region on the page.
 118  $navbc = $attemptobj->get_navigation_panel($output, 'quiz_attempt_nav_panel', $page);
 119  $regions = $PAGE->blocks->get_regions();
 120  $PAGE->blocks->add_fake_block($navbc, reset($regions));
 121  
 122  $title = get_string('attempt', 'quiz', $attemptobj->get_attempt_number());
 123  $headtags = $attemptobj->get_html_head_contributions($page);
 124  $PAGE->set_title($attemptobj->get_quiz_name());
 125  $PAGE->set_heading($attemptobj->get_course()->fullname);
 126  
 127  if ($attemptobj->is_last_page($page)) {
 128      $nextpage = -1;
 129  } else {
 130      $nextpage = $page + 1;
 131  }
 132  
 133  echo $output->attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id, $nextpage);


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1