MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / quiz / lib.php
blob1dd6175b2542802ac67fe353d4773b9b04f658b0
1 <?php // $Id$
2 /**
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}
7 * @author Martin Dougiamas and many others.
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package quiz
12 require_once($CFG->libdir.'/pagelib.php');
13 require_once($CFG->libdir.'/questionlib.php');
15 /// CONSTANTS ///////////////////////////////////////////////////////////////////
17 /**#@+
18 * The different review options are stored in the bits of $quiz->review
19 * These constants help to extract the options
21 * This is more of a mess than you might think necessary, because originally
22 * it was though that 3x6 bits were enough, but then they ran out. PHP integers
23 * are only reliably 32 bits signed, so the simplest solution was then to
24 * add 4x3 more bits.
26 /**
27 * The first 6 + 4 bits refer to the time immediately after the attempt
29 define('QUIZ_REVIEW_IMMEDIATELY', 0x3c003f);
30 /**
31 * the next 6 + 4 bits refer to the time after the attempt but while the quiz is open
33 define('QUIZ_REVIEW_OPEN', 0x3c00fc0);
34 /**
35 * the final 6 + 4 bits refer to the time after the quiz closes
37 define('QUIZ_REVIEW_CLOSED', 0x3c03f000);
39 // within each group of 6 bits we determine what should be shown
40 define('QUIZ_REVIEW_RESPONSES', 1*0x1041); // Show responses
41 define('QUIZ_REVIEW_SCORES', 2*0x1041); // Show scores
42 define('QUIZ_REVIEW_FEEDBACK', 4*0x1041); // Show question feedback
43 define('QUIZ_REVIEW_ANSWERS', 8*0x1041); // Show correct answers
44 // Some handling of worked solutions is already in the code but not yet fully supported
45 // and not switched on in the user interface.
46 define('QUIZ_REVIEW_SOLUTIONS', 16*0x1041); // Show solutions
47 define('QUIZ_REVIEW_GENERALFEEDBACK',32*0x1041); // Show question general feedback
48 define('QUIZ_REVIEW_OVERALLFEEDBACK', 1*0x4440000); // Show quiz overall feedback
49 // Multipliers 2*0x4440000, 4*0x4440000 and 8*0x4440000 are still available
50 /**#@-*/
52 /**
53 * If start and end date for the quiz are more than this many seconds apart
54 * they will be represented by two separate events in the calendar
56 define("QUIZ_MAX_EVENT_LENGTH", 5*24*60*60); // 5 days maximum
58 /// FUNCTIONS ///////////////////////////////////////////////////////////////////
60 /**
61 * Given an object containing all the necessary data,
62 * (defined by the form in mod.html) this function
63 * will create a new instance and return the id number
64 * of the new instance.
66 * @param object $quiz the data that came from the form.
67 * @return mixed the id of the new instance on success,
68 * false or a string error message on failure.
70 function quiz_add_instance($quiz) {
72 // Process the options from the form.
73 $quiz->created = time();
74 $quiz->questions = '';
75 $result = quiz_process_options($quiz);
76 if ($result && is_string($result)) {
77 return $result;
80 // Try to store it in the database.
81 if (!$quiz->id = insert_record("quiz", $quiz)) {
82 return false;
85 // Do the processing required after an add or an update.
86 quiz_after_add_or_update($quiz);
88 return $quiz->id;
91 /**
92 * Given an object containing all the necessary data,
93 * (defined by the form in mod.html) this function
94 * will update an existing instance with new data.
96 * @param object $quiz the data that came from the form.
97 * @return mixed true on success, false or a string error message on failure.
99 function quiz_update_instance($quiz) {
101 // Process the options from the form.
102 $result = quiz_process_options($quiz);
103 if ($result && is_string($result)) {
104 return $result;
107 // Update the database.
108 $quiz->id = $quiz->instance;
109 if (!update_record("quiz", $quiz)) {
110 return false; // some error occurred
113 // Do the processing required after an add or an update.
114 quiz_after_add_or_update($quiz);
116 // Delete any previous preview attempts
117 delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id);
119 return true;
123 function quiz_delete_instance($id) {
124 /// Given an ID of an instance of this module,
125 /// this function will permanently delete the instance
126 /// and any data that depends on it.
128 if (! $quiz = get_record("quiz", "id", "$id")) {
129 return false;
132 $result = true;
134 if ($attempts = get_records("quiz_attempts", "quiz", "$quiz->id")) {
135 foreach ($attempts as $attempt) {
136 // TODO: this should use the delete_attempt($attempt->uniqueid) function in questionlib.php
137 if (! delete_records("question_states", "attempt", "$attempt->uniqueid")) {
138 $result = false;
140 if (! delete_records("question_sessions", "attemptid", "$attempt->uniqueid")) {
141 $result = false;
146 $tables_to_purge = array(
147 'quiz_attempts' => 'quiz',
148 'quiz_grades' => 'quiz',
149 'quiz_question_instances' => 'quiz',
150 'quiz_grades' => 'quiz',
151 'quiz_feedback' => 'quizid',
152 'quiz' => 'id'
154 foreach ($tables_to_purge as $table => $keyfield) {
155 if (!delete_records($table, $keyfield, $quiz->id)) {
156 $result = false;
160 $pagetypes = page_import_types('mod/quiz/');
161 foreach($pagetypes as $pagetype) {
162 if(!delete_records('block_instance', 'pageid', $quiz->id, 'pagetype', $pagetype)) {
163 $result = false;
167 if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
168 foreach($events as $event) {
169 delete_event($event->id);
173 quiz_grade_item_delete($quiz);
175 return $result;
179 function quiz_user_outline($course, $user, $mod, $quiz) {
180 /// Return a small object with summary information about what a
181 /// user has done with a given particular instance of this module
182 /// Used for user activity reports.
183 /// $return->time = the time they did it
184 /// $return->info = a short text description
185 if ($grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
187 $result = new stdClass;
188 if ((float)$grade->grade) {
189 $result->info = get_string('grade').':&nbsp;'.round($grade->grade, $quiz->decimalpoints);
191 $result->time = $grade->timemodified;
192 return $result;
194 return NULL;
199 function quiz_user_complete($course, $user, $mod, $quiz) {
200 /// Print a detailed representation of what a user has done with
201 /// a given particular instance of this module, for user activity reports.
203 if ($attempts = get_records_select('quiz_attempts', "userid='$user->id' AND quiz='$quiz->id'", 'attempt ASC')) {
204 if ($quiz->grade and $quiz->sumgrades && $grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
205 echo get_string('grade').': '.round($grade->grade, $quiz->decimalpoints).'/'.$quiz->grade.'<br />';
207 foreach ($attempts as $attempt) {
208 echo get_string('attempt', 'quiz').' '.$attempt->attempt.': ';
209 if ($attempt->timefinish == 0) {
210 print_string('unfinished');
211 } else {
212 echo round($attempt->sumgrades, $quiz->decimalpoints).'/'.$quiz->sumgrades;
214 echo ' - '.userdate($attempt->timemodified).'<br />';
216 } else {
217 print_string('noattempts', 'quiz');
220 return true;
224 function quiz_cron () {
225 /// Function to be run periodically according to the moodle cron
226 /// This function searches for things that need to be done, such
227 /// as sending out mail, toggling flags etc ...
229 global $CFG;
231 return true;
235 * @param integer $quizid the quiz id.
236 * @param integer $userid the userid.
237 * @param string $status 'all', 'finished' or 'unfinished' to control
238 * @return an array of all the user's attempts at this quiz. Returns an empty array if there are none.
240 function quiz_get_user_attempts($quizid, $userid, $status = 'finished', $includepreviews = false) {
241 $status_condition = array(
242 'all' => '',
243 'finished' => ' AND timefinish > 0',
244 'unfinished' => ' AND timefinish = 0'
246 $previewclause = '';
247 if (!$includepreviews) {
248 $previewclause = ' AND preview = 0';
250 if ($attempts = get_records_select('quiz_attempts',
251 "quiz = '$quizid' AND userid = '$userid'" . $previewclause . $status_condition[$status],
252 'attempt ASC')) {
253 return $attempts;
254 } else {
255 return array();
260 * Return grade for given user or all users.
262 * @param int $quizid id of quiz
263 * @param int $userid optional user id, 0 means all users
264 * @return array array of grades, false if none
266 function quiz_get_user_grades($quiz, $userid=0) {
267 global $CFG;
269 $user = $userid ? "AND u.id = $userid" : "";
271 $sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade
272 FROM {$CFG->prefix}user u, {$CFG->prefix}quiz_grades g
273 WHERE u.id = g.userid AND g.quiz = $quiz->id
274 $user";
276 return get_records_sql($sql);
280 * Update grades in central gradebook
282 * @param object $quiz null means all quizs
283 * @param int $userid specific user only, 0 mean all
285 function quiz_update_grades($quiz=null, $userid=0, $nullifnone=true) {
286 global $CFG;
287 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
288 require_once($CFG->libdir.'/gradelib.php');
291 if ($quiz != null) {
292 quiz_grade_item_update($quiz); // Recreate it if necessary
293 if ($grades = quiz_get_user_grades($quiz, $userid)) {
294 grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grades);
296 } else if ($userid and $nullifnone) {
297 $grade = new object();
298 $grade->userid = $userid;
299 $grade->rawgrade = NULL;
300 grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grade);
303 } else {
304 $sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
305 FROM {$CFG->prefix}quiz a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
306 WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=a.id";
307 if ($rs = get_recordset_sql($sql)) {
308 if ($rs->RecordCount() > 0) {
309 while ($quiz = rs_fetch_next_record($rs)) {
310 quiz_grade_item_update($quiz);
311 if ($quiz->grade != 0) {
312 quiz_update_grades($quiz, 0, false);
316 rs_close($rs);
322 * Create grade item for given quiz
324 * @param object $quiz object with extra cmidnumber
325 * @return int 0 if ok, error code otherwise
327 function quiz_grade_item_update($quiz) {
328 global $CFG;
329 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
330 require_once($CFG->libdir.'/gradelib.php');
333 if (array_key_exists('cmidnumber', $quiz)) { //it may not be always present
334 $params = array('itemname'=>$quiz->name, 'idnumber'=>$quiz->cmidnumber);
335 } else {
336 $params = array('itemname'=>$quiz->name);
339 if ($quiz->grade > 0) {
340 $params['gradetype'] = GRADE_TYPE_VALUE;
341 $params['grademax'] = $quiz->grade;
342 $params['grademin'] = 0;
344 } else {
345 $params['gradetype'] = GRADE_TYPE_NONE;
348 return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, NULL, $params);
352 * Delete grade item for given quiz
354 * @param object $quiz object
355 * @return object quiz
357 function quiz_grade_item_delete($quiz) {
358 global $CFG;
359 require_once($CFG->libdir.'/gradelib.php');
361 return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, NULL, array('deleted'=>1));
365 function quiz_get_participants($quizid) {
366 /// Returns an array of users who have data in a given quiz
367 /// (users with records in quiz_attempts and quiz_question_versions)
369 global $CFG;
371 //Get users from attempts
372 $us_attempts = get_records_sql("SELECT DISTINCT u.id, u.id
373 FROM {$CFG->prefix}user u,
374 {$CFG->prefix}quiz_attempts a
375 WHERE a.quiz = '$quizid' and
376 u.id = a.userid");
378 //Get users from question_versions
379 $us_versions = get_records_sql("SELECT DISTINCT u.id, u.id
380 FROM {$CFG->prefix}user u,
381 {$CFG->prefix}quiz_question_versions v
382 WHERE v.quiz = '$quizid' and
383 u.id = v.userid");
385 //Add us_versions to us_attempts
386 if ($us_versions) {
387 foreach ($us_versions as $us_version) {
388 $us_attempts[$us_version->id] = $us_version;
391 //Return us_attempts array (it contains an array of unique users)
392 return ($us_attempts);
396 function quiz_refresh_events($courseid = 0) {
397 // This horrible function only seems to be called from mod/quiz/db/[dbtype].php.
399 // This standard function will check all instances of this module
400 // and make sure there are up-to-date events created for each of them.
401 // If courseid = 0, then every quiz event in the site is checked, else
402 // only quiz events belonging to the course specified are checked.
403 // This function is used, in its new format, by restore_refresh_events()
405 if ($courseid == 0) {
406 if (! $quizzes = get_records("quiz")) {
407 return true;
409 } else {
410 if (! $quizzes = get_records("quiz", "course", $courseid)) {
411 return true;
414 $moduleid = get_field('modules', 'id', 'name', 'quiz');
416 foreach ($quizzes as $quiz) {
417 $event = NULL;
418 $event2 = NULL;
419 $event2old = NULL;
421 if ($events = get_records_select('event', "modulename = 'quiz' AND instance = '$quiz->id' ORDER BY timestart")) {
422 $event = array_shift($events);
423 if (!empty($events)) {
424 $event2old = array_shift($events);
425 if (!empty($events)) {
426 foreach ($events as $badevent) {
427 delete_records('event', 'id', $badevent->id);
433 $event->name = addslashes($quiz->name);
434 $event->description = addslashes($quiz->intro);
435 $event->courseid = $quiz->course;
436 $event->groupid = 0;
437 $event->userid = 0;
438 $event->modulename = 'quiz';
439 $event->instance = $quiz->id;
440 $event->visible = instance_is_visible('quiz', $quiz);
441 $event->timestart = $quiz->timeopen;
442 $event->eventtype = 'open';
443 $event->timeduration = ($quiz->timeclose - $quiz->timeopen);
445 if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Set up two events
447 $event2 = $event;
449 $event->name = addslashes($quiz->name).' ('.get_string('quizopens', 'quiz').')';
450 $event->timeduration = 0;
452 $event2->name = addslashes($quiz->name).' ('.get_string('quizcloses', 'quiz').')';
453 $event2->timestart = $quiz->timeclose;
454 $event2->eventtype = 'close';
455 $event2->timeduration = 0;
457 if (empty($event2old->id)) {
458 unset($event2->id);
459 add_event($event2);
460 } else {
461 $event2->id = $event2old->id;
462 update_event($event2);
464 } else if (!empty($event2old->id)) {
465 delete_event($event2old->id);
468 if (empty($event->id)) {
469 if (!empty($event->timestart)) {
470 add_event($event);
472 } else {
473 update_event($event);
477 return true;
481 function quiz_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $quiz="0", $user="", $groupid="") {
482 // Returns all quizzes since a given time. If quiz is specified then
483 // this restricts the results
485 global $CFG;
487 if ($quiz) {
488 $quizselect = " AND cm.id = '$quiz'";
489 } else {
490 $quizselect = "";
492 if ($user) {
493 $userselect = " AND u.id = '$user'";
494 } else {
495 $userselect = "";
498 $quizzes = get_records_sql("SELECT qa.*, q.name, u.firstname, u.lastname, u.picture,
499 q.course, q.sumgrades as maxgrade, cm.instance, cm.section
500 FROM {$CFG->prefix}quiz_attempts qa,
501 {$CFG->prefix}quiz q,
502 {$CFG->prefix}user u,
503 {$CFG->prefix}course_modules cm
504 WHERE qa.timefinish > '$sincetime'
505 AND qa.userid = u.id $userselect
506 AND qa.quiz = q.id $quizselect
507 AND cm.instance = q.id
508 AND cm.course = '$courseid'
509 AND q.course = cm.course
510 ORDER BY qa.timefinish ASC");
512 if (empty($quizzes))
513 return;
515 foreach ($quizzes as $quiz) {
516 if (empty($groupid) || groups_is_member($groupid, $quiz->userid)) {
518 $tmpactivity = new Object;
520 $tmpactivity->type = "quiz";
521 $tmpactivity->defaultindex = $index;
522 $tmpactivity->instance = $quiz->quiz;
524 $tmpactivity->name = $quiz->name;
525 $tmpactivity->section = $quiz->section;
527 $tmpactivity->content->attemptid = $quiz->id;
528 $tmpactivity->content->sumgrades = $quiz->sumgrades;
529 $tmpactivity->content->maxgrade = $quiz->maxgrade;
530 $tmpactivity->content->attempt = $quiz->attempt;
532 $tmpactivity->user->userid = $quiz->userid;
533 $tmpactivity->user->fullname = fullname($quiz);
534 $tmpactivity->user->picture = $quiz->picture;
536 $tmpactivity->timestamp = $quiz->timefinish;
538 $activities[] = $tmpactivity;
540 $index++;
544 return;
548 function quiz_print_recent_mod_activity($activity, $course, $detail=false) {
549 global $CFG;
551 echo '<table border="0" cellpadding="3" cellspacing="0">';
553 echo "<tr><td class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
554 print_user_picture($activity->user->userid, $course, $activity->user->picture);
555 echo "</td><td style=\"width:100%;\"><font size=\"2\">";
557 if ($detail) {
558 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
559 "class=\"icon\" alt=\"$activity->type\" /> ";
560 echo "<a href=\"$CFG->wwwroot/mod/quiz/view.php?id=" . $activity->instance . "\">"
561 . format_string($activity->name,true) . "</a> - ";
565 if (has_capability('mod/quiz:grade', get_context_instance(CONTEXT_MODULE, $activity->instance))) {
566 $grades = "(" . $activity->content->sumgrades . " / " . $activity->content->maxgrade . ") ";
567 echo "<a href=\"$CFG->wwwroot/mod/quiz/review.php?q="
568 . $activity->instance . "&amp;attempt="
569 . $activity->content->attemptid . "\">" . $grades . "</a> ";
571 echo get_string("attempt", "quiz") . " - " . $activity->content->attempt . "<br />";
573 echo "<a href=\"$CFG->wwwroot/user/view.php?id="
574 . $activity->user->userid . "&amp;course=$course\">"
575 . $activity->user->fullname . "</a> ";
577 echo " - " . userdate($activity->timestamp);
579 echo "</font></td></tr>";
580 echo "</table>";
582 return;
586 * Pre-process the quiz options form data, making any necessary adjustments.
587 * Called by add/update instance in this file, and the save code in admin/module.php.
589 * @param object $quiz The variables set on the form.
591 function quiz_process_options(&$quiz) {
592 $quiz->timemodified = time();
594 // Quiz open time.
595 if (empty($quiz->timeopen)) {
596 $quiz->preventlate = 0;
599 // Quiz name.
600 if (!empty($quiz->name)) {
601 $quiz->name = trim($quiz->name);
604 // Time limit. (Get rid of it if the checkbox was not ticked.)
605 if (empty($quiz->timelimitenable)) {
606 $quiz->timelimit = 0;
608 $quiz->timelimit = round($quiz->timelimit);
610 // Password field - different in form to stop browsers that remember passwords
611 // getting confused.
612 $quiz->password = $quiz->quizpassword;
613 unset($quiz->quizpassword);
615 // Quiz feedback
616 if (isset($quiz->feedbacktext)) {
617 // Clean up the boundary text.
618 for ($i = 0; $i < count($quiz->feedbacktext); $i += 1) {
619 if (empty($quiz->feedbacktext[$i])) {
620 $quiz->feedbacktext[$i] = '';
621 } else {
622 $quiz->feedbacktext[$i] = trim($quiz->feedbacktext[$i]);
626 // Check the boundary value is a number or a percentage, and in range.
627 $i = 0;
628 while (!empty($quiz->feedbackboundaries[$i])) {
629 $boundary = trim($quiz->feedbackboundaries[$i]);
630 if (!is_numeric($boundary)) {
631 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
632 $boundary = trim(substr($boundary, 0, -1));
633 if (is_numeric($boundary)) {
634 $boundary = $boundary * $quiz->grade / 100.0;
635 } else {
636 return get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
640 if ($boundary <= 0 || $boundary >= $quiz->grade) {
641 return get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
643 if ($i > 0 && $boundary >= $quiz->feedbackboundaries[$i - 1]) {
644 return get_string('feedbackerrororder', 'quiz', $i + 1);
646 $quiz->feedbackboundaries[$i] = $boundary;
647 $i += 1;
649 $numboundaries = $i;
651 // Check there is nothing in the remaining unused fields.
652 for ($i = $numboundaries; $i < count($quiz->feedbackboundaries); $i += 1) {
653 if (!empty($quiz->feedbackboundaries[$i]) && trim($quiz->feedbackboundaries[$i]) != '') {
654 return get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
657 for ($i = $numboundaries + 1; $i < count($quiz->feedbacktext); $i += 1) {
658 if (!empty($quiz->feedbacktext[$i]) && trim($quiz->feedbacktext[$i]) != '') {
659 return get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
662 $quiz->feedbackboundaries[-1] = $quiz->grade + 1; // Needs to be bigger than $quiz->grade because of '<' test in quiz_feedback_for_grade().
663 $quiz->feedbackboundaries[$numboundaries] = 0;
664 $quiz->feedbackboundarycount = $numboundaries;
667 // Settings that get combined to go into the optionflags column.
668 $quiz->optionflags = 0;
669 if (!empty($quiz->adaptive)) {
670 $quiz->optionflags |= QUESTION_ADAPTIVE;
673 // Settings that get combined to go into the review column.
674 $review = 0;
675 if (isset($quiz->responsesimmediately)) {
676 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY);
677 unset($quiz->responsesimmediately);
679 if (isset($quiz->responsesopen)) {
680 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN);
681 unset($quiz->responsesopen);
683 if (isset($quiz->responsesclosed)) {
684 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED);
685 unset($quiz->responsesclosed);
688 if (isset($quiz->scoreimmediately)) {
689 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY);
690 unset($quiz->scoreimmediately);
692 if (isset($quiz->scoreopen)) {
693 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN);
694 unset($quiz->scoreopen);
696 if (isset($quiz->scoreclosed)) {
697 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED);
698 unset($quiz->scoreclosed);
701 if (isset($quiz->feedbackimmediately)) {
702 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
703 unset($quiz->feedbackimmediately);
705 if (isset($quiz->feedbackopen)) {
706 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN);
707 unset($quiz->feedbackopen);
709 if (isset($quiz->feedbackclosed)) {
710 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED);
711 unset($quiz->feedbackclosed);
714 if (isset($quiz->answersimmediately)) {
715 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY);
716 unset($quiz->answersimmediately);
718 if (isset($quiz->answersopen)) {
719 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN);
720 unset($quiz->answersopen);
722 if (isset($quiz->answersclosed)) {
723 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED);
724 unset($quiz->answersclosed);
727 if (isset($quiz->solutionsimmediately)) {
728 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_IMMEDIATELY);
729 unset($quiz->solutionsimmediately);
731 if (isset($quiz->solutionsopen)) {
732 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_OPEN);
733 unset($quiz->solutionsopen);
735 if (isset($quiz->solutionsclosed)) {
736 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_CLOSED);
737 unset($quiz->solutionsclosed);
740 if (isset($quiz->generalfeedbackimmediately)) {
741 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
742 unset($quiz->generalfeedbackimmediately);
744 if (isset($quiz->generalfeedbackopen)) {
745 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN);
746 unset($quiz->generalfeedbackopen);
748 if (isset($quiz->generalfeedbackclosed)) {
749 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED);
750 unset($quiz->generalfeedbackclosed);
753 if (isset($quiz->overallfeedbackimmediately)) {
754 $review += (QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
755 unset($quiz->overallfeedbackimmediately);
757 if (isset($quiz->overallfeedbackopen)) {
758 $review += (QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN);
759 unset($quiz->overallfeedbackopen);
761 if (isset($quiz->overallfeedbackclosed)) {
762 $review += (QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED);
763 unset($quiz->overallfeedbackclosed);
766 $quiz->review = $review;
770 * This function is called at the end of quiz_add_instance
771 * and quiz_update_instance, to do the common processing.
773 * @param object $quiz the quiz object.
775 function quiz_after_add_or_update($quiz) {
777 // Save the feedback
778 delete_records('quiz_feedback', 'quizid', $quiz->id);
780 for ($i = 0; $i <= $quiz->feedbackboundarycount; $i += 1) {
781 $feedback = new stdClass;
782 $feedback->quizid = $quiz->id;
783 $feedback->feedbacktext = $quiz->feedbacktext[$i];
784 $feedback->mingrade = $quiz->feedbackboundaries[$i];
785 $feedback->maxgrade = $quiz->feedbackboundaries[$i - 1];
786 if (!insert_record('quiz_feedback', $feedback, false)) {
787 return "Could not save quiz feedback.";
792 // Update the events relating to this quiz.
793 // This is slightly inefficient, deleting the old events and creating new ones. However,
794 // there are at most two events, and this keeps the code simpler.
795 if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
796 foreach($events as $event) {
797 delete_event($event->id);
801 $event = new stdClass;
802 $event->description = $quiz->intro;
803 $event->courseid = $quiz->course;
804 $event->groupid = 0;
805 $event->userid = 0;
806 $event->modulename = 'quiz';
807 $event->instance = $quiz->id;
808 $event->timestart = $quiz->timeopen;
809 $event->timeduration = $quiz->timeclose - $quiz->timeopen;
810 $event->visible = instance_is_visible('quiz', $quiz);
811 $event->eventtype = 'open';
813 if ($quiz->timeclose and $quiz->timeopen and $event->timeduration <= QUIZ_MAX_EVENT_LENGTH) {
814 // Single event for the whole quiz.
815 $event->name = $quiz->name;
816 add_event($event);
817 } else {
818 // Separate start and end events.
819 $event->timeduration = 0;
820 if ($quiz->timeopen) {
821 $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
822 add_event($event);
823 unset($event->id); // So we can use the same object for the close event.
825 if ($quiz->timeclose) {
826 $event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
827 $event->timestart = $quiz->timeclose;
828 $event->eventtype = 'close';
829 add_event($event);
833 //update related grade item
834 quiz_grade_item_update(stripslashes_recursive($quiz));
837 function quiz_get_view_actions() {
838 return array('view','view all','report');
841 function quiz_get_post_actions() {
842 return array('attempt','editquestions','review','submit');
846 * Returns an array of names of quizzes that use this question
848 * @param object $questionid
849 * @return array of strings
851 function quiz_question_list_instances($questionid) {
852 global $CFG;
854 // TODO: we should also consider other questions that are used by
855 // random questions in this quiz, but that is very hard.
857 $sql = "SELECT q.id, q.name
858 FROM {$CFG->prefix}quiz q
859 INNER JOIN
860 {$CFG->prefix}quiz_question_instances qqi
861 ON q.id = qqi.quiz
862 WHERE qqi.question = '$questionid'";
864 if ($instances = get_records_sql_menu($sql)) {
865 return $instances;
867 return array();
871 * Implementation of the function for printing the form elements that control
872 * whether the course reset functionality affects the quiz.
873 * @param $course The course id of the course the user is thinking of resetting.
875 function quiz_reset_course_form($course) {
876 echo '<p>';
877 print_checkbox('reset_quiz_attempts', 1, true, get_string('removeallquizattempts','quiz'));
878 echo '</p>';
882 * Actual implementation of the rest coures functionality, delete all the
883 * quiz attempts for course $data->courseid, if $data->reset_quiz_attempts is
884 * set and true.
886 * Also, move the quiz open and close dates, if the course start date is changing.
888 * @param $data the data submitted from the reset course forum.
889 * @param $showfeedback whether to output progress information as the reset
890 * progresses.
892 function quiz_delete_userdata($data, $showfeedback=true) {
893 global $CFG;
895 /// Delete attempts.
896 if (!empty($data->reset_quiz_attempts)) {
897 $conditiononquizids = 'quiz IN (SELECT id FROM ' .
898 $CFG->prefix . 'quiz q WHERE q.course = ' . $data->courseid . ')';
900 $attemptids = get_records_select('quiz_attempts', $conditiononquizids, '', 'id, uniqueid');
901 if ($attemptids) {
902 if ($showfeedback) {
903 echo '<div class="notifysuccess">', get_string('deletingquestionattempts', 'quiz');
904 $divider = ': ';
906 foreach ($attemptids as $attemptid) {
907 delete_attempt($attemptid->uniqueid);
908 if ($showfeedback) {
909 echo $divider, $attemptid->uniqueid;
910 $divider = ', ';
913 if ($showfeedback) {
914 echo "</div><br />\n";
917 if (delete_records_select('quiz_grades', $conditiononquizids) && $showfeedback) {
918 notify(get_string('gradesdeleted','quiz'), 'notifysuccess');
920 if (delete_records_select('quiz_attempts', $conditiononquizids) && $showfeedback) {
921 notify(get_string('attemptsdeleted','quiz'), 'notifysuccess');
925 /// Update open and close dates
926 if (!empty($data->reset_start_date)) {
927 /// Work out offset.
928 $olddate = get_field('course', 'startdate', 'id', $data->courseid);
929 $olddate = usergetmidnight($olddate); // time part of $olddate should be zero
930 $newdate = make_timestamp($data->startyear, $data->startmonth, $data->startday);
931 $interval = $newdate - $olddate;
933 /// Apply it to quizzes with an open or close date.
934 $success = true;
935 begin_sql();
936 $success = $success && execute_sql(
937 "UPDATE {$CFG->prefix}quiz
938 SET timeopen = timeopen + $interval
939 WHERE course = {$data->courseid} AND timeopen <> 0", false);
940 $success = $success && execute_sql(
941 "UPDATE {$CFG->prefix}quiz
942 SET timeclose = timeclose + $interval
943 WHERE course = {$data->courseid} AND timeclose <> 0", false);
945 if ($success) {
946 commit_sql();
947 if ($showfeedback) {
948 notify(get_string('openclosedatesupdated', 'quiz'), 'notifysuccess');
950 } else {
951 rollback_sql();
957 * Checks whether the current user is allowed to view a file uploaded in a quiz.
958 * Teachers can view any from their courses, students can only view their own.
960 * @param int $attemptid int attempt id
961 * @param int $questionid int question id
962 * @return boolean to indicate access granted or denied
964 function quiz_check_file_access($attemptid, $questionid) {
965 global $USER;
967 $attempt = get_record("quiz_attempts", 'id', $attemptid);
968 $quiz = get_record("quiz", 'id', $attempt->quiz);
969 $context = get_context_instance(CONTEXT_COURSE, $quiz->course);
971 // access granted if the current user submitted this file
972 if ($attempt->userid == $USER->id) {
973 return true;
974 // access granted if the current user has permission to grade quizzes in this course
975 } else if (has_capability('mod/quiz:viewreports', $context) || has_capability('mod/quiz:grade', $context)) {
976 return true;
979 // otherwise, this user does not have permission
980 return false;
984 * Prints quiz summaries on MyMoodle Page
986 function quiz_print_overview($courses, &$htmlarray) {
987 global $USER, $CFG;
988 /// These next 6 Lines are constant in all modules (just change module name)
989 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
990 return array();
993 if (!$quizs = get_all_instances_in_courses('quiz', $courses)) {
994 return;
997 /// Fetch some language strings outside the main loop.
998 $strquiz = get_string('modulename', 'quiz');
999 $strnoattempts = get_string('noattempts', 'quiz');
1001 /// We want to list quizzes that are currently available, and which have a close date.
1002 /// This is the same as what the lesson does, and the dabate is in MDL-10568.
1003 $now = date();
1004 foreach ($quizs as $quiz) {
1005 if ($quiz->timeclose >= $now && $quiz->timeopen < $now) {
1006 /// Give a link to the quiz, and the deadline.
1007 $str = '<div class="quiz overview">' .
1008 '<div class="name">' . $strquiz . ': <a ' . ($quiz->visible ? '' : ' class="dimmed"') .
1009 ' href="' . $CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->coursemodule . '">' .
1010 $quiz->name . '</a></div>';
1011 $str .= '<div class="info">' . get_string('quizcloseson', 'quiz', userdate($quiz->timeclose)) . '</div>';
1013 /// Now provide more information depending on the uers's role.
1014 $context = get_context_instance(CONTEXT_MODULE, $quiz->coursemodule);
1015 if (has_capability('mod/quiz:viewreports', $context)) {
1016 /// For teacher-like people, show a summary of the number of student attempts.
1017 $a = new stdClass;
1018 if ($a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0)) {
1019 $a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
1020 } else {
1021 $a->studentnum = 0;
1022 $a->attemptnum = 0;
1024 $a->studentstring = $course->students;
1025 $str .= '<div class="info">' . get_string('numattempts', 'quiz', $a) . '</div>';
1026 } else if (has_capability('mod/quiz:attempt', $context)){ // Student
1027 /// For student-like people, tell them how many attempts they have made.
1028 if (isset($USER->id) && ($attempts = quiz_get_user_attempts($quiz->id, $USER->id))) {
1029 $numattempts = count($attempts);
1030 $str .= '<div class="info">' . get_string('numattemptsmade', 'quiz', $numattempts) . '</div>';
1031 } else {
1032 $str .= '<div class="info">' . $strnoattempts . '</div>';
1034 } else {
1035 /// For ayone else, there is no point listing this quiz, so stop processing.
1036 continue;
1039 /// Add the output for this quiz to the rest.
1040 $str .= '</div>';
1041 if (empty($htmlarray[$quiz->course]['quiz'])) {
1042 $htmlarray[$quiz->course]['quiz'] = $str;
1043 } else {
1044 $htmlarray[$quiz->course]['quiz'] .= $str;