MDL-76614 quiz: move class quiz_attempt => mod_quiz\quiz_attempt
[moodle.git] / mod / quiz / view.php
blobeb1ac2713641f980d1220bcf63167ee99ff7daf6
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.
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/>.
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.
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
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;
31 require_once(__DIR__ . '/../../config.php');
32 require_once($CFG->libdir.'/gradelib.php');
33 require_once($CFG->dirroot.'/mod/quiz/locallib.php');
34 require_once($CFG->libdir . '/completionlib.php');
35 require_once($CFG->dirroot . '/course/format/lib.php');
37 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or ...
38 $q = optional_param('q', 0, PARAM_INT); // Quiz ID.
40 if ($id) {
41 if (!$cm = get_coursemodule_from_id('quiz', $id)) {
42 throw new \moodle_exception('invalidcoursemodule');
44 if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
45 throw new \moodle_exception('coursemisconf');
47 } else {
48 if (!$quiz = $DB->get_record('quiz', array('id' => $q))) {
49 throw new \moodle_exception('invalidquizid', 'quiz');
51 if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
52 throw new \moodle_exception('invalidcourseid');
54 if (!$cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
55 throw new \moodle_exception('invalidcoursemodule');
59 // Check login and get context.
60 require_login($course, false, $cm);
61 $context = context_module::instance($cm->id);
62 require_capability('mod/quiz:view', $context);
64 // Cache some other capabilities we use several times.
65 $canattempt = has_capability('mod/quiz:attempt', $context);
66 $canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
67 $canpreview = has_capability('mod/quiz:preview', $context);
69 // Create an object to manage all the other (non-roles) access rules.
70 $timenow = time();
71 $quizobj = quiz::create($cm->instance, $USER->id);
72 $accessmanager = new access_manager($quizobj, $timenow,
73 has_capability('mod/quiz:ignoretimelimits', $context, null, false));
74 $quiz = $quizobj->get_quiz();
76 // Trigger course_module_viewed event and completion.
77 quiz_view($quiz, $course, $cm, $context);
79 // Initialize $PAGE, compute blocks.
80 $PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id));
82 // Create view object which collects all the information the renderer will need.
83 $viewobj = new view_page();
84 $viewobj->accessmanager = $accessmanager;
85 $viewobj->canreviewmine = $canreviewmine || $canpreview;
87 // Get this user's attempts.
88 $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'finished', true);
89 $lastfinishedattempt = end($attempts);
90 $unfinished = false;
91 $unfinishedattemptid = null;
92 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
93 $attempts[] = $unfinishedattempt;
95 // If the attempt is now overdue, deal with that - and pass isonline = false.
96 // We want the student notified in this case.
97 $quizobj->create_attempt_object($unfinishedattempt)->handle_if_time_expired(time(), false);
99 $unfinished = $unfinishedattempt->state == quiz_attempt::IN_PROGRESS ||
100 $unfinishedattempt->state == quiz_attempt::OVERDUE;
101 if (!$unfinished) {
102 $lastfinishedattempt = $unfinishedattempt;
104 $unfinishedattemptid = $unfinishedattempt->id;
105 $unfinishedattempt = null; // To make it clear we do not use this again.
107 $numattempts = count($attempts);
109 $viewobj->attempts = $attempts;
110 $viewobj->attemptobjs = array();
111 foreach ($attempts as $attempt) {
112 $viewobj->attemptobjs[] = new quiz_attempt($attempt, $quiz, $cm, $course, false);
115 // Work out the final grade, checking whether it was overridden in the gradebook.
116 if (!$canpreview) {
117 $mygrade = quiz_get_best_grade($quiz, $USER->id);
118 } else if ($lastfinishedattempt) {
119 // Users who can preview the quiz don't get a proper grade, so work out a
120 // plausible value to display instead, so the page looks right.
121 $mygrade = quiz_rescale_grade($lastfinishedattempt->sumgrades, $quiz, false);
122 } else {
123 $mygrade = null;
126 $mygradeoverridden = false;
127 $gradebookfeedback = '';
129 $item = null;
131 $gradinginfo = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $USER->id);
132 if (!empty($gradinginfo->items)) {
133 $item = $gradinginfo->items[0];
134 if (isset($item->grades[$USER->id])) {
135 $grade = $item->grades[$USER->id];
137 if ($grade->overridden) {
138 $mygrade = $grade->grade + 0; // Convert to number.
139 $mygradeoverridden = true;
141 if (!empty($grade->str_feedback)) {
142 $gradebookfeedback = $grade->str_feedback;
147 $title = $course->shortname . ': ' . format_string($quiz->name);
148 $PAGE->set_title($title);
149 $PAGE->set_heading($course->fullname);
150 if (html_is_blank($quiz->intro)) {
151 $PAGE->activityheader->set_description('');
153 $PAGE->add_body_class('limitedwidth');
154 /** @var renderer $output */
155 $output = $PAGE->get_renderer('mod_quiz');
157 // Print table with existing attempts.
158 if ($attempts) {
159 // Work out which columns we need, taking account what data is available in each attempt.
160 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts);
162 $viewobj->attemptcolumn = $quiz->attempts != 1;
164 $viewobj->gradecolumn = $someoptions->marks >= question_display_options::MARK_AND_MAX &&
165 quiz_has_grades($quiz);
166 $viewobj->markcolumn = $viewobj->gradecolumn && ($quiz->grade != $quiz->sumgrades);
167 $viewobj->overallstats = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX;
169 $viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback;
172 $viewobj->timenow = $timenow;
173 $viewobj->numattempts = $numattempts;
174 $viewobj->mygrade = $mygrade;
175 $viewobj->moreattempts = $unfinished ||
176 !$accessmanager->is_finished($numattempts, $lastfinishedattempt);
177 $viewobj->mygradeoverridden = $mygradeoverridden;
178 $viewobj->gradebookfeedback = $gradebookfeedback;
179 $viewobj->lastfinishedattempt = $lastfinishedattempt;
180 $viewobj->canedit = has_capability('mod/quiz:manage', $context);
181 $viewobj->editurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cm->id));
182 $viewobj->backtocourseurl = new moodle_url('/course/view.php', array('id' => $course->id));
183 $viewobj->startattempturl = $quizobj->start_attempt_url();
185 if ($accessmanager->is_preflight_check_required($unfinishedattemptid)) {
186 $viewobj->preflightcheckform = $accessmanager->get_preflight_check_form(
187 $viewobj->startattempturl, $unfinishedattemptid);
189 $viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup();
190 $viewobj->popupoptions = $accessmanager->get_popup_options();
192 // Display information about this quiz.
193 $viewobj->infomessages = $viewobj->accessmanager->describe_rules();
194 if ($quiz->attempts != 1) {
195 $viewobj->infomessages[] = get_string('gradingmethod', 'quiz',
196 quiz_get_grading_option_name($quiz->grademethod));
199 // Inform user of the grade to pass if non-zero.
200 if ($item && grade_floats_different($item->gradepass, 0)) {
201 $a = new stdClass();
202 $a->grade = quiz_format_grade($quiz, $item->gradepass);
203 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
204 $viewobj->infomessages[] = get_string('gradetopassoutof', 'quiz', $a);
207 // Determine wheter a start attempt button should be displayed.
208 $viewobj->quizhasquestions = $quizobj->has_questions();
209 $viewobj->preventmessages = array();
210 if (!$viewobj->quizhasquestions) {
211 $viewobj->buttontext = '';
213 } else {
214 if ($unfinished) {
215 if ($canpreview) {
216 $viewobj->buttontext = get_string('continuepreview', 'quiz');
217 } else if ($canattempt) {
218 $viewobj->buttontext = get_string('continueattemptquiz', 'quiz');
220 } else {
221 if ($canpreview) {
222 $viewobj->buttontext = get_string('previewquizstart', 'quiz');
223 } else if ($canattempt) {
224 $viewobj->preventmessages = $viewobj->accessmanager->prevent_new_attempt(
225 $viewobj->numattempts, $viewobj->lastfinishedattempt);
226 if ($viewobj->preventmessages) {
227 $viewobj->buttontext = '';
228 } else if ($viewobj->numattempts == 0) {
229 $viewobj->buttontext = get_string('attemptquiz', 'quiz');
230 } else {
231 $viewobj->buttontext = get_string('reattemptquiz', 'quiz');
236 // Users who can preview the quiz should be able to see all messages for not being able to access the quiz.
237 if ($canpreview) {
238 $viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
239 } else if ($viewobj->buttontext) {
240 // If, so far, we think a button should be printed, so check if they will be allowed to access it.
241 if (!$viewobj->moreattempts) {
242 $viewobj->buttontext = '';
243 } else if ($canattempt) {
244 $viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
245 if ($viewobj->preventmessages) {
246 $viewobj->buttontext = '';
252 $viewobj->showbacktocourse = ($viewobj->buttontext === '' &&
253 course_get_format($course)->has_view_page());
255 echo $OUTPUT->header();
257 if (!empty($gradinginfo->errors)) {
258 foreach ($gradinginfo->errors as $error) {
259 $errortext = new \core\output\notification($error, \core\output\notification::NOTIFY_ERROR);
260 echo $OUTPUT->render($errortext);
264 if (isguestuser()) {
265 // Guests can't do a quiz, so offer them a choice of logging in or going back.
266 echo $output->view_page_guest($course, $quiz, $cm, $context, $viewobj->infomessages, $viewobj);
267 } else if (!isguestuser() && !($canattempt || $canpreview
268 || $viewobj->canreviewmine)) {
269 // If they are not enrolled in this course in a good enough role, tell them to enrol.
270 echo $output->view_page_notenrolled($course, $quiz, $cm, $context, $viewobj->infomessages, $viewobj);
271 } else {
272 echo $output->view_page($course, $quiz, $cm, $context, $viewobj);
275 echo $OUTPUT->footer();