2004-09-12 17:34:35 +00:00
|
|
|
<?php // $Id$
|
2005-05-06 06:24:04 +00:00
|
|
|
/**
|
|
|
|
* Library of functions for the quiz module.
|
|
|
|
*
|
|
|
|
* This contains functions that are called also from outside the quiz module
|
|
|
|
* Functions that are only called by the quiz module itself are in {@link locallib.php}
|
|
|
|
* @version $Id$
|
|
|
|
* @author Martin Dougiamas and many others.
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
* @package quiz
|
|
|
|
*/
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2005-02-28 04:23:52 +00:00
|
|
|
require_once($CFG->libdir.'/pagelib.php');
|
2005-07-06 06:44:18 +00:00
|
|
|
require_once($CFG->dirroot.'/mod/quiz/constants.php');
|
2004-07-21 13:01:08 +00:00
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
|
2002-10-13 07:17:48 +00:00
|
|
|
/// FUNCTIONS ///////////////////////////////////////////////////////////////////
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
function quiz_add_instance($quiz) {
|
2004-06-02 18:03:05 +00:00
|
|
|
/// Given an object containing all the necessary data,
|
|
|
|
/// (defined by the form in mod.html) this function
|
|
|
|
/// will create a new instance and return the id number
|
2002-10-13 07:17:48 +00:00
|
|
|
/// of the new instance.
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
quiz_process_options($quiz);
|
|
|
|
|
2002-10-15 16:22:18 +00:00
|
|
|
$quiz->created = time();
|
2002-10-04 02:59:05 +00:00
|
|
|
$quiz->timemodified = time();
|
2005-07-07 17:16:56 +00:00
|
|
|
// The following is adapted from the assignment module
|
|
|
|
if (empty($quiz->dueenable)) {
|
|
|
|
$quiz->timeclose = 0;
|
|
|
|
} else {
|
|
|
|
$quiz->timeclose = make_timestamp($quiz->dueyear, $quiz->duemonth,
|
|
|
|
$quiz->dueday, $quiz->duehour,
|
|
|
|
$quiz->dueminute);
|
|
|
|
}
|
|
|
|
if (empty($quiz->availableenable)) {
|
|
|
|
$quiz->timeopen = 0;
|
|
|
|
$quiz->preventlate = 0;
|
|
|
|
} else {
|
|
|
|
$quiz->timeopen = make_timestamp($quiz->availableyear, $quiz->availablemonth,
|
|
|
|
$quiz->availableday, $quiz->availablehour,
|
|
|
|
$quiz->availableminute);
|
|
|
|
}
|
2005-02-05 17:28:06 +00:00
|
|
|
|
2005-01-22 18:17:33 +00:00
|
|
|
if (empty($quiz->name)) {
|
|
|
|
if (empty($quiz->intro)) {
|
|
|
|
$quiz->name = get_string('modulename', 'quiz');
|
|
|
|
} else {
|
|
|
|
$quiz->name = strip_tags($quiz->intro);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$quiz->name = trim($quiz->name);
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
if (!$quiz->id = insert_record("quiz", $quiz)) {
|
|
|
|
return false; // some error occurred
|
|
|
|
}
|
2005-02-05 17:28:06 +00:00
|
|
|
|
2005-01-02 07:15:19 +00:00
|
|
|
if (isset($quiz->optionsettingspref)) {
|
|
|
|
set_user_preference('quiz_optionsettingspref', $quiz->optionsettingspref);
|
|
|
|
}
|
2002-10-14 12:21:18 +00:00
|
|
|
|
2004-05-23 10:25:46 +00:00
|
|
|
delete_records('event', 'modulename', 'quiz', 'instance', $quiz->id); // Just in case
|
|
|
|
|
2005-07-07 17:16:56 +00:00
|
|
|
unset($event);
|
2004-04-28 02:56:40 +00:00
|
|
|
$event->description = $quiz->intro;
|
|
|
|
$event->courseid = $quiz->course;
|
|
|
|
$event->groupid = 0;
|
|
|
|
$event->userid = 0;
|
|
|
|
$event->modulename = 'quiz';
|
|
|
|
$event->instance = $quiz->id;
|
|
|
|
$event->timestart = $quiz->timeopen;
|
2004-08-08 15:08:50 +00:00
|
|
|
$event->visible = instance_is_visible('quiz', $quiz);
|
2004-05-17 17:04:01 +00:00
|
|
|
|
2005-07-07 17:16:56 +00:00
|
|
|
if ($quiz->timeclose and $quiz->timeopen) {
|
|
|
|
// we have both a start and an end date
|
|
|
|
$event->eventtype = 'open';
|
|
|
|
$event->timeduration = ($quiz->timeclose - $quiz->timeopen);
|
|
|
|
|
|
|
|
if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Long durations create two events
|
|
|
|
|
|
|
|
$event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
|
|
|
|
$event->timeduration = 0;
|
|
|
|
add_event($event);
|
|
|
|
|
|
|
|
$event->timestart = $quiz->timeclose;
|
|
|
|
$event->eventtype = 'close';
|
|
|
|
$event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
|
|
|
|
unset($event->id);
|
|
|
|
add_event($event);
|
|
|
|
} else { // single event with duration
|
|
|
|
$event->name = $quiz->name;
|
|
|
|
add_event($event);
|
|
|
|
}
|
|
|
|
} elseif ($quiz->timeopen) { // only an open date
|
|
|
|
$event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
|
|
|
|
$event->eventtype = 'open';
|
|
|
|
$event->timeduration = 0;
|
|
|
|
add_event($event);
|
|
|
|
} elseif ($quiz->timeclose) { // only a closing date
|
|
|
|
$event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
|
|
|
|
$event->timestart = $quiz->timeclose;
|
|
|
|
$event->eventtype = 'close';
|
|
|
|
$event->timeduration = 0;
|
2004-04-28 02:56:40 +00:00
|
|
|
}
|
2004-05-17 17:04:01 +00:00
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
return $quiz->id;
|
2002-10-04 02:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function quiz_update_instance($quiz) {
|
2004-06-02 18:03:05 +00:00
|
|
|
/// Given an object containing all the necessary data,
|
2005-01-02 07:15:19 +00:00
|
|
|
/// (defined by the form in mod.html or edit.php) this function
|
2002-10-13 07:17:48 +00:00
|
|
|
/// will update an existing instance with new data.
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
quiz_process_options($quiz);
|
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
$quiz->timemodified = time();
|
2005-07-07 17:16:56 +00:00
|
|
|
// The following is adapted from the assignment module
|
|
|
|
if (empty($quiz->dueenable)) {
|
|
|
|
$quiz->timeclose = 0;
|
|
|
|
} else {
|
|
|
|
$quiz->timeclose = make_timestamp($quiz->dueyear, $quiz->duemonth,
|
|
|
|
$quiz->dueday, $quiz->duehour,
|
|
|
|
$quiz->dueminute);
|
|
|
|
}
|
|
|
|
if (empty($quiz->availableenable)) {
|
|
|
|
$quiz->timeopen = 0;
|
|
|
|
$quiz->preventlate = 0;
|
|
|
|
} else {
|
|
|
|
$quiz->timeopen = make_timestamp($quiz->availableyear, $quiz->availablemonth,
|
|
|
|
$quiz->availableday, $quiz->availablehour,
|
|
|
|
$quiz->availableminute);
|
2005-01-02 07:15:19 +00:00
|
|
|
}
|
2005-07-07 17:16:56 +00:00
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
$quiz->id = $quiz->instance;
|
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
if (!update_record("quiz", $quiz)) {
|
|
|
|
return false; // some error occurred
|
|
|
|
}
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
// Delete any preview attempts
|
|
|
|
delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id);
|
|
|
|
|
2005-01-02 07:15:19 +00:00
|
|
|
if (isset($quiz->optionsettingspref)) {
|
|
|
|
set_user_preference('quiz_optionsettingspref', $quiz->optionsettingspref);
|
|
|
|
}
|
2002-10-14 12:21:18 +00:00
|
|
|
|
2004-12-23 07:28:14 +00:00
|
|
|
// currently this code deletes all existing events and adds new ones
|
|
|
|
// this should be improved to update existing events only
|
|
|
|
if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
|
|
|
|
foreach($events as $event) {
|
|
|
|
delete_event($event->id);
|
|
|
|
}
|
|
|
|
}
|
2005-02-05 17:28:06 +00:00
|
|
|
|
2004-12-23 07:28:14 +00:00
|
|
|
unset($event);
|
2004-05-23 10:25:46 +00:00
|
|
|
$event->description = $quiz->intro;
|
|
|
|
$event->courseid = $quiz->course;
|
|
|
|
$event->groupid = 0;
|
|
|
|
$event->userid = 0;
|
|
|
|
$event->modulename = 'quiz';
|
|
|
|
$event->instance = $quiz->id;
|
|
|
|
$event->timestart = $quiz->timeopen;
|
2004-08-08 15:09:39 +00:00
|
|
|
$event->visible = instance_is_visible('quiz', $quiz);
|
2005-07-07 17:16:56 +00:00
|
|
|
if ($quiz->timeclose and $quiz->timeopen) {
|
|
|
|
// we have both a start and an end date
|
|
|
|
$event->eventtype = 'open';
|
|
|
|
$event->timeduration = ($quiz->timeclose - $quiz->timeopen);
|
|
|
|
|
|
|
|
if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Long durations create two events
|
|
|
|
|
|
|
|
$event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
|
|
|
|
$event->timeduration = 0;
|
|
|
|
add_event($event);
|
|
|
|
|
|
|
|
$event->timestart = $quiz->timeclose;
|
|
|
|
$event->eventtype = 'close';
|
|
|
|
$event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
|
|
|
|
unset($event->id);
|
|
|
|
add_event($event);
|
|
|
|
} else { // single event with duration
|
|
|
|
$event->name = $quiz->name;
|
|
|
|
add_event($event);
|
|
|
|
}
|
|
|
|
} elseif ($quiz->timeopen) { // only an open date
|
2004-12-23 07:28:14 +00:00
|
|
|
$event->name = $quiz->name.' ('.get_string('quizopens', 'quiz').')';
|
2005-07-07 17:16:56 +00:00
|
|
|
$event->eventtype = 'open';
|
|
|
|
$event->timeduration = 0;
|
2004-12-23 07:28:14 +00:00
|
|
|
add_event($event);
|
2005-07-07 17:16:56 +00:00
|
|
|
} elseif ($quiz->timeclose) { // only a closing date
|
|
|
|
$event->name = $quiz->name.' ('.get_string('quizcloses', 'quiz').')';
|
2004-12-23 07:28:14 +00:00
|
|
|
$event->timestart = $quiz->timeclose;
|
|
|
|
$event->eventtype = 'close';
|
2005-07-07 17:16:56 +00:00
|
|
|
$event->timeduration = 0;
|
2004-04-28 02:56:40 +00:00
|
|
|
}
|
|
|
|
|
2002-10-14 12:21:18 +00:00
|
|
|
return true;
|
2002-10-04 02:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function quiz_delete_instance($id) {
|
2004-06-02 18:03:05 +00:00
|
|
|
/// Given an ID of an instance of this module,
|
|
|
|
/// this function will permanently delete the instance
|
|
|
|
/// and any data that depends on it.
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
if (! $quiz = get_record("quiz", "id", "$id")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = true;
|
|
|
|
|
2004-04-07 23:38:48 +00:00
|
|
|
if ($attempts = get_records("quiz_attempts", "quiz", "$quiz->id")) {
|
2002-10-14 15:57:33 +00:00
|
|
|
foreach ($attempts as $attempt) {
|
2005-07-02 18:14:51 +00:00
|
|
|
if (! delete_records("quiz_states", "attempt", "$attempt->uniqueid")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
if (! delete_records("quiz_newest_states", "attemptid", "$attempt->uniqueid")) {
|
2002-10-14 15:57:33 +00:00
|
|
|
$result = false;
|
|
|
|
}
|
2005-11-01 03:29:58 +00:00
|
|
|
if (! delete_records("quiz_newest_states", "attemptid", "$attempt->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
2002-10-14 15:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! delete_records("quiz_attempts", "quiz", "$quiz->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! delete_records("quiz_grades", "quiz", "$quiz->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
if (! delete_records("quiz_question_instances", "quiz", "$quiz->id")) {
|
2002-10-14 15:57:33 +00:00
|
|
|
$result = false;
|
|
|
|
}
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
if (! delete_records("quiz", "id", "$quiz->id")) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
|
2005-02-28 04:18:13 +00:00
|
|
|
$pagetypes = page_import_types('mod/quiz/');
|
|
|
|
foreach($pagetypes as $pagetype) {
|
|
|
|
if(!delete_records('block_instance', 'pageid', $quiz->id, 'pagetype', $pagetype)) {
|
|
|
|
$result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-23 07:28:14 +00:00
|
|
|
if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
|
|
|
|
foreach($events as $event) {
|
|
|
|
delete_event($event->id);
|
|
|
|
}
|
2004-05-17 17:04:01 +00:00
|
|
|
}
|
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2005-05-31 22:35:04 +00:00
|
|
|
/**
|
|
|
|
* Given a course object, this function will clean up anything that
|
|
|
|
* would be leftover after all the instances were deleted.
|
|
|
|
* In this case, all non-used quiz categories are deleted and
|
|
|
|
* used categories (by other courses) are moved to site course.
|
|
|
|
*
|
|
|
|
* @param object $course an object representing the course to be analysed
|
|
|
|
* @param boolean $feedback to specify if the process must output a summary of its work
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function quiz_delete_course($course, $feedback=true) {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
//To detect if we have created the "container category"
|
|
|
|
$concatid = 0;
|
|
|
|
|
|
|
|
//The "container" category we'll create if we need if
|
|
|
|
$contcat = new object;
|
|
|
|
|
|
|
|
//To temporary store changes performed with parents
|
|
|
|
$parentchanged = array();
|
|
|
|
|
|
|
|
//To store feedback to be showed at the end of the process
|
|
|
|
$feedbackdata = array();
|
|
|
|
|
|
|
|
//Cache some strings
|
|
|
|
$strcatcontainer=get_string('containercategorycreated', 'quiz');
|
|
|
|
$strcatmoved = get_string('usedcategorymoved', 'quiz');
|
|
|
|
$strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
|
|
|
|
|
|
|
|
if ($categories = get_records('quiz_categories', 'course', $course->id, 'parent', 'id, parent, name, course')) {
|
|
|
|
require_once("locallib.php");
|
|
|
|
//Sort categories following their tree (parent-child) relationships
|
|
|
|
$categories = sort_categories_by_tree($categories);
|
|
|
|
|
|
|
|
foreach ($categories as $cat) {
|
|
|
|
|
|
|
|
//Get the full record
|
|
|
|
$category = get_record('quiz_categories', 'id', $cat->id);
|
|
|
|
|
|
|
|
//Check if the category is being used anywhere
|
|
|
|
if($usedbyquiz = quizzes_category_used($category->id,true)) {
|
|
|
|
//It's being used. Cannot delete it, so:
|
|
|
|
//Create a container category in SITEID course if it doesn't exist
|
|
|
|
if (!$concatid) {
|
|
|
|
$concat->course = SITEID;
|
2005-06-04 15:42:01 +00:00
|
|
|
if (!isset($course->shortname)) {
|
|
|
|
$course->shortname = 'id=' . $course->id;
|
2005-05-31 22:35:04 +00:00
|
|
|
}
|
|
|
|
$concat->name = get_string('savedfromdeletedcourse', 'quiz', $course->shortname);
|
|
|
|
$concat->info = $concat->name;
|
|
|
|
$concat->publish = 1;
|
|
|
|
$concat->stamp = make_unique_id_code();
|
|
|
|
$concatid = insert_record('quiz_categories', $concat);
|
|
|
|
|
|
|
|
//Fill feedback
|
2005-11-01 03:29:58 +00:00
|
|
|
$feedbackdata[] = array($concat->name, $strcatcontainer);
|
2005-05-31 22:35:04 +00:00
|
|
|
}
|
|
|
|
//Move the category to the container category in SITEID course
|
|
|
|
$category->course = SITEID;
|
|
|
|
//Assign to container if the category hasn't parent or if the parent is wrong (not belongs to the course)
|
2005-11-01 03:29:58 +00:00
|
|
|
if (!$category->parent || !isset($categories[$category->parent])) {
|
2005-05-31 22:35:04 +00:00
|
|
|
$category->parent = $concatid;
|
|
|
|
}
|
|
|
|
//If it's being used, its publish field should be 1
|
|
|
|
$category->publish = 1;
|
|
|
|
//Let's update it
|
|
|
|
update_record('quiz_categories', $category);
|
|
|
|
|
|
|
|
//Save this parent change for future use
|
|
|
|
$parentchanged[$category->id] = $category->parent;
|
|
|
|
|
|
|
|
//Fill feedback
|
2005-11-01 03:29:58 +00:00
|
|
|
$feedbackdata[] = array($category->name, $strcatmoved);
|
2005-05-31 22:35:04 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
//Category isn't being used so:
|
|
|
|
//Delete it completely (questions and category itself)
|
|
|
|
//deleting questions....not perfect until implemented in question classes (see bug 3366)
|
|
|
|
if ($questions = get_records("quiz_questions", "category", $category->id)) {
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
delete_records("quiz_answers", "question", $question->id);
|
|
|
|
delete_records("quiz_match", "question", $question->id);
|
|
|
|
delete_records("quiz_match_sub", "question", $question->id);
|
|
|
|
delete_records("quiz_multianswers", "question", $question->id);
|
|
|
|
delete_records("quiz_multichoice", "question", $question->id);
|
|
|
|
delete_records("quiz_numerical", "question", $question->id);
|
|
|
|
delete_records("quiz_numerical_units", "question", $question->id);
|
|
|
|
delete_records("quiz_calculated", "question", $question->id);
|
2005-09-19 00:25:11 +00:00
|
|
|
delete_records("quiz_question_datasets", "question", $question->id);
|
2005-05-31 22:35:04 +00:00
|
|
|
delete_records("quiz_randomsamatch", "question", $question->id);
|
|
|
|
delete_records("quiz_shortanswer", "question", $question->id);
|
|
|
|
delete_records("quiz_truefalse", "question", $question->id);
|
|
|
|
delete_records("quiz_rqp", "question", $question->id);
|
2005-09-19 22:35:03 +00:00
|
|
|
delete_records("quiz_essay", "question", $question->id);
|
2005-05-31 22:35:04 +00:00
|
|
|
delete_records("quiz_states", "question", $question->id);
|
2005-09-19 00:25:11 +00:00
|
|
|
delete_records("quiz_newest_states", "questionid", $question->id);
|
2005-05-31 22:35:04 +00:00
|
|
|
delete_records("quiz_question_versions", "oldquestion", $question->id);
|
|
|
|
delete_records("quiz_question_versions", "newquestion", $question->id);
|
|
|
|
}
|
|
|
|
delete_records("quiz_questions", "category", $category->id);
|
|
|
|
}
|
2005-11-01 03:29:58 +00:00
|
|
|
//delete the category
|
2005-05-31 22:35:04 +00:00
|
|
|
delete_records('quiz_categories', 'id', $category->id);
|
|
|
|
|
|
|
|
//Save this parent change for future use
|
|
|
|
if (!empty($category->parent)) {
|
|
|
|
$parentchanged[$category->id] = $category->parent;
|
|
|
|
} else {
|
|
|
|
$parentchanged[$category->id] = $concatid;
|
2004-01-21 16:44:22 +00:00
|
|
|
}
|
2005-05-31 22:35:04 +00:00
|
|
|
|
|
|
|
//Update all its child categories to re-parent them to grandparent.
|
|
|
|
set_field ('quiz_categories', 'parent', $parentchanged[$category->id], 'parent', $category->id);
|
|
|
|
|
|
|
|
//Fill feedback
|
2005-11-01 03:29:58 +00:00
|
|
|
$feedbackdata[] = array($category->name, $strcatdeleted);
|
2004-01-21 16:44:22 +00:00
|
|
|
}
|
|
|
|
}
|
2005-05-31 22:35:04 +00:00
|
|
|
//Inform about changes performed if feedback is enabled
|
|
|
|
if ($feedback) {
|
|
|
|
$table->head = array(get_string('category','quiz'), get_string('action'));
|
|
|
|
$table->data = $feedbackdata;
|
|
|
|
print_table($table);
|
|
|
|
}
|
2004-01-21 16:44:22 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
function quiz_user_outline($course, $user, $mod, $quiz) {
|
2004-06-02 18:03:05 +00:00
|
|
|
/// Return a small object with summary information about what a
|
2002-10-13 07:17:48 +00:00
|
|
|
/// user has done with a given particular instance of this module
|
|
|
|
/// Used for user activity reports.
|
|
|
|
/// $return->time = the time they did it
|
|
|
|
/// $return->info = a short text description
|
2005-01-27 02:28:04 +00:00
|
|
|
if ($grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
|
2004-06-02 18:03:05 +00:00
|
|
|
|
2005-02-18 14:42:04 +00:00
|
|
|
if ((float)$grade->grade) {
|
2005-05-07 13:48:25 +00:00
|
|
|
$result->info = get_string('grade').': '.round($grade->grade, $quiz->decimalpoints);
|
2002-10-17 08:28:18 +00:00
|
|
|
}
|
|
|
|
$result->time = $grade->timemodified;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
return NULL;
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
function quiz_user_complete($course, $user, $mod, $quiz) {
|
2004-06-02 18:03:05 +00:00
|
|
|
/// Print a detailed representation of what a user has done with
|
2002-10-13 07:17:48 +00:00
|
|
|
/// a given particular instance of this module, for user activity reports.
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
if ($attempts = get_records_select('quiz_attempts', "userid='$user->id' AND quiz='$quiz->id'", 'attempt ASC')) {
|
|
|
|
if ($quiz->grade != 0 && $grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
|
2005-05-07 13:48:25 +00:00
|
|
|
echo get_string('grade').': '.round($grade->grade, $quiz->decimalpoints).'/'.$quiz->grade.'<br />';
|
2005-05-06 06:24:04 +00:00
|
|
|
}
|
|
|
|
foreach ($attempts as $attempt) {
|
|
|
|
echo get_string('attempt', 'quiz').' '.$attempt->attempt.': ';
|
|
|
|
if ($attempt->timefinish == 0) {
|
|
|
|
print_string('unfinished');
|
|
|
|
} else {
|
2005-05-07 13:48:25 +00:00
|
|
|
echo round($attempt->sumgrades, $quiz->decimalpoints).'/'.$quiz->sumgrades;
|
2005-05-06 06:24:04 +00:00
|
|
|
}
|
|
|
|
echo ' - '.userdate($attempt->timemodified).'<br />';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print_string('noattempts', 'quiz');
|
|
|
|
}
|
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
function quiz_cron () {
|
2002-10-13 07:17:48 +00:00
|
|
|
/// Function to be run periodically according to the moodle cron
|
2004-06-02 18:03:05 +00:00
|
|
|
/// This function searches for things that need to be done, such
|
|
|
|
/// as sending out mail, toggling flags etc ...
|
2002-10-04 02:59:05 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-10-17 07:23:51 +00:00
|
|
|
function quiz_grades($quizid) {
|
2002-10-17 07:55:54 +00:00
|
|
|
/// Must return an array of grades, indexed by user, and a max grade.
|
|
|
|
|
2005-01-27 02:30:32 +00:00
|
|
|
$quiz = get_record('quiz', 'id', intval($quizid));
|
|
|
|
if (empty($quiz) || empty($quiz->grade)) {
|
2003-09-10 05:02:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-01-27 02:30:32 +00:00
|
|
|
$return->grades = get_records_menu('quiz_grades', 'quiz', $quiz->id, '', 'userid, grade');
|
|
|
|
$return->maxgrade = get_field('quiz', 'grade', 'id', $quiz->id);
|
2002-10-17 07:55:54 +00:00
|
|
|
return $return;
|
2002-10-17 07:23:51 +00:00
|
|
|
}
|
|
|
|
|
2003-09-28 14:57:16 +00:00
|
|
|
function quiz_get_participants($quizid) {
|
|
|
|
/// Returns an array of users who have data in a given quiz
|
2005-03-20 20:00:43 +00:00
|
|
|
/// (users with records in quiz_attempts and quiz_question_versions)
|
2003-09-28 14:57:16 +00:00
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
2005-03-20 20:00:43 +00:00
|
|
|
//Get users from attempts
|
|
|
|
$us_attempts = get_records_sql("SELECT DISTINCT u.id, u.id
|
|
|
|
FROM {$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}quiz_attempts a
|
|
|
|
WHERE a.quiz = '$quizid' and
|
|
|
|
u.id = a.userid");
|
|
|
|
|
|
|
|
//Get users from question_versions
|
|
|
|
$us_versions = get_records_sql("SELECT DISTINCT u.id, u.id
|
|
|
|
FROM {$CFG->prefix}user u,
|
2005-03-23 20:59:55 +00:00
|
|
|
{$CFG->prefix}quiz_question_versions v
|
2005-03-20 20:00:43 +00:00
|
|
|
WHERE v.quiz = '$quizid' and
|
|
|
|
u.id = v.userid");
|
|
|
|
|
|
|
|
//Add us_versions to us_attempts
|
|
|
|
if ($us_versions) {
|
|
|
|
foreach ($us_versions as $us_version) {
|
|
|
|
$us_attempts[$us_version->id] = $us_version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Return us_attempts array (it contains an array of unique users)
|
|
|
|
return ($us_attempts);
|
|
|
|
|
2003-09-28 14:57:16 +00:00
|
|
|
}
|
2002-10-04 02:59:05 +00:00
|
|
|
|
2004-04-28 02:56:40 +00:00
|
|
|
function quiz_refresh_events($courseid = 0) {
|
|
|
|
// This standard function will check all instances of this module
|
|
|
|
// and make sure there are up-to-date events created for each of them.
|
2004-05-17 17:04:01 +00:00
|
|
|
// If courseid = 0, then every quiz event in the site is checked, else
|
|
|
|
// only quiz events belonging to the course specified are checked.
|
2004-04-28 02:56:40 +00:00
|
|
|
// This function is used, in its new format, by restore_refresh_events()
|
|
|
|
|
|
|
|
if ($courseid == 0) {
|
|
|
|
if (! $quizzes = get_records("quiz")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (! $quizzes = get_records("quiz", "course", $courseid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2004-05-05 07:07:56 +00:00
|
|
|
$moduleid = get_field('modules', 'id', 'name', 'quiz');
|
2004-06-02 18:03:05 +00:00
|
|
|
|
2004-04-28 02:56:40 +00:00
|
|
|
foreach ($quizzes as $quiz) {
|
|
|
|
$event = NULL;
|
2004-05-17 17:04:01 +00:00
|
|
|
$event2 = NULL;
|
|
|
|
$event2old = NULL;
|
|
|
|
|
|
|
|
if ($events = get_records_select('event', "modulename = 'quiz' AND instance = '$quiz->id' ORDER BY timestart")) {
|
|
|
|
$event = array_shift($events);
|
|
|
|
if (!empty($events)) {
|
|
|
|
$event2old = array_shift($events);
|
|
|
|
if (!empty($events)) {
|
|
|
|
foreach ($events as $badevent) {
|
|
|
|
delete_records('event', 'id', $badevent->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-28 02:56:40 +00:00
|
|
|
$event->name = addslashes($quiz->name);
|
|
|
|
$event->description = addslashes($quiz->intro);
|
2004-05-17 17:04:01 +00:00
|
|
|
$event->courseid = $quiz->course;
|
|
|
|
$event->groupid = 0;
|
|
|
|
$event->userid = 0;
|
|
|
|
$event->modulename = 'quiz';
|
|
|
|
$event->instance = $quiz->id;
|
2004-08-08 15:09:39 +00:00
|
|
|
$event->visible = instance_is_visible('quiz', $quiz);
|
2004-04-28 02:56:40 +00:00
|
|
|
$event->timestart = $quiz->timeopen;
|
2004-05-17 17:04:01 +00:00
|
|
|
$event->eventtype = 'open';
|
2004-04-28 02:56:40 +00:00
|
|
|
$event->timeduration = ($quiz->timeclose - $quiz->timeopen);
|
|
|
|
|
2004-05-17 17:04:01 +00:00
|
|
|
if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Set up two events
|
2004-04-28 02:56:40 +00:00
|
|
|
|
2004-05-17 17:04:01 +00:00
|
|
|
$event2 = $event;
|
2004-04-28 02:56:40 +00:00
|
|
|
|
2004-05-17 17:04:01 +00:00
|
|
|
$event->name = addslashes($quiz->name).' ('.get_string('quizopens', 'quiz').')';
|
|
|
|
$event->timeduration = 0;
|
|
|
|
|
|
|
|
$event2->name = addslashes($quiz->name).' ('.get_string('quizcloses', 'quiz').')';
|
|
|
|
$event2->timestart = $quiz->timeclose;
|
|
|
|
$event2->eventtype = 'close';
|
|
|
|
$event2->timeduration = 0;
|
|
|
|
|
|
|
|
if (empty($event2old->id)) {
|
|
|
|
unset($event2->id);
|
|
|
|
add_event($event2);
|
|
|
|
} else {
|
|
|
|
$event2->id = $event2old->id;
|
|
|
|
update_event($event2);
|
|
|
|
}
|
|
|
|
} else if (!empty($event2->id)) {
|
|
|
|
delete_event($event2->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($event->id)) {
|
2004-04-28 02:56:40 +00:00
|
|
|
add_event($event);
|
2004-05-17 17:04:01 +00:00
|
|
|
} else {
|
|
|
|
update_event($event);
|
2004-04-28 02:56:40 +00:00
|
|
|
}
|
2004-05-17 17:04:01 +00:00
|
|
|
|
2004-04-28 02:56:40 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-13 03:04:48 +00:00
|
|
|
|
|
|
|
function quiz_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $quiz="0", $user="", $groupid="") {
|
|
|
|
// Returns all quizzes since a given time. If quiz is specified then
|
|
|
|
// this restricts the results
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if ($quiz) {
|
|
|
|
$quizselect = " AND cm.id = '$quiz'";
|
|
|
|
} else {
|
|
|
|
$quizselect = "";
|
|
|
|
}
|
|
|
|
if ($user) {
|
|
|
|
$userselect = " AND u.id = '$user'";
|
|
|
|
} else {
|
|
|
|
$userselect = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$quizzes = get_records_sql("SELECT qa.*, q.name, u.firstname, u.lastname, u.picture,
|
|
|
|
q.course, q.sumgrades as maxgrade, cm.instance, cm.section
|
|
|
|
FROM {$CFG->prefix}quiz_attempts qa,
|
|
|
|
{$CFG->prefix}quiz q,
|
|
|
|
{$CFG->prefix}user u,
|
|
|
|
{$CFG->prefix}course_modules cm
|
|
|
|
WHERE qa.timefinish > '$sincetime'
|
|
|
|
AND qa.userid = u.id $userselect
|
|
|
|
AND qa.quiz = q.id $quizselect
|
|
|
|
AND cm.instance = q.id
|
|
|
|
AND cm.course = '$courseid'
|
|
|
|
AND q.course = cm.course
|
|
|
|
ORDER BY qa.timefinish ASC");
|
|
|
|
|
|
|
|
if (empty($quizzes))
|
|
|
|
return;
|
|
|
|
|
|
|
|
foreach ($quizzes as $quiz) {
|
|
|
|
if (empty($groupid) || ismember($groupid, $quiz->userid)) {
|
|
|
|
|
2005-04-02 11:32:22 +00:00
|
|
|
$tmpactivity = new Object;
|
|
|
|
|
2005-01-13 03:04:48 +00:00
|
|
|
$tmpactivity->type = "quiz";
|
|
|
|
$tmpactivity->defaultindex = $index;
|
|
|
|
$tmpactivity->instance = $quiz->quiz;
|
|
|
|
|
|
|
|
$tmpactivity->name = $quiz->name;
|
|
|
|
$tmpactivity->section = $quiz->section;
|
|
|
|
|
|
|
|
$tmpactivity->content->attemptid = $quiz->id;
|
|
|
|
$tmpactivity->content->sumgrades = $quiz->sumgrades;
|
|
|
|
$tmpactivity->content->maxgrade = $quiz->maxgrade;
|
|
|
|
$tmpactivity->content->attempt = $quiz->attempt;
|
|
|
|
|
|
|
|
$tmpactivity->user->userid = $quiz->userid;
|
|
|
|
$tmpactivity->user->fullname = fullname($quiz);
|
|
|
|
$tmpactivity->user->picture = $quiz->picture;
|
|
|
|
|
|
|
|
$tmpactivity->timestamp = $quiz->timefinish;
|
|
|
|
|
|
|
|
$activities[] = $tmpactivity;
|
|
|
|
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function quiz_print_recent_mod_activity($activity, $course, $detail=false) {
|
2005-02-05 20:00:15 +00:00
|
|
|
global $CFG;
|
2005-01-13 03:04:48 +00:00
|
|
|
|
|
|
|
echo '<table border="0" cellpadding="3" cellspacing="0">';
|
|
|
|
|
2005-02-05 17:28:06 +00:00
|
|
|
echo "<tr><td class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
|
2005-01-13 03:04:48 +00:00
|
|
|
print_user_picture($activity->user->userid, $course, $activity->user->picture);
|
|
|
|
echo "</td><td width=\"100%\"><font size=\"2\">";
|
|
|
|
|
|
|
|
if ($detail) {
|
|
|
|
echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
|
|
|
|
"height=\"16\" width=\"16\" alt=\"$activity->type\" /> ";
|
|
|
|
echo "<a href=\"$CFG->wwwroot/mod/quiz/view.php?id=" . $activity->instance . "\">"
|
2005-04-02 23:19:01 +00:00
|
|
|
. format_string($activity->name,true) . "</a> - ";
|
2005-01-13 03:04:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isteacher($course)) {
|
|
|
|
$grades = "(" . $activity->content->sumgrades . " / " . $activity->content->maxgrade . ") ";
|
|
|
|
echo "<a href=\"$CFG->wwwroot/mod/quiz/review.php?q="
|
|
|
|
. $activity->instance . "&attempt="
|
|
|
|
. $activity->content->attemptid . "\">" . $grades . "</a> ";
|
|
|
|
|
|
|
|
echo get_string("attempt", "quiz") . " - " . $activity->content->attempt . "<br />";
|
|
|
|
}
|
|
|
|
echo "<a href=\"$CFG->wwwroot/user/view.php?id="
|
|
|
|
. $activity->user->userid . "&course=$course\">"
|
|
|
|
. $activity->user->fullname . "</a> ";
|
|
|
|
|
|
|
|
echo " - " . userdate($activity->timestamp);
|
|
|
|
|
|
|
|
echo "</font></td></tr>";
|
|
|
|
echo "</table>";
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-05-06 06:24:04 +00:00
|
|
|
/**
|
|
|
|
* Pre-process the options form data
|
|
|
|
*
|
|
|
|
* Encode the review options from the setup form into the bits of $form->review
|
|
|
|
* and other options into $form->optionflags
|
|
|
|
* The form data is passed by reference and modified by this function
|
|
|
|
* @return integer
|
|
|
|
* @param object $form The variables set on the form.
|
|
|
|
*/
|
|
|
|
function quiz_process_options(&$form) {
|
|
|
|
$optionflags = 0;
|
|
|
|
$review = 0;
|
|
|
|
|
|
|
|
if (!empty($form->adaptive)) {
|
2005-08-26 19:12:17 +00:00
|
|
|
$optionflags |= QUIZ_ADAPTIVE;
|
2005-05-06 06:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($form->responsesimmediately)) {
|
|
|
|
$review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_IMMEDIATELY);
|
|
|
|
unset($form->responsesimmediately);
|
|
|
|
}
|
|
|
|
if (isset($form->responsesopen)) {
|
|
|
|
$review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_OPEN);
|
|
|
|
unset($form->responsesopen);
|
|
|
|
}
|
|
|
|
if (isset($form->responsesclosed)) {
|
|
|
|
$review += (QUIZ_REVIEW_RESPONSES & QUIZ_REVIEW_CLOSED);
|
|
|
|
unset($form->responsesclosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($form->scoreimmediately)) {
|
|
|
|
$review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_IMMEDIATELY);
|
|
|
|
unset($form->scoreimmediately);
|
|
|
|
}
|
|
|
|
if (isset($form->scoreopen)) {
|
|
|
|
$review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN);
|
|
|
|
unset($form->scoreopen);
|
|
|
|
}
|
|
|
|
if (isset($form->scoreclosed)) {
|
|
|
|
$review += (QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED);
|
|
|
|
unset($form->scoreclosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($form->feedbackimmediately)) {
|
|
|
|
$review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_IMMEDIATELY);
|
|
|
|
unset($form->feedbackimmediately);
|
|
|
|
}
|
|
|
|
if (isset($form->feedbackopen)) {
|
|
|
|
$review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_OPEN);
|
|
|
|
unset($form->feedbackopen);
|
|
|
|
}
|
|
|
|
if (isset($form->feedbackclosed)) {
|
|
|
|
$review += (QUIZ_REVIEW_FEEDBACK & QUIZ_REVIEW_CLOSED);
|
|
|
|
unset($form->feedbackclosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($form->answersimmediately)) {
|
|
|
|
$review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_IMMEDIATELY);
|
|
|
|
unset($form->answersimmediately);
|
|
|
|
}
|
|
|
|
if (isset($form->answersopen)) {
|
|
|
|
$review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_OPEN);
|
|
|
|
unset($form->answersopen);
|
|
|
|
}
|
|
|
|
if (isset($form->answersclosed)) {
|
|
|
|
$review += (QUIZ_REVIEW_ANSWERS & QUIZ_REVIEW_CLOSED);
|
|
|
|
unset($form->answersclosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($form->solutionsimmediately)) {
|
|
|
|
$review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_IMMEDIATELY);
|
|
|
|
unset($form->solutionsimmediately);
|
|
|
|
}
|
|
|
|
if (isset($form->solutionsopen)) {
|
|
|
|
$review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_OPEN);
|
|
|
|
unset($form->solutionsopen);
|
|
|
|
}
|
|
|
|
if (isset($form->solutionsclosed)) {
|
|
|
|
$review += (QUIZ_REVIEW_SOLUTIONS & QUIZ_REVIEW_CLOSED);
|
|
|
|
unset($form->solutionsclosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->review = $review;
|
|
|
|
$form->optionflags = $optionflags;
|
2005-08-26 18:47:24 +00:00
|
|
|
|
|
|
|
// The following implements the time limit check box
|
|
|
|
if (empty($form->timelimitenable) or $form->timelimit < 1) {
|
|
|
|
$form->timelimit = 0;
|
|
|
|
}
|
2005-05-06 06:24:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-09-01 04:14:31 +00:00
|
|
|
function quiz_get_view_actions() {
|
|
|
|
return array('view','view all','report');
|
|
|
|
}
|
2005-05-06 06:24:04 +00:00
|
|
|
|
2005-09-01 04:14:31 +00:00
|
|
|
function quiz_get_post_actions() {
|
|
|
|
return array('attempt','editquestions','review','submit');
|
|
|
|
}
|
2005-05-06 06:24:04 +00:00
|
|
|
|
2002-10-04 02:59:05 +00:00
|
|
|
?>
|