Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/mod/quiz/ -> view.php (source)

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

   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  use mod_quiz\access_manager;
  27  use mod_quiz\output\renderer;
  28  use mod_quiz\output\view_page;
  29  use mod_quiz\quiz_attempt;
  30  use mod_quiz\quiz_settings;
  31  
  32  require_once(__DIR__ . '/../../config.php');
  33  require_once($CFG->libdir.'/gradelib.php');
  34  require_once($CFG->dirroot.'/mod/quiz/locallib.php');
  35  require_once($CFG->libdir . '/completionlib.php');
  36  require_once($CFG->dirroot . '/course/format/lib.php');
  37  
  38  $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or ...
  39  $q = optional_param('q',  0, PARAM_INT);  // Quiz ID.
  40  
  41  if ($id) {
  42      $quizobj = quiz_settings::create_for_cmid($id, $USER->id);
  43  } else {
  44      $quizobj = quiz_settings::create($q, $USER->id);
  45  }
  46  $quiz = $quizobj->get_quiz();
  47  $cm = $quizobj->get_cm();
  48  $course = $quizobj->get_course();
  49  
  50  // Check login and get context.
  51  require_login($course, false, $cm);
  52  $context = $quizobj->get_context();
  53  require_capability('mod/quiz:view', $context);
  54  
  55  // Cache some other capabilities we use several times.
  56  $canattempt = has_capability('mod/quiz:attempt', $context);
  57  $canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
  58  $canpreview = has_capability('mod/quiz:preview', $context);
  59  
  60  // Create an object to manage all the other (non-roles) access rules.
  61  $timenow = time();
  62  $accessmanager = new access_manager($quizobj, $timenow,
  63          has_capability('mod/quiz:ignoretimelimits', $context, null, false));
  64  
  65  // Trigger course_module_viewed event and completion.
  66  quiz_view($quiz, $course, $cm, $context);
  67  
  68  // Initialize $PAGE, compute blocks.
  69  $PAGE->set_url('/mod/quiz/view.php', ['id' => $cm->id]);
  70  
  71  // Create view object which collects all the information the renderer will need.
  72  $viewobj = new view_page();
  73  $viewobj->accessmanager = $accessmanager;
  74  $viewobj->canreviewmine = $canreviewmine || $canpreview;
  75  
  76  // Get this user's attempts.
  77  $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'finished', true);
  78  $lastfinishedattempt = end($attempts);
  79  $unfinished = false;
  80  $unfinishedattemptid = null;
  81  if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
  82      $attempts[] = $unfinishedattempt;
  83  
  84      // If the attempt is now overdue, deal with that - and pass isonline = false.
  85      // We want the student notified in this case.
  86      $quizobj->create_attempt_object($unfinishedattempt)->handle_if_time_expired(time(), false);
  87  
  88      $unfinished = $unfinishedattempt->state == quiz_attempt::IN_PROGRESS ||
  89              $unfinishedattempt->state == quiz_attempt::OVERDUE;
  90      if (!$unfinished) {
  91          $lastfinishedattempt = $unfinishedattempt;
  92      }
  93      $unfinishedattemptid = $unfinishedattempt->id;
  94      $unfinishedattempt = null; // To make it clear we do not use this again.
  95  }
  96  $numattempts = count($attempts);
  97  
  98  $viewobj->attempts = $attempts;
  99  $viewobj->attemptobjs = [];
 100  foreach ($attempts as $attempt) {
 101      $viewobj->attemptobjs[] = new quiz_attempt($attempt, $quiz, $cm, $course, false);
 102  }
 103  
 104  // Work out the final grade, checking whether it was overridden in the gradebook.
 105  if (!$canpreview) {
 106      $mygrade = quiz_get_best_grade($quiz, $USER->id);
 107  } else if ($lastfinishedattempt) {
 108      // Users who can preview the quiz don't get a proper grade, so work out a
 109      // plausible value to display instead, so the page looks right.
 110      $mygrade = quiz_rescale_grade($lastfinishedattempt->sumgrades, $quiz, false);
 111  } else {
 112      $mygrade = null;
 113  }
 114  
 115  $mygradeoverridden = false;
 116  $gradebookfeedback = '';
 117  
 118  $item = null;
 119  
 120  $gradinginfo = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $USER->id);
 121  if (!empty($gradinginfo->items)) {
 122      $item = $gradinginfo->items[0];
 123      if (isset($item->grades[$USER->id])) {
 124          $grade = $item->grades[$USER->id];
 125  
 126          if ($grade->overridden) {
 127              $mygrade = $grade->grade + 0; // Convert to number.
 128              $mygradeoverridden = true;
 129          }
 130          if (!empty($grade->str_feedback)) {
 131              $gradebookfeedback = $grade->str_feedback;
 132          }
 133      }
 134  }
 135  
 136  $title = $course->shortname . ': ' . format_string($quiz->name);
 137  $PAGE->set_title($title);
 138  $PAGE->set_heading($course->fullname);
 139  if (html_is_blank($quiz->intro)) {
 140      $PAGE->activityheader->set_description('');
 141  }
 142  $PAGE->add_body_class('limitedwidth');
 143  /** @var renderer $output */
 144  $output = $PAGE->get_renderer('mod_quiz');
 145  
 146  // Print table with existing attempts.
 147  if ($attempts) {
 148      // Work out which columns we need, taking account what data is available in each attempt.
 149      list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts);
 150  
 151      $viewobj->attemptcolumn  = $quiz->attempts != 1;
 152  
 153      $viewobj->gradecolumn    = $someoptions->marks >= question_display_options::MARK_AND_MAX &&
 154              quiz_has_grades($quiz);
 155      $viewobj->markcolumn     = $viewobj->gradecolumn && ($quiz->grade != $quiz->sumgrades);
 156      $viewobj->overallstats   = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX;
 157  
 158      $viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback;
 159  }
 160  
 161  $viewobj->timenow = $timenow;
 162  $viewobj->numattempts = $numattempts;
 163  $viewobj->mygrade = $mygrade;
 164  $viewobj->moreattempts = $unfinished ||
 165          !$accessmanager->is_finished($numattempts, $lastfinishedattempt);
 166  $viewobj->mygradeoverridden = $mygradeoverridden;
 167  $viewobj->gradebookfeedback = $gradebookfeedback;
 168  $viewobj->lastfinishedattempt = $lastfinishedattempt;
 169  $viewobj->canedit = has_capability('mod/quiz:manage', $context);
 170  $viewobj->editurl = new moodle_url('/mod/quiz/edit.php', ['cmid' => $cm->id]);
 171  $viewobj->backtocourseurl = new moodle_url('/course/view.php', ['id' => $course->id]);
 172  $viewobj->startattempturl = $quizobj->start_attempt_url();
 173  
 174  if ($accessmanager->is_preflight_check_required($unfinishedattemptid)) {
 175      $viewobj->preflightcheckform = $accessmanager->get_preflight_check_form(
 176              $viewobj->startattempturl, $unfinishedattemptid);
 177  }
 178  $viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup();
 179  $viewobj->popupoptions = $accessmanager->get_popup_options();
 180  
 181  // Display information about this quiz.
 182  $viewobj->infomessages = $viewobj->accessmanager->describe_rules();
 183  if ($quiz->attempts != 1) {
 184      $viewobj->infomessages[] = get_string('gradingmethod', 'quiz',
 185              quiz_get_grading_option_name($quiz->grademethod));
 186  }
 187  
 188  // Inform user of the grade to pass if non-zero.
 189  if ($item && grade_floats_different($item->gradepass, 0)) {
 190      $a = new stdClass();
 191      $a->grade = quiz_format_grade($quiz, $item->gradepass);
 192      $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
 193      $viewobj->infomessages[] = get_string('gradetopassoutof', 'quiz', $a);
 194  }
 195  
 196  // Determine whether a start attempt button should be displayed.
 197  $viewobj->quizhasquestions = $quizobj->has_questions();
 198  $viewobj->preventmessages = [];
 199  if (!$viewobj->quizhasquestions) {
 200      $viewobj->buttontext = '';
 201  
 202  } else {
 203      if ($unfinished) {
 204          if ($canpreview) {
 205              $viewobj->buttontext = get_string('continuepreview', 'quiz');
 206          } else if ($canattempt) {
 207              $viewobj->buttontext = get_string('continueattemptquiz', 'quiz');
 208          }
 209      } else {
 210          if ($canpreview) {
 211              $viewobj->buttontext = get_string('previewquizstart', 'quiz');
 212          } else if ($canattempt) {
 213              $viewobj->preventmessages = $viewobj->accessmanager->prevent_new_attempt(
 214                      $viewobj->numattempts, $viewobj->lastfinishedattempt);
 215              if ($viewobj->preventmessages) {
 216                  $viewobj->buttontext = '';
 217              } else if ($viewobj->numattempts == 0) {
 218                  $viewobj->buttontext = get_string('attemptquiz', 'quiz');
 219              } else {
 220                  $viewobj->buttontext = get_string('reattemptquiz', 'quiz');
 221              }
 222          }
 223      }
 224  
 225      // Users who can preview the quiz should be able to see all messages for not being able to access the quiz.
 226      if ($canpreview) {
 227          $viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
 228      } else if ($viewobj->buttontext) {
 229          // If, so far, we think a button should be printed, so check if they will be allowed to access it.
 230          if (!$viewobj->moreattempts) {
 231              $viewobj->buttontext = '';
 232          } else if ($canattempt) {
 233              $viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
 234              if ($viewobj->preventmessages) {
 235                  $viewobj->buttontext = '';
 236              }
 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 (!empty($gradinginfo->errors)) {
 247      foreach ($gradinginfo->errors as $error) {
 248          $errortext = new \core\output\notification($error, \core\output\notification::NOTIFY_ERROR);
 249          echo $OUTPUT->render($errortext);
 250      }
 251  }
 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();