2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Library of functions used by the quiz module.
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.
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
32 defined('MOODLE_INTERNAL') ||
die();
34 require_once($CFG->dirroot
. '/mod/quiz/lib.php');
35 require_once($CFG->dirroot
. '/mod/quiz/accessmanager.php');
36 require_once($CFG->dirroot
. '/mod/quiz/accessmanager_form.php');
37 require_once($CFG->dirroot
. '/mod/quiz/renderer.php');
38 require_once($CFG->dirroot
. '/mod/quiz/attemptlib.php');
39 require_once($CFG->libdir
. '/completionlib.php');
40 require_once($CFG->libdir
. '/filelib.php');
41 require_once($CFG->libdir
. '/questionlib.php');
45 * @var int We show the countdown timer if there is less than this amount of time left before the
46 * the quiz close date. (1 hour)
48 define('QUIZ_SHOW_TIME_BEFORE_DEADLINE', '3600');
51 * @var int If there are fewer than this many seconds left when the student submits
52 * a page of the quiz, then do not take them to the next page of the quiz. Instead
53 * close the quiz immediately.
55 define('QUIZ_MIN_TIME_TO_CONTINUE', '2');
58 * @var int We show no image when user selects No image from dropdown menu in quiz settings.
60 define('QUIZ_SHOWIMAGE_NONE', 0);
63 * @var int We show small image when user selects small image from dropdown menu in quiz settings.
65 define('QUIZ_SHOWIMAGE_SMALL', 1);
68 * @var int We show Large image when user selects Large image from dropdown menu in quiz settings.
70 define('QUIZ_SHOWIMAGE_LARGE', 2);
73 // Functions related to attempts ///////////////////////////////////////////////
76 * Creates an object to represent a new attempt at a quiz
78 * Creates an attempt object to represent an attempt at the quiz by the current
79 * user starting at the current time. The ->id field is not set. The object is
80 * NOT written to the database.
82 * @param object $quizobj the quiz object to create an attempt for.
83 * @param int $attemptnumber the sequence number for the attempt.
84 * @param object $lastattempt the previous attempt by this user, if any. Only needed
85 * if $attemptnumber > 1 and $quiz->attemptonlast is true.
86 * @param int $timenow the time the attempt was started at.
87 * @param bool $ispreview whether this new attempt is a preview.
88 * @param int $userid the id of the user attempting this quiz.
90 * @return object the newly created attempt object.
92 function quiz_create_attempt(quiz
$quizobj, $attemptnumber, $lastattempt, $timenow, $ispreview = false, $userid = null) {
95 if ($userid === null) {
99 $quiz = $quizobj->get_quiz();
100 if ($quiz->sumgrades
< 0.000005 && $quiz->grade
> 0.000005) {
101 throw new moodle_exception('cannotstartgradesmismatch', 'quiz',
102 new moodle_url('/mod/quiz/view.php', array('q' => $quiz->id
)),
103 array('grade' => quiz_format_grade($quiz, $quiz->grade
)));
106 if ($attemptnumber == 1 ||
!$quiz->attemptonlast
) {
107 // We are not building on last attempt so create a new attempt.
108 $attempt = new stdClass();
109 $attempt->quiz
= $quiz->id
;
110 $attempt->userid
= $userid;
111 $attempt->preview
= 0;
112 $attempt->layout
= '';
114 // Build on last attempt.
115 if (empty($lastattempt)) {
116 print_error('cannotfindprevattempt', 'quiz');
118 $attempt = $lastattempt;
121 $attempt->attempt
= $attemptnumber;
122 $attempt->timestart
= $timenow;
123 $attempt->timefinish
= 0;
124 $attempt->timemodified
= $timenow;
125 $attempt->timemodifiedoffline
= 0;
126 $attempt->state
= quiz_attempt
::IN_PROGRESS
;
127 $attempt->currentpage
= 0;
128 $attempt->sumgrades
= null;
130 // If this is a preview, mark it as such.
132 $attempt->preview
= 1;
135 $timeclose = $quizobj->get_access_manager($timenow)->get_end_time($attempt);
136 if ($timeclose === false ||
$ispreview) {
137 $attempt->timecheckstate
= null;
139 $attempt->timecheckstate
= $timeclose;
145 * Start a normal, new, quiz attempt.
147 * @param quiz $quizobj the quiz object to start an attempt for.
148 * @param question_usage_by_activity $quba
149 * @param object $attempt
150 * @param integer $attemptnumber starting from 1
151 * @param integer $timenow the attempt start time
152 * @param array $questionids slot number => question id. Used for random questions, to force the choice
153 * of a particular actual question. Intended for testing purposes only.
154 * @param array $forcedvariantsbyslot slot number => variant. Used for questions with variants,
155 * to force the choice of a particular variant. Intended for testing
157 * @throws moodle_exception
158 * @return object modified attempt object
160 function quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow,
161 $questionids = array(), $forcedvariantsbyslot = array()) {
163 // Usages for this user's previous quiz attempts.
164 $qubaids = new \mod_quiz\question\
qubaids_for_users_attempts(
165 $quizobj->get_quizid(), $attempt->userid
);
167 // Fully load all the questions in this quiz.
168 $quizobj->preload_questions();
169 $quizobj->load_questions();
171 // First load all the non-random questions.
172 $randomfound = false;
174 $questions = array();
177 foreach ($quizobj->get_questions() as $questiondata) {
179 $maxmark[$slot] = $questiondata->maxmark
;
180 $page[$slot] = $questiondata->page
;
181 if ($questiondata->qtype
== 'random') {
185 if (!$quizobj->get_quiz()->shuffleanswers
) {
186 $questiondata->options
->shuffleanswers
= false;
188 $questions[$slot] = question_bank
::make_question($questiondata);
191 // Then find a question to go in place of each random question.
194 $usedquestionids = array();
195 foreach ($questions as $question) {
196 if (isset($usedquestions[$question->id
])) {
197 $usedquestionids[$question->id
] +
= 1;
199 $usedquestionids[$question->id
] = 1;
202 $randomloader = new \core_question\bank\random_question_loader
($qubaids, $usedquestionids);
204 foreach ($quizobj->get_questions() as $questiondata) {
206 if ($questiondata->qtype
!= 'random') {
210 $tagids = quiz_retrieve_slot_tag_ids($questiondata->slotid
);
212 // Deal with fixed random choices for testing.
213 if (isset($questionids[$quba->next_slot_number()])) {
214 if ($randomloader->is_question_available($questiondata->category
,
215 (bool) $questiondata->questiontext
, $questionids[$quba->next_slot_number()], $tagids)) {
216 $questions[$slot] = question_bank
::load_question(
217 $questionids[$quba->next_slot_number()], $quizobj->get_quiz()->shuffleanswers
);
220 throw new coding_exception('Forced question id not available.');
224 // Normal case, pick one at random.
225 $questionid = $randomloader->get_next_question_id($questiondata->randomfromcategory
,
226 $questiondata->randomincludingsubcategories
, $tagids);
227 if ($questionid === null) {
228 throw new moodle_exception('notenoughrandomquestions', 'quiz',
229 $quizobj->view_url(), $questiondata);
232 $questions[$slot] = question_bank
::load_question($questionid,
233 $quizobj->get_quiz()->shuffleanswers
);
237 // Finally add them all to the usage.
239 foreach ($questions as $slot => $question) {
240 $newslot = $quba->add_question($question, $maxmark[$slot]);
241 if ($newslot != $slot) {
242 throw new coding_exception('Slot numbers have got confused.');
246 // Start all the questions.
247 $variantstrategy = new core_question\engine\variants\
least_used_strategy($quba, $qubaids);
249 if (!empty($forcedvariantsbyslot)) {
250 $forcedvariantsbyseed = question_variant_forced_choices_selection_strategy
::prepare_forced_choices_array(
251 $forcedvariantsbyslot, $quba);
252 $variantstrategy = new question_variant_forced_choices_selection_strategy(
253 $forcedvariantsbyseed, $variantstrategy);
256 $quba->start_all_questions($variantstrategy, $timenow, $attempt->userid
);
258 // Work out the attempt layout.
259 $sections = $quizobj->get_sections();
260 foreach ($sections as $i => $section) {
261 if (isset($sections[$i +
1])) {
262 $sections[$i]->lastslot
= $sections[$i +
1]->firstslot
- 1;
264 $sections[$i]->lastslot
= count($questions);
269 foreach ($sections as $section) {
270 if ($section->shufflequestions
) {
271 $questionsinthissection = array();
272 for ($slot = $section->firstslot
; $slot <= $section->lastslot
; $slot +
= 1) {
273 $questionsinthissection[] = $slot;
275 shuffle($questionsinthissection);
276 $questionsonthispage = 0;
277 foreach ($questionsinthissection as $slot) {
278 if ($questionsonthispage && $questionsonthispage == $quizobj->get_quiz()->questionsperpage
) {
280 $questionsonthispage = 0;
283 $questionsonthispage +
= 1;
287 $currentpage = $page[$section->firstslot
];
288 for ($slot = $section->firstslot
; $slot <= $section->lastslot
; $slot +
= 1) {
289 if ($currentpage !== null && $page[$slot] != $currentpage) {
293 $currentpage = $page[$slot];
297 // Each section ends with a page break.
300 $attempt->layout
= implode(',', $layout);
306 * Start a subsequent new attempt, in each attempt builds on last mode.
308 * @param question_usage_by_activity $quba this question usage
309 * @param object $attempt this attempt
310 * @param object $lastattempt last attempt
311 * @return object modified attempt object
314 function quiz_start_attempt_built_on_last($quba, $attempt, $lastattempt) {
315 $oldquba = question_engine
::load_questions_usage_by_activity($lastattempt->uniqueid
);
317 $oldnumberstonew = array();
318 foreach ($oldquba->get_attempt_iterator() as $oldslot => $oldqa) {
319 $newslot = $quba->add_question($oldqa->get_question(false), $oldqa->get_max_mark());
321 $quba->start_question_based_on($newslot, $oldqa);
323 $oldnumberstonew[$oldslot] = $newslot;
326 // Update attempt layout.
327 $newlayout = array();
328 foreach (explode(',', $lastattempt->layout
) as $oldslot) {
330 $newlayout[] = $oldnumberstonew[$oldslot];
335 $attempt->layout
= implode(',', $newlayout);
340 * The save started question usage and quiz attempt in db and log the started attempt.
342 * @param quiz $quizobj
343 * @param question_usage_by_activity $quba
344 * @param object $attempt
345 * @return object attempt object with uniqueid and id set.
347 function quiz_attempt_save_started($quizobj, $quba, $attempt) {
349 // Save the attempt in the database.
350 question_engine
::save_questions_usage_by_activity($quba);
351 $attempt->uniqueid
= $quba->get_id();
352 $attempt->id
= $DB->insert_record('quiz_attempts', $attempt);
354 // Params used by the events below.
356 'objectid' => $attempt->id
,
357 'relateduserid' => $attempt->userid
,
358 'courseid' => $quizobj->get_courseid(),
359 'context' => $quizobj->get_context()
361 // Decide which event we are using.
362 if ($attempt->preview
) {
363 $params['other'] = array(
364 'quizid' => $quizobj->get_quizid()
366 $event = \mod_quiz\event\attempt_preview_started
::create($params);
368 $event = \mod_quiz\event\attempt_started
::create($params);
372 // Trigger the event.
373 $event->add_record_snapshot('quiz', $quizobj->get_quiz());
374 $event->add_record_snapshot('quiz_attempts', $attempt);
381 * Returns an unfinished attempt (if there is one) for the given
382 * user on the given quiz. This function does not return preview attempts.
384 * @param int $quizid the id of the quiz.
385 * @param int $userid the id of the user.
387 * @return mixed the unfinished attempt if there is one, false if not.
389 function quiz_get_user_attempt_unfinished($quizid, $userid) {
390 $attempts = quiz_get_user_attempts($quizid, $userid, 'unfinished', true);
392 return array_shift($attempts);
399 * Delete a quiz attempt.
400 * @param mixed $attempt an integer attempt id or an attempt object
401 * (row of the quiz_attempts table).
402 * @param object $quiz the quiz object.
404 function quiz_delete_attempt($attempt, $quiz) {
406 if (is_numeric($attempt)) {
407 if (!$attempt = $DB->get_record('quiz_attempts', array('id' => $attempt))) {
412 if ($attempt->quiz
!= $quiz->id
) {
413 debugging("Trying to delete attempt $attempt->id which belongs to quiz $attempt->quiz " .
414 "but was passed quiz $quiz->id.");
418 if (!isset($quiz->cmid
)) {
419 $cm = get_coursemodule_from_instance('quiz', $quiz->id
, $quiz->course
);
420 $quiz->cmid
= $cm->id
;
423 question_engine
::delete_questions_usage_by_activity($attempt->uniqueid
);
424 $DB->delete_records('quiz_attempts', array('id' => $attempt->id
));
426 // Log the deletion of the attempt if not a preview.
427 if (!$attempt->preview
) {
429 'objectid' => $attempt->id
,
430 'relateduserid' => $attempt->userid
,
431 'context' => context_module
::instance($quiz->cmid
),
433 'quizid' => $quiz->id
436 $event = \mod_quiz\event\attempt_deleted
::create($params);
437 $event->add_record_snapshot('quiz_attempts', $attempt);
441 // Search quiz_attempts for other instances by this user.
442 // If none, then delete record for this quiz, this user from quiz_grades
443 // else recalculate best grade.
444 $userid = $attempt->userid
;
445 if (!$DB->record_exists('quiz_attempts', array('userid' => $userid, 'quiz' => $quiz->id
))) {
446 $DB->delete_records('quiz_grades', array('userid' => $userid, 'quiz' => $quiz->id
));
448 quiz_save_best_grade($quiz, $userid);
451 quiz_update_grades($quiz, $userid);
455 * Delete all the preview attempts at a quiz, or possibly all the attempts belonging
457 * @param object $quiz the quiz object.
458 * @param int $userid (optional) if given, only delete the previews belonging to this user.
460 function quiz_delete_previews($quiz, $userid = null) {
462 $conditions = array('quiz' => $quiz->id
, 'preview' => 1);
463 if (!empty($userid)) {
464 $conditions['userid'] = $userid;
466 $previewattempts = $DB->get_records('quiz_attempts', $conditions);
467 foreach ($previewattempts as $attempt) {
468 quiz_delete_attempt($attempt, $quiz);
473 * @param int $quizid The quiz id.
474 * @return bool whether this quiz has any (non-preview) attempts.
476 function quiz_has_attempts($quizid) {
478 return $DB->record_exists('quiz_attempts', array('quiz' => $quizid, 'preview' => 0));
481 // Functions to do with quiz layout and pages //////////////////////////////////
484 * Repaginate the questions in a quiz
485 * @param int $quizid the id of the quiz to repaginate.
486 * @param int $slotsperpage number of items to put on each page. 0 means unlimited.
488 function quiz_repaginate_questions($quizid, $slotsperpage) {
490 $trans = $DB->start_delegated_transaction();
492 $sections = $DB->get_records('quiz_sections', array('quizid' => $quizid), 'firstslot ASC');
493 $firstslots = array();
494 foreach ($sections as $section) {
495 if ((int)$section->firstslot
=== 1) {
498 $firstslots[] = $section->firstslot
;
501 $slots = $DB->get_records('quiz_slots', array('quizid' => $quizid),
504 $slotsonthispage = 0;
505 foreach ($slots as $slot) {
506 if (($firstslots && in_array($slot->slot
, $firstslots)) ||
507 ($slotsonthispage && $slotsonthispage == $slotsperpage)) {
509 $slotsonthispage = 0;
511 if ($slot->page
!= $currentpage) {
512 $DB->set_field('quiz_slots', 'page', $currentpage, array('id' => $slot->id
));
514 $slotsonthispage +
= 1;
517 $trans->allow_commit();
520 // Functions to do with quiz grades ////////////////////////////////////////////
523 * Convert the raw grade stored in $attempt into a grade out of the maximum
524 * grade for this quiz.
526 * @param float $rawgrade the unadjusted grade, fof example $attempt->sumgrades
527 * @param object $quiz the quiz object. Only the fields grade, sumgrades and decimalpoints are used.
528 * @param bool|string $format whether to format the results for display
529 * or 'question' to format a question grade (different number of decimal places.
530 * @return float|string the rescaled grade, or null/the lang string 'notyetgraded'
531 * if the $grade is null.
533 function quiz_rescale_grade($rawgrade, $quiz, $format = true) {
534 if (is_null($rawgrade)) {
536 } else if ($quiz->sumgrades
>= 0.000005) {
537 $grade = $rawgrade * $quiz->grade
/ $quiz->sumgrades
;
541 if ($format === 'question') {
542 $grade = quiz_format_question_grade($quiz, $grade);
543 } else if ($format) {
544 $grade = quiz_format_grade($quiz, $grade);
550 * Get the feedback object for this grade on this quiz.
552 * @param float $grade a grade on this quiz.
553 * @param object $quiz the quiz settings.
554 * @return false|stdClass the record object or false if there is not feedback for the given grade
557 function quiz_feedback_record_for_grade($grade, $quiz) {
560 // With CBM etc, it is possible to get -ve grades, which would then not match
561 // any feedback. Therefore, we replace -ve grades with 0.
562 $grade = max($grade, 0);
564 $feedback = $DB->get_record_select('quiz_feedback',
565 'quizid = ? AND mingrade <= ? AND ? < maxgrade', array($quiz->id
, $grade, $grade));
571 * Get the feedback text that should be show to a student who
572 * got this grade on this quiz. The feedback is processed ready for diplay.
574 * @param float $grade a grade on this quiz.
575 * @param object $quiz the quiz settings.
576 * @param object $context the quiz context.
577 * @return string the comment that corresponds to this grade (empty string if there is not one.
579 function quiz_feedback_for_grade($grade, $quiz, $context) {
581 if (is_null($grade)) {
585 $feedback = quiz_feedback_record_for_grade($grade, $quiz);
587 if (empty($feedback->feedbacktext
)) {
591 // Clean the text, ready for display.
592 $formatoptions = new stdClass();
593 $formatoptions->noclean
= true;
594 $feedbacktext = file_rewrite_pluginfile_urls($feedback->feedbacktext
, 'pluginfile.php',
595 $context->id
, 'mod_quiz', 'feedback', $feedback->id
);
596 $feedbacktext = format_text($feedbacktext, $feedback->feedbacktextformat
, $formatoptions);
598 return $feedbacktext;
602 * @param object $quiz the quiz database row.
603 * @return bool Whether this quiz has any non-blank feedback text.
605 function quiz_has_feedback($quiz) {
607 static $cache = array();
608 if (!array_key_exists($quiz->id
, $cache)) {
609 $cache[$quiz->id
] = quiz_has_grades($quiz) &&
610 $DB->record_exists_select('quiz_feedback', "quizid = ? AND " .
611 $DB->sql_isnotempty('quiz_feedback', 'feedbacktext', false, true),
614 return $cache[$quiz->id
];
618 * Update the sumgrades field of the quiz. This needs to be called whenever
619 * the grading structure of the quiz is changed. For example if a question is
620 * added or removed, or a question weight is changed.
622 * You should call {@link quiz_delete_previews()} before you call this function.
624 * @param object $quiz a quiz.
626 function quiz_update_sumgrades($quiz) {
629 $sql = 'UPDATE {quiz}
630 SET sumgrades = COALESCE((
633 WHERE quizid = {quiz}.id
636 $DB->execute($sql, array($quiz->id
));
637 $quiz->sumgrades
= $DB->get_field('quiz', 'sumgrades', array('id' => $quiz->id
));
639 if ($quiz->sumgrades
< 0.000005 && quiz_has_attempts($quiz->id
)) {
640 // If the quiz has been attempted, and the sumgrades has been
641 // set to 0, then we must also set the maximum possible grade to 0, or
642 // we will get a divide by zero error.
643 quiz_set_grade(0, $quiz);
648 * Update the sumgrades field of the attempts at a quiz.
650 * @param object $quiz a quiz.
652 function quiz_update_all_attempt_sumgrades($quiz) {
654 $dm = new question_engine_data_mapper();
657 $sql = "UPDATE {quiz_attempts}
659 timemodified = :timenow,
661 {$dm->sum_usage_marks_subquery('uniqueid')}
663 WHERE quiz = :quizid AND state = :finishedstate";
664 $DB->execute($sql, array('timenow' => $timenow, 'quizid' => $quiz->id
,
665 'finishedstate' => quiz_attempt
::FINISHED
));
669 * The quiz grade is the maximum that student's results are marked out of. When it
670 * changes, the corresponding data in quiz_grades and quiz_feedback needs to be
671 * rescaled. After calling this function, you probably need to call
672 * quiz_update_all_attempt_sumgrades, quiz_update_all_final_grades and
673 * quiz_update_grades.
675 * @param float $newgrade the new maximum grade for the quiz.
676 * @param object $quiz the quiz we are updating. Passed by reference so its
677 * grade field can be updated too.
678 * @return bool indicating success or failure.
680 function quiz_set_grade($newgrade, $quiz) {
682 // This is potentially expensive, so only do it if necessary.
683 if (abs($quiz->grade
- $newgrade) < 1e-7) {
688 $oldgrade = $quiz->grade
;
689 $quiz->grade
= $newgrade;
691 // Use a transaction, so that on those databases that support it, this is safer.
692 $transaction = $DB->start_delegated_transaction();
694 // Update the quiz table.
695 $DB->set_field('quiz', 'grade', $newgrade, array('id' => $quiz->instance
));
698 // If the old grade was zero, we cannot rescale, we have to recompute.
699 // We also recompute if the old grade was too small to avoid underflow problems.
700 quiz_update_all_final_grades($quiz);
703 // We can rescale the grades efficiently.
704 $timemodified = time();
707 SET grade = ? * grade, timemodified = ?
709 ", array($newgrade/$oldgrade, $timemodified, $quiz->id
));
712 if ($oldgrade > 1e-7) {
713 // Update the quiz_feedback table.
714 $factor = $newgrade/$oldgrade;
716 UPDATE {quiz_feedback}
717 SET mingrade = ? * mingrade, maxgrade = ? * maxgrade
719 ", array($factor, $factor, $quiz->id
));
722 // Update grade item and send all grades to gradebook.
723 quiz_grade_item_update($quiz);
724 quiz_update_grades($quiz);
726 $transaction->allow_commit();
731 * Save the overall grade for a user at a quiz in the quiz_grades table
733 * @param object $quiz The quiz for which the best grade is to be calculated and then saved.
734 * @param int $userid The userid to calculate the grade for. Defaults to the current user.
735 * @param array $attempts The attempts of this user. Useful if you are
736 * looping through many users. Attempts can be fetched in one master query to
737 * avoid repeated querying.
738 * @return bool Indicates success or failure.
740 function quiz_save_best_grade($quiz, $userid = null, $attempts = array()) {
741 global $DB, $OUTPUT, $USER;
743 if (empty($userid)) {
748 // Get all the attempts made by the user.
749 $attempts = quiz_get_user_attempts($quiz->id
, $userid);
752 // Calculate the best grade.
753 $bestgrade = quiz_calculate_best_grade($quiz, $attempts);
754 $bestgrade = quiz_rescale_grade($bestgrade, $quiz, false);
756 // Save the best grade in the database.
757 if (is_null($bestgrade)) {
758 $DB->delete_records('quiz_grades', array('quiz' => $quiz->id
, 'userid' => $userid));
760 } else if ($grade = $DB->get_record('quiz_grades',
761 array('quiz' => $quiz->id
, 'userid' => $userid))) {
762 $grade->grade
= $bestgrade;
763 $grade->timemodified
= time();
764 $DB->update_record('quiz_grades', $grade);
767 $grade = new stdClass();
768 $grade->quiz
= $quiz->id
;
769 $grade->userid
= $userid;
770 $grade->grade
= $bestgrade;
771 $grade->timemodified
= time();
772 $DB->insert_record('quiz_grades', $grade);
775 quiz_update_grades($quiz, $userid);
779 * Calculate the overall grade for a quiz given a number of attempts by a particular user.
781 * @param object $quiz the quiz settings object.
782 * @param array $attempts an array of all the user's attempts at this quiz in order.
783 * @return float the overall grade
785 function quiz_calculate_best_grade($quiz, $attempts) {
787 switch ($quiz->grademethod
) {
789 case QUIZ_ATTEMPTFIRST
:
790 $firstattempt = reset($attempts);
791 return $firstattempt->sumgrades
;
793 case QUIZ_ATTEMPTLAST
:
794 $lastattempt = end($attempts);
795 return $lastattempt->sumgrades
;
797 case QUIZ_GRADEAVERAGE
:
800 foreach ($attempts as $attempt) {
801 if (!is_null($attempt->sumgrades
)) {
802 $sum +
= $attempt->sumgrades
;
809 return $sum / $count;
811 case QUIZ_GRADEHIGHEST
:
814 foreach ($attempts as $attempt) {
815 if ($attempt->sumgrades
> $max) {
816 $max = $attempt->sumgrades
;
824 * Update the final grade at this quiz for all students.
826 * This function is equivalent to calling quiz_save_best_grade for all
827 * users, but much more efficient.
829 * @param object $quiz the quiz settings.
831 function quiz_update_all_final_grades($quiz) {
834 if (!$quiz->sumgrades
) {
838 $param = array('iquizid' => $quiz->id
, 'istatefinished' => quiz_attempt
::FINISHED
);
839 $firstlastattemptjoin = "JOIN (
842 MIN(attempt) AS firstattempt,
843 MAX(attempt) AS lastattempt
845 FROM {quiz_attempts} iquiza
848 iquiza.state = :istatefinished AND
849 iquiza.preview = 0 AND
850 iquiza.quiz = :iquizid
852 GROUP BY iquiza.userid
853 ) first_last_attempts ON first_last_attempts.userid = quiza.userid";
855 switch ($quiz->grademethod
) {
856 case QUIZ_ATTEMPTFIRST
:
857 // Because of the where clause, there will only be one row, but we
858 // must still use an aggregate function.
859 $select = 'MAX(quiza.sumgrades)';
860 $join = $firstlastattemptjoin;
861 $where = 'quiza.attempt = first_last_attempts.firstattempt AND';
864 case QUIZ_ATTEMPTLAST
:
865 // Because of the where clause, there will only be one row, but we
866 // must still use an aggregate function.
867 $select = 'MAX(quiza.sumgrades)';
868 $join = $firstlastattemptjoin;
869 $where = 'quiza.attempt = first_last_attempts.lastattempt AND';
872 case QUIZ_GRADEAVERAGE
:
873 $select = 'AVG(quiza.sumgrades)';
879 case QUIZ_GRADEHIGHEST
:
880 $select = 'MAX(quiza.sumgrades)';
886 if ($quiz->sumgrades
>= 0.000005) {
887 $finalgrade = $select . ' * ' . ($quiz->grade
/ $quiz->sumgrades
);
891 $param['quizid'] = $quiz->id
;
892 $param['quizid2'] = $quiz->id
;
893 $param['quizid3'] = $quiz->id
;
894 $param['quizid4'] = $quiz->id
;
895 $param['statefinished'] = quiz_attempt
::FINISHED
;
896 $param['statefinished2'] = quiz_attempt
::FINISHED
;
897 $finalgradesubquery = "
898 SELECT quiza.userid, $finalgrade AS newgrade
899 FROM {quiz_attempts} quiza
903 quiza.state = :statefinished AND
904 quiza.preview = 0 AND
905 quiza.quiz = :quizid3
906 GROUP BY quiza.userid";
908 $changedgrades = $DB->get_records_sql("
909 SELECT users.userid, qg.id, qg.grade, newgrades.newgrade
913 FROM {quiz_grades} qg
916 SELECT DISTINCT userid
917 FROM {quiz_attempts} quiza2
919 quiza2.state = :statefinished2 AND
920 quiza2.preview = 0 AND
921 quiza2.quiz = :quizid2
924 LEFT JOIN {quiz_grades} qg ON qg.userid = users.userid AND qg.quiz = :quizid4
928 ) newgrades ON newgrades.userid = users.userid
931 ABS(newgrades.newgrade - qg.grade) > 0.000005 OR
932 ((newgrades.newgrade IS NULL OR qg.grade IS NULL) AND NOT
933 (newgrades.newgrade IS NULL AND qg.grade IS NULL))",
934 // The mess on the previous line is detecting where the value is
935 // NULL in one column, and NOT NULL in the other, but SQL does
936 // not have an XOR operator, and MS SQL server can't cope with
937 // (newgrades.newgrade IS NULL) <> (qg.grade IS NULL).
942 foreach ($changedgrades as $changedgrade) {
944 if (is_null($changedgrade->newgrade
)) {
945 $todelete[] = $changedgrade->userid
;
947 } else if (is_null($changedgrade->grade
)) {
948 $toinsert = new stdClass();
949 $toinsert->quiz
= $quiz->id
;
950 $toinsert->userid
= $changedgrade->userid
;
951 $toinsert->timemodified
= $timenow;
952 $toinsert->grade
= $changedgrade->newgrade
;
953 $DB->insert_record('quiz_grades', $toinsert);
956 $toupdate = new stdClass();
957 $toupdate->id
= $changedgrade->id
;
958 $toupdate->grade
= $changedgrade->newgrade
;
959 $toupdate->timemodified
= $timenow;
960 $DB->update_record('quiz_grades', $toupdate);
964 if (!empty($todelete)) {
965 list($test, $params) = $DB->get_in_or_equal($todelete);
966 $DB->delete_records_select('quiz_grades', 'quiz = ? AND userid ' . $test,
967 array_merge(array($quiz->id
), $params));
972 * Return summary of the number of settings override that exist.
974 * To get a nice display of this, see the quiz_override_summary_links()
975 * quiz renderer method.
977 * @param stdClass $quiz the quiz settings. Only $quiz->id is used at the moment.
978 * @param stdClass|cm_info $cm the cm object. Only $cm->course, $cm->groupmode and
979 * $cm->groupingid fields are used at the moment.
980 * @param int $currentgroup if there is a concept of current group where this method is being called
981 * (e.g. a report) pass it in here. Default 0 which means no current group.
982 * @return array like 'group' => 3, 'user' => 12] where 3 is the number of group overrides,
983 * and 12 is the number of user ones.
985 function quiz_override_summary(stdClass
$quiz, stdClass
$cm, int $currentgroup = 0): array {
989 // Currently only interested in one group.
990 $groupcount = $DB->count_records('quiz_overrides', ['quiz' => $quiz->id
, 'groupid' => $currentgroup]);
991 $usercount = $DB->count_records_sql("
993 FROM {quiz_overrides} o
994 JOIN {groups_members} gm ON o.userid = gm.userid
997 ", [$quiz->id
, $currentgroup]);
998 return ['group' => $groupcount, 'user' => $usercount, 'mode' => 'onegroup'];
1001 $quizgroupmode = groups_get_activity_groupmode($cm);
1002 $accessallgroups = ($quizgroupmode == NOGROUPS
) ||
1003 has_capability('moodle/site:accessallgroups', context_module
::instance($cm->id
));
1005 if ($accessallgroups) {
1006 // User can see all groups.
1007 $groupcount = $DB->count_records_select('quiz_overrides',
1008 'quiz = ? AND groupid IS NOT NULL', [$quiz->id
]);
1009 $usercount = $DB->count_records_select('quiz_overrides',
1010 'quiz = ? AND userid IS NOT NULL', [$quiz->id
]);
1011 return ['group' => $groupcount, 'user' => $usercount, 'mode' => 'allgroups'];
1014 // User can only see groups they are in.
1015 $groups = groups_get_activity_allowed_groups($cm);
1017 return ['group' => 0, 'user' => 0, 'mode' => 'somegroups'];
1020 list($groupidtest, $params) = $DB->get_in_or_equal(array_keys($groups));
1021 $params[] = $quiz->id
;
1023 $groupcount = $DB->count_records_select('quiz_overrides',
1024 "groupid $groupidtest AND quiz = ?", $params);
1025 $usercount = $DB->count_records_sql("
1027 FROM {quiz_overrides} o
1028 JOIN {groups_members} gm ON o.userid = gm.userid
1029 WHERE gm.groupid $groupidtest
1033 return ['group' => $groupcount, 'user' => $usercount, 'mode' => 'somegroups'];
1038 * Efficiently update check state time on all open attempts
1040 * @param array $conditions optional restrictions on which attempts to update
1041 * Allowed conditions:
1042 * courseid => (array|int) attempts in given course(s)
1043 * userid => (array|int) attempts for given user(s)
1044 * quizid => (array|int) attempts in given quiz(s)
1045 * groupid => (array|int) quizzes with some override for given group(s)
1048 function quiz_update_open_attempts(array $conditions) {
1051 foreach ($conditions as &$value) {
1052 if (!is_array($value)) {
1053 $value = array($value);
1058 $wheres = array("quiza.state IN ('inprogress', 'overdue')");
1059 $iwheres = array("iquiza.state IN ('inprogress', 'overdue')");
1061 if (isset($conditions['courseid'])) {
1062 list ($incond, $inparams) = $DB->get_in_or_equal($conditions['courseid'], SQL_PARAMS_NAMED
, 'cid');
1063 $params = array_merge($params, $inparams);
1064 $wheres[] = "quiza.quiz IN (SELECT q.id FROM {quiz} q WHERE q.course $incond)";
1065 list ($incond, $inparams) = $DB->get_in_or_equal($conditions['courseid'], SQL_PARAMS_NAMED
, 'icid');
1066 $params = array_merge($params, $inparams);
1067 $iwheres[] = "iquiza.quiz IN (SELECT q.id FROM {quiz} q WHERE q.course $incond)";
1070 if (isset($conditions['userid'])) {
1071 list ($incond, $inparams) = $DB->get_in_or_equal($conditions['userid'], SQL_PARAMS_NAMED
, 'uid');
1072 $params = array_merge($params, $inparams);
1073 $wheres[] = "quiza.userid $incond";
1074 list ($incond, $inparams) = $DB->get_in_or_equal($conditions['userid'], SQL_PARAMS_NAMED
, 'iuid');
1075 $params = array_merge($params, $inparams);
1076 $iwheres[] = "iquiza.userid $incond";
1079 if (isset($conditions['quizid'])) {
1080 list ($incond, $inparams) = $DB->get_in_or_equal($conditions['quizid'], SQL_PARAMS_NAMED
, 'qid');
1081 $params = array_merge($params, $inparams);
1082 $wheres[] = "quiza.quiz $incond";
1083 list ($incond, $inparams) = $DB->get_in_or_equal($conditions['quizid'], SQL_PARAMS_NAMED
, 'iqid');
1084 $params = array_merge($params, $inparams);
1085 $iwheres[] = "iquiza.quiz $incond";
1088 if (isset($conditions['groupid'])) {
1089 list ($incond, $inparams) = $DB->get_in_or_equal($conditions['groupid'], SQL_PARAMS_NAMED
, 'gid');
1090 $params = array_merge($params, $inparams);
1091 $wheres[] = "quiza.quiz IN (SELECT qo.quiz FROM {quiz_overrides} qo WHERE qo.groupid $incond)";
1092 list ($incond, $inparams) = $DB->get_in_or_equal($conditions['groupid'], SQL_PARAMS_NAMED
, 'igid');
1093 $params = array_merge($params, $inparams);
1094 $iwheres[] = "iquiza.quiz IN (SELECT qo.quiz FROM {quiz_overrides} qo WHERE qo.groupid $incond)";
1097 // SQL to compute timeclose and timelimit for each attempt:
1098 $quizausersql = quiz_get_attempt_usertime_sql(
1099 implode("\n AND ", $iwheres));
1101 // SQL to compute the new timecheckstate
1102 $timecheckstatesql = "
1103 CASE WHEN quizauser.usertimelimit = 0 AND quizauser.usertimeclose = 0 THEN NULL
1104 WHEN quizauser.usertimelimit = 0 THEN quizauser.usertimeclose
1105 WHEN quizauser.usertimeclose = 0 THEN quiza.timestart + quizauser.usertimelimit
1106 WHEN quiza.timestart + quizauser.usertimelimit < quizauser.usertimeclose THEN quiza.timestart + quizauser.usertimelimit
1107 ELSE quizauser.usertimeclose END +
1108 CASE WHEN quiza.state = 'overdue' THEN quiz.graceperiod ELSE 0 END";
1110 // SQL to select which attempts to process
1111 $attemptselect = implode("\n AND ", $wheres);
1114 * Each database handles updates with inner joins differently:
1115 * - mysql does not allow a FROM clause
1116 * - postgres and mssql allow FROM but handle table aliases differently
1117 * - oracle requires a subquery
1119 * Different code for each database.
1122 $dbfamily = $DB->get_dbfamily();
1123 if ($dbfamily == 'mysql') {
1124 $updatesql = "UPDATE {quiz_attempts} quiza
1125 JOIN {quiz} quiz ON quiz.id = quiza.quiz
1126 JOIN ( $quizausersql ) quizauser ON quizauser.id = quiza.id
1127 SET quiza.timecheckstate = $timecheckstatesql
1128 WHERE $attemptselect";
1129 } else if ($dbfamily == 'postgres') {
1130 $updatesql = "UPDATE {quiz_attempts} quiza
1131 SET timecheckstate = $timecheckstatesql
1132 FROM {quiz} quiz, ( $quizausersql ) quizauser
1133 WHERE quiz.id = quiza.quiz
1134 AND quizauser.id = quiza.id
1135 AND $attemptselect";
1136 } else if ($dbfamily == 'mssql') {
1137 $updatesql = "UPDATE quiza
1138 SET timecheckstate = $timecheckstatesql
1139 FROM {quiz_attempts} quiza
1140 JOIN {quiz} quiz ON quiz.id = quiza.quiz
1141 JOIN ( $quizausersql ) quizauser ON quizauser.id = quiza.id
1142 WHERE $attemptselect";
1144 // oracle, sqlite and others
1145 $updatesql = "UPDATE {quiz_attempts} quiza
1146 SET timecheckstate = (
1147 SELECT $timecheckstatesql
1148 FROM {quiz} quiz, ( $quizausersql ) quizauser
1149 WHERE quiz.id = quiza.quiz
1150 AND quizauser.id = quiza.id
1152 WHERE $attemptselect";
1155 $DB->execute($updatesql, $params);
1159 * Returns SQL to compute timeclose and timelimit for every attempt, taking into account user and group overrides.
1160 * The query used herein is very similar to the one in function quiz_get_user_timeclose, so, in case you
1161 * would change either one of them, make sure to apply your changes to both.
1163 * @param string $redundantwhereclauses extra where clauses to add to the subquery
1164 * for performance. These can use the table alias iquiza for the quiz attempts table.
1165 * @return string SQL select with columns attempt.id, usertimeclose, usertimelimit.
1167 function quiz_get_attempt_usertime_sql($redundantwhereclauses = '') {
1168 if ($redundantwhereclauses) {
1169 $redundantwhereclauses = 'WHERE ' . $redundantwhereclauses;
1171 // The multiple qgo JOINS are necessary because we want timeclose/timelimit = 0 (unlimited) to supercede
1172 // any other group override
1175 COALESCE(MAX(quo.timeclose), MAX(qgo1.timeclose), MAX(qgo2.timeclose), iquiz.timeclose) AS usertimeclose,
1176 COALESCE(MAX(quo.timelimit), MAX(qgo3.timelimit), MAX(qgo4.timelimit), iquiz.timelimit) AS usertimelimit
1178 FROM {quiz_attempts} iquiza
1179 JOIN {quiz} iquiz ON iquiz.id = iquiza.quiz
1180 LEFT JOIN {quiz_overrides} quo ON quo.quiz = iquiza.quiz AND quo.userid = iquiza.userid
1181 LEFT JOIN {groups_members} gm ON gm.userid = iquiza.userid
1182 LEFT JOIN {quiz_overrides} qgo1 ON qgo1.quiz = iquiza.quiz AND qgo1.groupid = gm.groupid AND qgo1.timeclose = 0
1183 LEFT JOIN {quiz_overrides} qgo2 ON qgo2.quiz = iquiza.quiz AND qgo2.groupid = gm.groupid AND qgo2.timeclose > 0
1184 LEFT JOIN {quiz_overrides} qgo3 ON qgo3.quiz = iquiza.quiz AND qgo3.groupid = gm.groupid AND qgo3.timelimit = 0
1185 LEFT JOIN {quiz_overrides} qgo4 ON qgo4.quiz = iquiza.quiz AND qgo4.groupid = gm.groupid AND qgo4.timelimit > 0
1186 $redundantwhereclauses
1187 GROUP BY iquiza.id, iquiz.id, iquiz.timeclose, iquiz.timelimit";
1188 return $quizausersql;
1192 * Return the attempt with the best grade for a quiz
1194 * Which attempt is the best depends on $quiz->grademethod. If the grade
1195 * method is GRADEAVERAGE then this function simply returns the last attempt.
1196 * @return object The attempt with the best grade
1197 * @param object $quiz The quiz for which the best grade is to be calculated
1198 * @param array $attempts An array of all the attempts of the user at the quiz
1200 function quiz_calculate_best_attempt($quiz, $attempts) {
1202 switch ($quiz->grademethod
) {
1204 case QUIZ_ATTEMPTFIRST
:
1205 foreach ($attempts as $attempt) {
1210 case QUIZ_GRADEAVERAGE
: // We need to do something with it.
1211 case QUIZ_ATTEMPTLAST
:
1212 foreach ($attempts as $attempt) {
1218 case QUIZ_GRADEHIGHEST
:
1220 foreach ($attempts as $attempt) {
1221 if ($attempt->sumgrades
> $max) {
1222 $max = $attempt->sumgrades
;
1223 $maxattempt = $attempt;
1231 * @return array int => lang string the options for calculating the quiz grade
1232 * from the individual attempt grades.
1234 function quiz_get_grading_options() {
1236 QUIZ_GRADEHIGHEST
=> get_string('gradehighest', 'quiz'),
1237 QUIZ_GRADEAVERAGE
=> get_string('gradeaverage', 'quiz'),
1238 QUIZ_ATTEMPTFIRST
=> get_string('attemptfirst', 'quiz'),
1239 QUIZ_ATTEMPTLAST
=> get_string('attemptlast', 'quiz')
1244 * @param int $option one of the values QUIZ_GRADEHIGHEST, QUIZ_GRADEAVERAGE,
1245 * QUIZ_ATTEMPTFIRST or QUIZ_ATTEMPTLAST.
1246 * @return the lang string for that option.
1248 function quiz_get_grading_option_name($option) {
1249 $strings = quiz_get_grading_options();
1250 return $strings[$option];
1254 * @return array string => lang string the options for handling overdue quiz
1257 function quiz_get_overdue_handling_options() {
1259 'autosubmit' => get_string('overduehandlingautosubmit', 'quiz'),
1260 'graceperiod' => get_string('overduehandlinggraceperiod', 'quiz'),
1261 'autoabandon' => get_string('overduehandlingautoabandon', 'quiz'),
1266 * Get the choices for what size user picture to show.
1267 * @return array string => lang string the options for whether to display the user's picture.
1269 function quiz_get_user_image_options() {
1271 QUIZ_SHOWIMAGE_NONE
=> get_string('shownoimage', 'quiz'),
1272 QUIZ_SHOWIMAGE_SMALL
=> get_string('showsmallimage', 'quiz'),
1273 QUIZ_SHOWIMAGE_LARGE
=> get_string('showlargeimage', 'quiz'),
1278 * Return an user's timeclose for all quizzes in a course, hereby taking into account group and user overrides.
1280 * @param int $courseid the course id.
1281 * @return object An object with of all quizids and close unixdates in this course, taking into account the most lenient
1282 * overrides, if existing and 0 if no close date is set.
1284 function quiz_get_user_timeclose($courseid) {
1287 // For teacher and manager/admins return timeclose.
1288 if (has_capability('moodle/course:update', context_course
::instance($courseid))) {
1289 $sql = "SELECT quiz.id, quiz.timeclose AS usertimeclose
1291 WHERE quiz.course = :courseid";
1293 $results = $DB->get_records_sql($sql, array('courseid' => $courseid));
1297 $sql = "SELECT q.id,
1298 COALESCE(v.userclose, v.groupclose, q.timeclose, 0) AS usertimeclose
1300 SELECT quiz.id as quizid,
1301 MAX(quo.timeclose) AS userclose, MAX(qgo.timeclose) AS groupclose
1303 LEFT JOIN {quiz_overrides} quo on quiz.id = quo.quiz AND quo.userid = :userid
1304 LEFT JOIN {groups_members} gm ON gm.userid = :useringroupid
1305 LEFT JOIN {quiz_overrides} qgo on quiz.id = qgo.quiz AND qgo.groupid = gm.groupid
1306 WHERE quiz.course = :courseid
1308 JOIN {quiz} q ON q.id = v.quizid";
1310 $results = $DB->get_records_sql($sql, array('userid' => $USER->id
, 'useringroupid' => $USER->id
, 'courseid' => $courseid));
1316 * Get the choices to offer for the 'Questions per page' option.
1317 * @return array int => string.
1319 function quiz_questions_per_page_options() {
1320 $pageoptions = array();
1321 $pageoptions[0] = get_string('neverallononepage', 'quiz');
1322 $pageoptions[1] = get_string('everyquestion', 'quiz');
1323 for ($i = 2; $i <= QUIZ_MAX_QPP_OPTION
; ++
$i) {
1324 $pageoptions[$i] = get_string('everynquestions', 'quiz', $i);
1326 return $pageoptions;
1330 * Get the human-readable name for a quiz attempt state.
1331 * @param string $state one of the state constants like {@link quiz_attempt::IN_PROGRESS}.
1332 * @return string The lang string to describe that state.
1334 function quiz_attempt_state_name($state) {
1336 case quiz_attempt
::IN_PROGRESS
:
1337 return get_string('stateinprogress', 'quiz');
1338 case quiz_attempt
::OVERDUE
:
1339 return get_string('stateoverdue', 'quiz');
1340 case quiz_attempt
::FINISHED
:
1341 return get_string('statefinished', 'quiz');
1342 case quiz_attempt
::ABANDONED
:
1343 return get_string('stateabandoned', 'quiz');
1345 throw new coding_exception('Unknown quiz attempt state.');
1349 // Other quiz functions ////////////////////////////////////////////////////////
1352 * @param object $quiz the quiz.
1353 * @param int $cmid the course_module object for this quiz.
1354 * @param object $question the question.
1355 * @param string $returnurl url to return to after action is done.
1356 * @param int $variant which question variant to preview (optional).
1357 * @return string html for a number of icons linked to action pages for a
1358 * question - preview and edit / view icons depending on user capabilities.
1360 function quiz_question_action_icons($quiz, $cmid, $question, $returnurl, $variant = null) {
1361 $html = quiz_question_preview_button($quiz, $question, false, $variant) . ' ' .
1362 quiz_question_edit_button($cmid, $question, $returnurl);
1367 * @param int $cmid the course_module.id for this quiz.
1368 * @param object $question the question.
1369 * @param string $returnurl url to return to after action is done.
1370 * @param string $contentbeforeicon some HTML content to be added inside the link, before the icon.
1371 * @return the HTML for an edit icon, view icon, or nothing for a question
1372 * (depending on permissions).
1374 function quiz_question_edit_button($cmid, $question, $returnurl, $contentaftericon = '') {
1375 global $CFG, $OUTPUT;
1377 // Minor efficiency saving. Only get strings once, even if there are a lot of icons on one page.
1378 static $stredit = null;
1379 static $strview = null;
1380 if ($stredit === null) {
1381 $stredit = get_string('edit');
1382 $strview = get_string('view');
1385 // What sort of icon should we show?
1387 if (!empty($question->id
) &&
1388 (question_has_capability_on($question, 'edit') ||
1389 question_has_capability_on($question, 'move'))) {
1392 } else if (!empty($question->id
) &&
1393 question_has_capability_on($question, 'view')) {
1400 if ($returnurl instanceof moodle_url
) {
1401 $returnurl = $returnurl->out_as_local_url(false);
1403 $questionparams = array('returnurl' => $returnurl, 'cmid' => $cmid, 'id' => $question->id
);
1404 $questionurl = new moodle_url("$CFG->wwwroot/question/question.php", $questionparams);
1405 return '<a title="' . $action . '" href="' . $questionurl->out() . '" class="questioneditbutton">' .
1406 $OUTPUT->pix_icon($icon, $action) . $contentaftericon .
1408 } else if ($contentaftericon) {
1409 return '<span class="questioneditbutton">' . $contentaftericon . '</span>';
1416 * @param object $quiz the quiz settings
1417 * @param object $question the question
1418 * @param int $variant which question variant to preview (optional).
1419 * @return moodle_url to preview this question with the options from this quiz.
1421 function quiz_question_preview_url($quiz, $question, $variant = null) {
1422 // Get the appropriate display options.
1423 $displayoptions = mod_quiz_display_options
::make_from_quiz($quiz,
1424 mod_quiz_display_options
::DURING
);
1427 if (isset($question->maxmark
)) {
1428 $maxmark = $question->maxmark
;
1431 // Work out the correcte preview URL.
1432 return question_preview_url($question->id
, $quiz->preferredbehaviour
,
1433 $maxmark, $displayoptions, $variant);
1437 * @param object $quiz the quiz settings
1438 * @param object $question the question
1439 * @param bool $label if true, show the preview question label after the icon
1440 * @param int $variant which question variant to preview (optional).
1441 * @return the HTML for a preview question icon.
1443 function quiz_question_preview_button($quiz, $question, $label = false, $variant = null) {
1445 if (!question_has_capability_on($question, 'use')) {
1449 return $PAGE->get_renderer('mod_quiz', 'edit')->question_preview_icon($quiz, $question, $label, $variant);
1453 * @param object $attempt the attempt.
1454 * @param object $context the quiz context.
1455 * @return int whether flags should be shown/editable to the current user for this attempt.
1457 function quiz_get_flag_option($attempt, $context) {
1459 if (!has_capability('moodle/question:flag', $context)) {
1460 return question_display_options
::HIDDEN
;
1461 } else if ($attempt->userid
== $USER->id
) {
1462 return question_display_options
::EDITABLE
;
1464 return question_display_options
::VISIBLE
;
1469 * Work out what state this quiz attempt is in - in the sense used by
1470 * quiz_get_review_options, not in the sense of $attempt->state.
1471 * @param object $quiz the quiz settings
1472 * @param object $attempt the quiz_attempt database row.
1473 * @return int one of the mod_quiz_display_options::DURING,
1474 * IMMEDIATELY_AFTER, LATER_WHILE_OPEN or AFTER_CLOSE constants.
1476 function quiz_attempt_state($quiz, $attempt) {
1477 if ($attempt->state
== quiz_attempt
::IN_PROGRESS
) {
1478 return mod_quiz_display_options
::DURING
;
1479 } else if ($quiz->timeclose
&& time() >= $quiz->timeclose
) {
1480 return mod_quiz_display_options
::AFTER_CLOSE
;
1481 } else if (time() < $attempt->timefinish +
120) {
1482 return mod_quiz_display_options
::IMMEDIATELY_AFTER
;
1484 return mod_quiz_display_options
::LATER_WHILE_OPEN
;
1489 * The the appropraite mod_quiz_display_options object for this attempt at this
1492 * @param stdClass $quiz the quiz instance.
1493 * @param stdClass $attempt the attempt in question.
1494 * @param context $context the quiz context.
1496 * @return mod_quiz_display_options
1498 function quiz_get_review_options($quiz, $attempt, $context) {
1499 $options = mod_quiz_display_options
::make_from_quiz($quiz, quiz_attempt_state($quiz, $attempt));
1501 $options->readonly
= true;
1502 $options->flags
= quiz_get_flag_option($attempt, $context);
1503 if (!empty($attempt->id
)) {
1504 $options->questionreviewlink
= new moodle_url('/mod/quiz/reviewquestion.php',
1505 array('attempt' => $attempt->id
));
1508 // Show a link to the comment box only for closed attempts.
1509 if (!empty($attempt->id
) && $attempt->state
== quiz_attempt
::FINISHED
&& !$attempt->preview
&&
1510 !is_null($context) && has_capability('mod/quiz:grade', $context)) {
1511 $options->manualcomment
= question_display_options
::VISIBLE
;
1512 $options->manualcommentlink
= new moodle_url('/mod/quiz/comment.php',
1513 array('attempt' => $attempt->id
));
1516 if (!is_null($context) && !$attempt->preview
&&
1517 has_capability('mod/quiz:viewreports', $context) &&
1518 has_capability('moodle/grade:viewhidden', $context)) {
1519 // People who can see reports and hidden grades should be shown everything,
1520 // except during preview when teachers want to see what students see.
1521 $options->attempt
= question_display_options
::VISIBLE
;
1522 $options->correctness
= question_display_options
::VISIBLE
;
1523 $options->marks
= question_display_options
::MARK_AND_MAX
;
1524 $options->feedback
= question_display_options
::VISIBLE
;
1525 $options->numpartscorrect
= question_display_options
::VISIBLE
;
1526 $options->manualcomment
= question_display_options
::VISIBLE
;
1527 $options->generalfeedback
= question_display_options
::VISIBLE
;
1528 $options->rightanswer
= question_display_options
::VISIBLE
;
1529 $options->overallfeedback
= question_display_options
::VISIBLE
;
1530 $options->history
= question_display_options
::VISIBLE
;
1531 $options->userinfoinhistory
= $attempt->userid
;
1539 * Combines the review options from a number of different quiz attempts.
1540 * Returns an array of two ojects, so the suggested way of calling this
1542 * list($someoptions, $alloptions) = quiz_get_combined_reviewoptions(...)
1544 * @param object $quiz the quiz instance.
1545 * @param array $attempts an array of attempt objects.
1547 * @return array of two options objects, one showing which options are true for
1548 * at least one of the attempts, the other showing which options are true
1551 function quiz_get_combined_reviewoptions($quiz, $attempts) {
1552 $fields = array('feedback', 'generalfeedback', 'rightanswer', 'overallfeedback');
1553 $someoptions = new stdClass();
1554 $alloptions = new stdClass();
1555 foreach ($fields as $field) {
1556 $someoptions->$field = false;
1557 $alloptions->$field = true;
1559 $someoptions->marks
= question_display_options
::HIDDEN
;
1560 $alloptions->marks
= question_display_options
::MARK_AND_MAX
;
1562 // This shouldn't happen, but we need to prevent reveal information.
1563 if (empty($attempts)) {
1564 return array($someoptions, $someoptions);
1567 foreach ($attempts as $attempt) {
1568 $attemptoptions = mod_quiz_display_options
::make_from_quiz($quiz,
1569 quiz_attempt_state($quiz, $attempt));
1570 foreach ($fields as $field) {
1571 $someoptions->$field = $someoptions->$field ||
$attemptoptions->$field;
1572 $alloptions->$field = $alloptions->$field && $attemptoptions->$field;
1574 $someoptions->marks
= max($someoptions->marks
, $attemptoptions->marks
);
1575 $alloptions->marks
= min($alloptions->marks
, $attemptoptions->marks
);
1577 return array($someoptions, $alloptions);
1580 // Functions for sending notification messages /////////////////////////////////
1583 * Sends a confirmation message to the student confirming that the attempt was processed.
1585 * @param object $a lots of useful information that can be used in the message
1588 * @return int|false as for {@link message_send()}.
1590 function quiz_send_confirmation($recipient, $a) {
1592 // Add information about the recipient to $a.
1593 // Don't do idnumber. we want idnumber to be the submitter's idnumber.
1594 $a->username
= fullname($recipient);
1595 $a->userusername
= $recipient->username
;
1597 // Prepare the message.
1598 $eventdata = new \core\message\
message();
1599 $eventdata->courseid
= $a->courseid
;
1600 $eventdata->component
= 'mod_quiz';
1601 $eventdata->name
= 'confirmation';
1602 $eventdata->notification
= 1;
1604 $eventdata->userfrom
= core_user
::get_noreply_user();
1605 $eventdata->userto
= $recipient;
1606 $eventdata->subject
= get_string('emailconfirmsubject', 'quiz', $a);
1607 $eventdata->fullmessage
= get_string('emailconfirmbody', 'quiz', $a);
1608 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
1609 $eventdata->fullmessagehtml
= '';
1611 $eventdata->smallmessage
= get_string('emailconfirmsmall', 'quiz', $a);
1612 $eventdata->contexturl
= $a->quizurl
;
1613 $eventdata->contexturlname
= $a->quizname
;
1614 $eventdata->customdata
= [
1615 'cmid' => $a->quizcmid
,
1616 'instance' => $a->quizid
,
1617 'attemptid' => $a->attemptid
,
1621 return message_send($eventdata);
1625 * Sends notification messages to the interested parties that assign the role capability
1627 * @param object $recipient user object of the intended recipient
1628 * @param object $a associative array of replaceable fields for the templates
1630 * @return int|false as for {@link message_send()}.
1632 function quiz_send_notification($recipient, $submitter, $a) {
1635 // Recipient info for template.
1636 $a->useridnumber
= $recipient->idnumber
;
1637 $a->username
= fullname($recipient);
1638 $a->userusername
= $recipient->username
;
1640 // Prepare the message.
1641 $eventdata = new \core\message\
message();
1642 $eventdata->courseid
= $a->courseid
;
1643 $eventdata->component
= 'mod_quiz';
1644 $eventdata->name
= 'submission';
1645 $eventdata->notification
= 1;
1647 $eventdata->userfrom
= $submitter;
1648 $eventdata->userto
= $recipient;
1649 $eventdata->subject
= get_string('emailnotifysubject', 'quiz', $a);
1650 $eventdata->fullmessage
= get_string('emailnotifybody', 'quiz', $a);
1651 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
1652 $eventdata->fullmessagehtml
= '';
1654 $eventdata->smallmessage
= get_string('emailnotifysmall', 'quiz', $a);
1655 $eventdata->contexturl
= $a->quizreviewurl
;
1656 $eventdata->contexturlname
= $a->quizname
;
1657 $userpicture = new user_picture($submitter);
1658 $userpicture->size
= 1; // Use f1 size.
1659 $userpicture->includetoken
= $recipient->id
; // Generate an out-of-session token for the user receiving the message.
1660 $eventdata->customdata
= [
1661 'cmid' => $a->quizcmid
,
1662 'instance' => $a->quizid
,
1663 'attemptid' => $a->attemptid
,
1664 'notificationiconurl' => $userpicture->get_url($PAGE)->out(false),
1668 return message_send($eventdata);
1672 * Send all the requried messages when a quiz attempt is submitted.
1674 * @param object $course the course
1675 * @param object $quiz the quiz
1676 * @param object $attempt this attempt just finished
1677 * @param object $context the quiz context
1678 * @param object $cm the coursemodule for this quiz
1680 * @return bool true if all necessary messages were sent successfully, else false.
1682 function quiz_send_notification_messages($course, $quiz, $attempt, $context, $cm) {
1685 // Do nothing if required objects not present.
1686 if (empty($course) or empty($quiz) or empty($attempt) or empty($context)) {
1687 throw new coding_exception('$course, $quiz, $attempt, $context and $cm must all be set.');
1690 $submitter = $DB->get_record('user', array('id' => $attempt->userid
), '*', MUST_EXIST
);
1692 // Check for confirmation required.
1693 $sendconfirm = false;
1694 $notifyexcludeusers = '';
1695 if (has_capability('mod/quiz:emailconfirmsubmission', $context, $submitter, false)) {
1696 $notifyexcludeusers = $submitter->id
;
1697 $sendconfirm = true;
1700 // Check for notifications required.
1701 $notifyfields = 'u.id, u.username, u.idnumber, u.email, u.emailstop, u.lang,
1702 u.timezone, u.mailformat, u.maildisplay, u.auth, u.suspended, u.deleted, ';
1703 $userfieldsapi = \core_user\fields
::for_name();
1704 $notifyfields .= $userfieldsapi->get_sql('u', false, '', '', false)->selects
;
1705 $groups = groups_get_all_groups($course->id
, $submitter->id
, $cm->groupingid
);
1706 if (is_array($groups) && count($groups) > 0) {
1707 $groups = array_keys($groups);
1708 } else if (groups_get_activity_groupmode($cm, $course) != NOGROUPS
) {
1709 // If the user is not in a group, and the quiz is set to group mode,
1710 // then set $groups to a non-existant id so that only users with
1711 // 'moodle/site:accessallgroups' get notified.
1716 $userstonotify = get_users_by_capability($context, 'mod/quiz:emailnotifysubmission',
1717 $notifyfields, '', '', '', $groups, $notifyexcludeusers, false, false, true);
1719 if (empty($userstonotify) && !$sendconfirm) {
1720 return true; // Nothing to do.
1723 $a = new stdClass();
1725 $a->courseid
= $course->id
;
1726 $a->coursename
= $course->fullname
;
1727 $a->courseshortname
= $course->shortname
;
1729 $a->quizname
= $quiz->name
;
1730 $a->quizreporturl
= $CFG->wwwroot
. '/mod/quiz/report.php?id=' . $cm->id
;
1731 $a->quizreportlink
= '<a href="' . $a->quizreporturl
. '">' .
1732 format_string($quiz->name
) . ' report</a>';
1733 $a->quizurl
= $CFG->wwwroot
. '/mod/quiz/view.php?id=' . $cm->id
;
1734 $a->quizlink
= '<a href="' . $a->quizurl
. '">' . format_string($quiz->name
) . '</a>';
1735 $a->quizid
= $quiz->id
;
1736 $a->quizcmid
= $cm->id
;
1738 $a->submissiontime
= userdate($attempt->timefinish
);
1739 $a->timetaken
= format_time($attempt->timefinish
- $attempt->timestart
);
1740 $a->quizreviewurl
= $CFG->wwwroot
. '/mod/quiz/review.php?attempt=' . $attempt->id
;
1741 $a->quizreviewlink
= '<a href="' . $a->quizreviewurl
. '">' .
1742 format_string($quiz->name
) . ' review</a>';
1743 $a->attemptid
= $attempt->id
;
1744 // Student who sat the quiz info.
1745 $a->studentidnumber
= $submitter->idnumber
;
1746 $a->studentname
= fullname($submitter);
1747 $a->studentusername
= $submitter->username
;
1751 // Send notifications if required.
1752 if (!empty($userstonotify)) {
1753 foreach ($userstonotify as $recipient) {
1754 $allok = $allok && quiz_send_notification($recipient, $submitter, $a);
1758 // Send confirmation if required. We send the student confirmation last, so
1759 // that if message sending is being intermittently buggy, which means we send
1760 // some but not all messages, and then try again later, then teachers may get
1761 // duplicate messages, but the student will always get exactly one.
1763 $allok = $allok && quiz_send_confirmation($submitter, $a);
1770 * Send the notification message when a quiz attempt becomes overdue.
1772 * @param quiz_attempt $attemptobj all the data about the quiz attempt.
1774 function quiz_send_overdue_message($attemptobj) {
1777 $submitter = $DB->get_record('user', array('id' => $attemptobj->get_userid()), '*', MUST_EXIST
);
1779 if (!$attemptobj->has_capability('mod/quiz:emailwarnoverdue', $submitter->id
, false)) {
1780 return; // Message not required.
1783 if (!$attemptobj->has_response_to_at_least_one_graded_question()) {
1784 return; // Message not required.
1787 // Prepare lots of useful information that admins might want to include in
1788 // the email message.
1789 $quizname = format_string($attemptobj->get_quiz_name());
1791 $deadlines = array();
1792 if ($attemptobj->get_quiz()->timelimit
) {
1793 $deadlines[] = $attemptobj->get_attempt()->timestart +
$attemptobj->get_quiz()->timelimit
;
1795 if ($attemptobj->get_quiz()->timeclose
) {
1796 $deadlines[] = $attemptobj->get_quiz()->timeclose
;
1798 $duedate = min($deadlines);
1799 $graceend = $duedate +
$attemptobj->get_quiz()->graceperiod
;
1801 $a = new stdClass();
1803 $a->courseid
= $attemptobj->get_course()->id
;
1804 $a->coursename
= format_string($attemptobj->get_course()->fullname
);
1805 $a->courseshortname
= format_string($attemptobj->get_course()->shortname
);
1807 $a->quizname
= $quizname;
1808 $a->quizurl
= $attemptobj->view_url();
1809 $a->quizlink
= '<a href="' . $a->quizurl
. '">' . $quizname . '</a>';
1811 $a->attemptduedate
= userdate($duedate);
1812 $a->attemptgraceend
= userdate($graceend);
1813 $a->attemptsummaryurl
= $attemptobj->summary_url()->out(false);
1814 $a->attemptsummarylink
= '<a href="' . $a->attemptsummaryurl
. '">' . $quizname . ' review</a>';
1816 $a->studentidnumber
= $submitter->idnumber
;
1817 $a->studentname
= fullname($submitter);
1818 $a->studentusername
= $submitter->username
;
1820 // Prepare the message.
1821 $eventdata = new \core\message\
message();
1822 $eventdata->courseid
= $a->courseid
;
1823 $eventdata->component
= 'mod_quiz';
1824 $eventdata->name
= 'attempt_overdue';
1825 $eventdata->notification
= 1;
1827 $eventdata->userfrom
= core_user
::get_noreply_user();
1828 $eventdata->userto
= $submitter;
1829 $eventdata->subject
= get_string('emailoverduesubject', 'quiz', $a);
1830 $eventdata->fullmessage
= get_string('emailoverduebody', 'quiz', $a);
1831 $eventdata->fullmessageformat
= FORMAT_PLAIN
;
1832 $eventdata->fullmessagehtml
= '';
1834 $eventdata->smallmessage
= get_string('emailoverduesmall', 'quiz', $a);
1835 $eventdata->contexturl
= $a->quizurl
;
1836 $eventdata->contexturlname
= $a->quizname
;
1837 $eventdata->customdata
= [
1838 'cmid' => $attemptobj->get_cmid(),
1839 'instance' => $attemptobj->get_quizid(),
1840 'attemptid' => $attemptobj->get_attemptid(),
1843 // Send the message.
1844 return message_send($eventdata);
1848 * Handle the quiz_attempt_submitted event.
1850 * This sends the confirmation and notification messages, if required.
1852 * @param object $event the event object.
1854 function quiz_attempt_submitted_handler($event) {
1857 $course = $DB->get_record('course', array('id' => $event->courseid
));
1858 $attempt = $event->get_record_snapshot('quiz_attempts', $event->objectid
);
1859 $quiz = $event->get_record_snapshot('quiz', $attempt->quiz
);
1860 $cm = get_coursemodule_from_id('quiz', $event->get_context()->instanceid
, $event->courseid
);
1862 if (!($course && $quiz && $cm && $attempt)) {
1863 // Something has been deleted since the event was raised. Therefore, the
1864 // event is no longer relevant.
1868 // Update completion state.
1869 $completion = new completion_info($course);
1870 if ($completion->is_enabled($cm) &&
1871 ($quiz->completionattemptsexhausted ||
$quiz->completionpass ||
$quiz->completionminattempts
)) {
1872 $completion->update_state($cm, COMPLETION_COMPLETE
, $event->userid
);
1874 return quiz_send_notification_messages($course, $quiz, $attempt,
1875 context_module
::instance($cm->id
), $cm);
1879 * Handle groups_member_added event
1881 * @param object $event the event object.
1882 * @deprecated since 2.6, see {@link \mod_quiz\group_observers::group_member_added()}.
1884 function quiz_groups_member_added_handler($event) {
1885 debugging('quiz_groups_member_added_handler() is deprecated, please use ' .
1886 '\mod_quiz\group_observers::group_member_added() instead.', DEBUG_DEVELOPER
);
1887 quiz_update_open_attempts(array('userid'=>$event->userid
, 'groupid'=>$event->groupid
));
1891 * Handle groups_member_removed event
1893 * @param object $event the event object.
1894 * @deprecated since 2.6, see {@link \mod_quiz\group_observers::group_member_removed()}.
1896 function quiz_groups_member_removed_handler($event) {
1897 debugging('quiz_groups_member_removed_handler() is deprecated, please use ' .
1898 '\mod_quiz\group_observers::group_member_removed() instead.', DEBUG_DEVELOPER
);
1899 quiz_update_open_attempts(array('userid'=>$event->userid
, 'groupid'=>$event->groupid
));
1903 * Handle groups_group_deleted event
1905 * @param object $event the event object.
1906 * @deprecated since 2.6, see {@link \mod_quiz\group_observers::group_deleted()}.
1908 function quiz_groups_group_deleted_handler($event) {
1910 debugging('quiz_groups_group_deleted_handler() is deprecated, please use ' .
1911 '\mod_quiz\group_observers::group_deleted() instead.', DEBUG_DEVELOPER
);
1912 quiz_process_group_deleted_in_course($event->courseid
);
1916 * Logic to happen when a/some group(s) has/have been deleted in a course.
1918 * @param int $courseid The course ID.
1921 function quiz_process_group_deleted_in_course($courseid) {
1924 // It would be nice if we got the groupid that was deleted.
1925 // Instead, we just update all quizzes with orphaned group overrides.
1926 $sql = "SELECT o.id, o.quiz, o.groupid
1927 FROM {quiz_overrides} o
1928 JOIN {quiz} quiz ON quiz.id = o.quiz
1929 LEFT JOIN {groups} grp ON grp.id = o.groupid
1930 WHERE quiz.course = :courseid
1931 AND o.groupid IS NOT NULL
1932 AND grp.id IS NULL";
1933 $params = array('courseid' => $courseid);
1934 $records = $DB->get_records_sql($sql, $params);
1936 return; // Nothing to do.
1938 $DB->delete_records_list('quiz_overrides', 'id', array_keys($records));
1939 $cache = cache
::make('mod_quiz', 'overrides');
1940 foreach ($records as $record) {
1941 $cache->delete("{$record->quiz}_g_{$record->groupid}");
1943 quiz_update_open_attempts(['quizid' => array_unique(array_column($records, 'quiz'))]);
1947 * Handle groups_members_removed event
1949 * @param object $event the event object.
1950 * @deprecated since 2.6, see {@link \mod_quiz\group_observers::group_member_removed()}.
1952 function quiz_groups_members_removed_handler($event) {
1953 debugging('quiz_groups_members_removed_handler() is deprecated, please use ' .
1954 '\mod_quiz\group_observers::group_member_removed() instead.', DEBUG_DEVELOPER
);
1955 if ($event->userid
== 0) {
1956 quiz_update_open_attempts(array('courseid'=>$event->courseid
));
1958 quiz_update_open_attempts(array('courseid'=>$event->courseid
, 'userid'=>$event->userid
));
1963 * Get the information about the standard quiz JavaScript module.
1964 * @return array a standard jsmodule structure.
1966 function quiz_get_js_module() {
1970 'name' => 'mod_quiz',
1971 'fullpath' => '/mod/quiz/module.js',
1972 'requires' => array('base', 'dom', 'event-delegate', 'event-key',
1973 'core_question_engine', 'moodle-core-formchangechecker'),
1975 array('cancel', 'moodle'),
1976 array('flagged', 'question'),
1977 array('functiondisabledbysecuremode', 'quiz'),
1978 array('startattempt', 'quiz'),
1979 array('timesup', 'quiz'),
1980 array('changesmadereallygoaway', 'moodle'),
1987 * An extension of question_display_options that includes the extra options used
1990 * @copyright 2010 The Open University
1991 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1993 class mod_quiz_display_options
extends question_display_options
{
1995 * @var integer bits used to indicate various times in relation to a
1998 const DURING
= 0x10000;
1999 const IMMEDIATELY_AFTER
= 0x01000;
2000 const LATER_WHILE_OPEN
= 0x00100;
2001 const AFTER_CLOSE
= 0x00010;
2005 * @var boolean if this is false, then the student is not allowed to review
2006 * anything about the attempt.
2008 public $attempt = true;
2011 * @var boolean if this is false, then the student is not allowed to review
2012 * anything about the attempt.
2014 public $overallfeedback = self
::VISIBLE
;
2017 * Set up the various options from the quiz settings, and a time constant.
2018 * @param object $quiz the quiz settings.
2019 * @param int $one of the {@link DURING}, {@link IMMEDIATELY_AFTER},
2020 * {@link LATER_WHILE_OPEN} or {@link AFTER_CLOSE} constants.
2021 * @return mod_quiz_display_options set up appropriately.
2023 public static function make_from_quiz($quiz, $when) {
2024 $options = new self();
2026 $options->attempt
= self
::extract($quiz->reviewattempt
, $when, true, false);
2027 $options->correctness
= self
::extract($quiz->reviewcorrectness
, $when);
2028 $options->marks
= self
::extract($quiz->reviewmarks
, $when,
2029 self
::MARK_AND_MAX
, self
::MAX_ONLY
);
2030 $options->feedback
= self
::extract($quiz->reviewspecificfeedback
, $when);
2031 $options->generalfeedback
= self
::extract($quiz->reviewgeneralfeedback
, $when);
2032 $options->rightanswer
= self
::extract($quiz->reviewrightanswer
, $when);
2033 $options->overallfeedback
= self
::extract($quiz->reviewoverallfeedback
, $when);
2035 $options->numpartscorrect
= $options->feedback
;
2036 $options->manualcomment
= $options->feedback
;
2038 if ($quiz->questiondecimalpoints
!= -1) {
2039 $options->markdp
= $quiz->questiondecimalpoints
;
2041 $options->markdp
= $quiz->decimalpoints
;
2047 protected static function extract($bitmask, $bit,
2048 $whenset = self
::VISIBLE
, $whennotset = self
::HIDDEN
) {
2049 if ($bitmask & $bit) {
2058 * A {@link qubaid_condition} for finding all the question usages belonging to
2059 * a particular quiz.
2061 * @copyright 2010 The Open University
2062 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2064 class qubaids_for_quiz
extends qubaid_join
{
2065 public function __construct($quizid, $includepreviews = true, $onlyfinished = false) {
2066 $where = 'quiza.quiz = :quizaquiz';
2067 $params = array('quizaquiz' => $quizid);
2069 if (!$includepreviews) {
2070 $where .= ' AND preview = 0';
2073 if ($onlyfinished) {
2074 $where .= ' AND state = :statefinished';
2075 $params['statefinished'] = quiz_attempt
::FINISHED
;
2078 parent
::__construct('{quiz_attempts} quiza', 'quiza.uniqueid', $where, $params);
2083 * A {@link qubaid_condition} for finding all the question usages belonging to a particular user and quiz combination.
2085 * @copyright 2018 Andrew Nicols <andrwe@nicols.co.uk>
2086 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2088 class qubaids_for_quiz_user
extends qubaid_join
{
2090 * Constructor for this qubaid.
2092 * @param int $quizid The quiz to search.
2093 * @param int $userid The user to filter on
2094 * @param bool $includepreviews Whether to include preview attempts
2095 * @param bool $onlyfinished Whether to only include finished attempts or not
2097 public function __construct($quizid, $userid, $includepreviews = true, $onlyfinished = false) {
2098 $where = 'quiza.quiz = :quizaquiz AND quiza.userid = :quizauserid';
2100 'quizaquiz' => $quizid,
2101 'quizauserid' => $userid,
2104 if (!$includepreviews) {
2105 $where .= ' AND preview = 0';
2108 if ($onlyfinished) {
2109 $where .= ' AND state = :statefinished';
2110 $params['statefinished'] = quiz_attempt
::FINISHED
;
2113 parent
::__construct('{quiz_attempts} quiza', 'quiza.uniqueid', $where, $params);
2118 * Creates a textual representation of a question for display.
2120 * @param object $question A question object from the database questions table
2121 * @param bool $showicon If true, show the question's icon with the question. False by default.
2122 * @param bool $showquestiontext If true (default), show question text after question name.
2123 * If false, show only question name.
2124 * @param bool $showidnumber If true, show the question's idnumber, if any. False by default.
2125 * @param core_tag_tag[]|bool $showtags if array passed, show those tags. Else, if true, get and show tags,
2126 * else, don't show tags (which is the default).
2127 * @return string HTML fragment.
2129 function quiz_question_tostring($question, $showicon = false, $showquestiontext = true,
2130 $showidnumber = false, $showtags = false) {
2135 $name = shorten_text(format_string($question->name
), 200);
2137 $name .= print_question_icon($question) . ' ' . $name;
2139 $result .= html_writer
::span($name, 'questionname');
2141 // Question idnumber.
2142 if ($showidnumber && $question->idnumber
!== null && $question->idnumber
!== '') {
2143 $result .= ' ' . html_writer
::span(
2144 html_writer
::span(get_string('idnumber', 'question'), 'accesshide') .
2145 ' ' . s($question->idnumber
), 'badge badge-primary');
2149 if (is_array($showtags)) {
2151 } else if ($showtags) {
2152 $tags = core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
2157 $result .= $OUTPUT->tag_list($tags, null, 'd-inline', 0, null, true);
2161 if ($showquestiontext) {
2162 $questiontext = question_utils
::to_plain_text($question->questiontext
,
2163 $question->questiontextformat
, array('noclean' => true, 'para' => false));
2164 $questiontext = shorten_text($questiontext, 200);
2165 if ($questiontext) {
2166 $result .= ' ' . html_writer
::span(s($questiontext), 'questiontext');
2174 * Verify that the question exists, and the user has permission to use it.
2175 * Does not return. Throws an exception if the question cannot be used.
2176 * @param int $questionid The id of the question.
2178 function quiz_require_question_use($questionid) {
2180 $question = $DB->get_record('question', array('id' => $questionid), '*', MUST_EXIST
);
2181 question_require_capability_on($question, 'use');
2185 * Verify that the question exists, and the user has permission to use it.
2186 * @param object $quiz the quiz settings.
2187 * @param int $slot which question in the quiz to test.
2188 * @return bool whether the user can use this question.
2190 function quiz_has_question_use($quiz, $slot) {
2192 $question = $DB->get_record_sql("
2194 FROM {quiz_slots} slot
2195 JOIN {question} q ON q.id = slot.questionid
2196 WHERE slot.quizid = ? AND slot.slot = ?", array($quiz->id
, $slot));
2200 return question_has_capability_on($question, 'use');
2204 * Add a question to a quiz
2206 * Adds a question to a quiz by updating $quiz as well as the
2207 * quiz and quiz_slots tables. It also adds a page break if required.
2208 * @param int $questionid The id of the question to be added
2209 * @param object $quiz The extended quiz object as used by edit.php
2210 * This is updated by this function
2211 * @param int $page Which page in quiz to add the question on. If 0 (default),
2213 * @param float $maxmark The maximum mark to set for this question. (Optional,
2214 * defaults to question.defaultmark.
2215 * @return bool false if the question was already in the quiz
2217 function quiz_add_quiz_question($questionid, $quiz, $page = 0, $maxmark = null) {
2220 // Make sue the question is not of the "random" type.
2221 $questiontype = $DB->get_field('question', 'qtype', array('id' => $questionid));
2222 if ($questiontype == 'random') {
2223 throw new coding_exception(
2224 'Adding "random" questions via quiz_add_quiz_question() is deprecated. Please use quiz_add_random_questions().'
2228 $trans = $DB->start_delegated_transaction();
2229 $slots = $DB->get_records('quiz_slots', array('quizid' => $quiz->id
),
2230 'slot', 'questionid, slot, page, id');
2231 if (array_key_exists($questionid, $slots)) {
2232 $trans->allow_commit();
2238 foreach ($slots as $slot) {
2239 if ($slot->page
> $maxpage) {
2240 $maxpage = $slot->page
;
2243 $numonlastpage +
= 1;
2247 // Add the new question instance.
2248 $slot = new stdClass();
2249 $slot->quizid
= $quiz->id
;
2250 $slot->questionid
= $questionid;
2252 if ($maxmark !== null) {
2253 $slot->maxmark
= $maxmark;
2255 $slot->maxmark
= $DB->get_field('question', 'defaultmark', array('id' => $questionid));
2258 if (is_int($page) && $page >= 1) {
2259 // Adding on a given page.
2260 $lastslotbefore = 0;
2261 foreach (array_reverse($slots) as $otherslot) {
2262 if ($otherslot->page
> $page) {
2263 $DB->set_field('quiz_slots', 'slot', $otherslot->slot +
1, array('id' => $otherslot->id
));
2265 $lastslotbefore = $otherslot->slot
;
2269 $slot->slot
= $lastslotbefore +
1;
2270 $slot->page
= min($page, $maxpage +
1);
2272 quiz_update_section_firstslots($quiz->id
, 1, max($lastslotbefore, 1));
2275 $lastslot = end($slots);
2277 $slot->slot
= $lastslot->slot +
1;
2281 if ($quiz->questionsperpage
&& $numonlastpage >= $quiz->questionsperpage
) {
2282 $slot->page
= $maxpage +
1;
2284 $slot->page
= $maxpage;
2288 $DB->insert_record('quiz_slots', $slot);
2289 $trans->allow_commit();
2293 * Move all the section headings in a certain slot range by a certain offset.
2295 * @param int $quizid the id of a quiz
2296 * @param int $direction amount to adjust section heading positions. Normally +1 or -1.
2297 * @param int $afterslot adjust headings that start after this slot.
2298 * @param int|null $beforeslot optionally, only adjust headings before this slot.
2300 function quiz_update_section_firstslots($quizid, $direction, $afterslot, $beforeslot = null) {
2302 $where = 'quizid = ? AND firstslot > ?';
2303 $params = [$direction, $quizid, $afterslot];
2305 $where .= ' AND firstslot < ?';
2306 $params[] = $beforeslot;
2308 $firstslotschanges = $DB->get_records_select_menu('quiz_sections',
2309 $where, $params, '', 'firstslot, firstslot + ?');
2310 update_field_with_unique_index('quiz_sections', 'firstslot', $firstslotschanges, ['quizid' => $quizid]);
2314 * Add a random question to the quiz at a given point.
2315 * @param stdClass $quiz the quiz settings.
2316 * @param int $addonpage the page on which to add the question.
2317 * @param int $categoryid the question category to add the question from.
2318 * @param int $number the number of random questions to add.
2319 * @param bool $includesubcategories whether to include questoins from subcategories.
2320 * @param int[] $tagids Array of tagids. The question that will be picked randomly should be tagged with all these tags.
2322 function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number,
2323 $includesubcategories, $tagids = []) {
2326 $category = $DB->get_record('question_categories', array('id' => $categoryid));
2328 print_error('invalidcategoryid', 'error');
2331 $catcontext = context
::instance_by_id($category->contextid
);
2332 require_capability('moodle/question:useall', $catcontext);
2334 $tags = \core_tag_tag
::get_bulk($tagids, 'id, name');
2336 foreach ($tags as $tag) {
2337 $tagstrings[] = "{$tag->id},{$tag->name}";
2340 // Find existing random questions in this category that are
2341 // not used by any quiz.
2342 $existingquestions = $DB->get_records_sql(
2343 "SELECT q.id, q.qtype FROM {question} q
2344 WHERE qtype = 'random'
2346 AND " . $DB->sql_compare_text('questiontext') . " = ?
2350 WHERE questionid = q.id)
2351 ORDER BY id", array($category->id
, $includesubcategories ?
'1' : '0'));
2353 for ($i = 0; $i < $number; $i++
) {
2354 // Take as many of orphaned "random" questions as needed.
2355 if (!$question = array_shift($existingquestions)) {
2356 $form = new stdClass();
2357 $form->category
= $category->id
. ',' . $category->contextid
;
2358 $form->includesubcategories
= $includesubcategories;
2359 $form->fromtags
= $tagstrings;
2360 $form->defaultmark
= 1;
2362 $form->stamp
= make_unique_id_code(); // Set the unique code (not to be changed).
2363 $question = new stdClass();
2364 $question->qtype
= 'random';
2365 $question = question_bank
::get_qtype('random')->save_question($question, $form);
2366 if (!isset($question->id
)) {
2367 print_error('cannotinsertrandomquestion', 'quiz');
2371 $randomslotdata = new stdClass();
2372 $randomslotdata->quizid
= $quiz->id
;
2373 $randomslotdata->questionid
= $question->id
;
2374 $randomslotdata->questioncategoryid
= $categoryid;
2375 $randomslotdata->includingsubcategories
= $includesubcategories ?
1 : 0;
2376 $randomslotdata->maxmark
= 1;
2378 $randomslot = new \mod_quiz\local\structure\
slot_random($randomslotdata);
2379 $randomslot->set_quiz($quiz);
2380 $randomslot->set_tags($tags);
2381 $randomslot->insert($addonpage);
2386 * Mark the activity completed (if required) and trigger the course_module_viewed event.
2388 * @param stdClass $quiz quiz object
2389 * @param stdClass $course course object
2390 * @param stdClass $cm course module object
2391 * @param stdClass $context context object
2394 function quiz_view($quiz, $course, $cm, $context) {
2397 'objectid' => $quiz->id
,
2398 'context' => $context
2401 $event = \mod_quiz\event\course_module_viewed
::create($params);
2402 $event->add_record_snapshot('quiz', $quiz);
2406 $completion = new completion_info($course);
2407 $completion->set_module_viewed($cm);
2411 * Validate permissions for creating a new attempt and start a new preview attempt if required.
2413 * @param quiz $quizobj quiz object
2414 * @param quiz_access_manager $accessmanager quiz access manager
2415 * @param bool $forcenew whether was required to start a new preview attempt
2416 * @param int $page page to jump to in the attempt
2417 * @param bool $redirect whether to redirect or throw exceptions (for web or ws usage)
2418 * @return array an array containing the attempt information, access error messages and the page to jump to in the attempt
2419 * @throws moodle_quiz_exception
2422 function quiz_validate_new_attempt(quiz
$quizobj, quiz_access_manager
$accessmanager, $forcenew, $page, $redirect) {
2426 if ($quizobj->is_preview_user() && $forcenew) {
2427 $accessmanager->current_attempt_finished();
2430 // Check capabilities.
2431 if (!$quizobj->is_preview_user()) {
2432 $quizobj->require_capability('mod/quiz:attempt');
2435 // Check to see if a new preview was requested.
2436 if ($quizobj->is_preview_user() && $forcenew) {
2437 // To force the creation of a new preview, we mark the current attempt (if any)
2438 // as abandoned. It will then automatically be deleted below.
2439 $DB->set_field('quiz_attempts', 'state', quiz_attempt
::ABANDONED
,
2440 array('quiz' => $quizobj->get_quizid(), 'userid' => $USER->id
));
2443 // Look for an existing attempt.
2444 $attempts = quiz_get_user_attempts($quizobj->get_quizid(), $USER->id
, 'all', true);
2445 $lastattempt = end($attempts);
2447 $attemptnumber = null;
2448 // If an in-progress attempt exists, check password then redirect to it.
2449 if ($lastattempt && ($lastattempt->state
== quiz_attempt
::IN_PROGRESS ||
2450 $lastattempt->state
== quiz_attempt
::OVERDUE
)) {
2451 $currentattemptid = $lastattempt->id
;
2452 $messages = $accessmanager->prevent_access();
2454 // If the attempt is now overdue, deal with that.
2455 $quizobj->create_attempt_object($lastattempt)->handle_if_time_expired($timenow, true);
2457 // And, if the attempt is now no longer in progress, redirect to the appropriate place.
2458 if ($lastattempt->state
== quiz_attempt
::ABANDONED ||
$lastattempt->state
== quiz_attempt
::FINISHED
) {
2460 redirect($quizobj->review_url($lastattempt->id
));
2462 throw new moodle_quiz_exception($quizobj, 'attemptalreadyclosed');
2466 // If the page number was not explicitly in the URL, go to the current page.
2468 $page = $lastattempt->currentpage
;
2472 while ($lastattempt && $lastattempt->preview
) {
2473 $lastattempt = array_pop($attempts);
2476 // Get number for the next or unfinished attempt.
2478 $attemptnumber = $lastattempt->attempt +
1;
2480 $lastattempt = false;
2483 $currentattemptid = null;
2485 $messages = $accessmanager->prevent_access() +
2486 $accessmanager->prevent_new_attempt(count($attempts), $lastattempt);
2492 return array($currentattemptid, $attemptnumber, $lastattempt, $messages, $page);
2496 * Prepare and start a new attempt deleting the previous preview attempts.
2498 * @param quiz $quizobj quiz object
2499 * @param int $attemptnumber the attempt number
2500 * @param object $lastattempt last attempt object
2501 * @param bool $offlineattempt whether is an offline attempt or not
2502 * @param array $forcedrandomquestions slot number => question id. Used for random questions,
2503 * to force the choice of a particular actual question. Intended for testing purposes only.
2504 * @param array $forcedvariants slot number => variant. Used for questions with variants,
2505 * to force the choice of a particular variant. Intended for testing purposes only.
2506 * @param int $userid Specific user id to create an attempt for that user, null for current logged in user
2507 * @return object the new attempt
2510 function quiz_prepare_and_start_new_attempt(quiz
$quizobj, $attemptnumber, $lastattempt,
2511 $offlineattempt = false, $forcedrandomquestions = [], $forcedvariants = [], $userid = null) {
2514 if ($userid === null) {
2515 $userid = $USER->id
;
2516 $ispreviewuser = $quizobj->is_preview_user();
2518 $ispreviewuser = has_capability('mod/quiz:preview', $quizobj->get_context(), $userid);
2520 // Delete any previous preview attempts belonging to this user.
2521 quiz_delete_previews($quizobj->get_quiz(), $userid);
2523 $quba = question_engine
::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
2524 $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour
);
2526 // Create the new attempt and initialize the question sessions
2527 $timenow = time(); // Update time now, in case the server is running really slowly.
2528 $attempt = quiz_create_attempt($quizobj, $attemptnumber, $lastattempt, $timenow, $ispreviewuser, $userid);
2530 if (!($quizobj->get_quiz()->attemptonlast
&& $lastattempt)) {
2531 $attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow,
2532 $forcedrandomquestions, $forcedvariants);
2534 $attempt = quiz_start_attempt_built_on_last($quba, $attempt, $lastattempt);
2537 $transaction = $DB->start_delegated_transaction();
2539 // Init the timemodifiedoffline for offline attempts.
2540 if ($offlineattempt) {
2541 $attempt->timemodifiedoffline
= $attempt->timemodified
;
2543 $attempt = quiz_attempt_save_started($quizobj, $quba, $attempt);
2545 $transaction->allow_commit();
2551 * Check if the given calendar_event is either a user or group override
2554 * @param calendar_event $event The calendar event to check
2557 function quiz_is_overriden_calendar_event(\calendar_event
$event) {
2560 if (!isset($event->modulename
)) {
2564 if ($event->modulename
!= 'quiz') {
2568 if (!isset($event->instance
)) {
2572 if (!isset($event->userid
) && !isset($event->groupid
)) {
2577 'quiz' => $event->instance
2580 if (isset($event->groupid
)) {
2581 $overrideparams['groupid'] = $event->groupid
;
2582 } else if (isset($event->userid
)) {
2583 $overrideparams['userid'] = $event->userid
;
2586 return $DB->record_exists('quiz_overrides', $overrideparams);
2590 * Retrieves tag information for the given list of quiz slot ids.
2591 * Currently the only slots that have tags are random question slots.
2594 * If we have 3 slots with id 1, 2, and 3. The first slot has two tags, the second
2595 * has one tag, and the third has zero tags. The return structure will look like:
2598 * quiz_slot_tags.id => { ...tag data... },
2599 * quiz_slot_tags.id => { ...tag data... },
2602 * quiz_slot_tags.id => { ...tag data... },
2607 * @param int[] $slotids The list of id for the quiz slots.
2608 * @return array[] List of quiz_slot_tags records indexed by slot id.
2610 function quiz_retrieve_tags_for_slot_ids($slotids) {
2613 if (empty($slotids)) {
2617 $slottags = $DB->get_records_list('quiz_slot_tags', 'slotid', $slotids);
2618 $tagsbyid = core_tag_tag
::get_bulk(array_filter(array_column($slottags, 'tagid')), 'id, name');
2619 $tagsbyname = false; // It will be loaded later if required.
2620 $emptytagids = array_reduce($slotids, function($carry, $slotid) {
2621 $carry[$slotid] = [];
2625 return array_reduce(
2627 function($carry, $slottag) use ($slottags, $tagsbyid, $tagsbyname) {
2628 if (isset($tagsbyid[$slottag->tagid
])) {
2629 // Make sure that we're returning the most updated tag name.
2630 $slottag->tagname
= $tagsbyid[$slottag->tagid
]->name
;
2632 if ($tagsbyname === false) {
2633 // We were hoping that this query could be avoided, but life
2634 // showed its other side to us!
2635 $tagcollid = core_tag_area
::get_collection('core', 'question');
2636 $tagsbyname = core_tag_tag
::get_by_name_bulk(
2638 array_column($slottags, 'tagname'),
2642 if (isset($tagsbyname[$slottag->tagname
])) {
2643 // Make sure that we're returning the current tag id that matches
2644 // the given tag name.
2645 $slottag->tagid
= $tagsbyname[$slottag->tagname
]->id
;
2647 // The tag does not exist anymore (neither the tag id nor the tag name
2648 // matches an existing tag).
2649 // We still need to include this row in the result as some callers might
2650 // be interested in these rows. An example is the editing forms that still
2651 // need to display tag names even if they don't exist anymore.
2652 $slottag->tagid
= null;
2656 $carry[$slottag->slotid
][$slottag->id
] = $slottag;
2664 * Retrieves tag information for the given quiz slot.
2665 * A quiz slot have some tags if and only if it is representing a random question by tags.
2667 * @param int $slotid The id of the quiz slot.
2668 * @return stdClass[] List of quiz_slot_tags records.
2670 function quiz_retrieve_slot_tags($slotid) {
2671 $slottags = quiz_retrieve_tags_for_slot_ids([$slotid]);
2672 return $slottags[$slotid];
2676 * Retrieves tag ids for the given quiz slot.
2677 * A quiz slot have some tags if and only if it is representing a random question by tags.
2679 * @param int $slotid The id of the quiz slot.
2682 function quiz_retrieve_slot_tag_ids($slotid) {
2683 $tags = quiz_retrieve_slot_tags($slotid);
2685 // Only work with tags that exist.
2686 return array_filter(array_column($tags, 'tagid'));
2690 * Get quiz attempt and handling error.
2692 * @param int $attemptid the id of the current attempt.
2693 * @param int|null $cmid the course_module id for this quiz.
2694 * @return quiz_attempt $attemptobj all the data about the quiz attempt.
2695 * @throws moodle_exception
2697 function quiz_create_attempt_handling_errors($attemptid, $cmid = null) {
2699 $attempobj = quiz_attempt
::create($attemptid);
2700 } catch (moodle_exception
$e) {
2701 if (!empty($cmid)) {
2702 list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'quiz');
2703 $continuelink = new moodle_url('/mod/quiz/view.php', array('id' => $cmid));
2704 $context = context_module
::instance($cm->id
);
2705 if (has_capability('mod/quiz:preview', $context)) {
2706 throw new moodle_exception('attempterrorcontentchange', 'quiz', $continuelink);
2708 throw new moodle_exception('attempterrorcontentchangeforuser', 'quiz', $continuelink);
2711 throw new moodle_exception('attempterrorinvalid', 'quiz');
2714 if (!empty($cmid) && $attempobj->get_cmid() != $cmid) {
2715 throw new moodle_exception('invalidcoursemodule');