1 |
efrain |
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\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;
|
|
|
32 |
|
|
|
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');
|
|
|
38 |
|
|
|
39 |
$id = optional_param('id', 0, PARAM_INT); // Course Module ID, or ...
|
|
|
40 |
$q = optional_param('q', 0, PARAM_INT); // Quiz ID.
|
|
|
41 |
|
|
|
42 |
if ($id) {
|
|
|
43 |
$quizobj = quiz_settings::create_for_cmid($id, $USER->id);
|
|
|
44 |
} else {
|
|
|
45 |
$quizobj = quiz_settings::create($q, $USER->id);
|
|
|
46 |
}
|
|
|
47 |
$quiz = $quizobj->get_quiz();
|
|
|
48 |
$cm = $quizobj->get_cm();
|
|
|
49 |
$course = $quizobj->get_course();
|
|
|
50 |
|
|
|
51 |
// Check login and get context.
|
|
|
52 |
require_login($course, false, $cm);
|
|
|
53 |
$context = $quizobj->get_context();
|
|
|
54 |
require_capability('mod/quiz:view', $context);
|
|
|
55 |
|
|
|
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);
|
|
|
60 |
|
|
|
61 |
// Create an object to manage all the other (non-roles) access rules.
|
|
|
62 |
$timenow = time();
|
|
|
63 |
$accessmanager = new access_manager($quizobj, $timenow,
|
|
|
64 |
has_capability('mod/quiz:ignoretimelimits', $context, null, false));
|
|
|
65 |
|
|
|
66 |
// Trigger course_module_viewed event and completion.
|
|
|
67 |
quiz_view($quiz, $course, $cm, $context);
|
|
|
68 |
|
|
|
69 |
// Initialize $PAGE, compute blocks.
|
|
|
70 |
$PAGE->set_url('/mod/quiz/view.php', ['id' => $cm->id]);
|
|
|
71 |
|
|
|
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;
|
|
|
76 |
|
|
|
77 |
// Get this user's attempts.
|
|
|
78 |
$attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'finished', true);
|
|
|
79 |
$lastfinishedattempt = end($attempts);
|
|
|
80 |
$unfinished = false;
|
|
|
81 |
$unfinishedattemptid = null;
|
|
|
82 |
if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
|
|
|
83 |
$attempts[] = $unfinishedattempt;
|
|
|
84 |
|
|
|
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);
|
|
|
88 |
|
|
|
89 |
$unfinished = $unfinishedattempt->state == quiz_attempt::IN_PROGRESS ||
|
|
|
90 |
$unfinishedattempt->state == quiz_attempt::OVERDUE;
|
|
|
91 |
if (!$unfinished) {
|
|
|
92 |
$lastfinishedattempt = $unfinishedattempt;
|
|
|
93 |
}
|
|
|
94 |
$unfinishedattemptid = $unfinishedattempt->id;
|
|
|
95 |
$unfinishedattempt = null; // To make it clear we do not use this again.
|
|
|
96 |
}
|
|
|
97 |
$numattempts = count($attempts);
|
|
|
98 |
|
|
|
99 |
$gradeitemmarks = $quizobj->get_grade_calculator()->compute_grade_item_totals_for_attempts(
|
|
|
100 |
array_column($attempts, 'uniqueid'));
|
|
|
101 |
|
|
|
102 |
$viewobj->attempts = $attempts;
|
|
|
103 |
$viewobj->attemptobjs = [];
|
|
|
104 |
foreach ($attempts as $attempt) {
|
|
|
105 |
$attemptobj = new quiz_attempt($attempt, $quiz, $cm, $course, false);
|
|
|
106 |
$attemptobj->set_grade_item_totals($gradeitemmarks[$attempt->uniqueid]);
|
|
|
107 |
$viewobj->attemptobjs[] = $attemptobj;
|
|
|
108 |
|
|
|
109 |
}
|
|
|
110 |
$viewobj->attemptslist = new list_of_attempts($timenow);
|
|
|
111 |
foreach (array_reverse($viewobj->attemptobjs) as $attemptobj) {
|
|
|
112 |
$viewobj->attemptslist->add_attempt($attemptobj);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
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;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
$mygradeoverridden = false;
|
|
|
127 |
$gradebookfeedback = '';
|
|
|
128 |
|
|
|
129 |
$gradeitem = grade_item::fetch([
|
|
|
130 |
'itemtype' => 'mod',
|
|
|
131 |
'itemmodule' => 'quiz',
|
|
|
132 |
'iteminstance' => $quiz->id,
|
|
|
133 |
'itemnumber' => 0,
|
|
|
134 |
'courseid' => $course->id,
|
|
|
135 |
]);
|
|
|
136 |
|
|
|
137 |
if ($gradeitem) {
|
|
|
138 |
if ($gradeitem->refresh_grades($USER->id)) {
|
|
|
139 |
$grade = $gradeitem->get_grade($USER->id, false);
|
|
|
140 |
if ($grade->overridden) {
|
|
|
141 |
if ($gradeitem->needsupdate) {
|
|
|
142 |
// It is Error, but let's be consistent with the old code.
|
|
|
143 |
$mygrade = 0;
|
|
|
144 |
} else {
|
|
|
145 |
$mygrade = $grade->finalgrade;
|
|
|
146 |
}
|
|
|
147 |
$mygradeoverridden = true;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if (!empty($grade->feedback)) {
|
|
|
151 |
$gradebookfeedback = $grade->feedback;
|
|
|
152 |
}
|
|
|
153 |
} else {
|
|
|
154 |
// It is Error, but let's be consistent with the old code.
|
|
|
155 |
$mygrade = 0;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$title = $course->shortname . ': ' . format_string($quiz->name);
|
|
|
160 |
$PAGE->set_title($title);
|
|
|
161 |
$PAGE->set_heading($course->fullname);
|
|
|
162 |
if (html_is_blank($quiz->intro)) {
|
|
|
163 |
$PAGE->activityheader->set_description('');
|
|
|
164 |
}
|
|
|
165 |
$PAGE->add_body_class('limitedwidth');
|
|
|
166 |
/** @var renderer $output */
|
|
|
167 |
$output = $PAGE->get_renderer('mod_quiz');
|
|
|
168 |
|
|
|
169 |
// Print table with existing attempts.
|
|
|
170 |
if ($attempts) {
|
|
|
171 |
// Work out which columns we need, taking account what data is available in each attempt.
|
|
|
172 |
list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts);
|
|
|
173 |
|
|
|
174 |
$viewobj->attemptcolumn = $quiz->attempts != 1;
|
|
|
175 |
|
|
|
176 |
$viewobj->gradecolumn = $someoptions->marks >= question_display_options::MARK_AND_MAX &&
|
|
|
177 |
quiz_has_grades($quiz);
|
|
|
178 |
$viewobj->markcolumn = $viewobj->gradecolumn && ($quiz->grade != $quiz->sumgrades);
|
|
|
179 |
$viewobj->overallstats = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX;
|
|
|
180 |
|
|
|
181 |
$viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
$viewobj->timenow = $timenow;
|
|
|
185 |
$viewobj->numattempts = $numattempts;
|
|
|
186 |
$viewobj->mygrade = $mygrade;
|
|
|
187 |
$viewobj->moreattempts = $unfinished ||
|
|
|
188 |
!$accessmanager->is_finished($numattempts, $lastfinishedattempt);
|
|
|
189 |
$viewobj->mygradeoverridden = $mygradeoverridden;
|
|
|
190 |
$viewobj->gradebookfeedback = $gradebookfeedback;
|
|
|
191 |
$viewobj->lastfinishedattempt = $lastfinishedattempt;
|
|
|
192 |
$viewobj->canedit = has_capability('mod/quiz:manage', $context);
|
|
|
193 |
$viewobj->editurl = new moodle_url('/mod/quiz/edit.php', ['cmid' => $cm->id]);
|
|
|
194 |
$viewobj->backtocourseurl = new moodle_url('/course/view.php', ['id' => $course->id]);
|
|
|
195 |
$viewobj->startattempturl = $quizobj->start_attempt_url();
|
|
|
196 |
|
|
|
197 |
if ($accessmanager->is_preflight_check_required($unfinishedattemptid)) {
|
|
|
198 |
$viewobj->preflightcheckform = $accessmanager->get_preflight_check_form(
|
|
|
199 |
$viewobj->startattempturl, $unfinishedattemptid);
|
|
|
200 |
}
|
|
|
201 |
$viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup();
|
|
|
202 |
$viewobj->popupoptions = $accessmanager->get_popup_options();
|
|
|
203 |
|
|
|
204 |
// Display information about this quiz.
|
|
|
205 |
$viewobj->infomessages = $viewobj->accessmanager->describe_rules();
|
|
|
206 |
if ($quiz->attempts != 1) {
|
|
|
207 |
$viewobj->infomessages[] = get_string('gradingmethod', 'quiz',
|
|
|
208 |
quiz_get_grading_option_name($quiz->grademethod));
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
// Inform user of the grade to pass if non-zero.
|
|
|
212 |
if ($gradeitem && grade_floats_different($gradeitem->gradepass, 0)) {
|
|
|
213 |
$a = new stdClass();
|
|
|
214 |
$a->grade = quiz_format_grade($quiz, $gradeitem->gradepass);
|
|
|
215 |
$a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
|
|
|
216 |
$viewobj->infomessages[] = get_string('gradetopassoutof', 'quiz', $a);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
// Determine whether a start attempt button should be displayed.
|
|
|
220 |
$viewobj->quizhasquestions = $quizobj->has_questions();
|
|
|
221 |
$viewobj->preventmessages = [];
|
|
|
222 |
if (!$viewobj->quizhasquestions) {
|
|
|
223 |
$viewobj->buttontext = '';
|
|
|
224 |
|
|
|
225 |
} else {
|
|
|
226 |
if ($unfinished) {
|
|
|
227 |
if ($canpreview) {
|
|
|
228 |
$viewobj->buttontext = get_string('continuepreview', 'quiz');
|
|
|
229 |
} else if ($canattempt) {
|
|
|
230 |
$viewobj->buttontext = get_string('continueattemptquiz', 'quiz');
|
|
|
231 |
}
|
|
|
232 |
} else {
|
|
|
233 |
if ($canpreview) {
|
|
|
234 |
$viewobj->buttontext = get_string('previewquizstart', 'quiz');
|
|
|
235 |
} else if ($canattempt) {
|
|
|
236 |
$viewobj->preventmessages = $viewobj->accessmanager->prevent_new_attempt(
|
|
|
237 |
$viewobj->numattempts, $viewobj->lastfinishedattempt);
|
|
|
238 |
if ($viewobj->preventmessages) {
|
|
|
239 |
$viewobj->buttontext = '';
|
|
|
240 |
} else if ($viewobj->numattempts == 0) {
|
|
|
241 |
$viewobj->buttontext = get_string('attemptquiz', 'quiz');
|
|
|
242 |
} else {
|
|
|
243 |
$viewobj->buttontext = get_string('reattemptquiz', 'quiz');
|
|
|
244 |
}
|
|
|
245 |
}
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
// Users who can preview the quiz should be able to see all messages for not being able to access the quiz.
|
|
|
249 |
if ($canpreview) {
|
|
|
250 |
$viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
|
|
|
251 |
} else if ($viewobj->buttontext) {
|
|
|
252 |
// If, so far, we think a button should be printed, so check if they will be allowed to access it.
|
|
|
253 |
if (!$viewobj->moreattempts) {
|
|
|
254 |
$viewobj->buttontext = '';
|
|
|
255 |
} else if ($canattempt) {
|
|
|
256 |
$viewobj->preventmessages = $viewobj->accessmanager->prevent_access();
|
|
|
257 |
if ($viewobj->preventmessages) {
|
|
|
258 |
$viewobj->buttontext = '';
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
$viewobj->showbacktocourse = ($viewobj->buttontext === '' &&
|
|
|
265 |
course_get_format($course)->has_view_page());
|
|
|
266 |
|
|
|
267 |
echo $OUTPUT->header();
|
|
|
268 |
|
|
|
269 |
if (!empty($gradinginfo->errors)) {
|
|
|
270 |
foreach ($gradinginfo->errors as $error) {
|
|
|
271 |
$errortext = new \core\output\notification($error, \core\output\notification::NOTIFY_ERROR);
|
|
|
272 |
echo $OUTPUT->render($errortext);
|
|
|
273 |
}
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
if (isguestuser()) {
|
|
|
277 |
// Guests can't do a quiz, so offer them a choice of logging in or going back.
|
|
|
278 |
echo $output->view_page_guest($course, $quiz, $cm, $context, $viewobj->infomessages, $viewobj);
|
|
|
279 |
} else if (!isguestuser() && !($canattempt || $canpreview
|
|
|
280 |
|| $viewobj->canreviewmine)) {
|
|
|
281 |
// If they are not enrolled in this course in a good enough role, tell them to enrol.
|
|
|
282 |
echo $output->view_page_notenrolled($course, $quiz, $cm, $context, $viewobj->infomessages, $viewobj);
|
|
|
283 |
} else {
|
|
|
284 |
echo $output->view_page($course, $quiz, $cm, $context, $viewobj);
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
echo $OUTPUT->footer();
|