2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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.
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\list_of_attempts
;
28 use mod_quiz\output\renderer
;
29 use mod_quiz\output\view_page
;
30 use mod_quiz\quiz_attempt
;
31 use mod_quiz\quiz_settings
;
33 require_once(__DIR__
. '/../../config.php');
34 require_once($CFG->libdir
.'/gradelib.php');
35 require_once($CFG->dirroot
.'/mod/quiz/locallib.php');
36 require_once($CFG->libdir
. '/completionlib.php');
37 require_once($CFG->dirroot
. '/course/format/lib.php');
39 $id = optional_param('id', 0, PARAM_INT
); // Course Module ID, or ...
40 $q = optional_param('q', 0, PARAM_INT
); // Quiz ID.
43 $quizobj = quiz_settings
::create_for_cmid($id, $USER->id
);
45 $quizobj = quiz_settings
::create($q, $USER->id
);
47 $quiz = $quizobj->get_quiz();
48 $cm = $quizobj->get_cm();
49 $course = $quizobj->get_course();
51 // Check login and get context.
52 require_login($course, false, $cm);
53 $context = $quizobj->get_context();
54 require_capability('mod/quiz:view', $context);
56 // Cache some other capabilities we use several times.
57 $canattempt = has_capability('mod/quiz:attempt', $context);
58 $canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
59 $canpreview = has_capability('mod/quiz:preview', $context);
61 // Create an object to manage all the other (non-roles) access rules.
63 $accessmanager = new access_manager($quizobj, $timenow,
64 has_capability('mod/quiz:ignoretimelimits', $context, null, false));
66 // Trigger course_module_viewed event and completion.
67 quiz_view($quiz, $course, $cm, $context);
69 // Initialize $PAGE, compute blocks.
70 $PAGE->set_url('/mod/quiz/view.php', ['id' => $cm->id
]);
72 // Create view object which collects all the information the renderer will need.
73 $viewobj = new view_page();
74 $viewobj->accessmanager
= $accessmanager;
75 $viewobj->canreviewmine
= $canreviewmine ||
$canpreview;
77 // Get this user's attempts.
78 $attempts = quiz_get_user_attempts($quiz->id
, $USER->id
, 'finished', true);
79 $lastfinishedattempt = end($attempts);
81 $unfinishedattemptid = null;
82 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id
, $USER->id
)) {
83 $attempts[] = $unfinishedattempt;
85 // If the attempt is now overdue, deal with that - and pass isonline = false.
86 // We want the student notified in this case.
87 $quizobj->create_attempt_object($unfinishedattempt)->handle_if_time_expired(time(), false);
89 $unfinished = $unfinishedattempt->state
== quiz_attempt
::IN_PROGRESS ||
90 $unfinishedattempt->state
== quiz_attempt
::OVERDUE
;
92 $lastfinishedattempt = $unfinishedattempt;
94 $unfinishedattemptid = $unfinishedattempt->id
;
95 $unfinishedattempt = null; // To make it clear we do not use this again.
97 $numattempts = count($attempts);
99 $viewobj->attempts
= $attempts;
100 $viewobj->attemptobjs
= [];
101 foreach ($attempts as $attempt) {
102 $viewobj->attemptobjs
[] = new quiz_attempt($attempt, $quiz, $cm, $course, false);
104 $viewobj->attemptslist
= new list_of_attempts($timenow);
105 foreach (array_reverse($viewobj->attemptobjs
) as $attemptobj) {
106 $viewobj->attemptslist
->add_attempt($attemptobj);
109 // Work out the final grade, checking whether it was overridden in the gradebook.
111 $mygrade = quiz_get_best_grade($quiz, $USER->id
);
112 } else if ($lastfinishedattempt) {
113 // Users who can preview the quiz don't get a proper grade, so work out a
114 // plausible value to display instead, so the page looks right.
115 $mygrade = quiz_rescale_grade($lastfinishedattempt->sumgrades
, $quiz, false);
120 $mygradeoverridden = false;
121 $gradebookfeedback = '';
125 $gradinginfo = grade_get_grades($course->id
, 'mod', 'quiz', $quiz->id
, $USER->id
);
126 if (!empty($gradinginfo->items
)) {
127 $item = $gradinginfo->items
[0];
128 if (isset($item->grades
[$USER->id
])) {
129 $grade = $item->grades
[$USER->id
];
131 if ($grade->overridden
) {
132 $mygrade = $grade->grade +
0; // Convert to number.
133 $mygradeoverridden = true;
135 if (!empty($grade->str_feedback
)) {
136 $gradebookfeedback = $grade->str_feedback
;
141 $title = $course->shortname
. ': ' . format_string($quiz->name
);
142 $PAGE->set_title($title);
143 $PAGE->set_heading($course->fullname
);
144 if (html_is_blank($quiz->intro
)) {
145 $PAGE->activityheader
->set_description('');
147 $PAGE->add_body_class('limitedwidth');
148 /** @var renderer $output */
149 $output = $PAGE->get_renderer('mod_quiz');
151 // Print table with existing attempts.
153 // Work out which columns we need, taking account what data is available in each attempt.
154 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts);
156 $viewobj->attemptcolumn
= $quiz->attempts
!= 1;
158 $viewobj->gradecolumn
= $someoptions->marks
>= question_display_options
::MARK_AND_MAX
&&
159 quiz_has_grades($quiz);
160 $viewobj->markcolumn
= $viewobj->gradecolumn
&& ($quiz->grade
!= $quiz->sumgrades
);
161 $viewobj->overallstats
= $lastfinishedattempt && $alloptions->marks
>= question_display_options
::MARK_AND_MAX
;
163 $viewobj->feedbackcolumn
= quiz_has_feedback($quiz) && $alloptions->overallfeedback
;
166 $viewobj->timenow
= $timenow;
167 $viewobj->numattempts
= $numattempts;
168 $viewobj->mygrade
= $mygrade;
169 $viewobj->moreattempts
= $unfinished ||
170 !$accessmanager->is_finished($numattempts, $lastfinishedattempt);
171 $viewobj->mygradeoverridden
= $mygradeoverridden;
172 $viewobj->gradebookfeedback
= $gradebookfeedback;
173 $viewobj->lastfinishedattempt
= $lastfinishedattempt;
174 $viewobj->canedit
= has_capability('mod/quiz:manage', $context);
175 $viewobj->editurl
= new moodle_url('/mod/quiz/edit.php', ['cmid' => $cm->id
]);
176 $viewobj->backtocourseurl
= new moodle_url('/course/view.php', ['id' => $course->id
]);
177 $viewobj->startattempturl
= $quizobj->start_attempt_url();
179 if ($accessmanager->is_preflight_check_required($unfinishedattemptid)) {
180 $viewobj->preflightcheckform
= $accessmanager->get_preflight_check_form(
181 $viewobj->startattempturl
, $unfinishedattemptid);
183 $viewobj->popuprequired
= $accessmanager->attempt_must_be_in_popup();
184 $viewobj->popupoptions
= $accessmanager->get_popup_options();
186 // Display information about this quiz.
187 $viewobj->infomessages
= $viewobj->accessmanager
->describe_rules();
188 if ($quiz->attempts
!= 1) {
189 $viewobj->infomessages
[] = get_string('gradingmethod', 'quiz',
190 quiz_get_grading_option_name($quiz->grademethod
));
193 // Inform user of the grade to pass if non-zero.
194 if ($item && grade_floats_different($item->gradepass
, 0)) {
196 $a->grade
= quiz_format_grade($quiz, $item->gradepass
);
197 $a->maxgrade
= quiz_format_grade($quiz, $quiz->grade
);
198 $viewobj->infomessages
[] = get_string('gradetopassoutof', 'quiz', $a);
201 // Determine whether a start attempt button should be displayed.
202 $viewobj->quizhasquestions
= $quizobj->has_questions();
203 $viewobj->preventmessages
= [];
204 if (!$viewobj->quizhasquestions
) {
205 $viewobj->buttontext
= '';
210 $viewobj->buttontext
= get_string('continuepreview', 'quiz');
211 } else if ($canattempt) {
212 $viewobj->buttontext
= get_string('continueattemptquiz', 'quiz');
216 $viewobj->buttontext
= get_string('previewquizstart', 'quiz');
217 } else if ($canattempt) {
218 $viewobj->preventmessages
= $viewobj->accessmanager
->prevent_new_attempt(
219 $viewobj->numattempts
, $viewobj->lastfinishedattempt
);
220 if ($viewobj->preventmessages
) {
221 $viewobj->buttontext
= '';
222 } else if ($viewobj->numattempts
== 0) {
223 $viewobj->buttontext
= get_string('attemptquiz', 'quiz');
225 $viewobj->buttontext
= get_string('reattemptquiz', 'quiz');
230 // Users who can preview the quiz should be able to see all messages for not being able to access the quiz.
232 $viewobj->preventmessages
= $viewobj->accessmanager
->prevent_access();
233 } else if ($viewobj->buttontext
) {
234 // If, so far, we think a button should be printed, so check if they will be allowed to access it.
235 if (!$viewobj->moreattempts
) {
236 $viewobj->buttontext
= '';
237 } else if ($canattempt) {
238 $viewobj->preventmessages
= $viewobj->accessmanager
->prevent_access();
239 if ($viewobj->preventmessages
) {
240 $viewobj->buttontext
= '';
246 $viewobj->showbacktocourse
= ($viewobj->buttontext
=== '' &&
247 course_get_format($course)->has_view_page());
249 echo $OUTPUT->header();
251 if (!empty($gradinginfo->errors
)) {
252 foreach ($gradinginfo->errors
as $error) {
253 $errortext = new \core\output\notification
($error, \core\output\notification
::NOTIFY_ERROR
);
254 echo $OUTPUT->render($errortext);
259 // Guests can't do a quiz, so offer them a choice of logging in or going back.
260 echo $output->view_page_guest($course, $quiz, $cm, $context, $viewobj->infomessages
, $viewobj);
261 } else if (!isguestuser() && !($canattempt ||
$canpreview
262 ||
$viewobj->canreviewmine
)) {
263 // If they are not enrolled in this course in a good enough role, tell them to enrol.
264 echo $output->view_page_notenrolled($course, $quiz, $cm, $context, $viewobj->infomessages
, $viewobj);
266 echo $output->view_page($course, $quiz, $cm, $context, $viewobj);
269 echo $OUTPUT->footer();