Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/mod/quiz/ -> view.php (source)

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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 page is the entry page into the quiz UI. Displays information about the
  19   * quiz to students and teachers, and lets students see their previous attempts.
  20   *
  21   * @package   mod_quiz
  22   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  require_once(__DIR__ . '/../../config.php');
  28  require_once($CFG->libdir.'/gradelib.php');
  29  require_once($CFG->dirroot.'/mod/quiz/locallib.php');
  30  require_once($CFG->libdir . '/completionlib.php');
  31  require_once($CFG->dirroot . '/course/format/lib.php');
  32  
  33  $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or ...
  34  $q = optional_param('q',  0, PARAM_INT);  // Quiz ID.
  35  
  36  if ($id) {
  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  } else {
  44      if (!$quiz = $DB->get_record('quiz', array('id' => $q))) {
  45          print_error('invalidquizid', 'quiz');
  46      }
  47      if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
  48          print_error('invalidcourseid');
  49      }
  50      if (!$cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
  51          print_error('invalidcoursemodule');
  52      }
  53  }
  54  
  55  // Check login and get context.
  56  require_login($course, false, $cm);
  57  $context = context_module::instance($cm->id);
  58  require_capability('mod/quiz:view', $context);
  59  
  60  // Cache some other capabilities we use several times.
  61  $canattempt = has_capability('mod/quiz:attempt', $context);
  62  $canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
  63  $canpreview = has_capability('mod/quiz:preview', $context);
  64  
  65  // Create an object to manage all the other (non-roles) access rules.
  66  $timenow = time();
  67  $quizobj = quiz::create($cm->instance, $USER->id);
  68  $accessmanager = new quiz_access_manager($quizobj, $timenow,
  69          has_capability('mod/quiz:ignoretimelimits', $context, null, false));
  70  $quiz = $quizobj->get_quiz();
  71  
  72  // Trigger course_module_viewed event and completion.
  73  quiz_view($quiz, $course, $cm, $context);
  74  
  75  // Initialize $PAGE, compute blocks.
  76  $PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id));
  77  
  78  // Create view object which collects all the information the renderer will need.
  79  $viewobj = new mod_quiz_view_object();
  80  $viewobj->accessmanager = $accessmanager;
  81  $viewobj->canreviewmine = $canreviewmine || $canpreview;
  82  
  83  // Get this user's attempts.
  84  $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'finished', true);
  85  $lastfinishedattempt = end($attempts);
  86  $unfinished = false;
  87  $unfinishedattemptid = null;
  88  if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
  89      $attempts[] = $unfinishedattempt;
  90  
  91      // If the attempt is now overdue, deal with that - and pass isonline = false.
  92      // We want the student notified in this case.
  93      $quizobj->create_attempt_object($unfinishedattempt)->handle_if_time_expired(time(), false);
  94  
  95      $unfinished = $unfinishedattempt->state == quiz_attempt::IN_PROGRESS ||
  96              $unfinishedattempt->state == quiz_attempt::OVERDUE;
  97      if (!$unfinished) {
  98          $lastfinishedattempt = $unfinishedattempt;
  99      }
 100      $unfinishedattemptid = $unfinishedattempt->id;
 101      $unfinishedattempt = null; // To make it clear we do not use this again.
 102  }
 103  $numattempts = count($attempts);
 104  
 105  $viewobj->attempts = $attempts;
 106  $viewobj->attemptobjs = array();
 107  foreach ($attempts as $attempt) {
 108      $viewobj->attemptobjs[] = new quiz_attempt($attempt, $quiz, $cm, $course, false);
 109  }
 110  
 111  // Work out the final grade, checking whether it was overridden in the gradebook.
 112  if (!$canpreview) {
 113      $mygrade = quiz_get_best_grade($quiz, $USER->id);
 114  } else if ($lastfinishedattempt) {
 115      // Users who can preview the quiz don't get a proper grade, so work out a
 116      // plausible value to display instead, so the page looks right.
 117      $mygrade = quiz_rescale_grade($lastfinishedattempt->sumgrades, $quiz, false);
 118  } else {
 119      $mygrade = null;
 120  }
 121  
 122  $mygradeoverridden = false;
 123  $gradebookfeedback = '';
 124  
 125  $item = null;
 126  
 127  $grading_info = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $USER->id);
 128  if (!empty($grading_info->items)) {
 129      $item = $grading_info->items[0];
 130      if (isset($item->grades[$USER->id])) {
 131          $grade = $item->grades[$USER->id];
 132  
 133          if ($grade->overridden) {
 134              $mygrade = $grade->grade + 0; // Convert to number.
 135              $mygradeoverridden = true;
 136          }
 137          if (!empty($grade->str_feedback)) {
 138              $gradebookfeedback = $grade->str_feedback;
 139          }
 140      }
 141  }
 142  
 143  $title = $course->shortname . ': ' . format_string($quiz->name);
 144  $PAGE->set_title($title);
 145  $PAGE->set_heading($course->fullname);
 146  $output = $PAGE->get_renderer('mod_quiz');
 147  
 148  // Print table with existing attempts.
 149  if ($attempts) {
 150      // Work out which columns we need, taking account what data is available in each attempt.
 151      list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts);
 152  
 153      $viewobj->attemptcolumn  = $quiz->attempts != 1;
 154  
 155      $viewobj->gradecolumn    = $someoptions->marks >= question_display_options::MARK_AND_MAX &&
 156              quiz_has_grades($quiz);
 157      $viewobj->markcolumn     = $viewobj->gradecolumn && ($quiz->grade != $quiz->sumgrades);
 158      $viewobj->overallstats   = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX;
 159  
 160      $viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback;
 161  }
 162  
 163  $viewobj->timenow = $timenow;
 164  $viewobj->numattempts = $numattempts;
 165  $viewobj->mygrade = $mygrade;
 166  $viewobj->moreattempts = $unfinished ||
 167          !$accessmanager->is_finished($numattempts, $lastfinishedattempt);
 168  $viewobj->mygradeoverridden = $mygradeoverridden;
 169  $viewobj->gradebookfeedback = $gradebookfeedback;
 170  $viewobj->lastfinishedattempt = $lastfinishedattempt;
 171  $viewobj->canedit = has_capability('mod/quiz:manage', $context);
 172  $viewobj->editurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cm->id));
 173  $viewobj->backtocourseurl = new moodle_url('/course/view.php', array('id' => $course->id));
 174  $viewobj->startattempturl = $quizobj->start_attempt_url();
 175  
 176  if ($accessmanager->is_preflight_check_required($unfinishedattemptid)) {
 177      $viewobj->preflightcheckform = $accessmanager->get_preflight_check_form(
 178              $viewobj->startattempturl, $unfinishedattemptid);
 179  }
 180  $viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup();
 181  $viewobj->popupoptions = $accessmanager->get_popup_options();
 182  
 183  // Display information about this quiz.
 184  $viewobj->infomessages = $viewobj->accessmanager->describe_rules();
 185  if ($quiz->attempts != 1) {
 186      $viewobj->infomessages[] = get_string('gradingmethod', 'quiz',
 187              quiz_get_grading_option_name($quiz->grademethod));
 188  }
 189  
 190  // Inform user of the grade to pass if non-zero.
 191  if ($item && grade_floats_different($item->gradepass, 0)) {
 192      $a = new stdClass();
 193      $a->grade = quiz_format_grade($quiz, $item->gradepass);
 194      $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
 195      $viewobj->infomessages[] = get_string('gradetopassoutof', 'quiz', $a);
 196  }
 197  
 198  // Determine wheter a start attempt button should be displayed.
 199  $viewobj->quizhasquestions = $quizobj->has_questions();
 200  $viewobj->preventmessages = array();
 201  if (!$viewobj->quizhasquestions) {
 202      $viewobj->buttontext = '';
 203  
 204  } else {
 205      if ($unfinished) {
 206          if ($canattempt) {
 207              $viewobj->buttontext = get_string('continueattemptquiz', 'quiz');
 208          } else if ($canpreview) {
 209              $viewobj->buttontext = get_string('continuepreview', 'quiz');
 210          }
 211  
 212      } else {
 213          if ($canattempt) {
 214              $viewobj->preventmessages = $viewobj->accessmanager->prevent_new_attempt(
 215                      $viewobj->numattempts, $viewobj->lastfinishedattempt);
 216              if ($viewobj->preventmessages) {
 217                  $viewobj->buttontext = '';
 218              } else if ($viewobj->numattempts == 0) {
 219                  $viewobj->buttontext = get_string('attemptquiznow', 'quiz');
 220              } else {
 221                  $viewobj->buttontext = get_string('reattemptquiz', 'quiz');
 222              }
 223  
 224          } else if ($canpreview) {
 225              $viewobj->buttontext = get_string('previewquiznow', 'quiz');
 226          }
 227      }
 228  
 229      // If, so far, we think a button should be printed, so check if they will be
 230      // allowed to access it.
 231      if ($viewobj->buttontext) {
 232          if (!$viewobj->moreattempts) {
 233              $viewobj->buttontext = '';
 234          } else if ($canattempt
 235                  && $viewobj->preventmessages = $viewobj->accessmanager->prevent_access()) {
 236              $viewobj->buttontext = '';
 237          }
 238      }
 239  }
 240  
 241  $viewobj->showbacktocourse = ($viewobj->buttontext === '' &&
 242          course_get_format($course)->has_view_page());
 243  
 244  echo $OUTPUT->header();
 245  
 246  if (isguestuser()) {
 247      // Guests can't do a quiz, so offer them a choice of logging in or going back.
 248      echo $output->view_page_guest($course, $quiz, $cm, $context, $viewobj->infomessages);
 249  } else if (!isguestuser() && !($canattempt || $canpreview
 250            || $viewobj->canreviewmine)) {
 251      // If they are not enrolled in this course in a good enough role, tell them to enrol.
 252      echo $output->view_page_notenrolled($course, $quiz, $cm, $context, $viewobj->infomessages);
 253  } else {
 254      echo $output->view_page($course, $quiz, $cm, $context, $viewobj);
 255  }
 256  
 257  echo $OUTPUT->footer();