MDL-16263 A way for students to flag/bookmark, particular questions during a quiz...
[moodle.git] / mod / quiz / lib.php
CommitLineData
d1290cec 1<?php // $Id$
ee1fb969 2/**
3* Library of functions for the quiz module.
4*
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}
ee1fb969 7* @author Martin Dougiamas and many others.
8* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9* @package quiz
10*/
730fd187 11
c4d588cc 12require_once($CFG->libdir.'/pagelib.php');
f67172b6 13require_once($CFG->libdir.'/questionlib.php');
3b120e46 14require_once($CFG->libdir.'/eventslib.php');
8966a111 15
75cd257b 16/// CONSTANTS ///////////////////////////////////////////////////////////////////
17
e2249afe 18/**#@+
19 * Options determining how the grades from individual attempts are combined to give
20 * the overall grade for a user
21 */
22define("QUIZ_GRADEHIGHEST", "1");
23define("QUIZ_GRADEAVERAGE", "2");
24define("QUIZ_ATTEMPTFIRST", "3");
25define("QUIZ_ATTEMPTLAST", "4");
26/**#@-*/
27
75cd257b 28/**#@+
29 * The different review options are stored in the bits of $quiz->review
30 * These constants help to extract the options
b159da78 31 *
00719c02 32 * This is more of a mess than you might think necessary, because originally
33 * it was though that 3x6 bits were enough, but then they ran out. PHP integers
34 * are only reliably 32 bits signed, so the simplest solution was then to
b159da78 35 * add 4x3 more bits.
75cd257b 36 */
37/**
00719c02 38 * The first 6 + 4 bits refer to the time immediately after the attempt
75cd257b 39 */
00719c02 40define('QUIZ_REVIEW_IMMEDIATELY', 0x3c003f);
75cd257b 41/**
00719c02 42 * the next 6 + 4 bits refer to the time after the attempt but while the quiz is open
75cd257b 43 */
00719c02 44define('QUIZ_REVIEW_OPEN', 0x3c00fc0);
75cd257b 45/**
00719c02 46 * the final 6 + 4 bits refer to the time after the quiz closes
75cd257b 47 */
00719c02 48define('QUIZ_REVIEW_CLOSED', 0x3c03f000);
75cd257b 49
50// within each group of 6 bits we determine what should be shown
00719c02 51define('QUIZ_REVIEW_RESPONSES', 1*0x1041); // Show responses
52define('QUIZ_REVIEW_SCORES', 2*0x1041); // Show scores
53define('QUIZ_REVIEW_FEEDBACK', 4*0x1041); // Show question feedback
54define('QUIZ_REVIEW_ANSWERS', 8*0x1041); // Show correct answers
75cd257b 55// Some handling of worked solutions is already in the code but not yet fully supported
56// and not switched on in the user interface.
00719c02 57define('QUIZ_REVIEW_SOLUTIONS', 16*0x1041); // Show solutions
58define('QUIZ_REVIEW_GENERALFEEDBACK',32*0x1041); // Show question general feedback
59define('QUIZ_REVIEW_OVERALLFEEDBACK', 1*0x4440000); // Show quiz overall feedback
60// Multipliers 2*0x4440000, 4*0x4440000 and 8*0x4440000 are still available
75cd257b 61/**#@-*/
62
63/**
64 * If start and end date for the quiz are more than this many seconds apart
65 * they will be represented by two separate events in the calendar
66 */
00719c02 67define("QUIZ_MAX_EVENT_LENGTH", 5*24*60*60); // 5 days maximum
ee1fb969 68
a5e1f35c 69/// FUNCTIONS ///////////////////////////////////////////////////////////////////
730fd187 70
4e781c7b 71/**
3b120e46 72 * Code to be executed when a module is installed
3b120e46 73 */
74function quiz_install() {
3b120e46 75 return true;
76}
77
920b93d1 78/**
79 * Given an object containing all the necessary data,
7cac0c4b 80 * (defined by the form in mod_form.php) this function
920b93d1 81 * will create a new instance and return the id number
82 * of the new instance.
a23f0aaf 83 *
920b93d1 84 * @param object $quiz the data that came from the form.
212b7b8c 85 * @return mixed the id of the new instance on success,
86 * false or a string error message on failure.
920b93d1 87 */
730fd187 88function quiz_add_instance($quiz) {
c18269c7 89 global $DB;
730fd187 90
920b93d1 91 // Process the options from the form.
92 $quiz->created = time();
bc569413 93 $quiz->questions = '';
212b7b8c 94 $result = quiz_process_options($quiz);
95 if ($result && is_string($result)) {
96 return $result;
97 }
6f797013 98
920b93d1 99 // Try to store it in the database.
c18269c7 100 if (!$quiz->id = $DB->insert_record("quiz", $quiz)) {
920b93d1 101 return false;
34283aa8 102 }
7bd1aa1d 103
920b93d1 104 // Do the processing required after an add or an update.
105 quiz_after_add_or_update($quiz);
a23f0aaf 106
7bd1aa1d 107 return $quiz->id;
730fd187 108}
109
920b93d1 110/**
111 * Given an object containing all the necessary data,
7cac0c4b 112 * (defined by the form in mod_form.php) this function
920b93d1 113 * will update an existing instance with new data.
a23f0aaf 114 *
920b93d1 115 * @param object $quiz the data that came from the form.
212b7b8c 116 * @return mixed true on success, false or a string error message on failure.
920b93d1 117 */
730fd187 118function quiz_update_instance($quiz) {
c18269c7 119 global $DB;
730fd187 120
920b93d1 121 // Process the options from the form.
212b7b8c 122 $result = quiz_process_options($quiz);
123 if ($result && is_string($result)) {
124 return $result;
125 }
ee1fb969 126
920b93d1 127 // Update the database.
730fd187 128 $quiz->id = $quiz->instance;
c18269c7 129 if (!$DB->update_record("quiz", $quiz)) {
7bd1aa1d 130 return false; // some error occurred
131 }
730fd187 132
920b93d1 133 // Do the processing required after an add or an update.
134 quiz_after_add_or_update($quiz);
ee1fb969 135
920b93d1 136 // Delete any previous preview attempts
9cf4a18b 137 $DB->delete_records('quiz_attempts', array('preview' => '1', 'quiz'=>$quiz->id));
d2f308c0 138
7bd1aa1d 139 return true;
730fd187 140}
141
142
143function quiz_delete_instance($id) {
c18269c7 144 global $DB;
f41e824f 145/// Given an ID of an instance of this module,
146/// this function will permanently delete the instance
147/// and any data that depends on it.
730fd187 148
c18269c7 149 if (! $quiz = $DB->get_record("quiz", array("id"=>$id))) {
730fd187 150 return false;
151 }
152
153 $result = true;
154
c18269c7 155 if ($attempts = $DB->get_records("quiz_attempts", array("quiz"=>$quiz->id))) {
10b9291c 156 foreach ($attempts as $attempt) {
212b7b8c 157 // TODO: this should use the delete_attempt($attempt->uniqueid) function in questionlib.php
c18269c7 158 if (! $DB->delete_records("question_states", array("attempt"=>$attempt->uniqueid))) {
d115d8c7 159 $result = false;
160 }
c18269c7 161 if (! $DB->delete_records("question_sessions", array("attemptid"=>$attempt->uniqueid))) {
10b9291c 162 $result = false;
708b521a 163 }
10b9291c 164 }
165 }
166
212b7b8c 167 $tables_to_purge = array(
168 'quiz_attempts' => 'quiz',
169 'quiz_grades' => 'quiz',
170 'quiz_question_instances' => 'quiz',
212b7b8c 171 'quiz_feedback' => 'quizid',
172 'quiz' => 'id'
173 );
174 foreach ($tables_to_purge as $table => $keyfield) {
c18269c7 175 if (!$DB->delete_records($table, array($keyfield=>$quiz->id))) {
212b7b8c 176 $result = false;
177 }
730fd187 178 }
179
880d8675 180 $pagetypes = page_import_types('mod/quiz/');
181 foreach($pagetypes as $pagetype) {
c18269c7 182 if (!$DB->delete_records('block_instance', array('pageid'=>$quiz->id, 'pagetype'=>$pagetype))) {
880d8675 183 $result = false;
184 }
185 }
186
c18269c7 187 if ($events = $DB->get_records('event', array("modulename"=>'quiz', "instance"=>$quiz->id))) {
78036f78 188 foreach($events as $event) {
189 delete_event($event->id);
190 }
b2a3cd2d 191 }
192
d6dd2108 193 quiz_grade_item_delete($quiz);
194
730fd187 195 return $result;
196}
197
198function quiz_user_outline($course, $user, $mod, $quiz) {
9cf4a18b 199 global $DB;
f41e824f 200/// Return a small object with summary information about what a
a5e1f35c 201/// user has done with a given particular instance of this module
202/// Used for user activity reports.
203/// $return->time = the time they did it
204/// $return->info = a short text description
9cf4a18b 205 if ($grade = $DB->get_record('quiz_grades', array('userid' => $user->id, 'quiz' => $quiz->id))) {
ce687025 206 $result = new stdClass;
090cf95a 207 if ((float)$grade->grade) {
f88fb62c 208 $result->info = get_string('grade').':&nbsp;'.quiz_format_grade($quiz, $grade->grade);
98092498 209 }
210 $result->time = $grade->timemodified;
211 return $result;
212 }
213 return NULL;
730fd187 214}
215
216function quiz_user_complete($course, $user, $mod, $quiz) {
9cf4a18b 217 global $DB;
f41e824f 218/// Print a detailed representation of what a user has done with
a5e1f35c 219/// a given particular instance of this module, for user activity reports.
730fd187 220
9cf4a18b 221 if ($attempts = $DB->get_records_select('quiz_attempts', "userid=? AND quiz=?", 'attempt ASC', array($user->id, $quiz->id))) {
f88fb62c 222 if ($quiz->grade && $quiz->sumgrades && $grade = quiz_get_best_grade($quiz, $user->id)) {
223 echo get_string('grade') . ': ' . $grade . '/' . $quiz->grade . '<br />';
ee1fb969 224 }
225 foreach ($attempts as $attempt) {
226 echo get_string('attempt', 'quiz').' '.$attempt->attempt.': ';
227 if ($attempt->timefinish == 0) {
228 print_string('unfinished');
229 } else {
f88fb62c 230 echo quiz_format_grade($quiz, $attempt->sumgrades).'/'.$quiz->sumgrades;
ee1fb969 231 }
232 echo ' - '.userdate($attempt->timemodified).'<br />';
233 }
234 } else {
235 print_string('noattempts', 'quiz');
236 }
237
730fd187 238 return true;
239}
240
ee1fb969 241
730fd187 242function quiz_cron () {
a5e1f35c 243/// Function to be run periodically according to the moodle cron
f41e824f 244/// This function searches for things that need to be done, such
245/// as sending out mail, toggling flags etc ...
730fd187 246
247 global $CFG;
248
249 return true;
250}
251
b5a16eb7 252/**
253 * @param integer $quizid the quiz id.
254 * @param integer $userid the userid.
255 * @param string $status 'all', 'finished' or 'unfinished' to control
256 * @return an array of all the user's attempts at this quiz. Returns an empty array if there are none.
257 */
98f38217 258function quiz_get_user_attempts($quizid, $userid=0, $status = 'finished', $includepreviews = false) {
9cf4a18b 259 global $DB;
b5a16eb7 260 $status_condition = array(
261 'all' => '',
262 'finished' => ' AND timefinish > 0',
263 'unfinished' => ' AND timefinish = 0'
264 );
265 $previewclause = '';
266 if (!$includepreviews) {
267 $previewclause = ' AND preview = 0';
268 }
98f38217 269 $params=array($quizid);
270 if ($userid){
271 $userclause = ' AND userid = ?';
272 $params[]=$userid;
273 } else {
274 $userclause = '';
275 }
9cf4a18b 276 if ($attempts = $DB->get_records_select('quiz_attempts',
98f38217 277 "quiz = ?" .$userclause. $previewclause . $status_condition[$status], $params,
b5a16eb7 278 'attempt ASC')) {
279 return $attempts;
280 } else {
281 return array();
282 }
283}
858deff0 284
d6dd2108 285/**
286 * Return grade for given user or all users.
287 *
288 * @param int $quizid id of quiz
289 * @param int $userid optional user id, 0 means all users
f88fb62c 290 * @return array array of grades, false if none. These are raw grades. They should
291 * be processed with quiz_format_grade for display.
d6dd2108 292 */
293function quiz_get_user_grades($quiz, $userid=0) {
9cf4a18b 294 global $CFG, $DB;
d6dd2108 295
9cf4a18b 296 $params = array($quiz->id);
297 $wheresql = '';
298 if ($userid) {
299 $params[] = $userid;
300 $wheresql = "AND u.id = ?";
301 }
d82b018f 302 $sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade, g.timemodified AS dategraded, MAX(a.timefinish) AS datesubmitted
9cf4a18b 303 FROM {user} u, {quiz_grades} g, {quiz_attempts} a
304 WHERE u.id = g.userid AND g.quiz = ? AND a.quiz = g.quiz AND u.id = a.userid $wheresql
d82b018f 305 GROUP BY u.id, g.grade, g.timemodified";
306
9cf4a18b 307 return $DB->get_records_sql($sql, $params);
d6dd2108 308}
309
f88fb62c 310/**
311 * Round a grade to to the correct number of decimal places, and format it for display.
312 *
313 * @param object $quiz The quiz table row, only $quiz->decimalpoints is used.
314 * @param float $grade The grade to round.
315 */
316function quiz_format_grade($quiz, $grade) {
317 return format_float($grade, $quiz->decimalpoints);
318}
319
d6dd2108 320/**
321 * Update grades in central gradebook
322 *
775f811a 323 * @param object $quiz
324 * @param int $userid specific user only, 0 means all
d6dd2108 325 */
775f811a 326function quiz_update_grades($quiz, $userid=0, $nullifnone=true) {
9cf4a18b 327 global $CFG, $DB;
775f811a 328 require_once($CFG->libdir.'/gradelib.php');
ed1daaa9 329
775f811a 330 if ($quiz->grade == 0) {
331 quiz_grade_item_update($quiz);
d6dd2108 332
775f811a 333 } else if ($grades = quiz_get_user_grades($quiz, $userid)) {
334 quiz_grade_item_update($quiz, $grades);
eafb9d9e 335
775f811a 336 } else if ($userid and $nullifnone) {
337 $grade = new object();
338 $grade->userid = $userid;
339 $grade->rawgrade = NULL;
340 quiz_grade_item_update($quiz, $grade);
d6dd2108 341
342 } else {
775f811a 343 quiz_grade_item_update($quiz);
344 }
345}
346
347/**
348 * Update all grades in gradebook.
349 */
350function quiz_upgrade_grades() {
351 global $DB;
352
353 $sql = "SELECT COUNT('x')
354 FROM {quiz} a, {course_modules} cm, {modules} m
355 WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=a.id";
356 $count = $DB->count_records_sql($sql);
357
358 $sql = "SELECT a.*, cm.idnumber AS cmidnumber, a.course AS courseid
359 FROM {quiz} a, {course_modules} cm, {modules} m
360 WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=a.id";
361 if ($rs = $DB->get_recordset_sql($sql)) {
362 $prevdebug = $DB->get_debug();
363 $DB->set_debug(false);
364 $pbar = new progress_bar('quizupgradegrades', 500, true);
365 $i=0;
366 foreach ($rs as $quiz) {
367 $i++;
368 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
369 quiz_update_grades($quiz, 0, false);
370 $pbar->update($i, $count, "Updating Quiz grades ($i/$count).");
d6dd2108 371 }
775f811a 372 $DB->set_debug($prevdebug);
373 $rs->close();
d6dd2108 374 }
d0ac6bc2 375}
376
d6dd2108 377/**
378 * Create grade item for given quiz
379 *
380 * @param object $quiz object with extra cmidnumber
0b5a80a1 381 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
d6dd2108 382 * @return int 0 if ok, error code otherwise
383 */
ced5ee59 384function quiz_grade_item_update($quiz, $grades=NULL) {
d6dd2108 385 global $CFG;
386 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
387 require_once($CFG->libdir.'/gradelib.php');
388 }
389
390 if (array_key_exists('cmidnumber', $quiz)) { //it may not be always present
391 $params = array('itemname'=>$quiz->name, 'idnumber'=>$quiz->cmidnumber);
392 } else {
393 $params = array('itemname'=>$quiz->name);
394 }
395
396 if ($quiz->grade > 0) {
397 $params['gradetype'] = GRADE_TYPE_VALUE;
398 $params['grademax'] = $quiz->grade;
399 $params['grademin'] = 0;
400
401 } else {
402 $params['gradetype'] = GRADE_TYPE_NONE;
403 }
404
1223d24a 405/* description by TJ:
4061/ If the quiz is set to not show scores while the quiz is still open, and is set to show scores after
407 the quiz is closed, then create the grade_item with a show-after date that is the quiz close date.
4082/ If the quiz is set to not show scores at either of those times, create the grade_item as hidden.
4093/ If the quiz is set to show scores, create the grade_item visible.
410*/
411 if (!($quiz->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED)
412 and !($quiz->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN)) {
413 $params['hidden'] = 1;
414
415 } else if ( ($quiz->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED)
416 and !($quiz->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN)) {
417 if ($quiz->timeclose) {
418 $params['hidden'] = $quiz->timeclose;
419 } else {
420 $params['hidden'] = 1;
421 }
422
423 } else {
424 // a) both open and closed enabled
425 // b) open enabled, closed disabled - we can not "hide after", grades are kept visible even after closing
426 $params['hidden'] = 0;
427 }
428
0b5a80a1 429 if ($grades === 'reset') {
430 $params['reset'] = true;
431 $grades = NULL;
432 }
9cf4a18b 433
49460d84 434 $gradebook_grades = grade_get_grades($quiz->course, 'mod', 'quiz', $quiz->id);
d45459b7 435 if (!empty($gradebook_grades->items)) {
436 $grade_item = $gradebook_grades->items[0];
437 if ($grade_item->locked) {
438 $confirm_regrade = optional_param('confirm_regrade', 0, PARAM_INT);
439 if (!$confirm_regrade) {
440 $message = get_string('gradeitemislocked', 'grades');
441 $back_link = $CFG->wwwroot . '/mod/quiz/report.php?q=' . $quiz->id . '&amp;mode=overview';
442 $regrade_link = qualified_me() . '&amp;confirm_regrade=1';
443 print_box_start('generalbox', 'notice');
444 echo '<p>'. $message .'</p>';
445 echo '<div class="buttons">';
446 print_single_button($regrade_link, null, get_string('regradeanyway', 'grades'), 'post', $CFG->framename);
447 print_single_button($back_link, null, get_string('cancel'), 'post', $CFG->framename);
448 echo '</div>';
449 print_box_end();
9cf4a18b 450
d45459b7 451 return GRADE_UPDATE_ITEM_LOCKED;
452 }
49460d84 453 }
454 }
0b5a80a1 455
ced5ee59 456 return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grades, $params);
d6dd2108 457}
458
459/**
460 * Delete grade item for given quiz
461 *
462 * @param object $quiz object
463 * @return object quiz
464 */
465function quiz_grade_item_delete($quiz) {
466 global $CFG;
467 require_once($CFG->libdir.'/gradelib.php');
468
469 return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, NULL, array('deleted'=>1));
470}
471
e2249afe 472/**
473 * @return the options for calculating the quiz grade from the individual attempt grades.
474 */
475function quiz_get_grading_options() {
476 return array (
477 QUIZ_GRADEHIGHEST => get_string('gradehighest', 'quiz'),
478 QUIZ_GRADEAVERAGE => get_string('gradeaverage', 'quiz'),
479 QUIZ_ATTEMPTFIRST => get_string('attemptfirst', 'quiz'),
480 QUIZ_ATTEMPTLAST => get_string('attemptlast', 'quiz'));
481}
d6dd2108 482
d061d883 483function quiz_get_participants($quizid) {
484/// Returns an array of users who have data in a given quiz
9cf4a18b 485 global $CFG, $DB;
d061d883 486
e4acc4ce 487 //Get users from attempts
9cf4a18b 488 $us_attempts = $DB->get_records_sql("SELECT DISTINCT u.id, u.id
489 FROM {user} u,
490 {quiz_attempts} a
491 WHERE a.quiz = ? and
492 u.id = a.userid", array($quizid));
e4acc4ce 493
e4acc4ce 494 //Return us_attempts array (it contains an array of unique users)
6b224376 495 return $us_attempts;
e4acc4ce 496
d061d883 497}
730fd187 498
d2f308c0 499function quiz_refresh_events($courseid = 0) {
9cf4a18b 500 global $DB;
920b93d1 501// This horrible function only seems to be called from mod/quiz/db/[dbtype].php.
502
d2f308c0 503// This standard function will check all instances of this module
504// and make sure there are up-to-date events created for each of them.
b2a3cd2d 505// If courseid = 0, then every quiz event in the site is checked, else
506// only quiz events belonging to the course specified are checked.
d2f308c0 507// This function is used, in its new format, by restore_refresh_events()
508
509 if ($courseid == 0) {
9cf4a18b 510 if (! $quizzes = $DB->get_records('quiz')) {
d2f308c0 511 return true;
512 }
513 } else {
9cf4a18b 514 if (! $quizzes = $DB->get_records('quiz', array('course' => $courseid))) {
d2f308c0 515 return true;
516 }
517 }
9cf4a18b 518 $moduleid = $DB->get_field('modules', 'id', array('name' => 'quiz'));
f41e824f 519
d2f308c0 520 foreach ($quizzes as $quiz) {
521 $event = NULL;
b2a3cd2d 522 $event2 = NULL;
523 $event2old = NULL;
524
9cf4a18b 525 if ($events = $DB->get_records_select('event', "modulename = 'quiz' AND instance = ? ORDER BY timestart", array($quiz->id))) {
b2a3cd2d 526 $event = array_shift($events);
527 if (!empty($events)) {
528 $event2old = array_shift($events);
529 if (!empty($events)) {
530 foreach ($events as $badevent) {
9cf4a18b 531 $DB->delete_records('event', array('id' => $badevent->id));
b2a3cd2d 532 }
533 }
534 }
535 }
536
ae040d4b 537 $event->name = $quiz->name;
538 $event->description = $quiz->intro;
b2a3cd2d 539 $event->courseid = $quiz->course;
540 $event->groupid = 0;
541 $event->userid = 0;
542 $event->modulename = 'quiz';
543 $event->instance = $quiz->id;
ba288539 544 $event->visible = instance_is_visible('quiz', $quiz);
d2f308c0 545 $event->timestart = $quiz->timeopen;
b2a3cd2d 546 $event->eventtype = 'open';
d2f308c0 547 $event->timeduration = ($quiz->timeclose - $quiz->timeopen);
d2f308c0 548
b2a3cd2d 549 if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Set up two events
d2f308c0 550
b2a3cd2d 551 $event2 = $event;
d2f308c0 552
ae040d4b 553 $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
b2a3cd2d 554 $event->timeduration = 0;
555
ae040d4b 556 $event2->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
b2a3cd2d 557 $event2->timestart = $quiz->timeclose;
558 $event2->eventtype = 'close';
559 $event2->timeduration = 0;
560
561 if (empty($event2old->id)) {
562 unset($event2->id);
563 add_event($event2);
564 } else {
565 $event2->id = $event2old->id;
566 update_event($event2);
567 }
acda04bf 568 } else if (!empty($event2old->id)) {
569 delete_event($event2old->id);
b2a3cd2d 570 }
571
572 if (empty($event->id)) {
acda04bf 573 if (!empty($event->timestart)) {
574 add_event($event);
575 }
b2a3cd2d 576 } else {
577 update_event($event);
d2f308c0 578 }
b2a3cd2d 579
d2f308c0 580 }
581 return true;
582}
583
dd97c328 584/**
585 * Returns all quiz graded users since a given time for specified quiz
586 */
587function quiz_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
9cf4a18b 588 global $CFG, $COURSE, $USER, $DB;
6710ec87 589
dd97c328 590 if ($COURSE->id == $courseid) {
591 $course = $COURSE;
6710ec87 592 } else {
9cf4a18b 593 $course = $DB->get_record('course', array('id' => $courseid));
6710ec87 594 }
6710ec87 595
dd97c328 596 $modinfo =& get_fast_modinfo($course);
6710ec87 597
dd97c328 598 $cm = $modinfo->cms[$cmid];
6710ec87 599
9cf4a18b 600 $params = array($timestart, $cm->instance);
601
dd97c328 602 if ($userid) {
9cf4a18b 603 $userselect = "AND u.id = ?";
604 $params[] = $userid;
dd97c328 605 } else {
606 $userselect = "";
607 }
ac21ad39 608
dd97c328 609 if ($groupid) {
9cf4a18b 610 $groupselect = "AND gm.groupid = ?";
611 $groupjoin = "JOIN {groups_members} gm ON gm.userid=u.id";
612 $params[] = $groupid;
dd97c328 613 } else {
614 $groupselect = "";
615 $groupjoin = "";
616 }
6710ec87 617
9cf4a18b 618 if (!$attempts = $DB->get_records_sql("SELECT qa.*, q.sumgrades AS maxgrade,
619 u.firstname, u.lastname, u.email, u.picture
620 FROM {quiz_attempts} qa
621 JOIN {quiz} q ON q.id = qa.quiz
622 JOIN {user} u ON u.id = qa.userid
dd97c328 623 $groupjoin
624 WHERE qa.timefinish > $timestart AND q.id = $cm->instance
625 $userselect $groupselect
9cf4a18b 626 ORDER BY qa.timefinish ASC", $params)) {
dd97c328 627 return;
628 }
6710ec87 629
6710ec87 630
dd97c328 631 $cm_context = get_context_instance(CONTEXT_MODULE, $cm->id);
632 $grader = has_capability('moodle/grade:viewall', $cm_context);
633 $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context);
634 $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context);
635 $grader = has_capability('mod/quiz:grade', $cm_context);
636 $groupmode = groups_get_activity_groupmode($cm, $course);
6710ec87 637
dd97c328 638 if (is_null($modinfo->groups)) {
639 $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
640 }
6710ec87 641
dd97c328 642 $aname = format_string($cm->name,true);
643 foreach ($attempts as $attempt) {
644 if ($attempt->userid != $USER->id) {
645 if (!$grader) {
646 // grade permission required
647 continue;
648 }
6710ec87 649
9cf4a18b 650 if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
dd97c328 651 $usersgroups = groups_get_all_groups($course->id, $attempt->userid, $cm->groupingid);
652 if (!is_array($usersgroups)) {
653 continue;
654 }
655 $usersgroups = array_keys($usersgroups);
656 $interset = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
657 if (empty($intersect)) {
658 continue;
659 }
660 }
661 }
662
663 $tmpactivity = new object();
664
665 $tmpactivity->type = 'quiz';
666 $tmpactivity->cmid = $cm->id;
667 $tmpactivity->name = $aname;
76cbde41 668 $tmpactivity->sectionnum= $cm->sectionnum;
dd97c328 669 $tmpactivity->timestamp = $attempt->timefinish;
9cf4a18b 670
dd97c328 671 $tmpactivity->content->attemptid = $attempt->id;
672 $tmpactivity->content->sumgrades = $attempt->sumgrades;
673 $tmpactivity->content->maxgrade = $attempt->maxgrade;
674 $tmpactivity->content->attempt = $attempt->attempt;
9cf4a18b 675
dd97c328 676 $tmpactivity->user->userid = $attempt->userid;
677 $tmpactivity->user->fullname = fullname($attempt, $viewfullnames);
678 $tmpactivity->user->picture = $attempt->picture;
9cf4a18b 679
dd97c328 680 $activities[$index++] = $tmpactivity;
6710ec87 681 }
682
683 return;
684}
685
686
dd97c328 687function quiz_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
6eaae5bd 688 global $CFG;
6710ec87 689
dd97c328 690 echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
6710ec87 691
dd97c328 692 echo "<tr><td class=\"userpicture\" valign=\"top\">";
693 print_user_picture($activity->user->userid, $courseid, $activity->user->picture);
694 echo "</td><td>";
6710ec87 695
696 if ($detail) {
dd97c328 697 $modname = $modnames[$activity->type];
698 echo '<div class="title">';
699 echo "<img src=\"$CFG->modpixpath/{$activity->type}/icon.gif\" ".
700 "class=\"icon\" alt=\"$modname\" />";
701 echo "<a href=\"$CFG->wwwroot/mod/quiz/view.php?id={$activity->cmid}\">{$activity->name}</a>";
702 echo '</div>';
6710ec87 703 }
704
dd97c328 705 echo '<div class="grade">';
706 echo get_string("attempt", "quiz")." {$activity->content->attempt}: ";
707 $grades = "({$activity->content->sumgrades} / {$activity->content->maxgrade})";
708 echo "<a href=\"$CFG->wwwroot/mod/quiz/review.php?attempt={$activity->content->attemptid}\">$grades</a>";
709 echo '</div>';
6710ec87 710
dd97c328 711 echo '<div class="user">';
712 echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->userid}&amp;course=$courseid\">"
713 ."{$activity->user->fullname}</a> - ".userdate($activity->timestamp);
714 echo '</div>';
6710ec87 715
dd97c328 716 echo "</td></tr></table>";
6710ec87 717
718 return;
719}
720
ee1fb969 721/**
920b93d1 722 * Pre-process the quiz options form data, making any necessary adjustments.
a0807a00 723 * Called by add/update instance in this file, and the save code in admin/module.php.
b159da78 724 *
920b93d1 725 * @param object $quiz The variables set on the form.
726 */
727function quiz_process_options(&$quiz) {
728 $quiz->timemodified = time();
ee1fb969 729
920b93d1 730 // Quiz open time.
a23f0aaf 731 if (empty($quiz->timeopen)) {
920b93d1 732 $quiz->preventlate = 0;
ee1fb969 733 }
734
dc5c6851 735 // Quiz name.
736 if (!empty($quiz->name)) {
737 $quiz->name = trim($quiz->name);
738 }
a23f0aaf 739
920b93d1 740 // Time limit. (Get rid of it if the checkbox was not ticked.)
a23f0aaf 741 if (empty($quiz->timelimitenable)) {
920b93d1 742 $quiz->timelimit = 0;
743 }
744 $quiz->timelimit = round($quiz->timelimit);
a23f0aaf 745
ab0a8ff2 746 // Password field - different in form to stop browsers that remember passwords
747 // getting confused.
748 $quiz->password = $quiz->quizpassword;
749 unset($quiz->quizpassword);
750
212b7b8c 751 // Quiz feedback
a0807a00 752 if (isset($quiz->feedbacktext)) {
753 // Clean up the boundary text.
754 for ($i = 0; $i < count($quiz->feedbacktext); $i += 1) {
755 if (empty($quiz->feedbacktext[$i])) {
756 $quiz->feedbacktext[$i] = '';
757 } else {
758 $quiz->feedbacktext[$i] = trim($quiz->feedbacktext[$i]);
759 }
212b7b8c 760 }
b159da78 761
a0807a00 762 // Check the boundary value is a number or a percentage, and in range.
763 $i = 0;
764 while (!empty($quiz->feedbackboundaries[$i])) {
765 $boundary = trim($quiz->feedbackboundaries[$i]);
766 if (!is_numeric($boundary)) {
767 if (strlen($boundary) > 0 && $boundary[strlen($boundary) - 1] == '%') {
768 $boundary = trim(substr($boundary, 0, -1));
769 if (is_numeric($boundary)) {
770 $boundary = $boundary * $quiz->grade / 100.0;
771 } else {
772 return get_string('feedbackerrorboundaryformat', 'quiz', $i + 1);
773 }
212b7b8c 774 }
775 }
a0807a00 776 if ($boundary <= 0 || $boundary >= $quiz->grade) {
777 return get_string('feedbackerrorboundaryoutofrange', 'quiz', $i + 1);
778 }
779 if ($i > 0 && $boundary >= $quiz->feedbackboundaries[$i - 1]) {
780 return get_string('feedbackerrororder', 'quiz', $i + 1);
781 }
782 $quiz->feedbackboundaries[$i] = $boundary;
783 $i += 1;
212b7b8c 784 }
a0807a00 785 $numboundaries = $i;
b159da78 786
a0807a00 787 // Check there is nothing in the remaining unused fields.
788 for ($i = $numboundaries; $i < count($quiz->feedbackboundaries); $i += 1) {
789 if (!empty($quiz->feedbackboundaries[$i]) && trim($quiz->feedbackboundaries[$i]) != '') {
790 return get_string('feedbackerrorjunkinboundary', 'quiz', $i + 1);
791 }
212b7b8c 792 }
a0807a00 793 for ($i = $numboundaries + 1; $i < count($quiz->feedbacktext); $i += 1) {
794 if (!empty($quiz->feedbacktext[$i]) && trim($quiz->feedbacktext[$i]) != '') {
795 return get_string('feedbackerrorjunkinfeedback', 'quiz', $i + 1);
796 }
212b7b8c 797 }
a0807a00 798 $quiz->feedbackboundaries[-1] = $quiz->grade + 1; // Needs to be bigger than $quiz->grade because of '<' test in quiz_feedback_for_grade().
799 $quiz->feedbackboundaries[$numboundaries] = 0;
800 $quiz->feedbackboundarycount = $numboundaries;
212b7b8c 801 }
a23f0aaf 802
920b93d1 803 // Settings that get combined to go into the optionflags column.
804 $quiz->optionflags = 0;
805 if (!empty($quiz->adaptive)) {
806 $quiz->optionflags |= QUESTION_ADAPTIVE;
807 }
808
809 // Settings that get combined to go into the review column.
810 $review = 0;
811 if (isset($quiz->responsesimmediately)) {
ee1fb969 812 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY);
920b93d1 813 unset($quiz->responsesimmediately);
ee1fb969 814 }
920b93d1 815 if (isset($quiz->responsesopen)) {
ee1fb969 816 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN);
920b93d1 817 unset($quiz->responsesopen);
ee1fb969 818 }
920b93d1 819 if (isset($quiz->responsesclosed)) {
ee1fb969 820 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED);
920b93d1 821 unset($quiz->responsesclosed);
ee1fb969 822 }
823
920b93d1 824 if (isset($quiz->scoreimmediately)) {
ee1fb969 825 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY);
920b93d1 826 unset($quiz->scoreimmediately);
ee1fb969 827 }
920b93d1 828 if (isset($quiz->scoreopen)) {
ee1fb969 829 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN);
920b93d1 830 unset($quiz->scoreopen);
ee1fb969 831 }
920b93d1 832 if (isset($quiz->scoreclosed)) {
ee1fb969 833 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED);
920b93d1 834 unset($quiz->scoreclosed);
ee1fb969 835 }
836
920b93d1 837 if (isset($quiz->feedbackimmediately)) {
ee1fb969 838 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
920b93d1 839 unset($quiz->feedbackimmediately);
ee1fb969 840 }
920b93d1 841 if (isset($quiz->feedbackopen)) {
ee1fb969 842 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN);
920b93d1 843 unset($quiz->feedbackopen);
ee1fb969 844 }
920b93d1 845 if (isset($quiz->feedbackclosed)) {
ee1fb969 846 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED);
920b93d1 847 unset($quiz->feedbackclosed);
ee1fb969 848 }
849
920b93d1 850 if (isset($quiz->answersimmediately)) {
ee1fb969 851 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY);
920b93d1 852 unset($quiz->answersimmediately);
ee1fb969 853 }
920b93d1 854 if (isset($quiz->answersopen)) {
ee1fb969 855 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN);
920b93d1 856 unset($quiz->answersopen);
ee1fb969 857 }
920b93d1 858 if (isset($quiz->answersclosed)) {
ee1fb969 859 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED);
920b93d1 860 unset($quiz->answersclosed);
ee1fb969 861 }
862
920b93d1 863 if (isset($quiz->solutionsimmediately)) {
ee1fb969 864 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_IMMEDIATELY);
920b93d1 865 unset($quiz->solutionsimmediately);
ee1fb969 866 }
920b93d1 867 if (isset($quiz->solutionsopen)) {
ee1fb969 868 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_OPEN);
920b93d1 869 unset($quiz->solutionsopen);
ee1fb969 870 }
920b93d1 871 if (isset($quiz->solutionsclosed)) {
ee1fb969 872 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_CLOSED);
920b93d1 873 unset($quiz->solutionsclosed);
ee1fb969 874 }
875
a4514d91 876 if (isset($quiz->generalfeedbackimmediately)) {
877 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
00719c02 878 unset($quiz->generalfeedbackimmediately);
1b8a7434 879 }
a4514d91 880 if (isset($quiz->generalfeedbackopen)) {
881 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_OPEN);
00719c02 882 unset($quiz->generalfeedbackopen);
1b8a7434 883 }
a4514d91 884 if (isset($quiz->generalfeedbackclosed)) {
885 $review += (QUIZ_REVIEW_GENERALFEEDBACK & QUIZ_REVIEW_CLOSED);
00719c02 886 unset($quiz->generalfeedbackclosed);
887 }
888
889 if (isset($quiz->overallfeedbackimmediately)) {
890 $review += (QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
891 unset($quiz->overallfeedbackimmediately);
892 }
893 if (isset($quiz->overallfeedbackopen)) {
894 $review += (QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_OPEN);
895 unset($quiz->overallfeedbackopen);
896 }
897 if (isset($quiz->overallfeedbackclosed)) {
898 $review += (QUIZ_REVIEW_OVERALLFEEDBACK & QUIZ_REVIEW_CLOSED);
899 unset($quiz->overallfeedbackclosed);
1b8a7434 900 }
901
920b93d1 902 $quiz->review = $review;
903}
904
905/**
906 * This function is called at the end of quiz_add_instance
907 * and quiz_update_instance, to do the common processing.
a23f0aaf 908 *
920b93d1 909 * @param object $quiz the quiz object.
910 */
911function quiz_after_add_or_update($quiz) {
c18269c7 912 global $DB;
920b93d1 913
212b7b8c 914 // Save the feedback
c18269c7 915 $DB->delete_records('quiz_feedback', array('quizid'=>$quiz->id));
a23f0aaf 916
212b7b8c 917 for ($i = 0; $i <= $quiz->feedbackboundarycount; $i += 1) {
918 $feedback = new stdClass;
919 $feedback->quizid = $quiz->id;
920 $feedback->feedbacktext = $quiz->feedbacktext[$i];
921 $feedback->mingrade = $quiz->feedbackboundaries[$i];
922 $feedback->maxgrade = $quiz->feedbackboundaries[$i - 1];
c18269c7 923 if (!$DB->insert_record('quiz_feedback', $feedback, false)) {
212b7b8c 924 return "Could not save quiz feedback.";
925 }
926 }
927
ee1fb969 928
920b93d1 929 // Update the events relating to this quiz.
930 // This is slightly inefficient, deleting the old events and creating new ones. However,
931 // there are at most two events, and this keeps the code simpler.
c18269c7 932 if ($events = $DB->get_records('event', array('modulename'=>'quiz', 'instance'=>$quiz->id))) {
920b93d1 933 foreach($events as $event) {
934 delete_event($event->id);
935 }
936 }
937
938 $event = new stdClass;
939 $event->description = $quiz->intro;
940 $event->courseid = $quiz->course;
941 $event->groupid = 0;
942 $event->userid = 0;
943 $event->modulename = 'quiz';
944 $event->instance = $quiz->id;
945 $event->timestart = $quiz->timeopen;
946 $event->timeduration = $quiz->timeclose - $quiz->timeopen;
947 $event->visible = instance_is_visible('quiz', $quiz);
948 $event->eventtype = 'open';
949
950 if ($quiz->timeclose and $quiz->timeopen and $event->timeduration <= QUIZ_MAX_EVENT_LENGTH) {
951 // Single event for the whole quiz.
952 $event->name = $quiz->name;
953 add_event($event);
954 } else {
955 // Separate start and end events.
956 $event->timeduration = 0;
957 if ($quiz->timeopen) {
958 $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
959 add_event($event);
960 unset($event->id); // So we can use the same object for the close event.
961 }
962 if ($quiz->timeclose) {
963 $event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
964 $event->timestart = $quiz->timeclose;
965 $event->eventtype = 'close';
966 add_event($event);
967 }
968 }
d6dd2108 969
970 //update related grade item
c18269c7 971 quiz_grade_item_update($quiz);
ee1fb969 972}
973
f3221af9 974function quiz_get_view_actions() {
975 return array('view','view all','report');
976}
ee1fb969 977
f3221af9 978function quiz_get_post_actions() {
979 return array('attempt','editquestions','review','submit');
980}
ee1fb969 981
f67172b6 982/**
983 * Returns an array of names of quizzes that use this question
984 *
f67172b6 985 * @param object $questionid
986 * @return array of strings
987 */
988function quiz_question_list_instances($questionid) {
c6307ef2 989 global $CFG, $DB;
e8666d9a 990
991 // TODO: we should also consider other questions that are used by
992 // random questions in this quiz, but that is very hard.
993
994 $sql = "SELECT q.id, q.name
c6307ef2 995 FROM {quiz} q
996 JOIN {quiz_question_instances} qqi ON q.id = qqi.quiz
997 WHERE qqi.question = ?";
e8666d9a 998
c6307ef2 999 if ($instances = $DB->get_records_sql_menu($sql, array($questionid))) {
e8666d9a 1000 return $instances;
1001 }
f67172b6 1002 return array();
1003}
1004
7a6f4066 1005/**
1006 * Implementation of the function for printing the form elements that control
1007 * whether the course reset functionality affects the quiz.
0b5a80a1 1008 * @param $mform form passed by reference
1009 */
1010function quiz_reset_course_form_definition(&$mform) {
1011 $mform->addElement('header', 'forumheader', get_string('modulenameplural', 'quiz'));
1012 $mform->addElement('advcheckbox', 'reset_quiz_attempts', get_string('removeallquizattempts','quiz'));
1013}
1014
1015/**
1016 * Course reset form defaults.
1017 */
1018function quiz_reset_course_form_defaults($course) {
1019 return array('reset_quiz_attempts'=>1);
1020}
1021
1022/**
1023 * Removes all grades from gradebook
1024 * @param int $courseid
1025 * @param string optional type
7a6f4066 1026 */
0b5a80a1 1027function quiz_reset_gradebook($courseid, $type='') {
9cf4a18b 1028 global $CFG, $DB;
0b5a80a1 1029
1030 $sql = "SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid
9cf4a18b 1031 FROM {quiz} q, {course_modules} cm, {modules} m
1032 WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=q.id AND q.course=?";
0b5a80a1 1033
9cf4a18b 1034 if ($quizs = $DB->get_records_sql($sql, array($courseid))) {
0b5a80a1 1035 foreach ($quizs as $quiz) {
1036 quiz_grade_item_update($quiz, 'reset');
1037 }
1038 }
7a6f4066 1039}
1040
1041/**
1042 * Actual implementation of the rest coures functionality, delete all the
1043 * quiz attempts for course $data->courseid, if $data->reset_quiz_attempts is
1044 * set and true.
6ef56c99 1045 *
1046 * Also, move the quiz open and close dates, if the course start date is changing.
0b5a80a1 1047 * @param $data the data submitted from the reset course.
1048 * @return array status array
7a6f4066 1049 */
0b5a80a1 1050function quiz_reset_userdata($data) {
9cf4a18b 1051 global $CFG, $QTYPES, $DB;
0b5a80a1 1052
1053 $componentstr = get_string('modulenameplural', 'quiz');
1054 $status = array();
b159da78 1055
6ef56c99 1056 /// Delete attempts.
1057 if (!empty($data->reset_quiz_attempts)) {
9cf4a18b 1058 $params = array($data->courseid);
0b5a80a1 1059 $stateslistsql = "SELECT s.id
9cf4a18b 1060 FROM {question_states} s
1061 INNER JOIN {quiz_attempts} qza ON s.attempt=qza.uniqueid
1062 INNER JOIN {quiz} q ON qza.quiz=q.id
1063 WHERE q.course=?";
0b5a80a1 1064
1065 $attemptssql = "SELECT a.uniqueid
9cf4a18b 1066 FROM {quiz_attempts} a, {quiz} q
1067 WHERE q.course=? AND a.quiz=q.id";
0b5a80a1 1068
1069 $quizessql = "SELECT q.id
9cf4a18b 1070 FROM {quiz} q
1071 WHERE q.course=?";
0b5a80a1 1072
9cf4a18b 1073 if ($states = $DB->get_records_sql($stateslistsql, $params)) {
0b5a80a1 1074 //TODO: not sure if this works
1075 $stateslist = implode(',', array_keys($states));
1076 foreach ($QTYPES as $qtype) {
1077 $qtype->delete_states($stateslist);
7a6f4066 1078 }
1079 }
0b5a80a1 1080
9cf4a18b 1081 $DB->delete_records_select('question_states', "attempt IN ($attemptssql)", $params);
1082 $DB->delete_records_select('question_sessions', "attemptid IN ($attemptssql)", $params);
1083 $DB->delete_records_select('question_attempts', "id IN ($attemptssql)", $params);
0b5a80a1 1084
1085 // remove all grades from gradebook
1086 if (empty($data->reset_gradebook_grades)) {
1087 quiz_reset_gradebook($data->courseid);
7a6f4066 1088 }
0b5a80a1 1089
9cf4a18b 1090 $DB->delete_records_select('quiz_grades', "quiz IN ($quizessql)", $params);
0b5a80a1 1091 $status[] = array('component'=>$componentstr, 'item'=>get_string('gradesdeleted','quiz'), 'error'=>false);
1092
9cf4a18b 1093 $DB->delete_records_select('quiz_attempts', "quiz IN ($quizessql)", $params);
0b5a80a1 1094 $status[] = array('component'=>$componentstr, 'item'=>get_string('attemptsdeleted','quiz'), 'error'=>false);
7a6f4066 1095 }
6ef56c99 1096
0b5a80a1 1097 /// updating dates - shift may be negative too
1098 if ($data->timeshift) {
1099 shift_course_mod_dates('quiz', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
1100 $status[] = array('component'=>$componentstr, 'item'=>get_string('openclosedatesupdated', 'quiz'), 'error'=>false);
7a6f4066 1101 }
0b5a80a1 1102
1103 return $status;
7a6f4066 1104}
14e6dc79 1105
1106/**
1107 * Checks whether the current user is allowed to view a file uploaded in a quiz.
1108 * Teachers can view any from their courses, students can only view their own.
b159da78 1109 *
95de57b8 1110 * @param int $attemptuniqueid int attempt id
14e6dc79 1111 * @param int $questionid int question id
b159da78 1112 * @return boolean to indicate access granted or denied
14e6dc79 1113 */
95de57b8 1114function quiz_check_file_access($attemptuniqueid, $questionid) {
9cf4a18b 1115 global $USER, $DB;
b159da78 1116
6102a59d 1117 $attempt = $DB->get_record('quiz_attempts', array('uniqueid' => $attemptuniqueid));
9cf4a18b 1118 $quiz = $DB->get_record('quiz', array('id' => $attempt->quiz));
14e6dc79 1119 $context = get_context_instance(CONTEXT_COURSE, $quiz->course);
b159da78 1120
14e6dc79 1121 // access granted if the current user submitted this file
1122 if ($attempt->userid == $USER->id) {
1123 return true;
1124 // access granted if the current user has permission to grade quizzes in this course
1125 } else if (has_capability('mod/quiz:viewreports', $context) || has_capability('mod/quiz:grade', $context)) {
1126 return true;
1127 }
b159da78 1128
1129 // otherwise, this user does not have permission
14e6dc79 1130 return false;
1131}
b5a16eb7 1132
1133/**
1134 * Prints quiz summaries on MyMoodle Page
1135 */
1136function quiz_print_overview($courses, &$htmlarray) {
1137 global $USER, $CFG;
1138/// These next 6 Lines are constant in all modules (just change module name)
1139 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
1140 return array();
1141 }
1142
2a13e454 1143 if (!$quizzes = get_all_instances_in_courses('quiz', $courses)) {
b5a16eb7 1144 return;
1145 }
1146
1147/// Fetch some language strings outside the main loop.
1148 $strquiz = get_string('modulename', 'quiz');
1149 $strnoattempts = get_string('noattempts', 'quiz');
1150
1151/// We want to list quizzes that are currently available, and which have a close date.
1152/// This is the same as what the lesson does, and the dabate is in MDL-10568.
6c58e198 1153 $now = time();
2a13e454 1154 foreach ($quizzes as $quiz) {
b5a16eb7 1155 if ($quiz->timeclose >= $now && $quiz->timeopen < $now) {
1156 /// Give a link to the quiz, and the deadline.
1157 $str = '<div class="quiz overview">' .
1158 '<div class="name">' . $strquiz . ': <a ' . ($quiz->visible ? '' : ' class="dimmed"') .
1159 ' href="' . $CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->coursemodule . '">' .
1160 $quiz->name . '</a></div>';
1161 $str .= '<div class="info">' . get_string('quizcloseson', 'quiz', userdate($quiz->timeclose)) . '</div>';
1162
1163 /// Now provide more information depending on the uers's role.
1164 $context = get_context_instance(CONTEXT_MODULE, $quiz->coursemodule);
1165 if (has_capability('mod/quiz:viewreports', $context)) {
1166 /// For teacher-like people, show a summary of the number of student attempts.
9cf4a18b 1167 // The $quiz objects returned by get_all_instances_in_course have the necessary $cm
2a13e454 1168 // fields set to make the following call work.
7956944f 1169 $str .= '<div class="info">' . quiz_num_attempt_summary($quiz, $quiz, true) . '</div>';
b5a16eb7 1170 } else if (has_capability('mod/quiz:attempt', $context)){ // Student
1171 /// For student-like people, tell them how many attempts they have made.
1172 if (isset($USER->id) && ($attempts = quiz_get_user_attempts($quiz->id, $USER->id))) {
1173 $numattempts = count($attempts);
9cf4a18b 1174 $str .= '<div class="info">' . get_string('numattemptsmade', 'quiz', $numattempts) . '</div>';
b5a16eb7 1175 } else {
1176 $str .= '<div class="info">' . $strnoattempts . '</div>';
1177 }
1178 } else {
1179 /// For ayone else, there is no point listing this quiz, so stop processing.
1180 continue;
1181 }
1182
1183 /// Add the output for this quiz to the rest.
1184 $str .= '</div>';
1185 if (empty($htmlarray[$quiz->course]['quiz'])) {
1186 $htmlarray[$quiz->course]['quiz'] = $str;
1187 } else {
1188 $htmlarray[$quiz->course]['quiz'] .= $str;
1189 }
1190 }
1191 }
1192}
6c58e198 1193
1194/**
1195 * Return a textual summary of the number of attemtps that have been made at a particular quiz,
1196 * returns '' if no attemtps have been made yet, unless $returnzero is passed as true.
1197 * @param object $quiz the quiz object. Only $quiz->id is used at the moment.
2a13e454 1198 * @param object $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid fields are used at the moment.
1199 * @param boolean $returnzero if false (default), when no attempts have been made '' is returned instead of 'Attempts: 0'.
1200 * @param int $currentgroup if there is a concept of current group where this method is being called
1201 * (e.g. a report) pass it in here. Default 0 which means no current group.
1202 * @return string a string like "Attempts: 123", "Attemtps 123 (45 from your groups)" or
1203 * "Attemtps 123 (45 from this group)".
6c58e198 1204 */
2a13e454 1205function quiz_num_attempt_summary($quiz, $cm, $returnzero = false, $currentgroup = 0) {
9cf4a18b 1206 global $CFG, $USER, $DB;
1207 $numattempts = $DB->count_records('quiz_attempts', array('quiz'=> $quiz->id, 'preview'=>0));
6c58e198 1208 if ($numattempts || $returnzero) {
2a13e454 1209 if (groups_get_activity_groupmode($cm)) {
1210 $a->total = $numattempts;
1211 if ($currentgroup) {
9cf4a18b 1212 $a->group = $DB->count_records_sql('SELECT count(1) FROM ' .
1213 '{quiz_attempts} qa JOIN ' .
1214 '{groups_members} gm ON qa.userid = gm.userid ' .
1215 'WHERE quiz = ? AND preview = 0 AND groupid = ?', array($quiz->id, $currentgroup));
2a13e454 1216 return get_string('attemptsnumthisgroup', 'quiz', $a);
9cf4a18b 1217 } else if ($groups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
1218 list($usql, $params) = $DB->get_in_or_equal(array_keys($groups));
1219 $a->group = $DB->count_records_sql('SELECT count(1) FROM ' .
1220 '{quiz_attempts} qa JOIN ' .
1221 '{groups_members} gm ON qa.userid = gm.userid ' .
1222 'WHERE quiz = ? AND preview = 0 AND ' .
1223 "groupid $usql", array_merge(array($quiz->id), $params));
2a13e454 1224 return get_string('attemptsnumyourgroups', 'quiz', $a);
1225 }
1226 }
6c58e198 1227 return get_string('attemptsnum', 'quiz', $numattempts);
1228 }
1229 return '';
1230}
f432bebf 1231
4e781c7b 1232/**
1233 * @param string $feature FEATURE_xx constant for requested feature
1234 * @return bool True if quiz supports feature
1235 */
1236function quiz_supports($feature) {
1237 switch($feature) {
1238 case FEATURE_GRADE_HAS_GRADE: return true;
1239 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
49f6e5f4 1240 default: return null;
4e781c7b 1241 }
1242}
1243
f432bebf 1244/**
1245 * Returns all other caps used in module
1246 */
1247function quiz_get_extra_capabilities() {
1248 return array(
1249 'moodle/site:accessallgroups',
1250 'moodle/question:add',
1251 'moodle/question:editmine',
1252 'moodle/question:editall',
1253 'moodle/question:viewmine',
1254 'moodle/question:viewall',
1255 'moodle/question:usemine',
1256 'moodle/question:useall',
1257 'moodle/question:movemine',
1258 'moodle/question:moveall',
1259 'moodle/question:managecategory',
62e76c67 1260 'moodle/question:flag',
f432bebf 1261 );
1262}
1263
f4d24ff5 1264?>