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.
/mod/quiz/ -> view.php (source)

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 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          throw new \moodle_exception('invalidcoursemodule');
  39      }
  40      if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
  41          throw new \moodle_exception('coursemisconf');
  42      }
  43  } else {
  44      if (!$quiz = $DB->get_record('quiz', array('id' => $q))) {
  45          throw new \moodle_exception('invalidquizid', 'quiz');
  46      }
  47      if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
  48          throw new \moodle_exception('invalidcourseid');
  49      }
  50      if (!$cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
  51          throw new \moodle_exception('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  if (html_is_blank($quiz->intro)) {
 147      $PAGE->activityheader->set_description('');
 148  }
 149  $PAGE->add_body_class('limitedwidth');
 150  /** @var mod_quiz_renderer $output */
 151  $output = $PAGE->get_renderer('mod_quiz');
 152  
 153  // Print table with existing attempts.
 154  if ($attempts) {
 155      // Work out which columns we need, taking account what data is available in each attempt.
 156      list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts);
 157  
 158      $viewobj->attemptcolumn  = $quiz->attempts != 1;
 159  
 160      $viewobj->gradecolumn    = $someoptions->marks >= question_display_options::MARK_AND_MAX &&
 161              quiz_has_grades($quiz);
 162      $viewobj->markcolumn     = $viewobj->gradecolumn && ($quiz->grade != $quiz->sumgrades);
 163      $viewobj->overallstats   = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX;
 164  
 165      $viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback;
 166  }
 167  
 168  $viewobj->timenow = $timenow;
 169  $viewobj->numattempts = $numattempts;
 170  $viewobj->mygrade = $mygrade;
 171  $viewobj->moreattempts = $unfinished ||
 172          !$accessmanager->is_finished($numattempts, $lastfinishedattempt);
 173  $viewobj->mygradeoverridden = $mygradeoverridden;
 174  $viewobj->gradebookfeedback = $gradebookfeedback;
 175  $viewobj->lastfinishedattempt = $lastfinishedattempt;
 176  $viewobj->canedit = has_capability('mod/quiz:manage', $context);
 177  $viewobj->editurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cm->id));
 178  $viewobj->backtocourseurl = new moodle_url('/course/view.php', array('id' => $course->id));
 179  $viewobj->startattempturl = $quizobj->start_attempt_url();
 180  
 181  if ($accessmanager->is_preflight_check_required($unfinishedattemptid)) {
 182      $viewobj->preflightcheckform = $accessmanager->get_preflight_check_form(
 183              $viewobj->startattempturl, $unfinishedattemptid);
 184  }
 185  $viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup();
 186  $viewobj->popupoptions = $accessmanager->get_popup_options();
 187  
 188  // Display information about this quiz.
 189  $viewobj->infomessages = $viewobj->accessmanager->describe_rules();
 190  if ($quiz->attempts != 1) {
 191      $viewobj->infomessages[] = get_string('gradingmethod', 'quiz',
 192              quiz_get_grading_option_name($quiz->grademethod));
 193  }
 194  
 195  // Inform user of the grade to pass if non-zero.
 196  if ($item && grade_floats_different($item->gradepass, 0)) {
 197      $a = new stdClass();
 198      $a->grade = quiz_format_grade($quiz, $item->gradepass);
 199      $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
 200      $viewobj->infomessages[] = get_string('gradetopassoutof', 'quiz', $a);
 201  }
 202  
 203  // Determine wheter a start attempt button should be displayed.
 204  $viewobj->quizhasquestions = $quizobj->has_questions();
 205  $viewobj->preventmessages = array();
 206  if (!$viewobj->quizhasquestions) {
 207      $viewobj->buttontext = '';
 208  
 209  } else {
 210      if ($unfinished) {
 211          if ($canpreview) {
 212              $viewobj->buttontext = get_string('continuepreview', 'quiz');
 213          } else if ($canattempt) {
 214              $viewobj->buttontext = get_string('continueattemptquiz', 'quiz');
 215          }
 216      } else {
 217          if ($canpreview) {
 218              $viewobj->buttontext = get_string('previewquizstart', 'quiz');
 219          } else if ($canattempt) {
 220              $viewobj->preventmessages = $viewobj->accessmanager->prevent_new_attempt(
 221                      $viewobj->numattempts, $viewobj->lastfinishedattempt);
 222              if ($viewobj->preventmessages) {
 223                  $viewobj->buttontext = '';
 224              } else if ($viewobj->numattempts == 0) {
 225                  $viewobj->buttontext = get_string('attemptquiz', 'quiz');
 226              } else {
 227                  $viewobj->buttontext = get_string('reattemptquiz', 'quiz');
 228              }
 229          }
 230      }
 231  
 232      // Users who can preview the quiz should be able to see all messages for not being able to access the quiz.
 233      if ($canpreview) {
 234          $viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
 235      } else if ($viewobj->buttontext) {
 236          // If, so far, we think a button should be printed, so check if they will be allowed to access it.
 237          if (!$viewobj->moreattempts) {
 238              $viewobj->buttontext = '';
 239          } else if ($canattempt) {
 240              $viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
 241              if ($viewobj->preventmessages) {
 242                  $viewobj->buttontext = '';
 243              }
 244          }
 245      }
 246  }
 247  
 248  $viewobj->showbacktocourse = ($viewobj->buttontext === '' &&
 249          course_get_format($course)->has_view_page());
 250  
 251  echo $OUTPUT->header();
 252  
 253  if (isguestuser()) {
 254      // Guests can't do a quiz, so offer them a choice of logging in or going back.
 255      echo $output->view_page_guest($course, $quiz, $cm, $context, $viewobj->infomessages, $viewobj);
 256  } else if (!isguestuser() && !($canattempt || $canpreview
 257            || $viewobj->canreviewmine)) {
 258      // If they are not enrolled in this course in a good enough role, tell them to enrol.
 259      echo $output->view_page_notenrolled($course, $quiz, $cm, $context, $viewobj->infomessages, $viewobj);
 260  } else {
 261      echo $output->view_page($course, $quiz, $cm, $context, $viewobj);
 262  }
 263  
 264  echo $OUTPUT->footer();