3 * Library of functions for the quiz module.
5 * This contains functions that are called also from outside the quiz module
6 * Functions that are only called by the quiz module itself are in {@link locallib.php}
8 * @author Martin Dougiamas and many others.
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
13 require_once($CFG->libdir
.'/pagelib.php');
14 require_once($CFG->libdir
.'/questionlib.php');
16 /// CONSTANTS ///////////////////////////////////////////////////////////////////
19 * The different review options are stored in the bits of $quiz->review
20 * These constants help to extract the options
22 * This is more of a mess than you might think necessary, because originally
23 * it was though that 3x6 bits were enough, but then they ran out. PHP integers
24 * are only reliably 32 bits signed, so the simplest solution was then to
28 * The first 6 + 4 bits refer to the time immediately after the attempt
30 define('QUIZ_REVIEW_IMMEDIATELY', 0x3c003f);
32 * the next 6 + 4 bits refer to the time after the attempt but while the quiz is open
34 define('QUIZ_REVIEW_OPEN', 0x3c00fc0);
36 * the final 6 + 4 bits refer to the time after the quiz closes
38 define('QUIZ_REVIEW_CLOSED', 0x3c03f000);
40 // within each group of 6 bits we determine what should be shown
41 define('QUIZ_REVIEW_RESPONSES', 1*0x1041); // Show responses
42 define('QUIZ_REVIEW_SCORES', 2*0x1041); // Show scores
43 define('QUIZ_REVIEW_FEEDBACK', 4*0x1041); // Show question feedback
44 define('QUIZ_REVIEW_ANSWERS', 8*0x1041); // Show correct answers
45 // Some handling of worked solutions is already in the code but not yet fully supported
46 // and not switched on in the user interface.
47 define('QUIZ_REVIEW_SOLUTIONS', 16*0x1041); // Show solutions
48 define('QUIZ_REVIEW_GENERALFEEDBACK',32*0x1041); // Show question general feedback
49 define('QUIZ_REVIEW_OVERALLFEEDBACK', 1*0x4440000); // Show quiz overall feedback
50 // Multipliers 2*0x4440000, 4*0x4440000 and 8*0x4440000 are still available
54 * If start and end date for the quiz are more than this many seconds apart
55 * they will be represented by two separate events in the calendar
57 define("QUIZ_MAX_EVENT_LENGTH", 5*24*60*60); // 5 days maximum
59 /// FUNCTIONS ///////////////////////////////////////////////////////////////////
62 * Given an object containing all the necessary data,
63 * (defined by the form in mod.html) this function
64 * will create a new instance and return the id number
65 * of the new instance.
67 * @param object $quiz the data that came from the form.
68 * @return mixed the id of the new instance on success,
69 * false or a string error message on failure.
71 function quiz_add_instance($quiz) {
73 // Process the options from the form.
74 $quiz->created
= time();
75 $quiz->questions
= '';
76 $result = quiz_process_options($quiz);
77 if ($result && is_string($result)) {
81 // Try to store it in the database.
82 if (!$quiz->id
= insert_record("quiz", $quiz)) {
86 // Do the processing required after an add or an update.
87 quiz_after_add_or_update($quiz);
93 * Given an object containing all the necessary data,
94 * (defined by the form in mod.html) this function
95 * will update an existing instance with new data.
97 * @param object $quiz the data that came from the form.
98 * @return mixed true on success, false or a string error message on failure.
100 function quiz_update_instance($quiz) {
102 // Process the options from the form.
103 $result = quiz_process_options($quiz);
104 if ($result && is_string($result)) {
108 // Update the database.
109 $quiz->id
= $quiz->instance
;
110 if (!update_record("quiz", $quiz)) {
111 return false; // some error occurred
114 // Do the processing required after an add or an update.
115 quiz_after_add_or_update($quiz);
117 // Delete any previous preview attempts
118 delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id
);
124 function quiz_delete_instance($id) {
125 /// Given an ID of an instance of this module,
126 /// this function will permanently delete the instance
127 /// and any data that depends on it.
129 if (! $quiz = get_record("quiz", "id", "$id")) {
135 if ($attempts = get_records("quiz_attempts", "quiz", "$quiz->id")) {
136 foreach ($attempts as $attempt) {
137 // TODO: this should use the delete_attempt($attempt->uniqueid) function in questionlib.php
138 if (! delete_records("question_states", "attempt", "$attempt->uniqueid")) {
141 if (! delete_records("question_sessions", "attemptid", "$attempt->uniqueid")) {
147 $tables_to_purge = array(
148 'quiz_attempts' => 'quiz',
149 'quiz_grades' => 'quiz',
150 'quiz_question_instances' => 'quiz',
151 'quiz_grades' => 'quiz',
152 'quiz_feedback' => 'quizid',
155 foreach ($tables_to_purge as $table => $keyfield) {
156 if (!delete_records($table, $keyfield, $quiz->id
)) {
161 $pagetypes = page_import_types('mod/quiz/');
162 foreach($pagetypes as $pagetype) {
163 if(!delete_records('block_instance', 'pageid', $quiz->id
, 'pagetype', $pagetype)) {
168 if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
169 foreach($events as $event) {
170 delete_event($event->id
);
174 quiz_grade_item_delete($quiz);
180 function quiz_user_outline($course, $user, $mod, $quiz) {
181 /// Return a small object with summary information about what a
182 /// user has done with a given particular instance of this module
183 /// Used for user activity reports.
184 /// $return->time = the time they did it
185 /// $return->info = a short text description
186 if ($grade = get_record('quiz_grades', 'userid', $user->id
, 'quiz', $quiz->id
)) {
188 $result = new stdClass
;
189 if ((float)$grade->grade
) {
190 $result->info
= get_string('grade').': '.round($grade->grade
, $quiz->decimalpoints
);
192 $result->time
= $grade->timemodified
;
200 function quiz_user_complete($course, $user, $mod, $quiz) {
201 /// Print a detailed representation of what a user has done with
202 /// a given particular instance of this module, for user activity reports.
204 if ($attempts = get_records_select('quiz_attempts', "userid='$user->id' AND quiz='$quiz->id'", 'attempt ASC')) {
205 if ($quiz->grade
and $quiz->sumgrades
&& $grade = get_record('quiz_grades', 'userid', $user->id
, 'quiz', $quiz->id
)) {
206 echo get_string('grade').': '.round($grade->grade
, $quiz->decimalpoints
).'/'.$quiz->grade
.'<br />';
208 foreach ($attempts as $attempt) {
209 echo get_string('attempt', 'quiz').' '.$attempt->attempt
.': ';
210 if ($attempt->timefinish
== 0) {
211 print_string('unfinished');
213 echo round($attempt->sumgrades
, $quiz->decimalpoints
).'/'.$quiz->sumgrades
;
215 echo ' - '.userdate($attempt->timemodified
).'<br />';
218 print_string('noattempts', 'quiz');
225 function quiz_cron () {
226 /// Function to be run periodically according to the moodle cron
227 /// This function searches for things that need to be done, such
228 /// as sending out mail, toggling flags etc ...
237 * Return grade for given user or all users.
239 * @param int $quizid id of quiz
240 * @param int $userid optional user id, 0 means all users
241 * @return array array of grades, false if none
243 function quiz_get_user_grades($quiz, $userid=0) {
246 $user = $userid ?
"AND u.id = $userid" : "";
248 $sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade
249 FROM {$CFG->prefix}user u, {$CFG->prefix}quiz_grades g
250 WHERE u.id = g.userid AND g.quiz = $quiz->id
253 return get_records_sql($sql);
257 * Update grades in central gradebook
259 * @param object $quiz null means all quizs
260 * @param int $userid specific user only, 0 mean all
262 function quiz_update_grades($quiz=null, $userid=0, $nullifnone=true) {
264 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
265 require_once($CFG->libdir
.'/gradelib.php');
269 if ($grades = quiz_get_user_grades($quiz, $userid)) {
270 grade_update('mod/quiz', $quiz->course
, 'mod', 'quiz', $quiz->id
, 0, $grades);
272 } else if ($userid and $nullifnone) {
273 $grade = new object();
274 $grade->userid
= $userid;
275 $grade->rawgrade
= NULL;
276 grade_update('mod/quiz', $quiz->course
, 'mod', 'quiz', $quiz->id
, 0, $grade);
280 $sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
281 FROM {$CFG->prefix}quiz a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
282 WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=a.id";
283 if ($rs = get_recordset_sql($sql)) {
284 if ($rs->RecordCount() > 0) {
285 while ($quiz = rs_fetch_next_record($rs)) {
286 quiz_grade_item_update($quiz);
287 if ($quiz->grade
!= 0) {
288 quiz_update_grades($quiz, 0, false);
298 * Create grade item for given quiz
300 * @param object $quiz object with extra cmidnumber
301 * @return int 0 if ok, error code otherwise
303 function quiz_grade_item_update($quiz) {
305 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
306 require_once($CFG->libdir
.'/gradelib.php');
309 if (array_key_exists('cmidnumber', $quiz)) { //it may not be always present
310 $params = array('itemname'=>$quiz->name
, 'idnumber'=>$quiz->cmidnumber
);
312 $params = array('itemname'=>$quiz->name
);
315 if ($quiz->grade
> 0) {
316 $params['gradetype'] = GRADE_TYPE_VALUE
;
317 $params['grademax'] = $quiz->grade
;
318 $params['grademin'] = 0;
321 $params['gradetype'] = GRADE_TYPE_NONE
;
324 return grade_update('mod/quiz', $quiz->course
, 'mod', 'quiz', $quiz->id
, 0, NULL, $params);
328 * Delete grade item for given quiz
330 * @param object $quiz object
331 * @return object quiz
333 function quiz_grade_item_delete($quiz) {
335 require_once($CFG->libdir
.'/gradelib.php');
337 return grade_update('mod/quiz', $quiz->course
, 'mod', 'quiz', $quiz->id
, 0, NULL, array('deleted'=>1));
341 function quiz_get_participants($quizid) {
342 /// Returns an array of users who have data in a given quiz
343 /// (users with records in quiz_attempts and quiz_question_versions)
347 //Get users from attempts
348 $us_attempts = get_records_sql("SELECT DISTINCT u.id, u.id
349 FROM {$CFG->prefix}user u,
350 {$CFG->prefix}quiz_attempts a
351 WHERE a.quiz = '$quizid' and
354 //Get users from question_versions
355 $us_versions = get_records_sql("SELECT DISTINCT u.id, u.id
356 FROM {$CFG->prefix}user u,
357 {$CFG->prefix}quiz_question_versions v
358 WHERE v.quiz = '$quizid' and
361 //Add us_versions to us_attempts
363 foreach ($us_versions as $us_version) {
364 $us_attempts[$us_version->id
] = $us_version;
367 //Return us_attempts array (it contains an array of unique users)
368 return ($us_attempts);
372 function quiz_refresh_events($courseid = 0) {
373 // This horrible function only seems to be called from mod/quiz/db/[dbtype].php.
375 // This standard function will check all instances of this module
376 // and make sure there are up-to-date events created for each of them.
377 // If courseid = 0, then every quiz event in the site is checked, else
378 // only quiz events belonging to the course specified are checked.
379 // This function is used, in its new format, by restore_refresh_events()
381 if ($courseid == 0) {
382 if (! $quizzes = get_records("quiz")) {
386 if (! $quizzes = get_records("quiz", "course", $courseid)) {
390 $moduleid = get_field('modules', 'id', 'name', 'quiz');
392 foreach ($quizzes as $quiz) {
397 if ($events = get_records_select('event', "modulename = 'quiz' AND instance = '$quiz->id' ORDER BY timestart")) {
398 $event = array_shift($events);
399 if (!empty($events)) {
400 $event2old = array_shift($events);
401 if (!empty($events)) {
402 foreach ($events as $badevent) {
403 delete_records('event', 'id', $badevent->id
);
409 $event->name
= addslashes($quiz->name
);
410 $event->description
= addslashes($quiz->intro
);
411 $event->courseid
= $quiz->course
;
414 $event->modulename
= 'quiz';
415 $event->instance
= $quiz->id
;
416 $event->visible
= instance_is_visible('quiz', $quiz);
417 $event->timestart
= $quiz->timeopen
;
418 $event->eventtype
= 'open';
419 $event->timeduration
= ($quiz->timeclose
- $quiz->timeopen
);
421 if ($event->timeduration
> QUIZ_MAX_EVENT_LENGTH
) { /// Set up two events
425 $event->name
= addslashes($quiz->name
).' ('.get_string('quizopens', 'quiz').')';
426 $event->timeduration
= 0;
428 $event2->name
= addslashes($quiz->name
).' ('.get_string('quizcloses', 'quiz').')';
429 $event2->timestart
= $quiz->timeclose
;
430 $event2->eventtype
= 'close';
431 $event2->timeduration
= 0;
433 if (empty($event2old->id
)) {
437 $event2->id
= $event2old->id
;
438 update_event($event2);
440 } else if (!empty($event2old->id
)) {
441 delete_event($event2old->id
);
444 if (empty($event->id
)) {
445 if (!empty($event->timestart
)) {
449 update_event($event);
457 function quiz_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $quiz="0", $user="", $groupid="") {
458 // Returns all quizzes since a given time. If quiz is specified then
459 // this restricts the results
464 $quizselect = " AND cm.id = '$quiz'";
469 $userselect = " AND u.id = '$user'";
474 $quizzes = get_records_sql("SELECT qa.*, q.name, u.firstname, u.lastname, u.picture,
475 q.course, q.sumgrades as maxgrade, cm.instance, cm.section
476 FROM {$CFG->prefix}quiz_attempts qa,
477 {$CFG->prefix}quiz q,
478 {$CFG->prefix}user u,
479 {$CFG->prefix}course_modules cm
480 WHERE qa.timefinish > '$sincetime'
481 AND qa.userid = u.id $userselect
482 AND qa.quiz = q.id $quizselect
483 AND cm.instance = q.id
484 AND cm.course = '$courseid'
485 AND q.course = cm.course
486 ORDER BY qa.timefinish ASC");
491 foreach ($quizzes as $quiz) {
492 if (empty($groupid) ||
groups_is_member($groupid, $quiz->userid
)) {
494 $tmpactivity = new Object;
496 $tmpactivity->type
= "quiz";
497 $tmpactivity->defaultindex
= $index;
498 $tmpactivity->instance
= $quiz->quiz
;
500 $tmpactivity->name
= $quiz->name
;
501 $tmpactivity->section
= $quiz->section
;
503 $tmpactivity->content
->attemptid
= $quiz->id
;
504 $tmpactivity->content
->sumgrades
= $quiz->sumgrades
;
505 $tmpactivity->content
->maxgrade
= $quiz->maxgrade
;
506 $tmpactivity->content
->attempt
= $quiz->attempt
;
508 $tmpactivity->user
->userid
= $quiz->userid
;
509 $tmpactivity->user
->fullname
= fullname($quiz);
510 $tmpactivity->user
->picture
= $quiz->picture
;
512 $tmpactivity->timestamp
= $quiz->timefinish
;
514 $activities[] = $tmpactivity;
524 function quiz_print_recent_mod_activity($activity, $course, $detail=false) {
527 echo '<table border="0" cellpadding="3" cellspacing="0">';
529 echo "<tr><td class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
530 print_user_picture($activity->user
->userid
, $course, $activity->user
->picture
);
531 echo "</td><td style=\"width:100%;\"><font size=\"2\">";
534 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
535 "class=\"icon\" alt=\"$activity->type\" /> ";
536 echo "<a href=\"$CFG->wwwroot/mod/quiz/view.php?id=" . $activity->instance
. "\">"
537 . format_string($activity->name
,true) . "</a> - ";
541 if (has_capability('mod/quiz:grade', get_context_instance(CONTEXT_MODULE
, $activity->instance
))) {
542 $grades = "(" . $activity->content
->sumgrades
. " / " . $activity->content
->maxgrade
. ") ";
543 echo "<a href=\"$CFG->wwwroot/mod/quiz/review.php?q="
544 . $activity->instance
. "&attempt="
545 . $activity->content
->attemptid
. "\">" . $grades . "</a> ";
547 echo get_string("attempt", "quiz") . " - " . $activity->content
->attempt
. "<br />";
549 echo "<a href=\"$CFG->wwwroot/user/view.php?id="
550 . $activity->user
->userid
. "&course=$course\">"
551 . $activity->user
->fullname
. "</a> ";
553 echo " - " . userdate($activity->timestamp
);
555 echo "</font></td></tr>";
562 * Pre-process the quiz options form data, making any necessary adjustments.
563 * Called by add/update instance in this file, and the save code in admin/module.php.
565 * @param object $quiz The variables set on the form.
567 function quiz_process_options(&$quiz) {
568 $quiz->timemodified
= time();
571 if (empty($quiz->timeopen
)) {
572 $quiz->preventlate
= 0;
576 if (!empty($quiz->name
)) {
577 $quiz->name
= trim($quiz->name
);
580 // Time limit. (Get rid of it if the checkbox was not ticked.)
581 if (empty($quiz->timelimitenable
)) {
582 $quiz->timelimit
= 0;
584 $quiz->timelimit
= round($quiz->timelimit
);
586 // Password field - different in form to stop browsers that remember passwords
588 $quiz->password
= $quiz->quizpassword
;
589 unset($quiz->quizpassword
);
592 if (isset($quiz->feedbacktext
)) {
593 // Clean up the boundary text.
594 for ($i = 0; $i < count($quiz->feedbacktext
); $i +
= 1) {
595 if (empty($quiz->feedbacktext
[$i])) {
596 $quiz->feedbacktext
[$i] = '';
598 $quiz->feedbacktext
[$i] = trim($quiz->feedbacktext
[$i]);
602 // Check the boundary value is a number or a percentage, and in range.
604 while (!empty($quiz->feedbackboundaries
[$i])) {
605 $boundary = trim($quiz->feedbackboundaries
[$i]);
606 if (!is_numeric($boundary)) {
607 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
608 $boundary = trim(substr($boundary, 0, -1));
609 if (is_numeric($boundary)) {
610 $boundary = $boundary * $quiz->grade
/ 100.0;
612 return get_string('feedbackerrorboundaryformat', 'quiz', $i +
1);
616 if ($boundary <= 0 ||
$boundary >= $quiz->grade
) {
617 return get_string('feedbackerrorboundaryoutofrange', 'quiz', $i +
1);
619 if ($i > 0 && $boundary >= $quiz->feedbackboundaries
[$i - 1]) {
620 return get_string('feedbackerrororder', 'quiz', $i +
1);
622 $quiz->feedbackboundaries
[$i] = $boundary;
627 // Check there is nothing in the remaining unused fields.
628 for ($i = $numboundaries; $i < count($quiz->feedbackboundaries
); $i +
= 1) {
629 if (!empty($quiz->feedbackboundaries
[$i]) && trim($quiz->feedbackboundaries
[$i]) != '') {
630 return get_string('feedbackerrorjunkinboundary', 'quiz', $i +
1);
633 for ($i = $numboundaries +
1; $i < count($quiz->feedbacktext
); $i +
= 1) {
634 if (!empty($quiz->feedbacktext
[$i]) && trim($quiz->feedbacktext
[$i]) != '') {
635 return get_string('feedbackerrorjunkinfeedback', 'quiz', $i +
1);
638 $quiz->feedbackboundaries
[-1] = $quiz->grade +
1; // Needs to be bigger than $quiz->grade because of '<' test in quiz_feedback_for_grade().
639 $quiz->feedbackboundaries
[$numboundaries] = 0;
640 $quiz->feedbackboundarycount
= $numboundaries;
643 // Settings that get combined to go into the optionflags column.
644 $quiz->optionflags
= 0;
645 if (!empty($quiz->adaptive
)) {
646 $quiz->optionflags |
= QUESTION_ADAPTIVE
;
649 // Settings that get combined to go into the review column.
651 if (isset($quiz->responsesimmediately
)) {
652 $review +
= (QUIZ_REVIEW_RESPONSES
& QUIZ_REVIEW_IMMEDIATELY
);
653 unset($quiz->responsesimmediately
);
655 if (isset($quiz->responsesopen
)) {
656 $review +
= (QUIZ_REVIEW_RESPONSES
& QUIZ_REVIEW_OPEN
);
657 unset($quiz->responsesopen
);
659 if (isset($quiz->responsesclosed
)) {
660 $review +
= (QUIZ_REVIEW_RESPONSES
& QUIZ_REVIEW_CLOSED
);
661 unset($quiz->responsesclosed
);
664 if (isset($quiz->scoreimmediately
)) {
665 $review +
= (QUIZ_REVIEW_SCORES
& QUIZ_REVIEW_IMMEDIATELY
);
666 unset($quiz->scoreimmediately
);
668 if (isset($quiz->scoreopen
)) {
669 $review +
= (QUIZ_REVIEW_SCORES
& QUIZ_REVIEW_OPEN
);
670 unset($quiz->scoreopen
);
672 if (isset($quiz->scoreclosed
)) {
673 $review +
= (QUIZ_REVIEW_SCORES
& QUIZ_REVIEW_CLOSED
);
674 unset($quiz->scoreclosed
);
677 if (isset($quiz->feedbackimmediately
)) {
678 $review +
= (QUIZ_REVIEW_FEEDBACK
& QUIZ_REVIEW_IMMEDIATELY
);
679 unset($quiz->feedbackimmediately
);
681 if (isset($quiz->feedbackopen
)) {
682 $review +
= (QUIZ_REVIEW_FEEDBACK
& QUIZ_REVIEW_OPEN
);
683 unset($quiz->feedbackopen
);
685 if (isset($quiz->feedbackclosed
)) {
686 $review +
= (QUIZ_REVIEW_FEEDBACK
& QUIZ_REVIEW_CLOSED
);
687 unset($quiz->feedbackclosed
);
690 if (isset($quiz->answersimmediately
)) {
691 $review +
= (QUIZ_REVIEW_ANSWERS
& QUIZ_REVIEW_IMMEDIATELY
);
692 unset($quiz->answersimmediately
);
694 if (isset($quiz->answersopen
)) {
695 $review +
= (QUIZ_REVIEW_ANSWERS
& QUIZ_REVIEW_OPEN
);
696 unset($quiz->answersopen
);
698 if (isset($quiz->answersclosed
)) {
699 $review +
= (QUIZ_REVIEW_ANSWERS
& QUIZ_REVIEW_CLOSED
);
700 unset($quiz->answersclosed
);
703 if (isset($quiz->solutionsimmediately
)) {
704 $review +
= (QUIZ_REVIEW_SOLUTIONS
& QUIZ_REVIEW_IMMEDIATELY
);
705 unset($quiz->solutionsimmediately
);
707 if (isset($quiz->solutionsopen
)) {
708 $review +
= (QUIZ_REVIEW_SOLUTIONS
& QUIZ_REVIEW_OPEN
);
709 unset($quiz->solutionsopen
);
711 if (isset($quiz->solutionsclosed
)) {
712 $review +
= (QUIZ_REVIEW_SOLUTIONS
& QUIZ_REVIEW_CLOSED
);
713 unset($quiz->solutionsclosed
);
716 if (isset($quiz->generalfeedbackimmediately
)) {
717 $review +
= (QUIZ_REVIEW_GENERALFEEDBACK
& QUIZ_REVIEW_IMMEDIATELY
);
718 unset($quiz->generalfeedbackimmediately
);
720 if (isset($quiz->generalfeedbackopen
)) {
721 $review +
= (QUIZ_REVIEW_GENERALFEEDBACK
& QUIZ_REVIEW_OPEN
);
722 unset($quiz->generalfeedbackopen
);
724 if (isset($quiz->generalfeedbackclosed
)) {
725 $review +
= (QUIZ_REVIEW_GENERALFEEDBACK
& QUIZ_REVIEW_CLOSED
);
726 unset($quiz->generalfeedbackclosed
);
729 if (isset($quiz->overallfeedbackimmediately
)) {
730 $review +
= (QUIZ_REVIEW_OVERALLFEEDBACK
& QUIZ_REVIEW_IMMEDIATELY
);
731 unset($quiz->overallfeedbackimmediately
);
733 if (isset($quiz->overallfeedbackopen
)) {
734 $review +
= (QUIZ_REVIEW_OVERALLFEEDBACK
& QUIZ_REVIEW_OPEN
);
735 unset($quiz->overallfeedbackopen
);
737 if (isset($quiz->overallfeedbackclosed
)) {
738 $review +
= (QUIZ_REVIEW_OVERALLFEEDBACK
& QUIZ_REVIEW_CLOSED
);
739 unset($quiz->overallfeedbackclosed
);
742 $quiz->review
= $review;
746 * This function is called at the end of quiz_add_instance
747 * and quiz_update_instance, to do the common processing.
749 * @param object $quiz the quiz object.
751 function quiz_after_add_or_update($quiz) {
754 delete_records('quiz_feedback', 'quizid', $quiz->id
);
756 for ($i = 0; $i <= $quiz->feedbackboundarycount
; $i +
= 1) {
757 $feedback = new stdClass
;
758 $feedback->quizid
= $quiz->id
;
759 $feedback->feedbacktext
= $quiz->feedbacktext
[$i];
760 $feedback->mingrade
= $quiz->feedbackboundaries
[$i];
761 $feedback->maxgrade
= $quiz->feedbackboundaries
[$i - 1];
762 if (!insert_record('quiz_feedback', $feedback, false)) {
763 return "Could not save quiz feedback.";
768 // Update the events relating to this quiz.
769 // This is slightly inefficient, deleting the old events and creating new ones. However,
770 // there are at most two events, and this keeps the code simpler.
771 if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
772 foreach($events as $event) {
773 delete_event($event->id
);
777 $event = new stdClass
;
778 $event->description
= $quiz->intro
;
779 $event->courseid
= $quiz->course
;
782 $event->modulename
= 'quiz';
783 $event->instance
= $quiz->id
;
784 $event->timestart
= $quiz->timeopen
;
785 $event->timeduration
= $quiz->timeclose
- $quiz->timeopen
;
786 $event->visible
= instance_is_visible('quiz', $quiz);
787 $event->eventtype
= 'open';
789 if ($quiz->timeclose
and $quiz->timeopen
and $event->timeduration
<= QUIZ_MAX_EVENT_LENGTH
) {
790 // Single event for the whole quiz.
791 $event->name
= $quiz->name
;
794 // Separate start and end events.
795 $event->timeduration
= 0;
796 if ($quiz->timeopen
) {
797 $event->name
= $quiz->name
.' ('.get_string('quizopens', 'quiz').')';
799 unset($event->id
); // So we can use the same object for the close event.
801 if ($quiz->timeclose
) {
802 $event->name
= $quiz->name
.' ('.get_string('quizcloses', 'quiz').')';
803 $event->timestart
= $quiz->timeclose
;
804 $event->eventtype
= 'close';
809 //update related grade item
810 quiz_grade_item_update(stripslashes_recursive($quiz));
813 function quiz_get_view_actions() {
814 return array('view','view all','report');
817 function quiz_get_post_actions() {
818 return array('attempt','editquestions','review','submit');
822 * Returns an array of names of quizzes that use this question
825 * @param object $questionid
826 * @return array of strings
828 function quiz_question_list_instances($questionid) {
833 * Implementation of the function for printing the form elements that control
834 * whether the course reset functionality affects the quiz.
835 * @param $course The course id of the course the user is thinking of resetting.
837 function quiz_reset_course_form($course) {
839 print_checkbox('reset_quiz_attempts', 1, true, get_string('removeallquizattempts','quiz'));
844 * Actual implementation of the rest coures functionality, delete all the
845 * quiz attempts for course $data->courseid, if $data->reset_quiz_attempts is
847 * @param $data the data submitted from the reset course forum.
848 * @param $showfeedback whether to output progress information as the reset
851 function quiz_delete_userdata($data, $showfeedback=true) {
854 if (empty($data->reset_quiz_attempts
)) {
858 $conditiononquizids = 'quiz IN (SELECT id FROM ' .
859 $CFG->prefix
. 'quiz q WHERE q.course = ' . $data->courseid
. ')';
861 $attemptids = get_records_select('quiz_attempts', $conditiononquizids, '', 'id, uniqueid');
864 echo '<div class="notifysuccess">', get_string('deletingquestionattempts', 'quiz');
867 foreach ($attemptids as $attemptid) {
868 delete_attempt($attemptid->uniqueid
);
870 echo $divider, $attemptid->uniqueid
;
875 echo "</div><br />\n";
878 if (delete_records_select('quiz_grades', $conditiononquizids) && $showfeedback) {
879 notify(get_string('gradesdeleted','quiz'), 'notifysuccess');
881 if (delete_records_select('quiz_attempts', $conditiononquizids) && $showfeedback) {
882 notify(get_string('attemptsdeleted','quiz'), 'notifysuccess');
887 * Checks whether the current user is allowed to view a file uploaded in a quiz.
888 * Teachers can view any from their courses, students can only view their own.
890 * @param int $attemptid int attempt id
891 * @param int $questionid int question id
892 * @return boolean to indicate access granted or denied
894 function quiz_check_file_access($attemptid, $questionid) {
897 $attempt = get_record("quiz_attempts", 'id', $attemptid);
898 $quiz = get_record("quiz", 'id', $attempt->quiz
);
899 $context = get_context_instance(CONTEXT_COURSE
, $quiz->course
);
901 // access granted if the current user submitted this file
902 if ($attempt->userid
== $USER->id
) {
904 // access granted if the current user has permission to grade quizzes in this course
905 } else if (has_capability('mod/quiz:viewreports', $context) ||
has_capability('mod/quiz:grade', $context)) {
909 // otherwise, this user does not have permission