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 |
* Library of functions used by the quiz module.
|
|
|
19 |
*
|
|
|
20 |
* This contains functions that are called from within the quiz module only
|
|
|
21 |
* Functions that are also called by core Moodle are in {@link lib.php}
|
|
|
22 |
* This script also loads the code in {@link questionlib.php} which holds
|
|
|
23 |
* the module-indpendent code for handling questions and which in turn
|
|
|
24 |
* initialises all the questiontype classes.
|
|
|
25 |
*
|
|
|
26 |
* @package mod_quiz
|
|
|
27 |
* @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
|
|
|
31 |
defined('MOODLE_INTERNAL') || die();
|
|
|
32 |
|
|
|
33 |
require_once($CFG->dirroot . '/mod/quiz/lib.php');
|
|
|
34 |
require_once($CFG->libdir . '/completionlib.php');
|
|
|
35 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
36 |
require_once($CFG->libdir . '/questionlib.php');
|
|
|
37 |
|
|
|
38 |
use core\di;
|
|
|
39 |
use core\hook;
|
|
|
40 |
use core_question\local\bank\condition;
|
|
|
41 |
use mod_quiz\access_manager;
|
|
|
42 |
use mod_quiz\event\attempt_submitted;
|
|
|
43 |
use mod_quiz\grade_calculator;
|
|
|
44 |
use mod_quiz\hook\attempt_state_changed;
|
|
|
45 |
use mod_quiz\local\override_manager;
|
|
|
46 |
use mod_quiz\question\bank\qbank_helper;
|
|
|
47 |
use mod_quiz\question\display_options;
|
|
|
48 |
use mod_quiz\quiz_attempt;
|
|
|
49 |
use mod_quiz\quiz_settings;
|
|
|
50 |
use mod_quiz\structure;
|
|
|
51 |
use qbank_previewquestion\question_preview_options;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* @var int We show the countdown timer if there is less than this amount of time left before the
|
|
|
55 |
* the quiz close date. (1 hour)
|
|
|
56 |
*/
|
|
|
57 |
define('QUIZ_SHOW_TIME_BEFORE_DEADLINE', '3600');
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* @var int If there are fewer than this many seconds left when the student submits
|
|
|
61 |
* a page of the quiz, then do not take them to the next page of the quiz. Instead
|
|
|
62 |
* close the quiz immediately.
|
|
|
63 |
*/
|
|
|
64 |
define('QUIZ_MIN_TIME_TO_CONTINUE', '2');
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* @var int We show no image when user selects No image from dropdown menu in quiz settings.
|
|
|
68 |
*/
|
|
|
69 |
define('QUIZ_SHOWIMAGE_NONE', 0);
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @var int We show small image when user selects small image from dropdown menu in quiz settings.
|
|
|
73 |
*/
|
|
|
74 |
define('QUIZ_SHOWIMAGE_SMALL', 1);
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* @var int We show Large image when user selects Large image from dropdown menu in quiz settings.
|
|
|
78 |
*/
|
|
|
79 |
define('QUIZ_SHOWIMAGE_LARGE', 2);
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
// Functions related to attempts ///////////////////////////////////////////////
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Creates an object to represent a new attempt at a quiz
|
|
|
86 |
*
|
|
|
87 |
* Creates an attempt object to represent an attempt at the quiz by the current
|
|
|
88 |
* user starting at the current time. The ->id field is not set. The object is
|
|
|
89 |
* NOT written to the database.
|
|
|
90 |
*
|
|
|
91 |
* @param quiz_settings $quizobj the quiz object to create an attempt for.
|
|
|
92 |
* @param int $attemptnumber the sequence number for the attempt.
|
|
|
93 |
* @param stdClass|false $lastattempt the previous attempt by this user, if any. Only needed
|
|
|
94 |
* if $attemptnumber > 1 and $quiz->attemptonlast is true.
|
|
|
95 |
* @param int $timenow the time the attempt was started at.
|
|
|
96 |
* @param bool $ispreview whether this new attempt is a preview.
|
|
|
97 |
* @param int|null $userid the id of the user attempting this quiz.
|
|
|
98 |
*
|
|
|
99 |
* @return stdClass the newly created attempt object.
|
|
|
100 |
*/
|
|
|
101 |
function quiz_create_attempt(quiz_settings $quizobj, $attemptnumber, $lastattempt, $timenow, $ispreview = false, $userid = null) {
|
|
|
102 |
global $USER;
|
|
|
103 |
|
|
|
104 |
if ($userid === null) {
|
|
|
105 |
$userid = $USER->id;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
$quiz = $quizobj->get_quiz();
|
|
|
109 |
if ($quiz->sumgrades < grade_calculator::ALMOST_ZERO && $quiz->grade > grade_calculator::ALMOST_ZERO) {
|
|
|
110 |
throw new moodle_exception('cannotstartgradesmismatch', 'quiz',
|
|
|
111 |
new moodle_url('/mod/quiz/view.php', ['q' => $quiz->id]),
|
|
|
112 |
['grade' => quiz_format_grade($quiz, $quiz->grade)]);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
if ($attemptnumber == 1 || !$quiz->attemptonlast) {
|
|
|
116 |
// We are not building on last attempt so create a new attempt.
|
|
|
117 |
$attempt = new stdClass();
|
|
|
118 |
$attempt->quiz = $quiz->id;
|
|
|
119 |
$attempt->userid = $userid;
|
|
|
120 |
$attempt->preview = 0;
|
|
|
121 |
$attempt->layout = '';
|
|
|
122 |
} else {
|
|
|
123 |
// Build on last attempt.
|
|
|
124 |
if (empty($lastattempt)) {
|
|
|
125 |
throw new \moodle_exception('cannotfindprevattempt', 'quiz');
|
|
|
126 |
}
|
|
|
127 |
$attempt = $lastattempt;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
$attempt->attempt = $attemptnumber;
|
|
|
131 |
$attempt->timestart = $timenow;
|
|
|
132 |
$attempt->timefinish = 0;
|
|
|
133 |
$attempt->timemodified = $timenow;
|
|
|
134 |
$attempt->timemodifiedoffline = 0;
|
|
|
135 |
$attempt->state = quiz_attempt::IN_PROGRESS;
|
|
|
136 |
$attempt->currentpage = 0;
|
|
|
137 |
$attempt->sumgrades = null;
|
|
|
138 |
$attempt->gradednotificationsenttime = null;
|
|
|
139 |
|
|
|
140 |
// If this is a preview, mark it as such.
|
|
|
141 |
if ($ispreview) {
|
|
|
142 |
$attempt->preview = 1;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
$timeclose = $quizobj->get_access_manager($timenow)->get_end_time($attempt);
|
|
|
146 |
if ($timeclose === false || $ispreview) {
|
|
|
147 |
$attempt->timecheckstate = null;
|
|
|
148 |
} else {
|
|
|
149 |
$attempt->timecheckstate = $timeclose;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
di::get(hook\manager::class)->dispatch(new attempt_state_changed(null, $attempt));
|
|
|
153 |
|
|
|
154 |
return $attempt;
|
|
|
155 |
}
|
|
|
156 |
/**
|
|
|
157 |
* Start a normal, new, quiz attempt.
|
|
|
158 |
*
|
|
|
159 |
* @param quiz_settings $quizobj the quiz object to start an attempt for.
|
|
|
160 |
* @param question_usage_by_activity $quba
|
|
|
161 |
* @param stdClass $attempt
|
|
|
162 |
* @param integer $attemptnumber starting from 1
|
|
|
163 |
* @param integer $timenow the attempt start time
|
|
|
164 |
* @param array $questionids slot number => question id. Used for random questions, to force the choice
|
|
|
165 |
* of a particular actual question. Intended for testing purposes only.
|
|
|
166 |
* @param array $forcedvariantsbyslot slot number => variant. Used for questions with variants,
|
|
|
167 |
* to force the choice of a particular variant. Intended for testing
|
|
|
168 |
* purposes only.
|
|
|
169 |
* @return stdClass modified attempt object
|
|
|
170 |
*/
|
|
|
171 |
function quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow,
|
|
|
172 |
$questionids = [], $forcedvariantsbyslot = []) {
|
|
|
173 |
|
|
|
174 |
// Usages for this user's previous quiz attempts.
|
|
|
175 |
$qubaids = new \mod_quiz\question\qubaids_for_users_attempts(
|
|
|
176 |
$quizobj->get_quizid(), $attempt->userid);
|
|
|
177 |
|
|
|
178 |
// Partially load all the questions in this quiz.
|
|
|
179 |
$quizobj->preload_questions();
|
|
|
180 |
|
|
|
181 |
// First load all the non-random questions.
|
|
|
182 |
$randomfound = false;
|
|
|
183 |
$slot = 0;
|
|
|
184 |
$questions = [];
|
|
|
185 |
$maxmark = [];
|
|
|
186 |
$page = [];
|
|
|
187 |
foreach ($quizobj->get_questions(null, false) as $questiondata) {
|
|
|
188 |
$slot += 1;
|
|
|
189 |
$maxmark[$slot] = $questiondata->maxmark;
|
|
|
190 |
$page[$slot] = $questiondata->page;
|
|
|
191 |
if ($questiondata->status == \core_question\local\bank\question_version_status::QUESTION_STATUS_DRAFT) {
|
|
|
192 |
throw new moodle_exception('questiondraftonly', 'mod_quiz', '', $questiondata->name);
|
|
|
193 |
}
|
|
|
194 |
if ($questiondata->qtype == 'random') {
|
|
|
195 |
$randomfound = true;
|
|
|
196 |
continue;
|
|
|
197 |
}
|
|
|
198 |
$questions[$slot] = question_bank::load_question($questiondata->questionid, $quizobj->get_quiz()->shuffleanswers);
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
// Then find a question to go in place of each random question.
|
|
|
202 |
if ($randomfound) {
|
|
|
203 |
$slot = 0;
|
|
|
204 |
$usedquestionids = [];
|
|
|
205 |
foreach ($questions as $question) {
|
|
|
206 |
if ($question->id && isset($usedquestions[$question->id])) {
|
|
|
207 |
$usedquestionids[$question->id] += 1;
|
|
|
208 |
} else {
|
|
|
209 |
$usedquestionids[$question->id] = 1;
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
$randomloader = new \core_question\local\bank\random_question_loader($qubaids, $usedquestionids);
|
|
|
213 |
|
|
|
214 |
foreach ($quizobj->get_questions(null, false) as $questiondata) {
|
|
|
215 |
$slot += 1;
|
|
|
216 |
if ($questiondata->qtype != 'random') {
|
|
|
217 |
continue;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
$tagids = qbank_helper::get_tag_ids_for_slot($questiondata);
|
|
|
221 |
|
|
|
222 |
// Deal with fixed random choices for testing.
|
|
|
223 |
if (isset($questionids[$quba->next_slot_number()])) {
|
|
|
224 |
$filtercondition = $questiondata->filtercondition;
|
|
|
225 |
$filters = $filtercondition['filter'] ?? [];
|
|
|
226 |
if ($randomloader->is_filtered_question_available($filters, $questionids[$quba->next_slot_number()])) {
|
|
|
227 |
$questions[$slot] = question_bank::load_question(
|
|
|
228 |
$questionids[$quba->next_slot_number()], $quizobj->get_quiz()->shuffleanswers);
|
|
|
229 |
continue;
|
|
|
230 |
} else {
|
|
|
231 |
throw new coding_exception('Forced question id not available.');
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
// Normal case, pick one at random.
|
|
|
236 |
$filtercondition = $questiondata->filtercondition;
|
|
|
237 |
$filters = $filtercondition['filter'] ?? [];
|
|
|
238 |
$questionid = $randomloader->get_next_filtered_question_id($filters);
|
|
|
239 |
|
|
|
240 |
if ($questionid === null) {
|
|
|
241 |
throw new moodle_exception('notenoughrandomquestions', 'quiz',
|
|
|
242 |
$quizobj->view_url(), $questiondata);
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
$questions[$slot] = question_bank::load_question($questionid,
|
|
|
246 |
$quizobj->get_quiz()->shuffleanswers);
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
// Finally add them all to the usage.
|
|
|
251 |
ksort($questions);
|
|
|
252 |
foreach ($questions as $slot => $question) {
|
|
|
253 |
$newslot = $quba->add_question($question, $maxmark[$slot]);
|
|
|
254 |
if ($newslot != $slot) {
|
|
|
255 |
throw new coding_exception('Slot numbers have got confused.');
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
// Start all the questions.
|
|
|
260 |
$variantstrategy = new core_question\engine\variants\least_used_strategy($quba, $qubaids);
|
|
|
261 |
|
|
|
262 |
if (!empty($forcedvariantsbyslot)) {
|
|
|
263 |
$forcedvariantsbyseed = question_variant_forced_choices_selection_strategy::prepare_forced_choices_array(
|
|
|
264 |
$forcedvariantsbyslot, $quba);
|
|
|
265 |
$variantstrategy = new question_variant_forced_choices_selection_strategy(
|
|
|
266 |
$forcedvariantsbyseed, $variantstrategy);
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
$quba->start_all_questions($variantstrategy, $timenow, $attempt->userid);
|
|
|
270 |
|
|
|
271 |
// Work out the attempt layout.
|
|
|
272 |
$sections = $quizobj->get_sections();
|
|
|
273 |
foreach ($sections as $i => $section) {
|
|
|
274 |
if (isset($sections[$i + 1])) {
|
|
|
275 |
$sections[$i]->lastslot = $sections[$i + 1]->firstslot - 1;
|
|
|
276 |
} else {
|
|
|
277 |
$sections[$i]->lastslot = count($questions);
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
$layout = [];
|
|
|
282 |
foreach ($sections as $section) {
|
|
|
283 |
if ($section->shufflequestions) {
|
|
|
284 |
$questionsinthissection = [];
|
|
|
285 |
for ($slot = $section->firstslot; $slot <= $section->lastslot; $slot += 1) {
|
|
|
286 |
$questionsinthissection[] = $slot;
|
|
|
287 |
}
|
|
|
288 |
shuffle($questionsinthissection);
|
|
|
289 |
$questionsonthispage = 0;
|
|
|
290 |
foreach ($questionsinthissection as $slot) {
|
|
|
291 |
if ($questionsonthispage && $questionsonthispage == $quizobj->get_quiz()->questionsperpage) {
|
|
|
292 |
$layout[] = 0;
|
|
|
293 |
$questionsonthispage = 0;
|
|
|
294 |
}
|
|
|
295 |
$layout[] = $slot;
|
|
|
296 |
$questionsonthispage += 1;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
} else {
|
|
|
300 |
$currentpage = $page[$section->firstslot];
|
|
|
301 |
for ($slot = $section->firstslot; $slot <= $section->lastslot; $slot += 1) {
|
|
|
302 |
if ($currentpage !== null && $page[$slot] != $currentpage) {
|
|
|
303 |
$layout[] = 0;
|
|
|
304 |
}
|
|
|
305 |
$layout[] = $slot;
|
|
|
306 |
$currentpage = $page[$slot];
|
|
|
307 |
}
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
// Each section ends with a page break.
|
|
|
311 |
$layout[] = 0;
|
|
|
312 |
}
|
|
|
313 |
$attempt->layout = implode(',', $layout);
|
|
|
314 |
|
|
|
315 |
return $attempt;
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* Start a subsequent new attempt, in each attempt builds on last mode.
|
|
|
320 |
*
|
|
|
321 |
* @param question_usage_by_activity $quba this question usage
|
|
|
322 |
* @param stdClass $attempt this attempt
|
|
|
323 |
* @param stdClass $lastattempt last attempt
|
|
|
324 |
* @return stdClass modified attempt object
|
|
|
325 |
*
|
|
|
326 |
*/
|
|
|
327 |
function quiz_start_attempt_built_on_last($quba, $attempt, $lastattempt) {
|
|
|
328 |
$oldquba = question_engine::load_questions_usage_by_activity($lastattempt->uniqueid);
|
|
|
329 |
|
|
|
330 |
$oldnumberstonew = [];
|
|
|
331 |
foreach ($oldquba->get_attempt_iterator() as $oldslot => $oldqa) {
|
|
|
332 |
$question = $oldqa->get_question(false);
|
|
|
333 |
if ($question->status == \core_question\local\bank\question_version_status::QUESTION_STATUS_DRAFT) {
|
|
|
334 |
throw new moodle_exception('questiondraftonly', 'mod_quiz', '', $question->name);
|
|
|
335 |
}
|
|
|
336 |
$newslot = $quba->add_question($question, $oldqa->get_max_mark());
|
|
|
337 |
|
|
|
338 |
$quba->start_question_based_on($newslot, $oldqa);
|
|
|
339 |
|
|
|
340 |
$oldnumberstonew[$oldslot] = $newslot;
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
// Update attempt layout.
|
|
|
344 |
$newlayout = [];
|
|
|
345 |
foreach (explode(',', $lastattempt->layout) as $oldslot) {
|
|
|
346 |
if ($oldslot != 0) {
|
|
|
347 |
$newlayout[] = $oldnumberstonew[$oldslot];
|
|
|
348 |
} else {
|
|
|
349 |
$newlayout[] = 0;
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
$attempt->layout = implode(',', $newlayout);
|
|
|
353 |
return $attempt;
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
* The save started question usage and quiz attempt in db and log the started attempt.
|
|
|
358 |
*
|
|
|
359 |
* @param quiz_settings $quizobj
|
|
|
360 |
* @param question_usage_by_activity $quba
|
|
|
361 |
* @param stdClass $attempt
|
|
|
362 |
* @return stdClass attempt object with uniqueid and id set.
|
|
|
363 |
*/
|
|
|
364 |
function quiz_attempt_save_started($quizobj, $quba, $attempt) {
|
|
|
365 |
global $DB;
|
|
|
366 |
// Save the attempt in the database.
|
|
|
367 |
question_engine::save_questions_usage_by_activity($quba);
|
|
|
368 |
$attempt->uniqueid = $quba->get_id();
|
|
|
369 |
$attempt->id = $DB->insert_record('quiz_attempts', $attempt);
|
|
|
370 |
|
|
|
371 |
// Params used by the events below.
|
|
|
372 |
$params = [
|
|
|
373 |
'objectid' => $attempt->id,
|
|
|
374 |
'relateduserid' => $attempt->userid,
|
|
|
375 |
'courseid' => $quizobj->get_courseid(),
|
|
|
376 |
'context' => $quizobj->get_context()
|
|
|
377 |
];
|
|
|
378 |
// Decide which event we are using.
|
|
|
379 |
if ($attempt->preview) {
|
|
|
380 |
$params['other'] = [
|
|
|
381 |
'quizid' => $quizobj->get_quizid()
|
|
|
382 |
];
|
|
|
383 |
$event = \mod_quiz\event\attempt_preview_started::create($params);
|
|
|
384 |
} else {
|
|
|
385 |
$event = \mod_quiz\event\attempt_started::create($params);
|
|
|
386 |
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
// Trigger the event.
|
|
|
390 |
$event->add_record_snapshot('quiz', $quizobj->get_quiz());
|
|
|
391 |
$event->add_record_snapshot('quiz_attempts', $attempt);
|
|
|
392 |
$event->trigger();
|
|
|
393 |
|
|
|
394 |
return $attempt;
|
|
|
395 |
}
|
|
|
396 |
|
|
|
397 |
/**
|
|
|
398 |
* Returns an unfinished attempt (if there is one) for the given
|
|
|
399 |
* user on the given quiz. This function does not return preview attempts.
|
|
|
400 |
*
|
|
|
401 |
* @param int $quizid the id of the quiz.
|
|
|
402 |
* @param int $userid the id of the user.
|
|
|
403 |
*
|
|
|
404 |
* @return mixed the unfinished attempt if there is one, false if not.
|
|
|
405 |
*/
|
|
|
406 |
function quiz_get_user_attempt_unfinished($quizid, $userid) {
|
|
|
407 |
$attempts = quiz_get_user_attempts($quizid, $userid, 'unfinished', true);
|
|
|
408 |
if ($attempts) {
|
|
|
409 |
return array_shift($attempts);
|
|
|
410 |
} else {
|
|
|
411 |
return false;
|
|
|
412 |
}
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
/**
|
|
|
416 |
* Delete a quiz attempt.
|
|
|
417 |
* @param mixed $attempt an integer attempt id or an attempt object
|
|
|
418 |
* (row of the quiz_attempts table).
|
|
|
419 |
* @param stdClass $quiz the quiz object.
|
|
|
420 |
*/
|
|
|
421 |
function quiz_delete_attempt($attempt, $quiz) {
|
|
|
422 |
global $DB;
|
|
|
423 |
if (is_numeric($attempt)) {
|
|
|
424 |
if (!$attempt = $DB->get_record('quiz_attempts', ['id' => $attempt])) {
|
|
|
425 |
return;
|
|
|
426 |
}
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
if ($attempt->quiz != $quiz->id) {
|
|
|
430 |
debugging("Trying to delete attempt $attempt->id which belongs to quiz $attempt->quiz " .
|
|
|
431 |
"but was passed quiz $quiz->id.");
|
|
|
432 |
return;
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
if (!isset($quiz->cmid)) {
|
|
|
436 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id, $quiz->course);
|
|
|
437 |
$quiz->cmid = $cm->id;
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
question_engine::delete_questions_usage_by_activity($attempt->uniqueid);
|
|
|
441 |
$DB->delete_records('quiz_attempts', ['id' => $attempt->id]);
|
|
|
442 |
|
|
|
443 |
// Log the deletion of the attempt if not a preview.
|
|
|
444 |
if (!$attempt->preview) {
|
|
|
445 |
$params = [
|
|
|
446 |
'objectid' => $attempt->id,
|
|
|
447 |
'relateduserid' => $attempt->userid,
|
|
|
448 |
'context' => context_module::instance($quiz->cmid),
|
|
|
449 |
'other' => [
|
|
|
450 |
'quizid' => $quiz->id
|
|
|
451 |
]
|
|
|
452 |
];
|
|
|
453 |
$event = \mod_quiz\event\attempt_deleted::create($params);
|
|
|
454 |
$event->add_record_snapshot('quiz_attempts', $attempt);
|
|
|
455 |
$event->trigger();
|
|
|
456 |
|
|
|
457 |
// This class callback is deprecated, and will be removed in Moodle 4.8 (MDL-80327).
|
|
|
458 |
// Use the attempt_state_changed hook instead.
|
|
|
459 |
$callbackclasses = \core_component::get_plugin_list_with_class('quiz', 'quiz_attempt_deleted');
|
|
|
460 |
foreach ($callbackclasses as $callbackclass) {
|
|
|
461 |
component_class_callback($callbackclass, 'callback', [$quiz->id], null, true);
|
|
|
462 |
}
|
|
|
463 |
|
|
|
464 |
di::get(hook\manager::class)->dispatch(new attempt_state_changed($attempt, null));
|
|
|
465 |
}
|
|
|
466 |
|
|
|
467 |
// Search quiz_attempts for other instances by this user.
|
|
|
468 |
// If none, then delete record for this quiz, this user from quiz_grades
|
|
|
469 |
// else recalculate best grade.
|
|
|
470 |
$userid = $attempt->userid;
|
|
|
471 |
$gradecalculator = quiz_settings::create($quiz->id)->get_grade_calculator();
|
|
|
472 |
if (!$DB->record_exists('quiz_attempts', ['userid' => $userid, 'quiz' => $quiz->id])) {
|
|
|
473 |
$DB->delete_records('quiz_grades', ['userid' => $userid, 'quiz' => $quiz->id]);
|
|
|
474 |
} else {
|
|
|
475 |
$gradecalculator->recompute_final_grade($userid);
|
|
|
476 |
}
|
|
|
477 |
|
|
|
478 |
quiz_update_grades($quiz, $userid);
|
|
|
479 |
}
|
|
|
480 |
|
|
|
481 |
/**
|
|
|
482 |
* Delete all the preview attempts at a quiz, or possibly all the attempts belonging
|
|
|
483 |
* to one user.
|
|
|
484 |
* @param stdClass $quiz the quiz object.
|
|
|
485 |
* @param int $userid (optional) if given, only delete the previews belonging to this user.
|
|
|
486 |
*/
|
|
|
487 |
function quiz_delete_previews($quiz, $userid = null) {
|
|
|
488 |
global $DB;
|
|
|
489 |
$conditions = ['quiz' => $quiz->id, 'preview' => 1];
|
|
|
490 |
if (!empty($userid)) {
|
|
|
491 |
$conditions['userid'] = $userid;
|
|
|
492 |
}
|
|
|
493 |
$previewattempts = $DB->get_records('quiz_attempts', $conditions);
|
|
|
494 |
foreach ($previewattempts as $attempt) {
|
|
|
495 |
quiz_delete_attempt($attempt, $quiz);
|
|
|
496 |
}
|
|
|
497 |
}
|
|
|
498 |
|
|
|
499 |
/**
|
|
|
500 |
* @param int $quizid The quiz id.
|
|
|
501 |
* @return bool whether this quiz has any (non-preview) attempts.
|
|
|
502 |
*/
|
|
|
503 |
function quiz_has_attempts($quizid) {
|
|
|
504 |
global $DB;
|
|
|
505 |
return $DB->record_exists('quiz_attempts', ['quiz' => $quizid, 'preview' => 0]);
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
// Functions to do with quiz layout and pages //////////////////////////////////
|
|
|
509 |
|
|
|
510 |
/**
|
|
|
511 |
* Repaginate the questions in a quiz
|
|
|
512 |
* @param int $quizid the id of the quiz to repaginate.
|
|
|
513 |
* @param int $slotsperpage number of items to put on each page. 0 means unlimited.
|
|
|
514 |
*/
|
|
|
515 |
function quiz_repaginate_questions($quizid, $slotsperpage) {
|
|
|
516 |
global $DB;
|
|
|
517 |
$trans = $DB->start_delegated_transaction();
|
|
|
518 |
|
|
|
519 |
$sections = $DB->get_records('quiz_sections', ['quizid' => $quizid], 'firstslot ASC');
|
|
|
520 |
$firstslots = [];
|
|
|
521 |
foreach ($sections as $section) {
|
|
|
522 |
if ((int)$section->firstslot === 1) {
|
|
|
523 |
continue;
|
|
|
524 |
}
|
|
|
525 |
$firstslots[] = $section->firstslot;
|
|
|
526 |
}
|
|
|
527 |
|
|
|
528 |
$slots = $DB->get_records('quiz_slots', ['quizid' => $quizid],
|
|
|
529 |
'slot');
|
|
|
530 |
$currentpage = 1;
|
|
|
531 |
$slotsonthispage = 0;
|
|
|
532 |
foreach ($slots as $slot) {
|
|
|
533 |
if (($firstslots && in_array($slot->slot, $firstslots)) ||
|
|
|
534 |
($slotsonthispage && $slotsonthispage == $slotsperpage)) {
|
|
|
535 |
$currentpage += 1;
|
|
|
536 |
$slotsonthispage = 0;
|
|
|
537 |
}
|
|
|
538 |
if ($slot->page != $currentpage) {
|
|
|
539 |
$DB->set_field('quiz_slots', 'page', $currentpage, ['id' => $slot->id]);
|
|
|
540 |
}
|
|
|
541 |
$slotsonthispage += 1;
|
|
|
542 |
}
|
|
|
543 |
|
|
|
544 |
$trans->allow_commit();
|
|
|
545 |
|
|
|
546 |
// Log quiz re-paginated event.
|
|
|
547 |
$cm = get_coursemodule_from_instance('quiz', $quizid);
|
|
|
548 |
$event = \mod_quiz\event\quiz_repaginated::create([
|
|
|
549 |
'context' => \context_module::instance($cm->id),
|
|
|
550 |
'objectid' => $quizid,
|
|
|
551 |
'other' => [
|
|
|
552 |
'slotsperpage' => $slotsperpage
|
|
|
553 |
]
|
|
|
554 |
]);
|
|
|
555 |
$event->trigger();
|
|
|
556 |
|
|
|
557 |
}
|
|
|
558 |
|
|
|
559 |
// Functions to do with quiz grades ////////////////////////////////////////////
|
|
|
560 |
// Note a lot of logic related to this is now in the grade_calculator class.
|
|
|
561 |
|
|
|
562 |
/**
|
|
|
563 |
* Convert the raw grade stored in $attempt into a grade out of the maximum
|
|
|
564 |
* grade for this quiz.
|
|
|
565 |
*
|
|
|
566 |
* @param float $rawgrade the unadjusted grade, fof example $attempt->sumgrades
|
|
|
567 |
* @param stdClass $quiz the quiz object. Only the fields grade, sumgrades and decimalpoints are used.
|
|
|
568 |
* @param bool|string $format whether to format the results for display
|
|
|
569 |
* or 'question' to format a question grade (different number of decimal places.
|
|
|
570 |
* @return float|string the rescaled grade, or null/the lang string 'notyetgraded'
|
|
|
571 |
* if the $grade is null.
|
|
|
572 |
*/
|
|
|
573 |
function quiz_rescale_grade($rawgrade, $quiz, $format = true) {
|
|
|
574 |
if (is_null($rawgrade)) {
|
|
|
575 |
$grade = null;
|
|
|
576 |
} else if ($quiz->sumgrades >= grade_calculator::ALMOST_ZERO) {
|
|
|
577 |
$grade = $rawgrade * $quiz->grade / $quiz->sumgrades;
|
|
|
578 |
} else {
|
|
|
579 |
$grade = 0;
|
|
|
580 |
}
|
|
|
581 |
if ($format === 'question') {
|
|
|
582 |
$grade = quiz_format_question_grade($quiz, $grade);
|
|
|
583 |
} else if ($format) {
|
|
|
584 |
$grade = quiz_format_grade($quiz, $grade);
|
|
|
585 |
}
|
|
|
586 |
return $grade;
|
|
|
587 |
}
|
|
|
588 |
|
|
|
589 |
/**
|
|
|
590 |
* Get the feedback object for this grade on this quiz.
|
|
|
591 |
*
|
|
|
592 |
* @param float $grade a grade on this quiz.
|
|
|
593 |
* @param stdClass $quiz the quiz settings.
|
|
|
594 |
* @return false|stdClass the record object or false if there is not feedback for the given grade
|
|
|
595 |
* @since Moodle 3.1
|
|
|
596 |
*/
|
|
|
597 |
function quiz_feedback_record_for_grade($grade, $quiz) {
|
|
|
598 |
global $DB;
|
|
|
599 |
|
|
|
600 |
// With CBM etc, it is possible to get -ve grades, which would then not match
|
|
|
601 |
// any feedback. Therefore, we replace -ve grades with 0.
|
|
|
602 |
$grade = max($grade, 0);
|
|
|
603 |
|
|
|
604 |
$feedback = $DB->get_record_select('quiz_feedback',
|
|
|
605 |
'quizid = ? AND mingrade <= ? AND ? < maxgrade', [$quiz->id, $grade, $grade]);
|
|
|
606 |
|
|
|
607 |
return $feedback;
|
|
|
608 |
}
|
|
|
609 |
|
|
|
610 |
/**
|
|
|
611 |
* Get the feedback text that should be show to a student who
|
|
|
612 |
* got this grade on this quiz. The feedback is processed ready for diplay.
|
|
|
613 |
*
|
|
|
614 |
* @param float $grade a grade on this quiz.
|
|
|
615 |
* @param stdClass $quiz the quiz settings.
|
|
|
616 |
* @param context_module $context the quiz context.
|
|
|
617 |
* @return string the comment that corresponds to this grade (empty string if there is not one.
|
|
|
618 |
*/
|
|
|
619 |
function quiz_feedback_for_grade($grade, $quiz, $context) {
|
|
|
620 |
|
|
|
621 |
if (is_null($grade)) {
|
|
|
622 |
return '';
|
|
|
623 |
}
|
|
|
624 |
|
|
|
625 |
$feedback = quiz_feedback_record_for_grade($grade, $quiz);
|
|
|
626 |
|
|
|
627 |
if (empty($feedback->feedbacktext)) {
|
|
|
628 |
return '';
|
|
|
629 |
}
|
|
|
630 |
|
|
|
631 |
// Clean the text, ready for display.
|
|
|
632 |
$formatoptions = new stdClass();
|
|
|
633 |
$formatoptions->noclean = true;
|
|
|
634 |
$feedbacktext = file_rewrite_pluginfile_urls($feedback->feedbacktext, 'pluginfile.php',
|
|
|
635 |
$context->id, 'mod_quiz', 'feedback', $feedback->id);
|
|
|
636 |
$feedbacktext = format_text($feedbacktext, $feedback->feedbacktextformat, $formatoptions);
|
|
|
637 |
|
|
|
638 |
return $feedbacktext;
|
|
|
639 |
}
|
|
|
640 |
|
|
|
641 |
/**
|
|
|
642 |
* @param stdClass $quiz the quiz database row.
|
|
|
643 |
* @return bool Whether this quiz has any non-blank feedback text.
|
|
|
644 |
*/
|
|
|
645 |
function quiz_has_feedback($quiz) {
|
|
|
646 |
global $DB;
|
|
|
647 |
static $cache = [];
|
|
|
648 |
if (!array_key_exists($quiz->id, $cache)) {
|
|
|
649 |
$cache[$quiz->id] = quiz_has_grades($quiz) &&
|
|
|
650 |
$DB->record_exists_select('quiz_feedback', "quizid = ? AND " .
|
|
|
651 |
$DB->sql_isnotempty('quiz_feedback', 'feedbacktext', false, true),
|
|
|
652 |
[$quiz->id]);
|
|
|
653 |
}
|
|
|
654 |
return $cache[$quiz->id];
|
|
|
655 |
}
|
|
|
656 |
|
|
|
657 |
/**
|
|
|
658 |
* Return summary of the number of settings override that exist.
|
|
|
659 |
*
|
|
|
660 |
* To get a nice display of this, see the quiz_override_summary_links()
|
|
|
661 |
* quiz renderer method.
|
|
|
662 |
*
|
|
|
663 |
* @param stdClass $quiz the quiz settings. Only $quiz->id is used at the moment.
|
|
|
664 |
* @param cm_info|stdClass $cm the cm object. Only $cm->course, $cm->groupmode and
|
|
|
665 |
* $cm->groupingid fields are used at the moment.
|
|
|
666 |
* @param int $currentgroup if there is a concept of current group where this method is being called
|
|
|
667 |
* (e.g. a report) pass it in here. Default 0 which means no current group.
|
|
|
668 |
* @return array like 'group' => 3, 'user' => 12] where 3 is the number of group overrides,
|
|
|
669 |
* and 12 is the number of user ones.
|
|
|
670 |
*/
|
|
|
671 |
function quiz_override_summary(stdClass $quiz, cm_info|stdClass $cm, int $currentgroup = 0): array {
|
|
|
672 |
global $DB;
|
|
|
673 |
|
|
|
674 |
if ($currentgroup) {
|
|
|
675 |
// Currently only interested in one group.
|
|
|
676 |
$groupcount = $DB->count_records('quiz_overrides', ['quiz' => $quiz->id, 'groupid' => $currentgroup]);
|
|
|
677 |
$usercount = $DB->count_records_sql("
|
|
|
678 |
SELECT COUNT(1)
|
|
|
679 |
FROM {quiz_overrides} o
|
|
|
680 |
JOIN {groups_members} gm ON o.userid = gm.userid
|
|
|
681 |
WHERE o.quiz = ?
|
|
|
682 |
AND gm.groupid = ?
|
|
|
683 |
", [$quiz->id, $currentgroup]);
|
|
|
684 |
return ['group' => $groupcount, 'user' => $usercount, 'mode' => 'onegroup'];
|
|
|
685 |
}
|
|
|
686 |
|
|
|
687 |
$quizgroupmode = groups_get_activity_groupmode($cm);
|
|
|
688 |
$accessallgroups = ($quizgroupmode == NOGROUPS) ||
|
|
|
689 |
has_capability('moodle/site:accessallgroups', context_module::instance($cm->id));
|
|
|
690 |
|
|
|
691 |
if ($accessallgroups) {
|
|
|
692 |
// User can see all groups.
|
|
|
693 |
$groupcount = $DB->count_records_select('quiz_overrides',
|
|
|
694 |
'quiz = ? AND groupid IS NOT NULL', [$quiz->id]);
|
|
|
695 |
$usercount = $DB->count_records_select('quiz_overrides',
|
|
|
696 |
'quiz = ? AND userid IS NOT NULL', [$quiz->id]);
|
|
|
697 |
return ['group' => $groupcount, 'user' => $usercount, 'mode' => 'allgroups'];
|
|
|
698 |
|
|
|
699 |
} else {
|
|
|
700 |
// User can only see groups they are in.
|
|
|
701 |
$groups = groups_get_activity_allowed_groups($cm);
|
|
|
702 |
if (!$groups) {
|
|
|
703 |
return ['group' => 0, 'user' => 0, 'mode' => 'somegroups'];
|
|
|
704 |
}
|
|
|
705 |
|
|
|
706 |
list($groupidtest, $params) = $DB->get_in_or_equal(array_keys($groups));
|
|
|
707 |
$params[] = $quiz->id;
|
|
|
708 |
|
|
|
709 |
$groupcount = $DB->count_records_select('quiz_overrides',
|
|
|
710 |
"groupid $groupidtest AND quiz = ?", $params);
|
|
|
711 |
$usercount = $DB->count_records_sql("
|
|
|
712 |
SELECT COUNT(1)
|
|
|
713 |
FROM {quiz_overrides} o
|
|
|
714 |
JOIN {groups_members} gm ON o.userid = gm.userid
|
|
|
715 |
WHERE gm.groupid $groupidtest
|
|
|
716 |
AND o.quiz = ?
|
|
|
717 |
", $params);
|
|
|
718 |
|
|
|
719 |
return ['group' => $groupcount, 'user' => $usercount, 'mode' => 'somegroups'];
|
|
|
720 |
}
|
|
|
721 |
}
|
|
|
722 |
|
|
|
723 |
/**
|
|
|
724 |
* Efficiently update check state time on all open attempts
|
|
|
725 |
*
|
|
|
726 |
* @param array $conditions optional restrictions on which attempts to update
|
|
|
727 |
* Allowed conditions:
|
|
|
728 |
* courseid => (array|int) attempts in given course(s)
|
|
|
729 |
* userid => (array|int) attempts for given user(s)
|
|
|
730 |
* quizid => (array|int) attempts in given quiz(s)
|
|
|
731 |
* groupid => (array|int) quizzes with some override for given group(s)
|
|
|
732 |
*
|
|
|
733 |
*/
|
|
|
734 |
function quiz_update_open_attempts(array $conditions) {
|
|
|
735 |
global $DB;
|
|
|
736 |
|
|
|
737 |
foreach ($conditions as &$value) {
|
|
|
738 |
if (!is_array($value)) {
|
|
|
739 |
$value = [$value];
|
|
|
740 |
}
|
|
|
741 |
}
|
|
|
742 |
|
|
|
743 |
$params = [];
|
|
|
744 |
$wheres = ["quiza.state IN ('inprogress', 'overdue')"];
|
|
|
745 |
$iwheres = ["iquiza.state IN ('inprogress', 'overdue')"];
|
|
|
746 |
|
|
|
747 |
if (isset($conditions['courseid'])) {
|
|
|
748 |
list ($incond, $inparams) = $DB->get_in_or_equal($conditions['courseid'], SQL_PARAMS_NAMED, 'cid');
|
|
|
749 |
$params = array_merge($params, $inparams);
|
|
|
750 |
$wheres[] = "quiza.quiz IN (SELECT q.id FROM {quiz} q WHERE q.course $incond)";
|
|
|
751 |
list ($incond, $inparams) = $DB->get_in_or_equal($conditions['courseid'], SQL_PARAMS_NAMED, 'icid');
|
|
|
752 |
$params = array_merge($params, $inparams);
|
|
|
753 |
$iwheres[] = "iquiza.quiz IN (SELECT q.id FROM {quiz} q WHERE q.course $incond)";
|
|
|
754 |
}
|
|
|
755 |
|
|
|
756 |
if (isset($conditions['userid'])) {
|
|
|
757 |
list ($incond, $inparams) = $DB->get_in_or_equal($conditions['userid'], SQL_PARAMS_NAMED, 'uid');
|
|
|
758 |
$params = array_merge($params, $inparams);
|
|
|
759 |
$wheres[] = "quiza.userid $incond";
|
|
|
760 |
list ($incond, $inparams) = $DB->get_in_or_equal($conditions['userid'], SQL_PARAMS_NAMED, 'iuid');
|
|
|
761 |
$params = array_merge($params, $inparams);
|
|
|
762 |
$iwheres[] = "iquiza.userid $incond";
|
|
|
763 |
}
|
|
|
764 |
|
|
|
765 |
if (isset($conditions['quizid'])) {
|
|
|
766 |
list ($incond, $inparams) = $DB->get_in_or_equal($conditions['quizid'], SQL_PARAMS_NAMED, 'qid');
|
|
|
767 |
$params = array_merge($params, $inparams);
|
|
|
768 |
$wheres[] = "quiza.quiz $incond";
|
|
|
769 |
list ($incond, $inparams) = $DB->get_in_or_equal($conditions['quizid'], SQL_PARAMS_NAMED, 'iqid');
|
|
|
770 |
$params = array_merge($params, $inparams);
|
|
|
771 |
$iwheres[] = "iquiza.quiz $incond";
|
|
|
772 |
}
|
|
|
773 |
|
|
|
774 |
if (isset($conditions['groupid'])) {
|
|
|
775 |
list ($incond, $inparams) = $DB->get_in_or_equal($conditions['groupid'], SQL_PARAMS_NAMED, 'gid');
|
|
|
776 |
$params = array_merge($params, $inparams);
|
|
|
777 |
$wheres[] = "quiza.quiz IN (SELECT qo.quiz FROM {quiz_overrides} qo WHERE qo.groupid $incond)";
|
|
|
778 |
list ($incond, $inparams) = $DB->get_in_or_equal($conditions['groupid'], SQL_PARAMS_NAMED, 'igid');
|
|
|
779 |
$params = array_merge($params, $inparams);
|
|
|
780 |
$iwheres[] = "iquiza.quiz IN (SELECT qo.quiz FROM {quiz_overrides} qo WHERE qo.groupid $incond)";
|
|
|
781 |
}
|
|
|
782 |
|
|
|
783 |
// SQL to compute timeclose and timelimit for each attempt:
|
|
|
784 |
$quizausersql = quiz_get_attempt_usertime_sql(
|
|
|
785 |
implode("\n AND ", $iwheres));
|
|
|
786 |
|
|
|
787 |
// SQL to compute the new timecheckstate
|
|
|
788 |
$timecheckstatesql = "
|
|
|
789 |
CASE WHEN quizauser.usertimelimit = 0 AND quizauser.usertimeclose = 0 THEN NULL
|
|
|
790 |
WHEN quizauser.usertimelimit = 0 THEN quizauser.usertimeclose
|
|
|
791 |
WHEN quizauser.usertimeclose = 0 THEN quiza.timestart + quizauser.usertimelimit
|
|
|
792 |
WHEN quiza.timestart + quizauser.usertimelimit < quizauser.usertimeclose THEN quiza.timestart + quizauser.usertimelimit
|
|
|
793 |
ELSE quizauser.usertimeclose END +
|
|
|
794 |
CASE WHEN quiza.state = 'overdue' THEN quiz.graceperiod ELSE 0 END";
|
|
|
795 |
|
|
|
796 |
// SQL to select which attempts to process
|
|
|
797 |
$attemptselect = implode("\n AND ", $wheres);
|
|
|
798 |
|
|
|
799 |
/*
|
|
|
800 |
* Each database handles updates with inner joins differently:
|
|
|
801 |
* - mysql does not allow a FROM clause
|
|
|
802 |
* - postgres and mssql allow FROM but handle table aliases differently
|
|
|
803 |
* - oracle requires a subquery
|
|
|
804 |
*
|
|
|
805 |
* Different code for each database.
|
|
|
806 |
*/
|
|
|
807 |
|
|
|
808 |
$dbfamily = $DB->get_dbfamily();
|
|
|
809 |
if ($dbfamily == 'mysql') {
|
|
|
810 |
$updatesql = "UPDATE {quiz_attempts} quiza
|
|
|
811 |
JOIN {quiz} quiz ON quiz.id = quiza.quiz
|
|
|
812 |
JOIN ( $quizausersql ) quizauser ON quizauser.id = quiza.id
|
|
|
813 |
SET quiza.timecheckstate = $timecheckstatesql
|
|
|
814 |
WHERE $attemptselect";
|
|
|
815 |
} else if ($dbfamily == 'postgres') {
|
|
|
816 |
$updatesql = "UPDATE {quiz_attempts} quiza
|
|
|
817 |
SET timecheckstate = $timecheckstatesql
|
|
|
818 |
FROM {quiz} quiz, ( $quizausersql ) quizauser
|
|
|
819 |
WHERE quiz.id = quiza.quiz
|
|
|
820 |
AND quizauser.id = quiza.id
|
|
|
821 |
AND $attemptselect";
|
|
|
822 |
} else if ($dbfamily == 'mssql') {
|
|
|
823 |
$updatesql = "UPDATE quiza
|
|
|
824 |
SET timecheckstate = $timecheckstatesql
|
|
|
825 |
FROM {quiz_attempts} quiza
|
|
|
826 |
JOIN {quiz} quiz ON quiz.id = quiza.quiz
|
|
|
827 |
JOIN ( $quizausersql ) quizauser ON quizauser.id = quiza.id
|
|
|
828 |
WHERE $attemptselect";
|
|
|
829 |
} else {
|
|
|
830 |
// oracle, sqlite and others
|
|
|
831 |
$updatesql = "UPDATE {quiz_attempts} quiza
|
|
|
832 |
SET timecheckstate = (
|
|
|
833 |
SELECT $timecheckstatesql
|
|
|
834 |
FROM {quiz} quiz, ( $quizausersql ) quizauser
|
|
|
835 |
WHERE quiz.id = quiza.quiz
|
|
|
836 |
AND quizauser.id = quiza.id
|
|
|
837 |
)
|
|
|
838 |
WHERE $attemptselect";
|
|
|
839 |
}
|
|
|
840 |
|
|
|
841 |
$DB->execute($updatesql, $params);
|
|
|
842 |
}
|
|
|
843 |
|
|
|
844 |
/**
|
|
|
845 |
* Returns SQL to compute timeclose and timelimit for every attempt, taking into account user and group overrides.
|
|
|
846 |
* The query used herein is very similar to the one in function quiz_get_user_timeclose, so, in case you
|
|
|
847 |
* would change either one of them, make sure to apply your changes to both.
|
|
|
848 |
*
|
|
|
849 |
* @param string $redundantwhereclauses extra where clauses to add to the subquery
|
|
|
850 |
* for performance. These can use the table alias iquiza for the quiz attempts table.
|
|
|
851 |
* @return string SQL select with columns attempt.id, usertimeclose, usertimelimit.
|
|
|
852 |
*/
|
|
|
853 |
function quiz_get_attempt_usertime_sql($redundantwhereclauses = '') {
|
|
|
854 |
if ($redundantwhereclauses) {
|
|
|
855 |
$redundantwhereclauses = 'WHERE ' . $redundantwhereclauses;
|
|
|
856 |
}
|
|
|
857 |
// The multiple qgo JOINS are necessary because we want timeclose/timelimit = 0 (unlimited) to supercede
|
|
|
858 |
// any other group override
|
|
|
859 |
$quizausersql = "
|
|
|
860 |
SELECT iquiza.id,
|
|
|
861 |
COALESCE(MAX(quo.timeclose), MAX(qgo1.timeclose), MAX(qgo2.timeclose), iquiz.timeclose) AS usertimeclose,
|
|
|
862 |
COALESCE(MAX(quo.timelimit), MAX(qgo3.timelimit), MAX(qgo4.timelimit), iquiz.timelimit) AS usertimelimit
|
|
|
863 |
|
|
|
864 |
FROM {quiz_attempts} iquiza
|
|
|
865 |
JOIN {quiz} iquiz ON iquiz.id = iquiza.quiz
|
|
|
866 |
LEFT JOIN {quiz_overrides} quo ON quo.quiz = iquiza.quiz AND quo.userid = iquiza.userid
|
|
|
867 |
LEFT JOIN {groups_members} gm ON gm.userid = iquiza.userid
|
|
|
868 |
LEFT JOIN {quiz_overrides} qgo1 ON qgo1.quiz = iquiza.quiz AND qgo1.groupid = gm.groupid AND qgo1.timeclose = 0
|
|
|
869 |
LEFT JOIN {quiz_overrides} qgo2 ON qgo2.quiz = iquiza.quiz AND qgo2.groupid = gm.groupid AND qgo2.timeclose > 0
|
|
|
870 |
LEFT JOIN {quiz_overrides} qgo3 ON qgo3.quiz = iquiza.quiz AND qgo3.groupid = gm.groupid AND qgo3.timelimit = 0
|
|
|
871 |
LEFT JOIN {quiz_overrides} qgo4 ON qgo4.quiz = iquiza.quiz AND qgo4.groupid = gm.groupid AND qgo4.timelimit > 0
|
|
|
872 |
$redundantwhereclauses
|
|
|
873 |
GROUP BY iquiza.id, iquiz.id, iquiz.timeclose, iquiz.timelimit";
|
|
|
874 |
return $quizausersql;
|
|
|
875 |
}
|
|
|
876 |
|
|
|
877 |
/**
|
|
|
878 |
* @return array int => lang string the options for calculating the quiz grade
|
|
|
879 |
* from the individual attempt grades.
|
|
|
880 |
*/
|
|
|
881 |
function quiz_get_grading_options() {
|
|
|
882 |
return [
|
|
|
883 |
QUIZ_GRADEHIGHEST => get_string('gradehighest', 'quiz'),
|
|
|
884 |
QUIZ_GRADEAVERAGE => get_string('gradeaverage', 'quiz'),
|
|
|
885 |
QUIZ_ATTEMPTFIRST => get_string('attemptfirst', 'quiz'),
|
|
|
886 |
QUIZ_ATTEMPTLAST => get_string('attemptlast', 'quiz')
|
|
|
887 |
];
|
|
|
888 |
}
|
|
|
889 |
|
|
|
890 |
/**
|
|
|
891 |
* @param int $option one of the values QUIZ_GRADEHIGHEST, QUIZ_GRADEAVERAGE,
|
|
|
892 |
* QUIZ_ATTEMPTFIRST or QUIZ_ATTEMPTLAST.
|
|
|
893 |
* @return the lang string for that option.
|
|
|
894 |
*/
|
|
|
895 |
function quiz_get_grading_option_name($option) {
|
|
|
896 |
$strings = quiz_get_grading_options();
|
|
|
897 |
return $strings[$option];
|
|
|
898 |
}
|
|
|
899 |
|
|
|
900 |
/**
|
|
|
901 |
* @return array string => lang string the options for handling overdue quiz
|
|
|
902 |
* attempts.
|
|
|
903 |
*/
|
|
|
904 |
function quiz_get_overdue_handling_options() {
|
|
|
905 |
return [
|
|
|
906 |
'autosubmit' => get_string('overduehandlingautosubmit', 'quiz'),
|
|
|
907 |
'graceperiod' => get_string('overduehandlinggraceperiod', 'quiz'),
|
|
|
908 |
'autoabandon' => get_string('overduehandlingautoabandon', 'quiz'),
|
|
|
909 |
];
|
|
|
910 |
}
|
|
|
911 |
|
|
|
912 |
/**
|
|
|
913 |
* Get the choices for what size user picture to show.
|
|
|
914 |
* @return array string => lang string the options for whether to display the user's picture.
|
|
|
915 |
*/
|
|
|
916 |
function quiz_get_user_image_options() {
|
|
|
917 |
return [
|
|
|
918 |
QUIZ_SHOWIMAGE_NONE => get_string('shownoimage', 'quiz'),
|
|
|
919 |
QUIZ_SHOWIMAGE_SMALL => get_string('showsmallimage', 'quiz'),
|
|
|
920 |
QUIZ_SHOWIMAGE_LARGE => get_string('showlargeimage', 'quiz'),
|
|
|
921 |
];
|
|
|
922 |
}
|
|
|
923 |
|
|
|
924 |
/**
|
|
|
925 |
* Return an user's timeclose for all quizzes in a course, hereby taking into account group and user overrides.
|
|
|
926 |
*
|
|
|
927 |
* @param int $courseid the course id.
|
|
|
928 |
* @return stdClass An object with of all quizids and close unixdates in this course, taking into account the most lenient
|
|
|
929 |
* overrides, if existing and 0 if no close date is set.
|
|
|
930 |
*/
|
|
|
931 |
function quiz_get_user_timeclose($courseid) {
|
|
|
932 |
global $DB, $USER;
|
|
|
933 |
|
|
|
934 |
// For teacher and manager/admins return timeclose.
|
|
|
935 |
if (has_capability('moodle/course:update', context_course::instance($courseid))) {
|
|
|
936 |
$sql = "SELECT quiz.id, quiz.timeclose AS usertimeclose
|
|
|
937 |
FROM {quiz} quiz
|
|
|
938 |
WHERE quiz.course = :courseid";
|
|
|
939 |
|
|
|
940 |
$results = $DB->get_records_sql($sql, ['courseid' => $courseid]);
|
|
|
941 |
return $results;
|
|
|
942 |
}
|
|
|
943 |
|
|
|
944 |
$sql = "SELECT q.id,
|
|
|
945 |
COALESCE(v.userclose, v.groupclose, q.timeclose, 0) AS usertimeclose
|
|
|
946 |
FROM (
|
|
|
947 |
SELECT quiz.id as quizid,
|
|
|
948 |
MAX(quo.timeclose) AS userclose, MAX(qgo.timeclose) AS groupclose
|
|
|
949 |
FROM {quiz} quiz
|
|
|
950 |
LEFT JOIN {quiz_overrides} quo on quiz.id = quo.quiz AND quo.userid = :userid
|
|
|
951 |
LEFT JOIN {groups_members} gm ON gm.userid = :useringroupid
|
|
|
952 |
LEFT JOIN {quiz_overrides} qgo on quiz.id = qgo.quiz AND qgo.groupid = gm.groupid
|
|
|
953 |
WHERE quiz.course = :courseid
|
|
|
954 |
GROUP BY quiz.id) v
|
|
|
955 |
JOIN {quiz} q ON q.id = v.quizid";
|
|
|
956 |
|
|
|
957 |
$results = $DB->get_records_sql($sql, ['userid' => $USER->id, 'useringroupid' => $USER->id, 'courseid' => $courseid]);
|
|
|
958 |
return $results;
|
|
|
959 |
|
|
|
960 |
}
|
|
|
961 |
|
|
|
962 |
/**
|
|
|
963 |
* Get the choices to offer for the 'Questions per page' option.
|
|
|
964 |
* @return array int => string.
|
|
|
965 |
*/
|
|
|
966 |
function quiz_questions_per_page_options() {
|
|
|
967 |
$pageoptions = [];
|
|
|
968 |
$pageoptions[0] = get_string('neverallononepage', 'quiz');
|
|
|
969 |
$pageoptions[1] = get_string('everyquestion', 'quiz');
|
|
|
970 |
for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION; ++$i) {
|
|
|
971 |
$pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
|
|
|
972 |
}
|
|
|
973 |
return $pageoptions;
|
|
|
974 |
}
|
|
|
975 |
|
|
|
976 |
/**
|
|
|
977 |
* Get the human-readable name for a quiz attempt state.
|
|
|
978 |
* @param string $state one of the state constants like {@see quiz_attempt::IN_PROGRESS}.
|
|
|
979 |
* @return string The lang string to describe that state.
|
|
|
980 |
*/
|
|
|
981 |
function quiz_attempt_state_name($state) {
|
|
|
982 |
switch ($state) {
|
|
|
983 |
case quiz_attempt::IN_PROGRESS:
|
|
|
984 |
return get_string('stateinprogress', 'quiz');
|
|
|
985 |
case quiz_attempt::OVERDUE:
|
|
|
986 |
return get_string('stateoverdue', 'quiz');
|
|
|
987 |
case quiz_attempt::FINISHED:
|
|
|
988 |
return get_string('statefinished', 'quiz');
|
|
|
989 |
case quiz_attempt::ABANDONED:
|
|
|
990 |
return get_string('stateabandoned', 'quiz');
|
|
|
991 |
default:
|
|
|
992 |
throw new coding_exception('Unknown quiz attempt state.');
|
|
|
993 |
}
|
|
|
994 |
}
|
|
|
995 |
|
|
|
996 |
// Other quiz functions ////////////////////////////////////////////////////////
|
|
|
997 |
|
|
|
998 |
/**
|
|
|
999 |
* @param stdClass $quiz the quiz.
|
|
|
1000 |
* @param int $cmid the course_module object for this quiz.
|
|
|
1001 |
* @param stdClass $question the question.
|
|
|
1002 |
* @param string $returnurl url to return to after action is done.
|
|
|
1003 |
* @param int $variant which question variant to preview (optional).
|
|
|
1004 |
* @return string html for a number of icons linked to action pages for a
|
|
|
1005 |
* question - preview and edit / view icons depending on user capabilities.
|
|
|
1006 |
*/
|
|
|
1007 |
function quiz_question_action_icons($quiz, $cmid, $question, $returnurl, $variant = null) {
|
|
|
1008 |
$html = '';
|
|
|
1009 |
if ($question->qtype !== 'random') {
|
|
|
1010 |
$html = quiz_question_preview_button($quiz, $question, false, $variant);
|
|
|
1011 |
}
|
|
|
1012 |
$html .= quiz_question_edit_button($cmid, $question, $returnurl);
|
|
|
1013 |
return $html;
|
|
|
1014 |
}
|
|
|
1015 |
|
|
|
1016 |
/**
|
|
|
1017 |
* @param int $cmid the course_module.id for this quiz.
|
|
|
1018 |
* @param stdClass $question the question.
|
|
|
1019 |
* @param string $returnurl url to return to after action is done.
|
|
|
1020 |
* @param string $contentbeforeicon some HTML content to be added inside the link, before the icon.
|
|
|
1021 |
* @return the HTML for an edit icon, view icon, or nothing for a question
|
|
|
1022 |
* (depending on permissions).
|
|
|
1023 |
*/
|
|
|
1024 |
function quiz_question_edit_button($cmid, $question, $returnurl, $contentaftericon = '') {
|
|
|
1025 |
global $CFG, $OUTPUT;
|
|
|
1026 |
|
|
|
1027 |
// Minor efficiency saving. Only get strings once, even if there are a lot of icons on one page.
|
|
|
1028 |
static $stredit = null;
|
|
|
1029 |
static $strview = null;
|
|
|
1030 |
if ($stredit === null) {
|
|
|
1031 |
$stredit = get_string('edit');
|
|
|
1032 |
$strview = get_string('view');
|
|
|
1033 |
}
|
|
|
1034 |
|
|
|
1035 |
// What sort of icon should we show?
|
|
|
1036 |
$action = '';
|
|
|
1037 |
if (!empty($question->id) &&
|
|
|
1038 |
(question_has_capability_on($question, 'edit') ||
|
|
|
1039 |
question_has_capability_on($question, 'move'))) {
|
|
|
1040 |
$action = $stredit;
|
|
|
1041 |
$icon = 't/edit';
|
|
|
1042 |
} else if (!empty($question->id) &&
|
|
|
1043 |
question_has_capability_on($question, 'view')) {
|
|
|
1044 |
$action = $strview;
|
|
|
1045 |
$icon = 'i/info';
|
|
|
1046 |
}
|
|
|
1047 |
|
|
|
1048 |
// Build the icon.
|
|
|
1049 |
if ($action) {
|
|
|
1050 |
if ($returnurl instanceof moodle_url) {
|
|
|
1051 |
$returnurl = $returnurl->out_as_local_url(false);
|
|
|
1052 |
}
|
|
|
1053 |
$questionparams = ['returnurl' => $returnurl, 'cmid' => $cmid, 'id' => $question->id];
|
|
|
1054 |
$questionurl = new moodle_url("$CFG->wwwroot/question/bank/editquestion/question.php", $questionparams);
|
|
|
1055 |
return '<a title="' . $action . '" href="' . $questionurl->out() . '" class="questioneditbutton">' .
|
|
|
1056 |
$OUTPUT->pix_icon($icon, $action) . $contentaftericon .
|
|
|
1057 |
'</a>';
|
|
|
1058 |
} else if ($contentaftericon) {
|
|
|
1059 |
return '<span class="questioneditbutton">' . $contentaftericon . '</span>';
|
|
|
1060 |
} else {
|
|
|
1061 |
return '';
|
|
|
1062 |
}
|
|
|
1063 |
}
|
|
|
1064 |
|
|
|
1065 |
/**
|
|
|
1066 |
* @param stdClass $quiz the quiz settings
|
|
|
1067 |
* @param stdClass $question the question
|
|
|
1068 |
* @param int $variant which question variant to preview (optional).
|
|
|
1069 |
* @param int $restartversion version of the question to use when restarting the preview.
|
|
|
1070 |
* @return moodle_url to preview this question with the options from this quiz.
|
|
|
1071 |
*/
|
|
|
1072 |
function quiz_question_preview_url($quiz, $question, $variant = null, $restartversion = null) {
|
|
|
1073 |
// Get the appropriate display options.
|
|
|
1074 |
$displayoptions = display_options::make_from_quiz($quiz,
|
|
|
1075 |
display_options::DURING);
|
|
|
1076 |
|
|
|
1077 |
$maxmark = null;
|
|
|
1078 |
if (isset($question->maxmark)) {
|
|
|
1079 |
$maxmark = $question->maxmark;
|
|
|
1080 |
}
|
|
|
1081 |
|
|
|
1082 |
// Work out the correcte preview URL.
|
|
|
1083 |
return \qbank_previewquestion\helper::question_preview_url($question->id, $quiz->preferredbehaviour,
|
|
|
1084 |
$maxmark, $displayoptions, $variant, null, null, $restartversion);
|
|
|
1085 |
}
|
|
|
1086 |
|
|
|
1087 |
/**
|
|
|
1088 |
* @param stdClass $quiz the quiz settings
|
|
|
1089 |
* @param stdClass $question the question
|
|
|
1090 |
* @param bool $label if true, show the preview question label after the icon
|
|
|
1091 |
* @param int $variant which question variant to preview (optional).
|
|
|
1092 |
* @param bool $random if question is random, true.
|
|
|
1093 |
* @return string the HTML for a preview question icon.
|
|
|
1094 |
*/
|
|
|
1095 |
function quiz_question_preview_button($quiz, $question, $label = false, $variant = null, $random = null) {
|
|
|
1096 |
global $PAGE;
|
|
|
1097 |
if (!question_has_capability_on($question, 'use')) {
|
|
|
1098 |
return '';
|
|
|
1099 |
}
|
|
|
1100 |
$structure = quiz_settings::create($quiz->id)->get_structure();
|
|
|
1101 |
if (!empty($question->slot)) {
|
|
|
1102 |
$requestedversion = $structure->get_slot_by_number($question->slot)->requestedversion
|
|
|
1103 |
?? question_preview_options::ALWAYS_LATEST;
|
|
|
1104 |
} else {
|
|
|
1105 |
$requestedversion = question_preview_options::ALWAYS_LATEST;
|
|
|
1106 |
}
|
|
|
1107 |
return $PAGE->get_renderer('mod_quiz', 'edit')->question_preview_icon(
|
|
|
1108 |
$quiz, $question, $label, $variant, $requestedversion);
|
|
|
1109 |
}
|
|
|
1110 |
|
|
|
1111 |
/**
|
|
|
1112 |
* @param stdClass $attempt the attempt.
|
|
|
1113 |
* @param stdClass $context the quiz context.
|
|
|
1114 |
* @return int whether flags should be shown/editable to the current user for this attempt.
|
|
|
1115 |
*/
|
|
|
1116 |
function quiz_get_flag_option($attempt, $context) {
|
|
|
1117 |
global $USER;
|
|
|
1118 |
if (!has_capability('moodle/question:flag', $context)) {
|
|
|
1119 |
return question_display_options::HIDDEN;
|
|
|
1120 |
} else if ($attempt->userid == $USER->id) {
|
|
|
1121 |
return question_display_options::EDITABLE;
|
|
|
1122 |
} else {
|
|
|
1123 |
return question_display_options::VISIBLE;
|
|
|
1124 |
}
|
|
|
1125 |
}
|
|
|
1126 |
|
|
|
1127 |
/**
|
|
|
1128 |
* Work out what state this quiz attempt is in - in the sense used by
|
|
|
1129 |
* quiz_get_review_options, not in the sense of $attempt->state.
|
|
|
1130 |
* @param stdClass $quiz the quiz settings
|
|
|
1131 |
* @param stdClass $attempt the quiz_attempt database row.
|
|
|
1132 |
* @return int one of the display_options::DURING,
|
|
|
1133 |
* IMMEDIATELY_AFTER, LATER_WHILE_OPEN or AFTER_CLOSE constants.
|
|
|
1134 |
*/
|
|
|
1135 |
function quiz_attempt_state($quiz, $attempt) {
|
|
|
1136 |
if ($attempt->state == quiz_attempt::IN_PROGRESS) {
|
|
|
1137 |
return display_options::DURING;
|
|
|
1138 |
} else if ($quiz->timeclose && time() >= $quiz->timeclose) {
|
|
|
1139 |
return display_options::AFTER_CLOSE;
|
|
|
1140 |
} else if (time() < $attempt->timefinish + quiz_attempt::IMMEDIATELY_AFTER_PERIOD) {
|
|
|
1141 |
return display_options::IMMEDIATELY_AFTER;
|
|
|
1142 |
} else {
|
|
|
1143 |
return display_options::LATER_WHILE_OPEN;
|
|
|
1144 |
}
|
|
|
1145 |
}
|
|
|
1146 |
|
|
|
1147 |
/**
|
|
|
1148 |
* The appropriate display_options object for this attempt at this quiz right now.
|
|
|
1149 |
*
|
|
|
1150 |
* @param stdClass $quiz the quiz instance.
|
|
|
1151 |
* @param stdClass $attempt the attempt in question.
|
|
|
1152 |
* @param context $context the quiz context.
|
|
|
1153 |
*
|
|
|
1154 |
* @return display_options
|
|
|
1155 |
*/
|
|
|
1156 |
function quiz_get_review_options($quiz, $attempt, $context) {
|
|
|
1157 |
$options = display_options::make_from_quiz($quiz, quiz_attempt_state($quiz, $attempt));
|
|
|
1158 |
|
|
|
1159 |
$options->readonly = true;
|
|
|
1160 |
$options->flags = quiz_get_flag_option($attempt, $context);
|
|
|
1161 |
if (!empty($attempt->id)) {
|
|
|
1162 |
$options->questionreviewlink = new moodle_url('/mod/quiz/reviewquestion.php',
|
|
|
1163 |
['attempt' => $attempt->id]);
|
|
|
1164 |
}
|
|
|
1165 |
|
|
|
1166 |
// Show a link to the comment box only for closed attempts.
|
|
|
1167 |
if (!empty($attempt->id) && $attempt->state == quiz_attempt::FINISHED && !$attempt->preview &&
|
|
|
1168 |
!is_null($context) && has_capability('mod/quiz:grade', $context)) {
|
|
|
1169 |
$options->manualcomment = question_display_options::VISIBLE;
|
|
|
1170 |
$options->manualcommentlink = new moodle_url('/mod/quiz/comment.php',
|
|
|
1171 |
['attempt' => $attempt->id]);
|
|
|
1172 |
}
|
|
|
1173 |
|
|
|
1174 |
if (!is_null($context) && !$attempt->preview &&
|
|
|
1175 |
has_capability('mod/quiz:viewreports', $context) &&
|
|
|
1176 |
has_capability('moodle/grade:viewhidden', $context)) {
|
|
|
1177 |
// People who can see reports and hidden grades should be shown everything,
|
|
|
1178 |
// except during preview when teachers want to see what students see.
|
|
|
1179 |
$options->attempt = question_display_options::VISIBLE;
|
|
|
1180 |
$options->correctness = question_display_options::VISIBLE;
|
|
|
1181 |
$options->marks = question_display_options::MARK_AND_MAX;
|
|
|
1182 |
$options->feedback = question_display_options::VISIBLE;
|
|
|
1183 |
$options->numpartscorrect = question_display_options::VISIBLE;
|
|
|
1184 |
$options->manualcomment = question_display_options::VISIBLE;
|
|
|
1185 |
$options->generalfeedback = question_display_options::VISIBLE;
|
|
|
1186 |
$options->rightanswer = question_display_options::VISIBLE;
|
|
|
1187 |
$options->overallfeedback = question_display_options::VISIBLE;
|
|
|
1188 |
$options->history = question_display_options::VISIBLE;
|
|
|
1189 |
$options->userinfoinhistory = $attempt->userid;
|
|
|
1190 |
|
|
|
1191 |
}
|
|
|
1192 |
|
|
|
1193 |
return $options;
|
|
|
1194 |
}
|
|
|
1195 |
|
|
|
1196 |
/**
|
|
|
1197 |
* Combines the review options from a number of different quiz attempts.
|
|
|
1198 |
* Returns an array of two ojects, so the suggested way of calling this
|
|
|
1199 |
* funciton is:
|
|
|
1200 |
* list($someoptions, $alloptions) = quiz_get_combined_reviewoptions(...)
|
|
|
1201 |
*
|
|
|
1202 |
* @param stdClass $quiz the quiz instance.
|
|
|
1203 |
* @param array $attempts an array of attempt objects.
|
|
|
1204 |
*
|
|
|
1205 |
* @return array of two options objects, one showing which options are true for
|
|
|
1206 |
* at least one of the attempts, the other showing which options are true
|
|
|
1207 |
* for all attempts.
|
|
|
1208 |
*/
|
|
|
1209 |
function quiz_get_combined_reviewoptions($quiz, $attempts) {
|
|
|
1210 |
$fields = ['feedback', 'generalfeedback', 'rightanswer', 'overallfeedback'];
|
|
|
1211 |
$someoptions = new stdClass();
|
|
|
1212 |
$alloptions = new stdClass();
|
|
|
1213 |
foreach ($fields as $field) {
|
|
|
1214 |
$someoptions->$field = false;
|
|
|
1215 |
$alloptions->$field = true;
|
|
|
1216 |
}
|
|
|
1217 |
$someoptions->marks = question_display_options::HIDDEN;
|
|
|
1218 |
$alloptions->marks = question_display_options::MARK_AND_MAX;
|
|
|
1219 |
|
|
|
1220 |
// This shouldn't happen, but we need to prevent reveal information.
|
|
|
1221 |
if (empty($attempts)) {
|
|
|
1222 |
return [$someoptions, $someoptions];
|
|
|
1223 |
}
|
|
|
1224 |
|
|
|
1225 |
foreach ($attempts as $attempt) {
|
|
|
1226 |
$attemptoptions = display_options::make_from_quiz($quiz,
|
|
|
1227 |
quiz_attempt_state($quiz, $attempt));
|
|
|
1228 |
foreach ($fields as $field) {
|
|
|
1229 |
$someoptions->$field = $someoptions->$field || $attemptoptions->$field;
|
|
|
1230 |
$alloptions->$field = $alloptions->$field && $attemptoptions->$field;
|
|
|
1231 |
}
|
|
|
1232 |
$someoptions->marks = max($someoptions->marks, $attemptoptions->marks);
|
|
|
1233 |
$alloptions->marks = min($alloptions->marks, $attemptoptions->marks);
|
|
|
1234 |
}
|
|
|
1235 |
return [$someoptions, $alloptions];
|
|
|
1236 |
}
|
|
|
1237 |
|
|
|
1238 |
// Functions for sending notification messages /////////////////////////////////
|
|
|
1239 |
|
|
|
1240 |
/**
|
|
|
1241 |
* Sends a confirmation message to the student confirming that the attempt was processed.
|
|
|
1242 |
*
|
|
|
1243 |
* @param stdClass $recipient user object for the recipient.
|
|
|
1244 |
* @param stdClass $a lots of useful information that can be used in the message
|
|
|
1245 |
* subject and body.
|
|
|
1246 |
* @param bool $studentisonline is the student currently interacting with Moodle?
|
|
|
1247 |
*
|
|
|
1248 |
* @return int|false as for {@link message_send()}.
|
|
|
1249 |
*/
|
|
|
1250 |
function quiz_send_confirmation($recipient, $a, $studentisonline) {
|
|
|
1251 |
|
|
|
1252 |
// Add information about the recipient to $a.
|
|
|
1253 |
// Don't do idnumber. we want idnumber to be the submitter's idnumber.
|
|
|
1254 |
$a->username = fullname($recipient);
|
|
|
1255 |
$a->userusername = $recipient->username;
|
|
|
1256 |
|
|
|
1257 |
// Prepare the message.
|
|
|
1258 |
$eventdata = new \core\message\message();
|
|
|
1259 |
$eventdata->courseid = $a->courseid;
|
|
|
1260 |
$eventdata->component = 'mod_quiz';
|
|
|
1261 |
$eventdata->name = 'confirmation';
|
|
|
1262 |
$eventdata->notification = 1;
|
|
|
1263 |
|
|
|
1264 |
$eventdata->userfrom = core_user::get_noreply_user();
|
|
|
1265 |
$eventdata->userto = $recipient;
|
|
|
1266 |
$eventdata->subject = get_string('emailconfirmsubject', 'quiz', $a);
|
|
|
1267 |
|
|
|
1268 |
if ($studentisonline) {
|
|
|
1269 |
$eventdata->fullmessage = get_string('emailconfirmbody', 'quiz', $a);
|
|
|
1270 |
} else {
|
|
|
1271 |
$eventdata->fullmessage = get_string('emailconfirmbodyautosubmit', 'quiz', $a);
|
|
|
1272 |
}
|
|
|
1273 |
|
|
|
1274 |
$eventdata->fullmessageformat = FORMAT_PLAIN;
|
|
|
1275 |
$eventdata->fullmessagehtml = '';
|
|
|
1276 |
|
|
|
1277 |
$eventdata->smallmessage = get_string('emailconfirmsmall', 'quiz', $a);
|
|
|
1278 |
$eventdata->contexturl = $a->quizurl;
|
|
|
1279 |
$eventdata->contexturlname = $a->quizname;
|
|
|
1280 |
$eventdata->customdata = [
|
|
|
1281 |
'cmid' => $a->quizcmid,
|
|
|
1282 |
'instance' => $a->quizid,
|
|
|
1283 |
'attemptid' => $a->attemptid,
|
|
|
1284 |
];
|
|
|
1285 |
|
|
|
1286 |
// ... and send it.
|
|
|
1287 |
return message_send($eventdata);
|
|
|
1288 |
}
|
|
|
1289 |
|
|
|
1290 |
/**
|
|
|
1291 |
* Sends notification messages to the interested parties that assign the role capability
|
|
|
1292 |
*
|
|
|
1293 |
* @param stdClass $recipient user object of the intended recipient
|
|
|
1294 |
* @param stdClass $submitter user object for the user who submitted the attempt.
|
|
|
1295 |
* @param stdClass $a associative array of replaceable fields for the templates
|
|
|
1296 |
*
|
|
|
1297 |
* @return int|false as for {@link message_send()}.
|
|
|
1298 |
*/
|
|
|
1299 |
function quiz_send_notification($recipient, $submitter, $a) {
|
|
|
1300 |
global $PAGE;
|
|
|
1301 |
|
|
|
1302 |
// Recipient info for template.
|
|
|
1303 |
$a->useridnumber = $recipient->idnumber;
|
|
|
1304 |
$a->username = fullname($recipient);
|
|
|
1305 |
$a->userusername = $recipient->username;
|
|
|
1306 |
|
|
|
1307 |
// Prepare the message.
|
|
|
1308 |
$eventdata = new \core\message\message();
|
|
|
1309 |
$eventdata->courseid = $a->courseid;
|
|
|
1310 |
$eventdata->component = 'mod_quiz';
|
|
|
1311 |
$eventdata->name = 'submission';
|
|
|
1312 |
$eventdata->notification = 1;
|
|
|
1313 |
|
|
|
1314 |
$eventdata->userfrom = $submitter;
|
|
|
1315 |
$eventdata->userto = $recipient;
|
|
|
1316 |
$eventdata->subject = get_string('emailnotifysubject', 'quiz', $a);
|
|
|
1317 |
$eventdata->fullmessage = get_string('emailnotifybody', 'quiz', $a);
|
|
|
1318 |
$eventdata->fullmessageformat = FORMAT_PLAIN;
|
|
|
1319 |
$eventdata->fullmessagehtml = '';
|
|
|
1320 |
|
|
|
1321 |
$eventdata->smallmessage = get_string('emailnotifysmall', 'quiz', $a);
|
|
|
1322 |
$eventdata->contexturl = $a->quizreviewurl;
|
|
|
1323 |
$eventdata->contexturlname = $a->quizname;
|
|
|
1324 |
$userpicture = new user_picture($submitter);
|
|
|
1325 |
$userpicture->size = 1; // Use f1 size.
|
|
|
1326 |
$userpicture->includetoken = $recipient->id; // Generate an out-of-session token for the user receiving the message.
|
|
|
1327 |
$eventdata->customdata = [
|
|
|
1328 |
'cmid' => $a->quizcmid,
|
|
|
1329 |
'instance' => $a->quizid,
|
|
|
1330 |
'attemptid' => $a->attemptid,
|
|
|
1331 |
'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
|
|
|
1332 |
];
|
|
|
1333 |
|
|
|
1334 |
// ... and send it.
|
|
|
1335 |
return message_send($eventdata);
|
|
|
1336 |
}
|
|
|
1337 |
|
|
|
1338 |
/**
|
|
|
1339 |
* Send all the requried messages when a quiz attempt is submitted.
|
|
|
1340 |
*
|
|
|
1341 |
* @param stdClass $course the course
|
|
|
1342 |
* @param stdClass $quiz the quiz
|
|
|
1343 |
* @param stdClass $attempt this attempt just finished
|
|
|
1344 |
* @param stdClass $context the quiz context
|
|
|
1345 |
* @param stdClass $cm the coursemodule for this quiz
|
|
|
1346 |
* @param bool $studentisonline is the student currently interacting with Moodle?
|
|
|
1347 |
*
|
|
|
1348 |
* @return bool true if all necessary messages were sent successfully, else false.
|
|
|
1349 |
*/
|
|
|
1350 |
function quiz_send_notification_messages($course, $quiz, $attempt, $context, $cm, $studentisonline) {
|
|
|
1351 |
global $CFG, $DB;
|
|
|
1352 |
|
|
|
1353 |
// Do nothing if required objects not present.
|
|
|
1354 |
if (empty($course) or empty($quiz) or empty($attempt) or empty($context)) {
|
|
|
1355 |
throw new coding_exception('$course, $quiz, $attempt, $context and $cm must all be set.');
|
|
|
1356 |
}
|
|
|
1357 |
|
|
|
1358 |
$submitter = $DB->get_record('user', ['id' => $attempt->userid], '*', MUST_EXIST);
|
|
|
1359 |
|
|
|
1360 |
// Check for confirmation required.
|
|
|
1361 |
$sendconfirm = false;
|
|
|
1362 |
$notifyexcludeusers = '';
|
|
|
1363 |
if (has_capability('mod/quiz:emailconfirmsubmission', $context, $submitter, false)) {
|
|
|
1364 |
$notifyexcludeusers = $submitter->id;
|
|
|
1365 |
$sendconfirm = true;
|
|
|
1366 |
}
|
|
|
1367 |
|
|
|
1368 |
// Check for notifications required.
|
|
|
1369 |
$notifyfields = 'u.id, u.username, u.idnumber, u.email, u.emailstop, u.lang,
|
|
|
1370 |
u.timezone, u.mailformat, u.maildisplay, u.auth, u.suspended, u.deleted, ';
|
|
|
1371 |
$userfieldsapi = \core_user\fields::for_name();
|
|
|
1372 |
$notifyfields .= $userfieldsapi->get_sql('u', false, '', '', false)->selects;
|
|
|
1373 |
$groups = groups_get_all_groups($course->id, $submitter->id, $cm->groupingid);
|
|
|
1374 |
if (is_array($groups) && count($groups) > 0) {
|
|
|
1375 |
$groups = array_keys($groups);
|
|
|
1376 |
} else if (groups_get_activity_groupmode($cm, $course) != NOGROUPS) {
|
|
|
1377 |
// If the user is not in a group, and the quiz is set to group mode,
|
|
|
1378 |
// then set $groups to a non-existant id so that only users with
|
|
|
1379 |
// 'moodle/site:accessallgroups' get notified.
|
|
|
1380 |
$groups = -1;
|
|
|
1381 |
} else {
|
|
|
1382 |
$groups = '';
|
|
|
1383 |
}
|
|
|
1384 |
$userstonotify = get_users_by_capability($context, 'mod/quiz:emailnotifysubmission',
|
|
|
1385 |
$notifyfields, '', '', '', $groups, $notifyexcludeusers, false, false, true);
|
|
|
1386 |
|
|
|
1387 |
if (empty($userstonotify) && !$sendconfirm) {
|
|
|
1388 |
return true; // Nothing to do.
|
|
|
1389 |
}
|
|
|
1390 |
|
|
|
1391 |
$a = new stdClass();
|
|
|
1392 |
// Course info.
|
|
|
1393 |
$a->courseid = $course->id;
|
|
|
1394 |
$a->coursename = $course->fullname;
|
|
|
1395 |
$a->courseshortname = $course->shortname;
|
|
|
1396 |
// Quiz info.
|
|
|
1397 |
$a->quizname = $quiz->name;
|
|
|
1398 |
$a->quizreporturl = $CFG->wwwroot . '/mod/quiz/report.php?id=' . $cm->id;
|
|
|
1399 |
$a->quizreportlink = '<a href="' . $a->quizreporturl . '">' .
|
|
|
1400 |
format_string($quiz->name) . ' report</a>';
|
|
|
1401 |
$a->quizurl = $CFG->wwwroot . '/mod/quiz/view.php?id=' . $cm->id;
|
|
|
1402 |
$a->quizlink = '<a href="' . $a->quizurl . '">' . format_string($quiz->name) . '</a>';
|
|
|
1403 |
$a->quizid = $quiz->id;
|
|
|
1404 |
$a->quizcmid = $cm->id;
|
|
|
1405 |
// Attempt info.
|
|
|
1406 |
$a->submissiontime = userdate($attempt->timefinish);
|
|
|
1407 |
$a->timetaken = format_time($attempt->timefinish - $attempt->timestart);
|
|
|
1408 |
$a->quizreviewurl = $CFG->wwwroot . '/mod/quiz/review.php?attempt=' . $attempt->id;
|
|
|
1409 |
$a->quizreviewlink = '<a href="' . $a->quizreviewurl . '">' .
|
|
|
1410 |
format_string($quiz->name) . ' review</a>';
|
|
|
1411 |
$a->attemptid = $attempt->id;
|
|
|
1412 |
// Student who sat the quiz info.
|
|
|
1413 |
$a->studentidnumber = $submitter->idnumber;
|
|
|
1414 |
$a->studentname = fullname($submitter);
|
|
|
1415 |
$a->studentusername = $submitter->username;
|
|
|
1416 |
|
|
|
1417 |
$allok = true;
|
|
|
1418 |
|
|
|
1419 |
// Send notifications if required.
|
|
|
1420 |
if (!empty($userstonotify)) {
|
|
|
1421 |
foreach ($userstonotify as $recipient) {
|
|
|
1422 |
$allok = $allok && quiz_send_notification($recipient, $submitter, $a);
|
|
|
1423 |
}
|
|
|
1424 |
}
|
|
|
1425 |
|
|
|
1426 |
// Send confirmation if required. We send the student confirmation last, so
|
|
|
1427 |
// that if message sending is being intermittently buggy, which means we send
|
|
|
1428 |
// some but not all messages, and then try again later, then teachers may get
|
|
|
1429 |
// duplicate messages, but the student will always get exactly one.
|
|
|
1430 |
if ($sendconfirm) {
|
|
|
1431 |
$allok = $allok && quiz_send_confirmation($submitter, $a, $studentisonline);
|
|
|
1432 |
}
|
|
|
1433 |
|
|
|
1434 |
return $allok;
|
|
|
1435 |
}
|
|
|
1436 |
|
|
|
1437 |
/**
|
|
|
1438 |
* Send the notification message when a quiz attempt becomes overdue.
|
|
|
1439 |
*
|
|
|
1440 |
* @param quiz_attempt $attemptobj all the data about the quiz attempt.
|
|
|
1441 |
*/
|
|
|
1442 |
function quiz_send_overdue_message($attemptobj) {
|
|
|
1443 |
global $CFG, $DB;
|
|
|
1444 |
|
|
|
1445 |
$submitter = $DB->get_record('user', ['id' => $attemptobj->get_userid()], '*', MUST_EXIST);
|
|
|
1446 |
|
|
|
1447 |
if (!$attemptobj->has_capability('mod/quiz:emailwarnoverdue', $submitter->id, false)) {
|
|
|
1448 |
return; // Message not required.
|
|
|
1449 |
}
|
|
|
1450 |
|
|
|
1451 |
if (!$attemptobj->has_response_to_at_least_one_graded_question()) {
|
|
|
1452 |
return; // Message not required.
|
|
|
1453 |
}
|
|
|
1454 |
|
|
|
1455 |
// Prepare lots of useful information that admins might want to include in
|
|
|
1456 |
// the email message.
|
|
|
1457 |
$quizname = format_string($attemptobj->get_quiz_name());
|
|
|
1458 |
|
|
|
1459 |
$deadlines = [];
|
|
|
1460 |
if ($attemptobj->get_quiz()->timelimit) {
|
|
|
1461 |
$deadlines[] = $attemptobj->get_attempt()->timestart + $attemptobj->get_quiz()->timelimit;
|
|
|
1462 |
}
|
|
|
1463 |
if ($attemptobj->get_quiz()->timeclose) {
|
|
|
1464 |
$deadlines[] = $attemptobj->get_quiz()->timeclose;
|
|
|
1465 |
}
|
|
|
1466 |
$duedate = min($deadlines);
|
|
|
1467 |
$graceend = $duedate + $attemptobj->get_quiz()->graceperiod;
|
|
|
1468 |
|
|
|
1469 |
$a = new stdClass();
|
|
|
1470 |
// Course info.
|
|
|
1471 |
$a->courseid = $attemptobj->get_course()->id;
|
|
|
1472 |
$a->coursename = format_string($attemptobj->get_course()->fullname);
|
|
|
1473 |
$a->courseshortname = format_string($attemptobj->get_course()->shortname);
|
|
|
1474 |
// Quiz info.
|
|
|
1475 |
$a->quizname = $quizname;
|
|
|
1476 |
$a->quizurl = $attemptobj->view_url()->out(false);
|
|
|
1477 |
$a->quizlink = '<a href="' . $a->quizurl . '">' . $quizname . '</a>';
|
|
|
1478 |
// Attempt info.
|
|
|
1479 |
$a->attemptduedate = userdate($duedate);
|
|
|
1480 |
$a->attemptgraceend = userdate($graceend);
|
|
|
1481 |
$a->attemptsummaryurl = $attemptobj->summary_url()->out(false);
|
|
|
1482 |
$a->attemptsummarylink = '<a href="' . $a->attemptsummaryurl . '">' . $quizname . ' review</a>';
|
|
|
1483 |
// Student's info.
|
|
|
1484 |
$a->studentidnumber = $submitter->idnumber;
|
|
|
1485 |
$a->studentname = fullname($submitter);
|
|
|
1486 |
$a->studentusername = $submitter->username;
|
|
|
1487 |
|
|
|
1488 |
// Prepare the message.
|
|
|
1489 |
$eventdata = new \core\message\message();
|
|
|
1490 |
$eventdata->courseid = $a->courseid;
|
|
|
1491 |
$eventdata->component = 'mod_quiz';
|
|
|
1492 |
$eventdata->name = 'attempt_overdue';
|
|
|
1493 |
$eventdata->notification = 1;
|
|
|
1494 |
|
|
|
1495 |
$eventdata->userfrom = core_user::get_noreply_user();
|
|
|
1496 |
$eventdata->userto = $submitter;
|
|
|
1497 |
$eventdata->subject = get_string('emailoverduesubject', 'quiz', $a);
|
|
|
1498 |
$eventdata->fullmessage = get_string('emailoverduebody', 'quiz', $a);
|
|
|
1499 |
$eventdata->fullmessageformat = FORMAT_PLAIN;
|
|
|
1500 |
$eventdata->fullmessagehtml = '';
|
|
|
1501 |
|
|
|
1502 |
$eventdata->smallmessage = get_string('emailoverduesmall', 'quiz', $a);
|
|
|
1503 |
$eventdata->contexturl = $a->quizurl;
|
|
|
1504 |
$eventdata->contexturlname = $a->quizname;
|
|
|
1505 |
$eventdata->customdata = [
|
|
|
1506 |
'cmid' => $attemptobj->get_cmid(),
|
|
|
1507 |
'instance' => $attemptobj->get_quizid(),
|
|
|
1508 |
'attemptid' => $attemptobj->get_attemptid(),
|
|
|
1509 |
];
|
|
|
1510 |
|
|
|
1511 |
// Send the message.
|
|
|
1512 |
return message_send($eventdata);
|
|
|
1513 |
}
|
|
|
1514 |
|
|
|
1515 |
/**
|
|
|
1516 |
* Handle the quiz_attempt_submitted event.
|
|
|
1517 |
*
|
|
|
1518 |
* This sends the confirmation and notification messages, if required.
|
|
|
1519 |
*
|
|
|
1520 |
* @param attempt_submitted $event the event object.
|
|
|
1521 |
*/
|
|
|
1522 |
function quiz_attempt_submitted_handler($event) {
|
|
|
1523 |
$course = get_course($event->courseid);
|
|
|
1524 |
$attempt = $event->get_record_snapshot('quiz_attempts', $event->objectid);
|
|
|
1525 |
$quiz = $event->get_record_snapshot('quiz', $attempt->quiz);
|
|
|
1526 |
$cm = get_coursemodule_from_id('quiz', $event->get_context()->instanceid, $event->courseid);
|
|
|
1527 |
$eventdata = $event->get_data();
|
|
|
1528 |
|
|
|
1529 |
if (!($course && $quiz && $cm && $attempt)) {
|
|
|
1530 |
// Something has been deleted since the event was raised. Therefore, the
|
|
|
1531 |
// event is no longer relevant.
|
|
|
1532 |
return true;
|
|
|
1533 |
}
|
|
|
1534 |
|
|
|
1535 |
// Update completion state.
|
|
|
1536 |
$completion = new completion_info($course);
|
|
|
1537 |
if ($completion->is_enabled($cm) &&
|
|
|
1538 |
($quiz->completionattemptsexhausted || $quiz->completionminattempts)) {
|
|
|
1539 |
$completion->update_state($cm, COMPLETION_COMPLETE, $event->userid);
|
|
|
1540 |
}
|
|
|
1541 |
return quiz_send_notification_messages($course, $quiz, $attempt,
|
|
|
1542 |
context_module::instance($cm->id), $cm, $eventdata['other']['studentisonline']);
|
|
|
1543 |
}
|
|
|
1544 |
|
|
|
1545 |
/**
|
|
|
1546 |
* Send the notification message when a quiz attempt has been manual graded.
|
|
|
1547 |
*
|
|
|
1548 |
* @param quiz_attempt $attemptobj Some data about the quiz attempt.
|
|
|
1549 |
* @param stdClass $userto
|
|
|
1550 |
* @return int|false As for message_send.
|
|
|
1551 |
*/
|
|
|
1552 |
function quiz_send_notify_manual_graded_message(quiz_attempt $attemptobj, object $userto): ?int {
|
|
|
1553 |
global $CFG;
|
|
|
1554 |
|
|
|
1555 |
$quizname = format_string($attemptobj->get_quiz_name());
|
|
|
1556 |
|
|
|
1557 |
$a = new stdClass();
|
|
|
1558 |
// Course info.
|
|
|
1559 |
$a->courseid = $attemptobj->get_courseid();
|
|
|
1560 |
$a->coursename = format_string($attemptobj->get_course()->fullname);
|
|
|
1561 |
// Quiz info.
|
|
|
1562 |
$a->quizname = $quizname;
|
|
|
1563 |
$a->quizurl = $CFG->wwwroot . '/mod/quiz/view.php?id=' . $attemptobj->get_cmid();
|
|
|
1564 |
|
|
|
1565 |
// Attempt info.
|
|
|
1566 |
$a->attempttimefinish = userdate($attemptobj->get_attempt()->timefinish);
|
|
|
1567 |
// Student's info.
|
|
|
1568 |
$a->studentidnumber = $userto->idnumber;
|
|
|
1569 |
$a->studentname = fullname($userto);
|
|
|
1570 |
|
|
|
1571 |
$eventdata = new \core\message\message();
|
|
|
1572 |
$eventdata->component = 'mod_quiz';
|
|
|
1573 |
$eventdata->name = 'attempt_grading_complete';
|
|
|
1574 |
$eventdata->userfrom = core_user::get_noreply_user();
|
|
|
1575 |
$eventdata->userto = $userto;
|
|
|
1576 |
|
|
|
1577 |
$eventdata->subject = get_string('emailmanualgradedsubject', 'quiz', $a);
|
|
|
1578 |
$eventdata->fullmessage = get_string('emailmanualgradedbody', 'quiz', $a);
|
|
|
1579 |
$eventdata->fullmessageformat = FORMAT_PLAIN;
|
|
|
1580 |
$eventdata->fullmessagehtml = '';
|
|
|
1581 |
|
|
|
1582 |
$eventdata->notification = 1;
|
|
|
1583 |
$eventdata->contexturl = $a->quizurl;
|
|
|
1584 |
$eventdata->contexturlname = $a->quizname;
|
|
|
1585 |
|
|
|
1586 |
// Send the message.
|
|
|
1587 |
return message_send($eventdata);
|
|
|
1588 |
}
|
|
|
1589 |
|
|
|
1590 |
|
|
|
1591 |
/**
|
|
|
1592 |
* Logic to happen when a/some group(s) has/have been deleted in a course.
|
|
|
1593 |
*
|
|
|
1594 |
* @param int $courseid The course ID.
|
|
|
1595 |
* @return void
|
|
|
1596 |
*/
|
|
|
1597 |
function quiz_process_group_deleted_in_course($courseid) {
|
|
|
1598 |
$affectedquizzes = override_manager::delete_orphaned_group_overrides_in_course($courseid);
|
|
|
1599 |
|
|
|
1600 |
if (!empty($affectedquizzes)) {
|
|
|
1601 |
quiz_update_open_attempts(['quizid' => $affectedquizzes]);
|
|
|
1602 |
}
|
|
|
1603 |
}
|
|
|
1604 |
|
|
|
1605 |
/**
|
|
|
1606 |
* Get the information about the standard quiz JavaScript module.
|
|
|
1607 |
* @return array a standard jsmodule structure.
|
|
|
1608 |
*/
|
|
|
1609 |
function quiz_get_js_module() {
|
|
|
1610 |
global $PAGE;
|
|
|
1611 |
|
|
|
1612 |
return [
|
|
|
1613 |
'name' => 'mod_quiz',
|
|
|
1614 |
'fullpath' => '/mod/quiz/module.js',
|
|
|
1615 |
'requires' => ['base', 'dom', 'event-delegate', 'event-key',
|
|
|
1616 |
'core_question_engine'],
|
|
|
1617 |
'strings' => [
|
|
|
1618 |
['cancel', 'moodle'],
|
|
|
1619 |
['flagged', 'question'],
|
|
|
1620 |
['functiondisabledbysecuremode', 'quiz'],
|
|
|
1621 |
['startattempt', 'quiz'],
|
|
|
1622 |
['timesup', 'quiz'],
|
|
|
1623 |
['show', 'moodle'],
|
|
|
1624 |
['hide', 'moodle'],
|
|
|
1625 |
],
|
|
|
1626 |
];
|
|
|
1627 |
}
|
|
|
1628 |
|
|
|
1629 |
|
|
|
1630 |
/**
|
|
|
1631 |
* Creates a textual representation of a question for display.
|
|
|
1632 |
*
|
|
|
1633 |
* @param stdClass $question A question object from the database questions table
|
|
|
1634 |
* @param bool $showicon If true, show the question's icon with the question. False by default.
|
|
|
1635 |
* @param bool $showquestiontext If true (default), show question text after question name.
|
|
|
1636 |
* If false, show only question name.
|
|
|
1637 |
* @param bool $showidnumber If true, show the question's idnumber, if any. False by default.
|
|
|
1638 |
* @param core_tag_tag[]|bool $showtags if array passed, show those tags. Else, if true, get and show tags,
|
|
|
1639 |
* else, don't show tags (which is the default).
|
|
|
1640 |
* @return string HTML fragment.
|
|
|
1641 |
*/
|
|
|
1642 |
function quiz_question_tostring($question, $showicon = false, $showquestiontext = true,
|
|
|
1643 |
$showidnumber = false, $showtags = false) {
|
|
|
1644 |
global $OUTPUT;
|
|
|
1645 |
$result = '';
|
|
|
1646 |
|
|
|
1647 |
// Question name.
|
|
|
1648 |
$name = shorten_text(format_string($question->name), 200);
|
|
|
1649 |
if ($showicon) {
|
|
|
1650 |
$name .= print_question_icon($question) . ' ' . $name;
|
|
|
1651 |
}
|
|
|
1652 |
$result .= html_writer::span($name, 'questionname');
|
|
|
1653 |
|
|
|
1654 |
// Question idnumber.
|
|
|
1655 |
if ($showidnumber && $question->idnumber !== null && $question->idnumber !== '') {
|
|
|
1656 |
$result .= ' ' . html_writer::span(
|
|
|
1657 |
html_writer::span(get_string('idnumber', 'question'), 'accesshide') .
|
|
|
1658 |
' ' . s($question->idnumber), 'badge bg-primary text-white');
|
|
|
1659 |
}
|
|
|
1660 |
|
|
|
1661 |
// Question tags.
|
|
|
1662 |
if (is_array($showtags)) {
|
|
|
1663 |
$tags = $showtags;
|
|
|
1664 |
} else if ($showtags) {
|
|
|
1665 |
$tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
|
|
|
1666 |
} else {
|
|
|
1667 |
$tags = [];
|
|
|
1668 |
}
|
|
|
1669 |
if ($tags) {
|
|
|
1670 |
$result .= $OUTPUT->tag_list($tags, null, 'd-inline', 0, null, true);
|
|
|
1671 |
}
|
|
|
1672 |
|
|
|
1673 |
// Question text.
|
|
|
1674 |
if ($showquestiontext) {
|
|
|
1675 |
$questiontext = question_utils::to_plain_text($question->questiontext,
|
|
|
1676 |
$question->questiontextformat, ['noclean' => true, 'para' => false, 'filter' => false]);
|
|
|
1677 |
$questiontext = shorten_text($questiontext, 50);
|
|
|
1678 |
if ($questiontext) {
|
|
|
1679 |
$result .= ' ' . html_writer::span(s($questiontext), 'questiontext');
|
|
|
1680 |
}
|
|
|
1681 |
}
|
|
|
1682 |
|
|
|
1683 |
return $result;
|
|
|
1684 |
}
|
|
|
1685 |
|
|
|
1686 |
/**
|
|
|
1687 |
* Verify that the question exists, and the user has permission to use it.
|
|
|
1688 |
* Does not return. Throws an exception if the question cannot be used.
|
|
|
1689 |
* @param int $questionid The id of the question.
|
|
|
1690 |
*/
|
|
|
1691 |
function quiz_require_question_use($questionid) {
|
|
|
1692 |
global $DB;
|
|
|
1693 |
$question = $DB->get_record('question', ['id' => $questionid], '*', MUST_EXIST);
|
|
|
1694 |
question_require_capability_on($question, 'use');
|
|
|
1695 |
}
|
|
|
1696 |
|
|
|
1697 |
/**
|
|
|
1698 |
* Add a question to a quiz
|
|
|
1699 |
*
|
|
|
1700 |
* Adds a question to a quiz by updating $quiz as well as the
|
|
|
1701 |
* quiz and quiz_slots tables. It also adds a page break if required.
|
|
|
1702 |
* @param int $questionid The id of the question to be added
|
|
|
1703 |
* @param stdClass $quiz The extended quiz object as used by edit.php
|
|
|
1704 |
* This is updated by this function
|
|
|
1705 |
* @param int $page Which page in quiz to add the question on. If 0 (default),
|
|
|
1706 |
* add at the end
|
|
|
1707 |
* @param float $maxmark The maximum mark to set for this question. (Optional,
|
|
|
1708 |
* defaults to question.defaultmark.
|
|
|
1709 |
* @return bool false if the question was already in the quiz
|
|
|
1710 |
*/
|
|
|
1711 |
function quiz_add_quiz_question($questionid, $quiz, $page = 0, $maxmark = null) {
|
|
|
1712 |
global $DB;
|
|
|
1713 |
|
|
|
1714 |
if (!isset($quiz->cmid)) {
|
|
|
1715 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id, $quiz->course);
|
|
|
1716 |
$quiz->cmid = $cm->id;
|
|
|
1717 |
}
|
|
|
1718 |
|
|
|
1719 |
// Make sue the question is not of the "random" type.
|
|
|
1720 |
$questiontype = $DB->get_field('question', 'qtype', ['id' => $questionid]);
|
|
|
1721 |
if ($questiontype == 'random') {
|
|
|
1722 |
throw new coding_exception(
|
|
|
1723 |
'Adding "random" questions via quiz_add_quiz_question() is deprecated. Please use quiz_add_random_questions().'
|
|
|
1724 |
);
|
|
|
1725 |
}
|
|
|
1726 |
|
|
|
1727 |
$trans = $DB->start_delegated_transaction();
|
|
|
1728 |
|
|
|
1729 |
$sql = "SELECT qbe.id
|
|
|
1730 |
FROM {quiz_slots} slot
|
|
|
1731 |
JOIN {question_references} qr ON qr.itemid = slot.id
|
|
|
1732 |
JOIN {question_bank_entries} qbe ON qbe.id = qr.questionbankentryid
|
|
|
1733 |
WHERE slot.quizid = ?
|
|
|
1734 |
AND qr.component = ?
|
|
|
1735 |
AND qr.questionarea = ?
|
|
|
1736 |
AND qr.usingcontextid = ?";
|
|
|
1737 |
|
|
|
1738 |
$questionslots = $DB->get_records_sql($sql, [$quiz->id, 'mod_quiz', 'slot',
|
|
|
1739 |
context_module::instance($quiz->cmid)->id]);
|
|
|
1740 |
|
|
|
1741 |
$currententry = get_question_bank_entry($questionid);
|
|
|
1742 |
|
|
|
1743 |
if (array_key_exists($currententry->id, $questionslots)) {
|
|
|
1744 |
$trans->allow_commit();
|
|
|
1745 |
return false;
|
|
|
1746 |
}
|
|
|
1747 |
|
|
|
1748 |
$sql = "SELECT slot.slot, slot.page, slot.id
|
|
|
1749 |
FROM {quiz_slots} slot
|
|
|
1750 |
WHERE slot.quizid = ?
|
|
|
1751 |
ORDER BY slot.slot";
|
|
|
1752 |
|
|
|
1753 |
$slots = $DB->get_records_sql($sql, [$quiz->id]);
|
|
|
1754 |
|
|
|
1755 |
$maxpage = 1;
|
|
|
1756 |
$numonlastpage = 0;
|
|
|
1757 |
foreach ($slots as $slot) {
|
|
|
1758 |
if ($slot->page > $maxpage) {
|
|
|
1759 |
$maxpage = $slot->page;
|
|
|
1760 |
$numonlastpage = 1;
|
|
|
1761 |
} else {
|
|
|
1762 |
$numonlastpage += 1;
|
|
|
1763 |
}
|
|
|
1764 |
}
|
|
|
1765 |
|
|
|
1766 |
// Add the new instance.
|
|
|
1767 |
$slot = new stdClass();
|
|
|
1768 |
$slot->quizid = $quiz->id;
|
|
|
1769 |
|
|
|
1770 |
if ($maxmark !== null) {
|
|
|
1771 |
$slot->maxmark = $maxmark;
|
|
|
1772 |
} else {
|
|
|
1773 |
$slot->maxmark = $DB->get_field('question', 'defaultmark', ['id' => $questionid]);
|
|
|
1774 |
}
|
|
|
1775 |
|
|
|
1776 |
if (is_int($page) && $page >= 1) {
|
|
|
1777 |
// Adding on a given page.
|
|
|
1778 |
$lastslotbefore = 0;
|
|
|
1779 |
foreach (array_reverse($slots) as $otherslot) {
|
|
|
1780 |
if ($otherslot->page > $page) {
|
|
|
1781 |
$DB->set_field('quiz_slots', 'slot', $otherslot->slot + 1, ['id' => $otherslot->id]);
|
|
|
1782 |
} else {
|
|
|
1783 |
$lastslotbefore = $otherslot->slot;
|
|
|
1784 |
break;
|
|
|
1785 |
}
|
|
|
1786 |
}
|
|
|
1787 |
$slot->slot = $lastslotbefore + 1;
|
|
|
1788 |
$slot->page = min($page, $maxpage + 1);
|
|
|
1789 |
|
|
|
1790 |
quiz_update_section_firstslots($quiz->id, 1, max($lastslotbefore, 1));
|
|
|
1791 |
|
|
|
1792 |
} else {
|
|
|
1793 |
$lastslot = end($slots);
|
|
|
1794 |
if ($lastslot) {
|
|
|
1795 |
$slot->slot = $lastslot->slot + 1;
|
|
|
1796 |
} else {
|
|
|
1797 |
$slot->slot = 1;
|
|
|
1798 |
}
|
|
|
1799 |
if ($quiz->questionsperpage && $numonlastpage >= $quiz->questionsperpage) {
|
|
|
1800 |
$slot->page = $maxpage + 1;
|
|
|
1801 |
} else {
|
|
|
1802 |
$slot->page = $maxpage;
|
|
|
1803 |
}
|
|
|
1804 |
}
|
|
|
1805 |
|
|
|
1806 |
$slotid = $DB->insert_record('quiz_slots', $slot);
|
|
|
1807 |
|
|
|
1808 |
// Update or insert record in question_reference table.
|
|
|
1809 |
$sql = "SELECT DISTINCT qr.id, qr.itemid
|
|
|
1810 |
FROM {question} q
|
|
|
1811 |
JOIN {question_versions} qv ON q.id = qv.questionid
|
|
|
1812 |
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
|
|
1813 |
JOIN {question_references} qr ON qbe.id = qr.questionbankentryid AND qr.version = qv.version
|
|
|
1814 |
JOIN {quiz_slots} qs ON qs.id = qr.itemid
|
|
|
1815 |
WHERE q.id = ?
|
|
|
1816 |
AND qs.id = ?
|
|
|
1817 |
AND qr.component = ?
|
|
|
1818 |
AND qr.questionarea = ?";
|
|
|
1819 |
$qreferenceitem = $DB->get_record_sql($sql, [$questionid, $slotid, 'mod_quiz', 'slot']);
|
|
|
1820 |
|
|
|
1821 |
if (!$qreferenceitem) {
|
|
|
1822 |
// Create a new reference record for questions created already.
|
|
|
1823 |
$questionreferences = new stdClass();
|
|
|
1824 |
$questionreferences->usingcontextid = context_module::instance($quiz->cmid)->id;
|
|
|
1825 |
$questionreferences->component = 'mod_quiz';
|
|
|
1826 |
$questionreferences->questionarea = 'slot';
|
|
|
1827 |
$questionreferences->itemid = $slotid;
|
|
|
1828 |
$questionreferences->questionbankentryid = get_question_bank_entry($questionid)->id;
|
|
|
1829 |
$questionreferences->version = null; // Always latest.
|
|
|
1830 |
$DB->insert_record('question_references', $questionreferences);
|
|
|
1831 |
|
|
|
1832 |
} else if ($qreferenceitem->itemid === 0 || $qreferenceitem->itemid === null) {
|
|
|
1833 |
$questionreferences = new stdClass();
|
|
|
1834 |
$questionreferences->id = $qreferenceitem->id;
|
|
|
1835 |
$questionreferences->itemid = $slotid;
|
|
|
1836 |
$DB->update_record('question_references', $questionreferences);
|
|
|
1837 |
} else {
|
|
|
1838 |
// If the reference record exits for another quiz.
|
|
|
1839 |
$questionreferences = new stdClass();
|
|
|
1840 |
$questionreferences->usingcontextid = context_module::instance($quiz->cmid)->id;
|
|
|
1841 |
$questionreferences->component = 'mod_quiz';
|
|
|
1842 |
$questionreferences->questionarea = 'slot';
|
|
|
1843 |
$questionreferences->itemid = $slotid;
|
|
|
1844 |
$questionreferences->questionbankentryid = get_question_bank_entry($questionid)->id;
|
|
|
1845 |
$questionreferences->version = null; // Always latest.
|
|
|
1846 |
$DB->insert_record('question_references', $questionreferences);
|
|
|
1847 |
}
|
|
|
1848 |
|
|
|
1849 |
$trans->allow_commit();
|
|
|
1850 |
|
|
|
1851 |
// Log slot created event.
|
|
|
1852 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
|
|
1853 |
$event = \mod_quiz\event\slot_created::create([
|
|
|
1854 |
'context' => context_module::instance($cm->id),
|
|
|
1855 |
'objectid' => $slotid,
|
|
|
1856 |
'other' => [
|
|
|
1857 |
'quizid' => $quiz->id,
|
|
|
1858 |
'slotnumber' => $slot->slot,
|
|
|
1859 |
'page' => $slot->page
|
|
|
1860 |
]
|
|
|
1861 |
]);
|
|
|
1862 |
$event->trigger();
|
|
|
1863 |
}
|
|
|
1864 |
|
|
|
1865 |
/**
|
|
|
1866 |
* Move all the section headings in a certain slot range by a certain offset.
|
|
|
1867 |
*
|
|
|
1868 |
* @param int $quizid the id of a quiz
|
|
|
1869 |
* @param int $direction amount to adjust section heading positions. Normally +1 or -1.
|
|
|
1870 |
* @param int $afterslot adjust headings that start after this slot.
|
|
|
1871 |
* @param int|null $beforeslot optionally, only adjust headings before this slot.
|
|
|
1872 |
*/
|
|
|
1873 |
function quiz_update_section_firstslots($quizid, $direction, $afterslot, $beforeslot = null) {
|
|
|
1874 |
global $DB;
|
|
|
1875 |
$where = 'quizid = ? AND firstslot > ?';
|
|
|
1876 |
$params = [$direction, $quizid, $afterslot];
|
|
|
1877 |
if ($beforeslot) {
|
|
|
1878 |
$where .= ' AND firstslot < ?';
|
|
|
1879 |
$params[] = $beforeslot;
|
|
|
1880 |
}
|
|
|
1881 |
$firstslotschanges = $DB->get_records_select_menu('quiz_sections',
|
|
|
1882 |
$where, $params, '', 'firstslot, firstslot + ?');
|
|
|
1883 |
update_field_with_unique_index('quiz_sections', 'firstslot', $firstslotschanges, ['quizid' => $quizid]);
|
|
|
1884 |
}
|
|
|
1885 |
|
|
|
1886 |
/**
|
|
|
1887 |
* Add a random question to the quiz at a given point.
|
|
|
1888 |
* @param stdClass $quiz the quiz settings.
|
|
|
1889 |
* @param int $addonpage the page on which to add the question.
|
|
|
1890 |
* @param int $categoryid the question category to add the question from.
|
|
|
1891 |
* @param int $number the number of random questions to add.
|
|
|
1892 |
* @deprecated Since Moodle 4.3 MDL-72321
|
|
|
1893 |
* @todo Final deprecation in Moodle 4.7 MDL-78091
|
|
|
1894 |
*/
|
|
|
1895 |
function quiz_add_random_questions(stdClass $quiz, int $addonpage, int $categoryid, int $number): void {
|
|
|
1896 |
debugging(
|
|
|
1897 |
'quiz_add_random_questions is deprecated. Please use mod_quiz\structure::add_random_questions() instead.',
|
|
|
1898 |
DEBUG_DEVELOPER
|
|
|
1899 |
);
|
|
|
1900 |
|
|
|
1901 |
$settings = quiz_settings::create($quiz->id);
|
|
|
1902 |
$structure = structure::create_for_quiz($settings);
|
|
|
1903 |
$structure->add_random_questions($addonpage, $number, [
|
|
|
1904 |
'filter' => [
|
|
|
1905 |
'category' => [
|
|
|
1906 |
'jointype' => condition::JOINTYPE_DEFAULT,
|
|
|
1907 |
'values' => [$categoryid],
|
|
|
1908 |
'filteroptions' => ['includesubcategories' => false],
|
|
|
1909 |
],
|
|
|
1910 |
],
|
|
|
1911 |
]);
|
|
|
1912 |
}
|
|
|
1913 |
|
|
|
1914 |
/**
|
|
|
1915 |
* Mark the activity completed (if required) and trigger the course_module_viewed event.
|
|
|
1916 |
*
|
|
|
1917 |
* @param stdClass $quiz quiz object
|
|
|
1918 |
* @param stdClass $course course object
|
|
|
1919 |
* @param stdClass $cm course module object
|
|
|
1920 |
* @param stdClass $context context object
|
|
|
1921 |
* @since Moodle 3.1
|
|
|
1922 |
*/
|
|
|
1923 |
function quiz_view($quiz, $course, $cm, $context) {
|
|
|
1924 |
|
|
|
1925 |
$params = [
|
|
|
1926 |
'objectid' => $quiz->id,
|
|
|
1927 |
'context' => $context
|
|
|
1928 |
];
|
|
|
1929 |
|
|
|
1930 |
$event = \mod_quiz\event\course_module_viewed::create($params);
|
|
|
1931 |
$event->add_record_snapshot('quiz', $quiz);
|
|
|
1932 |
$event->trigger();
|
|
|
1933 |
|
|
|
1934 |
// Completion.
|
|
|
1935 |
$completion = new completion_info($course);
|
|
|
1936 |
$completion->set_module_viewed($cm);
|
|
|
1937 |
}
|
|
|
1938 |
|
|
|
1939 |
/**
|
|
|
1940 |
* Validate permissions for creating a new attempt and start a new preview attempt if required.
|
|
|
1941 |
*
|
|
|
1942 |
* @param quiz_settings $quizobj quiz object
|
|
|
1943 |
* @param access_manager $accessmanager quiz access manager
|
|
|
1944 |
* @param bool $forcenew whether was required to start a new preview attempt
|
|
|
1945 |
* @param int $page page to jump to in the attempt
|
|
|
1946 |
* @param bool $redirect whether to redirect or throw exceptions (for web or ws usage)
|
|
|
1947 |
* @return array an array containing the attempt information, access error messages and the page to jump to in the attempt
|
|
|
1948 |
* @since Moodle 3.1
|
|
|
1949 |
*/
|
|
|
1950 |
function quiz_validate_new_attempt(quiz_settings $quizobj, access_manager $accessmanager, $forcenew, $page, $redirect) {
|
|
|
1951 |
global $DB, $USER;
|
|
|
1952 |
$timenow = time();
|
|
|
1953 |
|
|
|
1954 |
if ($quizobj->is_preview_user() && $forcenew) {
|
|
|
1955 |
$accessmanager->current_attempt_finished();
|
|
|
1956 |
}
|
|
|
1957 |
|
|
|
1958 |
// Check capabilities.
|
|
|
1959 |
if (!$quizobj->is_preview_user()) {
|
|
|
1960 |
$quizobj->require_capability('mod/quiz:attempt');
|
|
|
1961 |
}
|
|
|
1962 |
|
|
|
1963 |
// Check to see if a new preview was requested.
|
|
|
1964 |
if ($quizobj->is_preview_user() && $forcenew) {
|
|
|
1965 |
// To force the creation of a new preview, we mark the current attempt (if any)
|
|
|
1966 |
// as abandoned. It will then automatically be deleted below.
|
|
|
1967 |
$DB->set_field('quiz_attempts', 'state', quiz_attempt::ABANDONED,
|
|
|
1968 |
['quiz' => $quizobj->get_quizid(), 'userid' => $USER->id]);
|
|
|
1969 |
}
|
|
|
1970 |
|
|
|
1971 |
// Look for an existing attempt.
|
|
|
1972 |
$attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id, 'all', true);
|
|
|
1973 |
$lastattempt = end($attempts);
|
|
|
1974 |
|
|
|
1975 |
$attemptnumber = null;
|
|
|
1976 |
// If an in-progress attempt exists, check password then redirect to it.
|
|
|
1977 |
if ($lastattempt && ($lastattempt->state == quiz_attempt::IN_PROGRESS ||
|
|
|
1978 |
$lastattempt->state == quiz_attempt::OVERDUE)) {
|
|
|
1979 |
$currentattemptid = $lastattempt->id;
|
|
|
1980 |
$messages = $accessmanager->prevent_access();
|
|
|
1981 |
|
|
|
1982 |
// If the attempt is now overdue, deal with that.
|
|
|
1983 |
$quizobj->create_attempt_object($lastattempt)->handle_if_time_expired($timenow, true);
|
|
|
1984 |
|
|
|
1985 |
// And, if the attempt is now no longer in progress, redirect to the appropriate place.
|
|
|
1986 |
if ($lastattempt->state == quiz_attempt::ABANDONED || $lastattempt->state == quiz_attempt::FINISHED) {
|
|
|
1987 |
if ($redirect) {
|
|
|
1988 |
redirect($quizobj->review_url($lastattempt->id));
|
|
|
1989 |
} else {
|
|
|
1990 |
throw new moodle_exception('attemptalreadyclosed', 'quiz', $quizobj->view_url());
|
|
|
1991 |
}
|
|
|
1992 |
}
|
|
|
1993 |
|
|
|
1994 |
// If the page number was not explicitly in the URL, go to the current page.
|
|
|
1995 |
if ($page == -1) {
|
|
|
1996 |
$page = $lastattempt->currentpage;
|
|
|
1997 |
}
|
|
|
1998 |
|
|
|
1999 |
} else {
|
|
|
2000 |
while ($lastattempt && $lastattempt->preview) {
|
|
|
2001 |
$lastattempt = array_pop($attempts);
|
|
|
2002 |
}
|
|
|
2003 |
|
|
|
2004 |
// Get number for the next or unfinished attempt.
|
|
|
2005 |
if ($lastattempt) {
|
|
|
2006 |
$attemptnumber = $lastattempt->attempt + 1;
|
|
|
2007 |
} else {
|
|
|
2008 |
$lastattempt = false;
|
|
|
2009 |
$attemptnumber = 1;
|
|
|
2010 |
}
|
|
|
2011 |
$currentattemptid = null;
|
|
|
2012 |
|
|
|
2013 |
$messages = $accessmanager->prevent_access() +
|
|
|
2014 |
$accessmanager->prevent_new_attempt(count($attempts), $lastattempt);
|
|
|
2015 |
|
|
|
2016 |
if ($page == -1) {
|
|
|
2017 |
$page = 0;
|
|
|
2018 |
}
|
|
|
2019 |
}
|
|
|
2020 |
return [$currentattemptid, $attemptnumber, $lastattempt, $messages, $page];
|
|
|
2021 |
}
|
|
|
2022 |
|
|
|
2023 |
/**
|
|
|
2024 |
* Prepare and start a new attempt deleting the previous preview attempts.
|
|
|
2025 |
*
|
|
|
2026 |
* @param quiz_settings $quizobj quiz object
|
|
|
2027 |
* @param int $attemptnumber the attempt number
|
|
|
2028 |
* @param stdClass $lastattempt last attempt object
|
|
|
2029 |
* @param bool $offlineattempt whether is an offline attempt or not
|
|
|
2030 |
* @param array $forcedrandomquestions slot number => question id. Used for random questions,
|
|
|
2031 |
* to force the choice of a particular actual question. Intended for testing purposes only.
|
|
|
2032 |
* @param array $forcedvariants slot number => variant. Used for questions with variants,
|
|
|
2033 |
* to force the choice of a particular variant. Intended for testing purposes only.
|
|
|
2034 |
* @param int $userid Specific user id to create an attempt for that user, null for current logged in user
|
|
|
2035 |
* @return stdClass the new attempt
|
|
|
2036 |
* @since Moodle 3.1
|
|
|
2037 |
*/
|
|
|
2038 |
function quiz_prepare_and_start_new_attempt(quiz_settings $quizobj, $attemptnumber, $lastattempt,
|
|
|
2039 |
$offlineattempt = false, $forcedrandomquestions = [], $forcedvariants = [], $userid = null) {
|
|
|
2040 |
global $DB, $USER;
|
|
|
2041 |
|
|
|
2042 |
if ($userid === null) {
|
|
|
2043 |
$userid = $USER->id;
|
|
|
2044 |
$ispreviewuser = $quizobj->is_preview_user();
|
|
|
2045 |
} else {
|
|
|
2046 |
$ispreviewuser = has_capability('mod/quiz:preview', $quizobj->get_context(), $userid);
|
|
|
2047 |
}
|
|
|
2048 |
// Delete any previous preview attempts belonging to this user.
|
|
|
2049 |
quiz_delete_previews($quizobj->get_quiz(), $userid);
|
|
|
2050 |
|
|
|
2051 |
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
2052 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
2053 |
|
|
|
2054 |
// Create the new attempt and initialize the question sessions
|
|
|
2055 |
$timenow = time(); // Update time now, in case the server is running really slowly.
|
|
|
2056 |
$attempt = quiz_create_attempt($quizobj, $attemptnumber, $lastattempt, $timenow, $ispreviewuser, $userid);
|
|
|
2057 |
|
|
|
2058 |
if (!($quizobj->get_quiz()->attemptonlast && $lastattempt)) {
|
|
|
2059 |
$attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow,
|
|
|
2060 |
$forcedrandomquestions, $forcedvariants);
|
|
|
2061 |
} else {
|
|
|
2062 |
$attempt = quiz_start_attempt_built_on_last($quba, $attempt, $lastattempt);
|
|
|
2063 |
}
|
|
|
2064 |
|
|
|
2065 |
$transaction = $DB->start_delegated_transaction();
|
|
|
2066 |
|
|
|
2067 |
// Init the timemodifiedoffline for offline attempts.
|
|
|
2068 |
if ($offlineattempt) {
|
|
|
2069 |
$attempt->timemodifiedoffline = $attempt->timemodified;
|
|
|
2070 |
}
|
|
|
2071 |
$attempt = quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
2072 |
|
|
|
2073 |
$transaction->allow_commit();
|
|
|
2074 |
|
|
|
2075 |
return $attempt;
|
|
|
2076 |
}
|
|
|
2077 |
|
|
|
2078 |
/**
|
|
|
2079 |
* Check if the given calendar_event is either a user or group override
|
|
|
2080 |
* event for quiz.
|
|
|
2081 |
*
|
|
|
2082 |
* @param calendar_event $event The calendar event to check
|
|
|
2083 |
* @return bool
|
|
|
2084 |
*/
|
|
|
2085 |
function quiz_is_overriden_calendar_event(\calendar_event $event) {
|
|
|
2086 |
global $DB;
|
|
|
2087 |
|
|
|
2088 |
if (!isset($event->modulename)) {
|
|
|
2089 |
return false;
|
|
|
2090 |
}
|
|
|
2091 |
|
|
|
2092 |
if ($event->modulename != 'quiz') {
|
|
|
2093 |
return false;
|
|
|
2094 |
}
|
|
|
2095 |
|
|
|
2096 |
if (!isset($event->instance)) {
|
|
|
2097 |
return false;
|
|
|
2098 |
}
|
|
|
2099 |
|
|
|
2100 |
if (!isset($event->userid) && !isset($event->groupid)) {
|
|
|
2101 |
return false;
|
|
|
2102 |
}
|
|
|
2103 |
|
|
|
2104 |
$overrideparams = [
|
|
|
2105 |
'quiz' => $event->instance
|
|
|
2106 |
];
|
|
|
2107 |
|
|
|
2108 |
if (isset($event->groupid)) {
|
|
|
2109 |
$overrideparams['groupid'] = $event->groupid;
|
|
|
2110 |
} else if (isset($event->userid)) {
|
|
|
2111 |
$overrideparams['userid'] = $event->userid;
|
|
|
2112 |
}
|
|
|
2113 |
|
|
|
2114 |
return $DB->record_exists('quiz_overrides', $overrideparams);
|
|
|
2115 |
}
|
|
|
2116 |
|
|
|
2117 |
/**
|
|
|
2118 |
* Get quiz attempt and handling error.
|
|
|
2119 |
*
|
|
|
2120 |
* @param int $attemptid the id of the current attempt.
|
|
|
2121 |
* @param int|null $cmid the course_module id for this quiz.
|
|
|
2122 |
* @return quiz_attempt all the data about the quiz attempt.
|
|
|
2123 |
*/
|
|
|
2124 |
function quiz_create_attempt_handling_errors($attemptid, $cmid = null) {
|
|
|
2125 |
try {
|
|
|
2126 |
$attempobj = quiz_attempt::create($attemptid);
|
|
|
2127 |
} catch (moodle_exception $e) {
|
|
|
2128 |
if (!empty($cmid)) {
|
|
|
2129 |
list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'quiz');
|
|
|
2130 |
$continuelink = new moodle_url('/mod/quiz/view.php', ['id' => $cmid]);
|
|
|
2131 |
$context = context_module::instance($cm->id);
|
|
|
2132 |
if (has_capability('mod/quiz:preview', $context)) {
|
|
|
2133 |
throw new moodle_exception('attempterrorcontentchange', 'quiz', $continuelink);
|
|
|
2134 |
} else {
|
|
|
2135 |
throw new moodle_exception('attempterrorcontentchangeforuser', 'quiz', $continuelink);
|
|
|
2136 |
}
|
|
|
2137 |
} else {
|
|
|
2138 |
throw new moodle_exception('attempterrorinvalid', 'quiz');
|
|
|
2139 |
}
|
|
|
2140 |
}
|
|
|
2141 |
if (!empty($cmid) && $attempobj->get_cmid() != $cmid) {
|
|
|
2142 |
throw new moodle_exception('invalidcoursemodule');
|
|
|
2143 |
} else {
|
|
|
2144 |
return $attempobj;
|
|
|
2145 |
}
|
|
|
2146 |
}
|