Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / quiz / lib.php
blobde3529e73f8f7a56dec8f27d7806c285317219d3
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 * @version $Id$
8 * @author Martin Dougiamas and many others.
9 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10 * @package quiz
13 require_once($CFG->libdir.'/pagelib.php');
14 require_once($CFG->libdir.'/questionlib.php');
16 /// CONSTANTS ///////////////////////////////////////////////////////////////////
18 /**#@+
19 * The different review options are stored in the bits of $quiz->review
20 * These constants help to extract the options
22 /**
23 * The first 6 bits refer to the time immediately after the attempt
25 define('QUIZ_REVIEW_IMMEDIATELY', 0x3f);
26 /**
27 * the next 6 bits refer to the time after the attempt but while the quiz is open
29 define('QUIZ_REVIEW_OPEN', 0xfc0);
30 /**
31 * the final 6 bits refer to the time after the quiz closes
33 define('QUIZ_REVIEW_CLOSED', 0x3f000);
35 // within each group of 6 bits we determine what should be shown
36 define('QUIZ_REVIEW_RESPONSES', 1*0x1041); // Show responses
37 define('QUIZ_REVIEW_SCORES', 2*0x1041); // Show scores
38 define('QUIZ_REVIEW_FEEDBACK', 4*0x1041); // Show feedback
39 define('QUIZ_REVIEW_ANSWERS', 8*0x1041); // Show correct answers
40 // Some handling of worked solutions is already in the code but not yet fully supported
41 // and not switched on in the user interface.
42 define('QUIZ_REVIEW_SOLUTIONS', 16*0x1041); // Show solutions
43 define('QUIZ_REVIEW_GENERALFEEDBACK', 32*0x1041); // Show general feedback
44 /**#@-*/
46 /**
47 * If start and end date for the quiz are more than this many seconds apart
48 * they will be represented by two separate events in the calendar
50 define("QUIZ_MAX_EVENT_LENGTH", "432000"); // 5 days maximum
52 /// FUNCTIONS ///////////////////////////////////////////////////////////////////
54 /**
55 * Given an object containing all the necessary data,
56 * (defined by the form in mod.html) this function
57 * will create a new instance and return the id number
58 * of the new instance.
60 * @param object $quiz the data that came from the form.
61 * @return mixed the id of the new instance on success,
62 * false or a string error message on failure.
64 function quiz_add_instance($quiz) {
66 // Process the options from the form.
67 $quiz->created = time();
68 $quiz->questions = '';
69 $result = quiz_process_options($quiz);
70 if ($result && is_string($result)) {
71 return $result;
74 // Try to store it in the database.
75 if (!$quiz->id = insert_record("quiz", $quiz)) {
76 return false;
79 // Do the processing required after an add or an update.
80 quiz_after_add_or_update($quiz);
82 return $quiz->id;
85 /**
86 * Given an object containing all the necessary data,
87 * (defined by the form in mod.html) this function
88 * will update an existing instance with new data.
90 * @param object $quiz the data that came from the form.
91 * @return mixed true on success, false or a string error message on failure.
93 function quiz_update_instance($quiz) {
95 // Process the options from the form.
96 $result = quiz_process_options($quiz);
97 if ($result && is_string($result)) {
98 return $result;
101 // Update the database.
102 $quiz->id = $quiz->instance;
103 if (!update_record("quiz", $quiz)) {
104 return false; // some error occurred
107 // Do the processing required after an add or an update.
108 quiz_after_add_or_update($quiz);
110 // Delete any previous preview attempts
111 delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id);
113 return true;
117 function quiz_delete_instance($id) {
118 /// Given an ID of an instance of this module,
119 /// this function will permanently delete the instance
120 /// and any data that depends on it.
122 if (! $quiz = get_record("quiz", "id", "$id")) {
123 return false;
126 $result = true;
128 if ($attempts = get_records("quiz_attempts", "quiz", "$quiz->id")) {
129 foreach ($attempts as $attempt) {
130 // TODO: this should use the delete_attempt($attempt->uniqueid) function in questionlib.php
131 if (! delete_records("question_states", "attempt", "$attempt->uniqueid")) {
132 $result = false;
134 if (! delete_records("question_sessions", "attemptid", "$attempt->uniqueid")) {
135 $result = false;
140 $tables_to_purge = array(
141 'quiz_attempts' => 'quiz',
142 'quiz_grades' => 'quiz',
143 'quiz_question_instances' => 'quiz',
144 'quiz_grades' => 'quiz',
145 'quiz_feedback' => 'quizid',
146 'quiz' => 'id'
148 foreach ($tables_to_purge as $table => $keyfield) {
149 if (!delete_records($table, $keyfield, $quiz->id)) {
150 $result = false;
154 $pagetypes = page_import_types('mod/quiz/');
155 foreach($pagetypes as $pagetype) {
156 if(!delete_records('block_instance', 'pageid', $quiz->id, 'pagetype', $pagetype)) {
157 $result = false;
161 if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
162 foreach($events as $event) {
163 delete_event($event->id);
167 quiz_grade_item_delete($quiz);
169 return $result;
173 function quiz_user_outline($course, $user, $mod, $quiz) {
174 /// Return a small object with summary information about what a
175 /// user has done with a given particular instance of this module
176 /// Used for user activity reports.
177 /// $return->time = the time they did it
178 /// $return->info = a short text description
179 if ($grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
181 $result = new stdClass;
182 if ((float)$grade->grade) {
183 $result->info = get_string('grade').':&nbsp;'.round($grade->grade, $quiz->decimalpoints);
185 $result->time = $grade->timemodified;
186 return $result;
188 return NULL;
193 function quiz_user_complete($course, $user, $mod, $quiz) {
194 /// Print a detailed representation of what a user has done with
195 /// a given particular instance of this module, for user activity reports.
197 if ($attempts = get_records_select('quiz_attempts', "userid='$user->id' AND quiz='$quiz->id'", 'attempt ASC')) {
198 if ($quiz->grade and $quiz->sumgrades && $grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
199 echo get_string('grade').': '.round($grade->grade, $quiz->decimalpoints).'/'.$quiz->grade.'<br />';
201 foreach ($attempts as $attempt) {
202 echo get_string('attempt', 'quiz').' '.$attempt->attempt.': ';
203 if ($attempt->timefinish == 0) {
204 print_string('unfinished');
205 } else {
206 echo round($attempt->sumgrades, $quiz->decimalpoints).'/'.$quiz->sumgrades;
208 echo ' - '.userdate($attempt->timemodified).'<br />';
210 } else {
211 print_string('noattempts', 'quiz');
214 return true;
218 function quiz_cron () {
219 /// Function to be run periodically according to the moodle cron
220 /// This function searches for things that need to be done, such
221 /// as sending out mail, toggling flags etc ...
223 global $CFG;
225 return true;
230 * Return grade for given user or all users.
232 * @param int $quizid id of quiz
233 * @param int $userid optional user id, 0 means all users
234 * @return array array of grades, false if none
236 function quiz_get_user_grades($quiz, $userid=0) {
237 global $CFG;
239 $user = $userid ? "AND u.id = $userid" : "";
241 $sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade
242 FROM {$CFG->prefix}user u, {$CFG->prefix}quiz_grades g
243 WHERE u.id = g.userid AND g.quiz = $quiz->id
244 $user";
246 return get_records_sql($sql);
250 * Update grades in central gradebook
252 * @param object $quiz null means all quizs
253 * @param int $userid specific user only, 0 mean all
255 function quiz_update_grades($quiz=null, $userid=0, $nullifnone=true) {
256 global $CFG;
257 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
258 require_once($CFG->libdir.'/gradelib.php');
261 if ($quiz != null) {
262 if ($grades = quiz_get_user_grades($quiz, $userid)) {
263 grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grades);
265 } else if ($userid and $nullifnone) {
266 $grade = new object();
267 $grade->itemid = $quiz->id;
268 $grade->userid = $userid;
269 $grade->rawgrade = NULL;
270 grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grade);
273 } else {
274 $sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
275 FROM {$CFG->prefix}quiz a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
276 WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=a.id";
277 if ($rs = get_recordset_sql($sql)) {
278 if ($rs->RecordCount() > 0) {
279 while ($quiz = rs_fetch_next_record($rs)) {
280 quiz_grade_item_update($quiz);
281 if ($quiz->grade != 0) {
282 quiz_update_grades($quiz, 0, false);
286 rs_close($rs);
292 * Create grade item for given quiz
294 * @param object $quiz object with extra cmidnumber
295 * @return int 0 if ok, error code otherwise
297 function quiz_grade_item_update($quiz) {
298 global $CFG;
299 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
300 require_once($CFG->libdir.'/gradelib.php');
303 if (array_key_exists('cmidnumber', $quiz)) { //it may not be always present
304 $params = array('itemname'=>$quiz->name, 'idnumber'=>$quiz->cmidnumber);
305 } else {
306 $params = array('itemname'=>$quiz->name);
309 if ($quiz->grade > 0) {
310 $params['gradetype'] = GRADE_TYPE_VALUE;
311 $params['grademax'] = $quiz->grade;
312 $params['grademin'] = 0;
314 } else {
315 $params['gradetype'] = GRADE_TYPE_NONE;
318 return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, NULL, $params);
322 * Delete grade item for given quiz
324 * @param object $quiz object
325 * @return object quiz
327 function quiz_grade_item_delete($quiz) {
328 global $CFG;
329 require_once($CFG->libdir.'/gradelib.php');
331 return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, NULL, array('deleted'=>1));
335 function quiz_get_participants($quizid) {
336 /// Returns an array of users who have data in a given quiz
337 /// (users with records in quiz_attempts and quiz_question_versions)
339 global $CFG;
341 //Get users from attempts
342 $us_attempts = get_records_sql("SELECT DISTINCT u.id, u.id
343 FROM {$CFG->prefix}user u,
344 {$CFG->prefix}quiz_attempts a
345 WHERE a.quiz = '$quizid' and
346 u.id = a.userid");
348 //Get users from question_versions
349 $us_versions = get_records_sql("SELECT DISTINCT u.id, u.id
350 FROM {$CFG->prefix}user u,
351 {$CFG->prefix}quiz_question_versions v
352 WHERE v.quiz = '$quizid' and
353 u.id = v.userid");
355 //Add us_versions to us_attempts
356 if ($us_versions) {
357 foreach ($us_versions as $us_version) {
358 $us_attempts[$us_version->id] = $us_version;
361 //Return us_attempts array (it contains an array of unique users)
362 return ($us_attempts);
366 function quiz_refresh_events($courseid = 0) {
367 // This horrible function only seems to be called from mod/quiz/db/[dbtype].php.
369 // This standard function will check all instances of this module
370 // and make sure there are up-to-date events created for each of them.
371 // If courseid = 0, then every quiz event in the site is checked, else
372 // only quiz events belonging to the course specified are checked.
373 // This function is used, in its new format, by restore_refresh_events()
375 if ($courseid == 0) {
376 if (! $quizzes = get_records("quiz")) {
377 return true;
379 } else {
380 if (! $quizzes = get_records("quiz", "course", $courseid)) {
381 return true;
384 $moduleid = get_field('modules', 'id', 'name', 'quiz');
386 foreach ($quizzes as $quiz) {
387 $event = NULL;
388 $event2 = NULL;
389 $event2old = NULL;
391 if ($events = get_records_select('event', "modulename = 'quiz' AND instance = '$quiz->id' ORDER BY timestart")) {
392 $event = array_shift($events);
393 if (!empty($events)) {
394 $event2old = array_shift($events);
395 if (!empty($events)) {
396 foreach ($events as $badevent) {
397 delete_records('event', 'id', $badevent->id);
403 $event->name = addslashes($quiz->name);
404 $event->description = addslashes($quiz->intro);
405 $event->courseid = $quiz->course;
406 $event->groupid = 0;
407 $event->userid = 0;
408 $event->modulename = 'quiz';
409 $event->instance = $quiz->id;
410 $event->visible = instance_is_visible('quiz', $quiz);
411 $event->timestart = $quiz->timeopen;
412 $event->eventtype = 'open';
413 $event->timeduration = ($quiz->timeclose - $quiz->timeopen);
415 if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Set up two events
417 $event2 = $event;
419 $event->name = addslashes($quiz->name).' ('.get_string('quizopens', 'quiz').')';
420 $event->timeduration = 0;
422 $event2->name = addslashes($quiz->name).' ('.get_string('quizcloses', 'quiz').')';
423 $event2->timestart = $quiz->timeclose;
424 $event2->eventtype = 'close';
425 $event2->timeduration = 0;
427 if (empty($event2old->id)) {
428 unset($event2->id);
429 add_event($event2);
430 } else {
431 $event2->id = $event2old->id;
432 update_event($event2);
434 } else if (!empty($event2->id)) {
435 delete_event($event2->id);
438 if (empty($event->id)) {
439 add_event($event);
440 } else {
441 update_event($event);
445 return true;
449 function quiz_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $quiz="0", $user="", $groupid="") {
450 // Returns all quizzes since a given time. If quiz is specified then
451 // this restricts the results
453 global $CFG;
455 if ($quiz) {
456 $quizselect = " AND cm.id = '$quiz'";
457 } else {
458 $quizselect = "";
460 if ($user) {
461 $userselect = " AND u.id = '$user'";
462 } else {
463 $userselect = "";
466 $quizzes = get_records_sql("SELECT qa.*, q.name, u.firstname, u.lastname, u.picture,
467 q.course, q.sumgrades as maxgrade, cm.instance, cm.section
468 FROM {$CFG->prefix}quiz_attempts qa,
469 {$CFG->prefix}quiz q,
470 {$CFG->prefix}user u,
471 {$CFG->prefix}course_modules cm
472 WHERE qa.timefinish > '$sincetime'
473 AND qa.userid = u.id $userselect
474 AND qa.quiz = q.id $quizselect
475 AND cm.instance = q.id
476 AND cm.course = '$courseid'
477 AND q.course = cm.course
478 ORDER BY qa.timefinish ASC");
480 if (empty($quizzes))
481 return;
483 foreach ($quizzes as $quiz) {
484 if (empty($groupid) || groups_is_member($groupid, $quiz->userid)) {
486 $tmpactivity = new Object;
488 $tmpactivity->type = "quiz";
489 $tmpactivity->defaultindex = $index;
490 $tmpactivity->instance = $quiz->quiz;
492 $tmpactivity->name = $quiz->name;
493 $tmpactivity->section = $quiz->section;
495 $tmpactivity->content->attemptid = $quiz->id;
496 $tmpactivity->content->sumgrades = $quiz->sumgrades;
497 $tmpactivity->content->maxgrade = $quiz->maxgrade;
498 $tmpactivity->content->attempt = $quiz->attempt;
500 $tmpactivity->user->userid = $quiz->userid;
501 $tmpactivity->user->fullname = fullname($quiz);
502 $tmpactivity->user->picture = $quiz->picture;
504 $tmpactivity->timestamp = $quiz->timefinish;
506 $activities[] = $tmpactivity;
508 $index++;
512 return;
516 function quiz_print_recent_mod_activity($activity, $course, $detail=false) {
517 global $CFG;
519 echo '<table border="0" cellpadding="3" cellspacing="0">';
521 echo "<tr><td class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
522 print_user_picture($activity->user->userid, $course, $activity->user->picture);
523 echo "</td><td style=\"width:100%;\"><font size=\"2\">";
525 if ($detail) {
526 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
527 "class=\"icon\" alt=\"$activity->type\" /> ";
528 echo "<a href=\"$CFG->wwwroot/mod/quiz/view.php?id=" . $activity->instance . "\">"
529 . format_string($activity->name,true) . "</a> - ";
533 if (has_capability('mod/quiz:grade', get_context_instance(CONTEXT_MODULE, $course))) {
534 $grades = "(" . $activity->content->sumgrades . " / " . $activity->content->maxgrade . ") ";
535 echo "<a href=\"$CFG->wwwroot/mod/quiz/review.php?q="
536 . $activity->instance . "&amp;attempt="
537 . $activity->content->attemptid . "\">" . $grades . "</a> ";
539 echo get_string("attempt", "quiz") . " - " . $activity->content->attempt . "<br />";
541 echo "<a href=\"$CFG->wwwroot/user/view.php?id="
542 . $activity->user->userid . "&amp;course=$course\">"
543 . $activity->user->fullname . "</a> ";
545 echo " - " . userdate($activity->timestamp);
547 echo "</font></td></tr>";
548 echo "</table>";
550 return;
554 * Pre-process the quiz options form data, making any necessary adjustments.
555 * Called by add/update instance in this file, and the save code in admin/module.php.
557 * @param object $quiz The variables set on the form.
559 function quiz_process_options(&$quiz) {
560 $quiz->timemodified = time();
562 // Quiz open time.
563 if (empty($quiz->timeopen)) {
564 $quiz->preventlate = 0;
567 // Quiz name. (Make up a default if one was not given.)
568 if (empty($quiz->name)) {
569 if (empty($quiz->intro)) {
570 $quiz->name = get_string('modulename', 'quiz');
571 } else {
572 $quiz->name = shorten_text(strip_tags($quiz->intro));
575 $quiz->name = trim($quiz->name);
577 // Time limit. (Get rid of it if the checkbox was not ticked.)
578 if (empty($quiz->timelimitenable)) {
579 $quiz->timelimit = 0;
581 $quiz->timelimit = round($quiz->timelimit);
583 // Password field - different in form to stop browsers that remember passwords
584 // getting confused.
585 $quiz->password = $quiz->quizpassword;
586 unset($quiz->quizpassword);
588 // Quiz feedback
589 if (isset($quiz->feedbacktext)) {
590 // Clean up the boundary text.
591 for ($i = 0; $i < count($quiz->feedbacktext); $i += 1) {
592 if (empty($quiz->feedbacktext[$i])) {
593 $quiz->feedbacktext[$i] = '';
594 } else {
595 $quiz->feedbacktext[$i] = trim($quiz->feedbacktext[$i]);
599 // Check the boundary value is a number or a percentage, and in range.
600 $i = 0;
601 while (!empty($quiz->feedbackboundaries[$i])) {
602 $boundary = trim($quiz->feedbackboundaries[$i]);
603 if (!is_numeric($boundary)) {
604 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
605 $boundary = trim(substr($boundary, 0, -1));
606 if (is_numeric($boundary)) {
607 $boundary = $boundary * $quiz->grade / 100.0;
608 } else {
609 return get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
613 if ($boundary <= 0 || $boundary >= $quiz->grade) {
614 return get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
616 if ($i > 0 && $boundary >= $quiz->feedbackboundaries[$i - 1]) {
617 return get_string('feedbackerrororder', 'quiz', $i + 1);
619 $quiz->feedbackboundaries[$i] = $boundary;
620 $i += 1;
622 $numboundaries = $i;
624 // Check there is nothing in the remaining unused fields.
625 for ($i = $numboundaries; $i < count($quiz->feedbackboundaries); $i += 1) {
626 if (!empty($quiz->feedbackboundaries[$i]) && trim($quiz->feedbackboundaries[$i]) != '') {
627 return get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
630 for ($i = $numboundaries + 1; $i < count($quiz->feedbacktext); $i += 1) {
631 if (!empty($quiz->feedbacktext[$i]) && trim($quiz->feedbacktext[$i]) != '') {
632 return get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
635 $quiz->feedbackboundaries[-1] = $quiz->grade + 1; // Needs to be bigger than $quiz->grade because of '<' test in quiz_feedback_for_grade().
636 $quiz->feedbackboundaries[$numboundaries] = 0;
637 $quiz->feedbackboundarycount = $numboundaries;
640 // Settings that get combined to go into the optionflags column.
641 $quiz->optionflags = 0;
642 if (!empty($quiz->adaptive)) {
643 $quiz->optionflags |= QUESTION_ADAPTIVE;
646 // Settings that get combined to go into the review column.
647 $review = 0;
648 if (isset($quiz->responsesimmediately)) {
649 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY);
650 unset($quiz->responsesimmediately);
652 if (isset($quiz->responsesopen)) {
653 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN);
654 unset($quiz->responsesopen);
656 if (isset($quiz->responsesclosed)) {
657 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED);
658 unset($quiz->responsesclosed);
661 if (isset($quiz->scoreimmediately)) {
662 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY);
663 unset($quiz->scoreimmediately);
665 if (isset($quiz->scoreopen)) {
666 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN);
667 unset($quiz->scoreopen);
669 if (isset($quiz->scoreclosed)) {
670 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED);
671 unset($quiz->scoreclosed);
674 if (isset($quiz->feedbackimmediately)) {
675 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
676 unset($quiz->feedbackimmediately);
678 if (isset($quiz->feedbackopen)) {
679 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN);
680 unset($quiz->feedbackopen);
682 if (isset($quiz->feedbackclosed)) {
683 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED);
684 unset($quiz->feedbackclosed);
687 if (isset($quiz->answersimmediately)) {
688 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY);
689 unset($quiz->answersimmediately);
691 if (isset($quiz->answersopen)) {
692 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN);
693 unset($quiz->answersopen);
695 if (isset($quiz->answersclosed)) {
696 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED);
697 unset($quiz->answersclosed);
700 if (isset($quiz->solutionsimmediately)) {
701 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_IMMEDIATELY);
702 unset($quiz->solutionsimmediately);
704 if (isset($quiz->solutionsopen)) {
705 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_OPEN);
706 unset($quiz->solutionsopen);
708 if (isset($quiz->solutionsclosed)) {
709 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_CLOSED);
710 unset($quiz->solutionsclosed);
713 if (isset($quiz->generalfeedbackimmediately)) {
714 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
715 unset($quiz->solutionsimmediately);
717 if (isset($quiz->generalfeedbackopen)) {
718 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN);
719 unset($quiz->solutionsopen);
721 if (isset($quiz->generalfeedbackclosed)) {
722 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED);
723 unset($quiz->solutionsclosed);
726 $quiz->review = $review;
730 * This function is called at the end of quiz_add_instance
731 * and quiz_update_instance, to do the common processing.
733 * @param object $quiz the quiz object.
735 function quiz_after_add_or_update($quiz) {
737 // Save the feedback
738 delete_records('quiz_feedback', 'quizid', $quiz->id);
740 for ($i = 0; $i <= $quiz->feedbackboundarycount; $i += 1) {
741 $feedback = new stdClass;
742 $feedback->quizid = $quiz->id;
743 $feedback->feedbacktext = $quiz->feedbacktext[$i];
744 $feedback->mingrade = $quiz->feedbackboundaries[$i];
745 $feedback->maxgrade = $quiz->feedbackboundaries[$i - 1];
746 if (!insert_record('quiz_feedback', $feedback, false)) {
747 return "Could not save quiz feedback.";
752 // Update the events relating to this quiz.
753 // This is slightly inefficient, deleting the old events and creating new ones. However,
754 // there are at most two events, and this keeps the code simpler.
755 if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
756 foreach($events as $event) {
757 delete_event($event->id);
761 $event = new stdClass;
762 $event->description = $quiz->intro;
763 $event->courseid = $quiz->course;
764 $event->groupid = 0;
765 $event->userid = 0;
766 $event->modulename = 'quiz';
767 $event->instance = $quiz->id;
768 $event->timestart = $quiz->timeopen;
769 $event->timeduration = $quiz->timeclose - $quiz->timeopen;
770 $event->visible = instance_is_visible('quiz', $quiz);
771 $event->eventtype = 'open';
773 if ($quiz->timeclose and $quiz->timeopen and $event->timeduration <= QUIZ_MAX_EVENT_LENGTH) {
774 // Single event for the whole quiz.
775 $event->name = $quiz->name;
776 add_event($event);
777 } else {
778 // Separate start and end events.
779 $event->timeduration = 0;
780 if ($quiz->timeopen) {
781 $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
782 add_event($event);
783 unset($event->id); // So we can use the same object for the close event.
785 if ($quiz->timeclose) {
786 $event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
787 $event->timestart = $quiz->timeclose;
788 $event->eventtype = 'close';
789 add_event($event);
793 //update related grade item
794 quiz_grade_item_update(stripslashes_recursive($quiz));
797 function quiz_get_view_actions() {
798 return array('view','view all','report');
801 function quiz_get_post_actions() {
802 return array('attempt','editquestions','review','submit');
806 * Returns an array of names of quizzes that use this question
808 * TODO: write this
809 * @param object $questionid
810 * @return array of strings
812 function quiz_question_list_instances($questionid) {
813 return array();
817 * Implementation of the function for printing the form elements that control
818 * whether the course reset functionality affects the quiz.
819 * @param $course The course id of the course the user is thinking of resetting.
821 function quiz_reset_course_form($course) {
822 echo '<p>';
823 print_checkbox('reset_quiz_attempts', 1, true, get_string('removeallquizattempts','quiz'));
824 echo '</p>';
828 * Actual implementation of the rest coures functionality, delete all the
829 * quiz attempts for course $data->courseid, if $data->reset_quiz_attempts is
830 * set and true.
831 * @param $data the data submitted from the reset course forum.
832 * @param $showfeedback whether to output progress information as the reset
833 * progresses.
835 function quiz_delete_userdata($data, $showfeedback=true) {
836 global $CFG;
838 if (empty($data->reset_quiz_attempts)) {
839 return;
842 $conditiononquizids = 'quiz IN (SELECT id FROM ' .
843 $CFG->prefix . 'quiz q WHERE q.course = ' . $data->courseid . ')';
845 $attemptids = get_records_select('quiz_attempts', $conditiononquizids, '', 'id, uniqueid');
846 if ($attemptids) {
847 if ($showfeedback) {
848 echo '<div class="notifysuccess">', get_string('deletingquestionattempts', 'quiz');
849 $divider = ': ';
851 foreach ($attemptids as $attemptid) {
852 delete_attempt($attemptid->uniqueid);
853 if ($showfeedback) {
854 echo $divider, $attemptid->uniqueid;
855 $divider = ', ';
858 if ($showfeedback) {
859 echo "</div><br />\n";
862 if (delete_records_select('quiz_grades', $conditiononquizids) && $showfeedback) {
863 notify(get_string('gradesdeleted','quiz'), 'notifysuccess');
865 if (delete_records_select('quiz_attempts', $conditiononquizids) && $showfeedback) {
866 notify(get_string('attemptsdeleted','quiz'), 'notifysuccess');
871 * Checks whether the current user is allowed to view a file uploaded in a quiz.
872 * Teachers can view any from their courses, students can only view their own.
874 * @param int $attemptid int attempt id
875 * @param int $questionid int question id
876 * @return boolean to indicate access granted or denied
878 function quiz_check_file_access($attemptid, $questionid) {
879 global $USER;
881 $attempt = get_record("quiz_attempts", 'id', $attemptid);
882 $quiz = get_record("quiz", 'id', $attempt->quiz);
883 $context = get_context_instance(CONTEXT_COURSE, $quiz->course);
885 // access granted if the current user submitted this file
886 if ($attempt->userid == $USER->id) {
887 return true;
888 // access granted if the current user has permission to grade quizzes in this course
889 } else if (has_capability('mod/quiz:viewreports', $context) || has_capability('mod/quiz:grade', $context)) {
890 return true;
893 // otherwise, this user does not have permission
894 return false;