[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/quiz/ -> startattempt.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 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(dirname(__FILE__) . '/../../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      print_error('invalidcoursemodule');
  39  }
  40  if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
  41      print_error("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  
  48  // Check login and sesskey.
  49  require_login($quizobj->get_course(), false, $quizobj->get_cm());
  50  require_sesskey();
  51  $PAGE->set_heading($quizobj->get_course()->fullname);
  52  
  53  // If no questions have been set up yet redirect to edit.php or display an error.
  54  if (!$quizobj->has_questions()) {
  55      if ($quizobj->has_capability('mod/quiz:manage')) {
  56          redirect($quizobj->edit_url());
  57      } else {
  58          print_error('cannotstartnoquestions', 'quiz', $quizobj->view_url());
  59      }
  60  }
  61  
  62  // Create an object to manage all the other (non-roles) access rules.
  63  $timenow = time();
  64  $accessmanager = $quizobj->get_access_manager($timenow);
  65  if ($quizobj->is_preview_user() && $forcenew) {
  66      $accessmanager->current_attempt_finished();
  67  }
  68  
  69  // Check capabilities.
  70  if (!$quizobj->is_preview_user()) {
  71      $quizobj->require_capability('mod/quiz:attempt');
  72  }
  73  
  74  // Check to see if a new preview was requested.
  75  if ($quizobj->is_preview_user() && $forcenew) {
  76      // To force the creation of a new preview, we mark the current attempt (if any)
  77      // as finished. It will then automatically be deleted below.
  78      $DB->set_field('quiz_attempts', 'state', quiz_attempt::FINISHED,
  79              array('quiz' => $quizobj->get_quizid(), 'userid' => $USER->id));
  80  }
  81  
  82  // Look for an existing attempt.
  83  $attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id, 'all', true);
  84  $lastattempt = end($attempts);
  85  
  86  // If an in-progress attempt exists, check password then redirect to it.
  87  if ($lastattempt && ($lastattempt->state == quiz_attempt::IN_PROGRESS ||
  88          $lastattempt->state == quiz_attempt::OVERDUE)) {
  89      $currentattemptid = $lastattempt->id;
  90      $messages = $accessmanager->prevent_access();
  91  
  92      // If the attempt is now overdue, deal with that.
  93      $quizobj->create_attempt_object($lastattempt)->handle_if_time_expired($timenow, true);
  94  
  95      // And, if the attempt is now no longer in progress, redirect to the appropriate place.
  96      if ($lastattempt->state == quiz_attempt::ABANDONED || $lastattempt->state == quiz_attempt::FINISHED) {
  97          redirect($quizobj->review_url($lastattempt->id));
  98      }
  99  
 100      // If the page number was not explicitly in the URL, go to the current page.
 101      if ($page == -1) {
 102          $page = $lastattempt->currentpage;
 103      }
 104  
 105  } else {
 106      while ($lastattempt && $lastattempt->preview) {
 107          $lastattempt = array_pop($attempts);
 108      }
 109  
 110      // Get number for the next or unfinished attempt.
 111      if ($lastattempt) {
 112          $attemptnumber = $lastattempt->attempt + 1;
 113      } else {
 114          $lastattempt = false;
 115          $attemptnumber = 1;
 116      }
 117      $currentattemptid = null;
 118  
 119      $messages = $accessmanager->prevent_access() +
 120              $accessmanager->prevent_new_attempt(count($attempts), $lastattempt);
 121  
 122      if ($page == -1) {
 123          $page = 0;
 124      }
 125  }
 126  
 127  // Check access.
 128  $output = $PAGE->get_renderer('mod_quiz');
 129  if (!$quizobj->is_preview_user() && $messages) {
 130      print_error('attempterror', 'quiz', $quizobj->view_url(),
 131              $output->access_messages($messages));
 132  }
 133  
 134  if ($accessmanager->is_preflight_check_required($currentattemptid)) {
 135      // Need to do some checks before allowing the user to continue.
 136      $mform = $accessmanager->get_preflight_check_form(
 137              $quizobj->start_attempt_url($page), $currentattemptid);
 138  
 139      if ($mform->is_cancelled()) {
 140          $accessmanager->back_to_view_page($output);
 141  
 142      } else if (!$mform->get_data()) {
 143  
 144          // Form not submitted successfully, re-display it and stop.
 145          $PAGE->set_url($quizobj->start_attempt_url($page));
 146          $PAGE->set_title($quizobj->get_quiz_name());
 147          $accessmanager->setup_attempt_page($PAGE);
 148          if (empty($quizobj->get_quiz()->showblocks)) {
 149              $PAGE->blocks->show_only_fake_blocks();
 150          }
 151  
 152          echo $output->start_attempt_page($quizobj, $mform);
 153          die();
 154      }
 155  
 156      // Pre-flight check passed.
 157      $accessmanager->notify_preflight_check_passed($currentattemptid);
 158  }
 159  if ($currentattemptid) {
 160      if ($lastattempt->state == quiz_attempt::OVERDUE) {
 161          redirect($quizobj->summary_url($lastattempt->id));
 162      } else {
 163          redirect($quizobj->attempt_url($currentattemptid, $page));
 164      }
 165  }
 166  
 167  // Delete any previous preview attempts belonging to this user.
 168  quiz_delete_previews($quizobj->get_quiz(), $USER->id);
 169  
 170  $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 171  $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 172  
 173  // Create the new attempt and initialize the question sessions
 174  $timenow = time(); // Update time now, in case the server is running really slowly.
 175  $attempt = quiz_create_attempt($quizobj, $attemptnumber, $lastattempt, $timenow, $quizobj->is_preview_user());
 176  
 177  if (!($quizobj->get_quiz()->attemptonlast && $lastattempt)) {
 178      $attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow);
 179  } else {
 180      $attempt = quiz_start_attempt_built_on_last($quba, $attempt, $lastattempt);
 181  }
 182  
 183  $transaction = $DB->start_delegated_transaction();
 184  
 185  $attempt = quiz_attempt_save_started($quizobj, $quba, $attempt);
 186  
 187  $transaction->allow_commit();
 188  
 189  // Redirect to the attempt page.
 190  redirect($quizobj->attempt_url($attempt->id, $page));


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1