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