Rename quiz_default_questiontype to default_questiontype
[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}
7* @version $Id$
8* @author Martin Dougiamas and many others.
9* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
10* @package quiz
11*/
730fd187 12
c4d588cc 13require_once($CFG->libdir.'/pagelib.php');
7d0fdfd7 14require_once($CFG->dirroot.'/mod/quiz/constants.php');
8966a111 15
ee1fb969 16
a5e1f35c 17/// FUNCTIONS ///////////////////////////////////////////////////////////////////
730fd187 18
19function quiz_add_instance($quiz) {
f41e824f 20/// Given an object containing all the necessary data,
21/// (defined by the form in mod.html) this function
22/// will create a new instance and return the id number
a5e1f35c 23/// of the new instance.
730fd187 24
ee1fb969 25 quiz_process_options($quiz);
26
49dcdd18 27 $quiz->created = time();
730fd187 28 $quiz->timemodified = time();
bb52d44c 29 // The following is adapted from the assignment module
30 if (empty($quiz->dueenable)) {
31 $quiz->timeclose = 0;
32 } else {
33 $quiz->timeclose = make_timestamp($quiz->dueyear, $quiz->duemonth,
34 $quiz->dueday, $quiz->duehour,
35 $quiz->dueminute);
36 }
37 if (empty($quiz->availableenable)) {
38 $quiz->timeopen = 0;
39 $quiz->preventlate = 0;
40 } else {
41 $quiz->timeopen = make_timestamp($quiz->availableyear, $quiz->availablemonth,
42 $quiz->availableday, $quiz->availablehour,
43 $quiz->availableminute);
44 }
6f797013 45
ccccf04f 46 $quiz->timelimit = round($quiz->timelimit);
47
56b2152f 48 if (empty($quiz->name)) {
49 if (empty($quiz->intro)) {
50 $quiz->name = get_string('modulename', 'quiz');
51 } else {
52 $quiz->name = strip_tags($quiz->intro);
53 }
54 }
55 $quiz->name = trim($quiz->name);
730fd187 56
7bd1aa1d 57 if (!$quiz->id = insert_record("quiz", $quiz)) {
58 return false; // some error occurred
59 }
6f797013 60
34283aa8 61 if (isset($quiz->optionsettingspref)) {
62 set_user_preference('quiz_optionsettingspref', $quiz->optionsettingspref);
63 }
7bd1aa1d 64
565340c6 65 delete_records('event', 'modulename', 'quiz', 'instance', $quiz->id); // Just in case
66
bb52d44c 67 unset($event);
d2f308c0 68 $event->description = $quiz->intro;
69 $event->courseid = $quiz->course;
70 $event->groupid = 0;
71 $event->userid = 0;
72 $event->modulename = 'quiz';
73 $event->instance = $quiz->id;
d2f308c0 74 $event->timestart = $quiz->timeopen;
b71213e3 75 $event->visible = instance_is_visible('quiz', $quiz);
b2a3cd2d 76
bb52d44c 77 if ($quiz->timeclose and $quiz->timeopen) {
78 // we have both a start and an end date
79 $event->eventtype = 'open';
80 $event->timeduration = ($quiz->timeclose - $quiz->timeopen);
81
82 if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Long durations create two events
83
84 $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
85 $event->timeduration = 0;
86 add_event($event);
87
88 $event->timestart = $quiz->timeclose;
89 $event->eventtype = 'close';
90 $event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
91 unset($event->id);
92 add_event($event);
93 } else { // single event with duration
94 $event->name = $quiz->name;
95 add_event($event);
96 }
97 } elseif ($quiz->timeopen) { // only an open date
98 $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
99 $event->eventtype = 'open';
100 $event->timeduration = 0;
101 add_event($event);
102 } elseif ($quiz->timeclose) { // only a closing date
103 $event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
104 $event->timestart = $quiz->timeclose;
105 $event->eventtype = 'close';
106 $event->timeduration = 0;
001996c2 107 add_event($event);
d2f308c0 108 }
b2a3cd2d 109
7bd1aa1d 110 return $quiz->id;
730fd187 111}
112
113
114function quiz_update_instance($quiz) {
f41e824f 115/// Given an object containing all the necessary data,
34283aa8 116/// (defined by the form in mod.html or edit.php) this function
a5e1f35c 117/// will update an existing instance with new data.
730fd187 118
ee1fb969 119 quiz_process_options($quiz);
120
730fd187 121 $quiz->timemodified = time();
bb52d44c 122 // The following is adapted from the assignment module
123 if (empty($quiz->dueenable)) {
124 $quiz->timeclose = 0;
125 } else {
126 $quiz->timeclose = make_timestamp($quiz->dueyear, $quiz->duemonth,
127 $quiz->dueday, $quiz->duehour,
128 $quiz->dueminute);
129 }
130 if (empty($quiz->availableenable)) {
131 $quiz->timeopen = 0;
132 $quiz->preventlate = 0;
133 } else {
134 $quiz->timeopen = make_timestamp($quiz->availableyear, $quiz->availablemonth,
135 $quiz->availableday, $quiz->availablehour,
136 $quiz->availableminute);
34283aa8 137 }
bb52d44c 138
ccccf04f 139 $quiz->timelimit = round($quiz->timelimit);
140
730fd187 141 $quiz->id = $quiz->instance;
142
7bd1aa1d 143 if (!update_record("quiz", $quiz)) {
144 return false; // some error occurred
145 }
730fd187 146
ee1fb969 147 // Delete any preview attempts
148 delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id);
149
34283aa8 150 if (isset($quiz->optionsettingspref)) {
151 set_user_preference('quiz_optionsettingspref', $quiz->optionsettingspref);
152 }
7bd1aa1d 153
78036f78 154 // currently this code deletes all existing events and adds new ones
155 // this should be improved to update existing events only
156 if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
157 foreach($events as $event) {
158 delete_event($event->id);
159 }
160 }
6f797013 161
78036f78 162 unset($event);
565340c6 163 $event->description = $quiz->intro;
164 $event->courseid = $quiz->course;
165 $event->groupid = 0;
166 $event->userid = 0;
167 $event->modulename = 'quiz';
168 $event->instance = $quiz->id;
565340c6 169 $event->timestart = $quiz->timeopen;
ba288539 170 $event->visible = instance_is_visible('quiz', $quiz);
bb52d44c 171 if ($quiz->timeclose and $quiz->timeopen) {
172 // we have both a start and an end date
173 $event->eventtype = 'open';
174 $event->timeduration = ($quiz->timeclose - $quiz->timeopen);
175
176 if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Long durations create two events
177
178 $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
179 $event->timeduration = 0;
180 add_event($event);
181
182 $event->timestart = $quiz->timeclose;
183 $event->eventtype = 'close';
184 $event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
185 unset($event->id);
186 add_event($event);
187 } else { // single event with duration
188 $event->name = $quiz->name;
189 add_event($event);
190 }
191 } elseif ($quiz->timeopen) { // only an open date
78036f78 192 $event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
bb52d44c 193 $event->eventtype = 'open';
194 $event->timeduration = 0;
78036f78 195 add_event($event);
bb52d44c 196 } elseif ($quiz->timeclose) { // only a closing date
197 $event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
78036f78 198 $event->timestart = $quiz->timeclose;
199 $event->eventtype = 'close';
bb52d44c 200 $event->timeduration = 0;
64f36986 201 add_event($event);
d2f308c0 202 }
203
7bd1aa1d 204 return true;
730fd187 205}
206
207
208function quiz_delete_instance($id) {
f41e824f 209/// Given an ID of an instance of this module,
210/// this function will permanently delete the instance
211/// and any data that depends on it.
730fd187 212
213 if (! $quiz = get_record("quiz", "id", "$id")) {
214 return false;
215 }
216
217 $result = true;
218
464edd6d 219 if ($attempts = get_records("quiz_attempts", "quiz", "$quiz->id")) {
10b9291c 220 foreach ($attempts as $attempt) {
03d1753c 221 // TODO: this should use function in questionlib.php
4f48fb42 222 if (! delete_records("question_states", "attempt", "$attempt->uniqueid")) {
d115d8c7 223 $result = false;
224 }
03d1753c 225 if (! delete_records("question_sessions", "attemptid", "$attempt->uniqueid")) {
10b9291c 226 $result = false;
708b521a 227 }
10b9291c 228 }
229 }
230
231 if (! delete_records("quiz_attempts", "quiz", "$quiz->id")) {
232 $result = false;
233 }
234
235 if (! delete_records("quiz_grades", "quiz", "$quiz->id")) {
236 $result = false;
237 }
238
ee1fb969 239 if (! delete_records("quiz_question_instances", "quiz", "$quiz->id")) {
10b9291c 240 $result = false;
241 }
730fd187 242
243 if (! delete_records("quiz", "id", "$quiz->id")) {
244 $result = false;
245 }
246
880d8675 247 $pagetypes = page_import_types('mod/quiz/');
248 foreach($pagetypes as $pagetype) {
249 if(!delete_records('block_instance', 'pageid', $quiz->id, 'pagetype', $pagetype)) {
250 $result = false;
251 }
252 }
253
78036f78 254 if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
255 foreach($events as $event) {
256 delete_event($event->id);
257 }
b2a3cd2d 258 }
259
730fd187 260 return $result;
261}
262
9ea6ac51 263/**
264 * Given a course object, this function will clean up anything that
265 * would be leftover after all the instances were deleted.
266 * In this case, all non-used quiz categories are deleted and
267 * used categories (by other courses) are moved to site course.
268 *
269 * @param object $course an object representing the course to be analysed
270 * @param boolean $feedback to specify if the process must output a summary of its work
271 * @return boolean
272 */
273function quiz_delete_course($course, $feedback=true) {
274
ccccf04f 275 global $CFG, $QTYPES;
9ea6ac51 276
277 //To detect if we have created the "container category"
278 $concatid = 0;
279
280 //The "container" category we'll create if we need if
281 $contcat = new object;
282
283 //To temporary store changes performed with parents
284 $parentchanged = array();
285
286 //To store feedback to be showed at the end of the process
287 $feedbackdata = array();
288
289 //Cache some strings
290 $strcatcontainer=get_string('containercategorycreated', 'quiz');
291 $strcatmoved = get_string('usedcategorymoved', 'quiz');
292 $strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
293
dc1f00de 294 if ($categories = get_records('question_categories', 'course', $course->id, 'parent', 'id, parent, name, course')) {
bbba4759 295 require_once($CFG->dirroot.'/mod/quiz/locallib.php');
9ea6ac51 296 //Sort categories following their tree (parent-child) relationships
297 $categories = sort_categories_by_tree($categories);
298
299 foreach ($categories as $cat) {
300
301 //Get the full record
dc1f00de 302 $category = get_record('question_categories', 'id', $cat->id);
9ea6ac51 303
304 //Check if the category is being used anywhere
305 if($usedbyquiz = quizzes_category_used($category->id,true)) {
306 //It's being used. Cannot delete it, so:
307 //Create a container category in SITEID course if it doesn't exist
308 if (!$concatid) {
309 $concat->course = SITEID;
92b90adf 310 if (!isset($course->shortname)) {
311 $course->shortname = 'id=' . $course->id;
9ea6ac51 312 }
313 $concat->name = get_string('savedfromdeletedcourse', 'quiz', $course->shortname);
314 $concat->info = $concat->name;
315 $concat->publish = 1;
316 $concat->stamp = make_unique_id_code();
dc1f00de 317 $concatid = insert_record('question_categories', $concat);
9ea6ac51 318
319 //Fill feedback
708b521a 320 $feedbackdata[] = array($concat->name, $strcatcontainer);
9ea6ac51 321 }
322 //Move the category to the container category in SITEID course
323 $category->course = SITEID;
324 //Assign to container if the category hasn't parent or if the parent is wrong (not belongs to the course)
708b521a 325 if (!$category->parent || !isset($categories[$category->parent])) {
9ea6ac51 326 $category->parent = $concatid;
327 }
328 //If it's being used, its publish field should be 1
329 $category->publish = 1;
330 //Let's update it
dc1f00de 331 update_record('question_categories', $category);
9ea6ac51 332
333 //Save this parent change for future use
334 $parentchanged[$category->id] = $category->parent;
335
336 //Fill feedback
708b521a 337 $feedbackdata[] = array($category->name, $strcatmoved);
9ea6ac51 338
339 } else {
340 //Category isn't being used so:
341 //Delete it completely (questions and category itself)
cd00e988 342 //deleting questions
4f48fb42 343 if ($questions = get_records("question", "category", $category->id)) {
9ea6ac51 344 foreach ($questions as $question) {
4f48fb42 345 delete_question($question);
9ea6ac51 346 }
4f48fb42 347 delete_records("question", "category", $category->id);
9ea6ac51 348 }
708b521a 349 //delete the category
dc1f00de 350 delete_records('question_categories', 'id', $category->id);
9ea6ac51 351
352 //Save this parent change for future use
353 if (!empty($category->parent)) {
354 $parentchanged[$category->id] = $category->parent;
355 } else {
356 $parentchanged[$category->id] = $concatid;
b2d594c8 357 }
9ea6ac51 358
359 //Update all its child categories to re-parent them to grandparent.
dc1f00de 360 set_field ('question_categories', 'parent', $parentchanged[$category->id], 'parent', $category->id);
9ea6ac51 361
362 //Fill feedback
708b521a 363 $feedbackdata[] = array($category->name, $strcatdeleted);
b2d594c8 364 }
365 }
9ea6ac51 366 //Inform about changes performed if feedback is enabled
367 if ($feedback) {
368 $table->head = array(get_string('category','quiz'), get_string('action'));
369 $table->data = $feedbackdata;
370 print_table($table);
371 }
b2d594c8 372 }
373 return true;
374}
375
376
730fd187 377function quiz_user_outline($course, $user, $mod, $quiz) {
f41e824f 378/// Return a small object with summary information about what a
a5e1f35c 379/// user has done with a given particular instance of this module
380/// Used for user activity reports.
381/// $return->time = the time they did it
382/// $return->info = a short text description
9d677360 383 if ($grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
f41e824f 384
090cf95a 385 if ((float)$grade->grade) {
1105b32d 386 $result->info = get_string('grade').':&nbsp;'.round($grade->grade, $quiz->decimalpoints);
98092498 387 }
388 $result->time = $grade->timemodified;
389 return $result;
390 }
391 return NULL;
730fd187 392
730fd187 393}
394
ee1fb969 395
730fd187 396function quiz_user_complete($course, $user, $mod, $quiz) {
f41e824f 397/// Print a detailed representation of what a user has done with
a5e1f35c 398/// a given particular instance of this module, for user activity reports.
730fd187 399
ee1fb969 400 if ($attempts = get_records_select('quiz_attempts', "userid='$user->id' AND quiz='$quiz->id'", 'attempt ASC')) {
8779eab5 401 if ($quiz->grade and $quiz->sumgrades && $grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
1105b32d 402 echo get_string('grade').': '.round($grade->grade, $quiz->decimalpoints).'/'.$quiz->grade.'<br />';
ee1fb969 403 }
404 foreach ($attempts as $attempt) {
405 echo get_string('attempt', 'quiz').' '.$attempt->attempt.': ';
406 if ($attempt->timefinish == 0) {
407 print_string('unfinished');
408 } else {
1105b32d 409 echo round($attempt->sumgrades, $quiz->decimalpoints).'/'.$quiz->sumgrades;
ee1fb969 410 }
411 echo ' - '.userdate($attempt->timemodified).'<br />';
412 }
413 } else {
414 print_string('noattempts', 'quiz');
415 }
416
730fd187 417 return true;
418}
419
ee1fb969 420
730fd187 421function quiz_cron () {
a5e1f35c 422/// Function to be run periodically according to the moodle cron
f41e824f 423/// This function searches for things that need to be done, such
424/// as sending out mail, toggling flags etc ...
730fd187 425
426 global $CFG;
427
428 return true;
429}
430
d0ac6bc2 431function quiz_grades($quizid) {
858deff0 432/// Must return an array of grades, indexed by user, and a max grade.
433
d5838a4b 434 $quiz = get_record('quiz', 'id', intval($quizid));
435 if (empty($quiz) || empty($quiz->grade)) {
ed1daaa9 436 return NULL;
437 }
438
d5838a4b 439 $return->grades = get_records_menu('quiz_grades', 'quiz', $quiz->id, '', 'userid, grade');
440 $return->maxgrade = get_field('quiz', 'grade', 'id', $quiz->id);
858deff0 441 return $return;
d0ac6bc2 442}
443
d061d883 444function quiz_get_participants($quizid) {
445/// Returns an array of users who have data in a given quiz
e4acc4ce 446/// (users with records in quiz_attempts and quiz_question_versions)
d061d883 447
448 global $CFG;
449
e4acc4ce 450 //Get users from attempts
451 $us_attempts = get_records_sql("SELECT DISTINCT u.id, u.id
452 FROM {$CFG->prefix}user u,
453 {$CFG->prefix}quiz_attempts a
454 WHERE a.quiz = '$quizid' and
455 u.id = a.userid");
456
457 //Get users from question_versions
458 $us_versions = get_records_sql("SELECT DISTINCT u.id, u.id
459 FROM {$CFG->prefix}user u,
19098759 460 {$CFG->prefix}quiz_question_versions v
e4acc4ce 461 WHERE v.quiz = '$quizid' and
462 u.id = v.userid");
463
464 //Add us_versions to us_attempts
465 if ($us_versions) {
466 foreach ($us_versions as $us_version) {
467 $us_attempts[$us_version->id] = $us_version;
468 }
469 }
470 //Return us_attempts array (it contains an array of unique users)
471 return ($us_attempts);
472
d061d883 473}
730fd187 474
d2f308c0 475function quiz_refresh_events($courseid = 0) {
476// This standard function will check all instances of this module
477// and make sure there are up-to-date events created for each of them.
b2a3cd2d 478// If courseid = 0, then every quiz event in the site is checked, else
479// only quiz events belonging to the course specified are checked.
d2f308c0 480// This function is used, in its new format, by restore_refresh_events()
481
482 if ($courseid == 0) {
483 if (! $quizzes = get_records("quiz")) {
484 return true;
485 }
486 } else {
487 if (! $quizzes = get_records("quiz", "course", $courseid)) {
488 return true;
489 }
490 }
dcd338ff 491 $moduleid = get_field('modules', 'id', 'name', 'quiz');
f41e824f 492
d2f308c0 493 foreach ($quizzes as $quiz) {
494 $event = NULL;
b2a3cd2d 495 $event2 = NULL;
496 $event2old = NULL;
497
498 if ($events = get_records_select('event', "modulename = 'quiz' AND instance = '$quiz->id' ORDER BY timestart")) {
499 $event = array_shift($events);
500 if (!empty($events)) {
501 $event2old = array_shift($events);
502 if (!empty($events)) {
503 foreach ($events as $badevent) {
504 delete_records('event', 'id', $badevent->id);
505 }
506 }
507 }
508 }
509
d2f308c0 510 $event->name = addslashes($quiz->name);
511 $event->description = addslashes($quiz->intro);
b2a3cd2d 512 $event->courseid = $quiz->course;
513 $event->groupid = 0;
514 $event->userid = 0;
515 $event->modulename = 'quiz';
516 $event->instance = $quiz->id;
ba288539 517 $event->visible = instance_is_visible('quiz', $quiz);
d2f308c0 518 $event->timestart = $quiz->timeopen;
b2a3cd2d 519 $event->eventtype = 'open';
d2f308c0 520 $event->timeduration = ($quiz->timeclose - $quiz->timeopen);
d2f308c0 521
b2a3cd2d 522 if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Set up two events
d2f308c0 523
b2a3cd2d 524 $event2 = $event;
d2f308c0 525
b2a3cd2d 526 $event->name = addslashes($quiz->name).' ('.get_string('quizopens', 'quiz').')';
527 $event->timeduration = 0;
528
529 $event2->name = addslashes($quiz->name).' ('.get_string('quizcloses', 'quiz').')';
530 $event2->timestart = $quiz->timeclose;
531 $event2->eventtype = 'close';
532 $event2->timeduration = 0;
533
534 if (empty($event2old->id)) {
535 unset($event2->id);
536 add_event($event2);
537 } else {
538 $event2->id = $event2old->id;
539 update_event($event2);
540 }
541 } else if (!empty($event2->id)) {
542 delete_event($event2->id);
543 }
544
545 if (empty($event->id)) {
d2f308c0 546 add_event($event);
b2a3cd2d 547 } else {
548 update_event($event);
d2f308c0 549 }
b2a3cd2d 550
d2f308c0 551 }
552 return true;
553}
554
6710ec87 555
556function quiz_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $quiz="0", $user="", $groupid="") {
557// Returns all quizzes since a given time. If quiz is specified then
558// this restricts the results
559
560 global $CFG;
561
562 if ($quiz) {
563 $quizselect = " AND cm.id = '$quiz'";
564 } else {
565 $quizselect = "";
566 }
567 if ($user) {
568 $userselect = " AND u.id = '$user'";
569 } else {
570 $userselect = "";
571 }
572
573 $quizzes = get_records_sql("SELECT qa.*, q.name, u.firstname, u.lastname, u.picture,
574 q.course, q.sumgrades as maxgrade, cm.instance, cm.section
575 FROM {$CFG->prefix}quiz_attempts qa,
576 {$CFG->prefix}quiz q,
577 {$CFG->prefix}user u,
578 {$CFG->prefix}course_modules cm
579 WHERE qa.timefinish > '$sincetime'
580 AND qa.userid = u.id $userselect
581 AND qa.quiz = q.id $quizselect
582 AND cm.instance = q.id
583 AND cm.course = '$courseid'
584 AND q.course = cm.course
585 ORDER BY qa.timefinish ASC");
586
587 if (empty($quizzes))
588 return;
589
590 foreach ($quizzes as $quiz) {
591 if (empty($groupid) || ismember($groupid, $quiz->userid)) {
592
ac21ad39 593 $tmpactivity = new Object;
594
6710ec87 595 $tmpactivity->type = "quiz";
596 $tmpactivity->defaultindex = $index;
597 $tmpactivity->instance = $quiz->quiz;
598
599 $tmpactivity->name = $quiz->name;
600 $tmpactivity->section = $quiz->section;
601
602 $tmpactivity->content->attemptid = $quiz->id;
603 $tmpactivity->content->sumgrades = $quiz->sumgrades;
604 $tmpactivity->content->maxgrade = $quiz->maxgrade;
605 $tmpactivity->content->attempt = $quiz->attempt;
606
607 $tmpactivity->user->userid = $quiz->userid;
608 $tmpactivity->user->fullname = fullname($quiz);
609 $tmpactivity->user->picture = $quiz->picture;
610
611 $tmpactivity->timestamp = $quiz->timefinish;
612
613 $activities[] = $tmpactivity;
614
615 $index++;
616 }
617 }
618
619 return;
620}
621
622
623function quiz_print_recent_mod_activity($activity, $course, $detail=false) {
6eaae5bd 624 global $CFG;
6710ec87 625
626 echo '<table border="0" cellpadding="3" cellspacing="0">';
627
6f797013 628 echo "<tr><td class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
6710ec87 629 print_user_picture($activity->user->userid, $course, $activity->user->picture);
630 echo "</td><td width=\"100%\"><font size=\"2\">";
631
632 if ($detail) {
633 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
634 "height=\"16\" width=\"16\" alt=\"$activity->type\" /> ";
635 echo "<a href=\"$CFG->wwwroot/mod/quiz/view.php?id=" . $activity->instance . "\">"
95c67e2f 636 . format_string($activity->name,true) . "</a> - ";
6710ec87 637
638 }
639
640 if (isteacher($course)) {
641 $grades = "(" . $activity->content->sumgrades . " / " . $activity->content->maxgrade . ") ";
642 echo "<a href=\"$CFG->wwwroot/mod/quiz/review.php?q="
643 . $activity->instance . "&amp;attempt="
644 . $activity->content->attemptid . "\">" . $grades . "</a> ";
645
646 echo get_string("attempt", "quiz") . " - " . $activity->content->attempt . "<br />";
647 }
648 echo "<a href=\"$CFG->wwwroot/user/view.php?id="
649 . $activity->user->userid . "&amp;course=$course\">"
650 . $activity->user->fullname . "</a> ";
651
652 echo " - " . userdate($activity->timestamp);
653
654 echo "</font></td></tr>";
655 echo "</table>";
656
657 return;
658}
659
ee1fb969 660/**
661* Pre-process the options form data
662*
663* Encode the review options from the setup form into the bits of $form->review
664* and other options into $form->optionflags
665* The form data is passed by reference and modified by this function
666* @return integer
667* @param object $form The variables set on the form.
668*/
669function quiz_process_options(&$form) {
670 $optionflags = 0;
671 $review = 0;
672
673 if (!empty($form->adaptive)) {
6b11a0e8 674 $optionflags |= QUESTION_ADAPTIVE;
ee1fb969 675 }
676
677 if (isset($form->responsesimmediately)) {
678 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY);
679 unset($form->responsesimmediately);
680 }
681 if (isset($form->responsesopen)) {
682 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN);
683 unset($form->responsesopen);
684 }
685 if (isset($form->responsesclosed)) {
686 $review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED);
687 unset($form->responsesclosed);
688 }
689
690 if (isset($form->scoreimmediately)) {
691 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY);
692 unset($form->scoreimmediately);
693 }
694 if (isset($form->scoreopen)) {
695 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN);
696 unset($form->scoreopen);
697 }
698 if (isset($form->scoreclosed)) {
699 $review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED);
700 unset($form->scoreclosed);
701 }
702
703 if (isset($form->feedbackimmediately)) {
704 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
705 unset($form->feedbackimmediately);
706 }
707 if (isset($form->feedbackopen)) {
708 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN);
709 unset($form->feedbackopen);
710 }
711 if (isset($form->feedbackclosed)) {
712 $review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED);
713 unset($form->feedbackclosed);
714 }
715
716 if (isset($form->answersimmediately)) {
717 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY);
718 unset($form->answersimmediately);
719 }
720 if (isset($form->answersopen)) {
721 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN);
722 unset($form->answersopen);
723 }
724 if (isset($form->answersclosed)) {
725 $review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED);
726 unset($form->answersclosed);
727 }
728
729 if (isset($form->solutionsimmediately)) {
730 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_IMMEDIATELY);
731 unset($form->solutionsimmediately);
732 }
733 if (isset($form->solutionsopen)) {
734 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_OPEN);
735 unset($form->solutionsopen);
736 }
737 if (isset($form->solutionsclosed)) {
738 $review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_CLOSED);
739 unset($form->solutionsclosed);
740 }
741
742 $form->review = $review;
743 $form->optionflags = $optionflags;
c0bbeb67 744
745 // The following implements the time limit check box
746 if (empty($form->timelimitenable) or $form->timelimit < 1) {
747 $form->timelimit = 0;
748 }
ee1fb969 749
750}
751
f3221af9 752function quiz_get_view_actions() {
753 return array('view','view all','report');
754}
ee1fb969 755
f3221af9 756function quiz_get_post_actions() {
757 return array('attempt','editquestions','review','submit');
758}
ee1fb969 759
730fd187 760?>