libdir.'/pagelib.php');
/// CONSTANTS ///////////////////////////////////////////////////////////////////
/**#@+
* The different review options are stored in the bits of $quiz->review
* These constants help to extract the options
*
* This is more of a mess than you might think necessary, because originally
* it was though that 3x6 bits were enough, but then they ran out. PHP integers
* are only reliably 32 bits signed, so the simplest solution was then to
* add 4x3 more bits.
*/
/**
* The first 6 + 4 bits refer to the time immediately after the attempt
*/
define('QUIZ_REVIEW_IMMEDIATELY', 0x3c003f);
/**
* the next 6 + 4 bits refer to the time after the attempt but while the quiz is open
*/
define('QUIZ_REVIEW_OPEN', 0x3c00fc0);
/**
* the final 6 + 4 bits refer to the time after the quiz closes
*/
define('QUIZ_REVIEW_CLOSED', 0x3c03f000);
// within each group of 6 bits we determine what should be shown
define('QUIZ_REVIEW_RESPONSES', 1*0x1041); // Show responses
define('QUIZ_REVIEW_SCORES', 2*0x1041); // Show scores
define('QUIZ_REVIEW_FEEDBACK', 4*0x1041); // Show question feedback
define('QUIZ_REVIEW_ANSWERS', 8*0x1041); // Show correct answers
// Some handling of worked solutions is already in the code but not yet fully supported
// and not switched on in the user interface.
define('QUIZ_REVIEW_SOLUTIONS', 16*0x1041); // Show solutions
define('QUIZ_REVIEW_GENERALFEEDBACK',32*0x1041); // Show question general feedback
define('QUIZ_REVIEW_OVERALLFEEDBACK', 1*0x4440000); // Show quiz overall feedback
// Multipliers 2*0x4440000, 4*0x4440000 and 8*0x4440000 are still available
/**#@-*/
/**
* If start and end date for the quiz are more than this many seconds apart
* they will be represented by two separate events in the calendar
*/
define("QUIZ_MAX_EVENT_LENGTH", 5*24*60*60); // 5 days maximum
/// FUNCTIONS ///////////////////////////////////////////////////////////////////
/**
* 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
* of the new instance.
*
* @param object $quiz the data that came from the form.
* @return mixed the id of the new instance on success,
* false or a string error message on failure.
*/
function quiz_add_instance($quiz) {
// Process the options from the form.
$quiz->created = time();
$quiz->questions = '';
$result = quiz_process_options($quiz);
if ($result && is_string($result)) {
return $result;
}
// Try to store it in the database.
if (!$quiz->id = insert_record("quiz", $quiz)) {
return false;
}
// Do the processing required after an add or an update.
quiz_after_add_or_update($quiz);
return $quiz->id;
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod.html) this function
* will update an existing instance with new data.
*
* @param object $quiz the data that came from the form.
* @return mixed true on success, false or a string error message on failure.
*/
function quiz_update_instance($quiz) {
// Process the options from the form.
$result = quiz_process_options($quiz);
if ($result && is_string($result)) {
return $result;
}
// Update the database.
$quiz->id = $quiz->instance;
if (!update_record("quiz", $quiz)) {
return false; // some error occurred
}
// Do the processing required after an add or an update.
quiz_after_add_or_update($quiz);
// Delete any previous preview attempts
delete_records('quiz_attempts', 'preview', '1', 'quiz', $quiz->id);
return true;
}
function quiz_delete_instance($id) {
/// Given an ID of an instance of this module,
/// this function will permanently delete the instance
/// and any data that depends on it.
if (! $quiz = get_record("quiz", "id", "$id")) {
return false;
}
$result = true;
if ($attempts = get_records("quiz_attempts", "quiz", "$quiz->id")) {
// TODO: this should use the delete_attempt($attempt->uniqueid) function in questionlib.php
// require_once($CFG->libdir.'/questionlib.php');
foreach ($attempts as $attempt) {
if (! delete_records("question_states", "attempt", "$attempt->uniqueid")) {
$result = false;
}
if (! delete_records("question_sessions", "attemptid", "$attempt->uniqueid")) {
$result = false;
}
}
}
$tables_to_purge = array(
'quiz_attempts' => 'quiz',
'quiz_grades' => 'quiz',
'quiz_question_instances' => 'quiz',
'quiz_grades' => 'quiz',
'quiz_feedback' => 'quizid',
'quiz' => 'id'
);
foreach ($tables_to_purge as $table => $keyfield) {
if (!delete_records($table, $keyfield, $quiz->id)) {
$result = false;
}
}
$pagetypes = page_import_types('mod/quiz/');
foreach($pagetypes as $pagetype) {
if(!blocks_delete_all_on_page($pagetype, $quiz->id)) {
$result = false;
}
}
if ($events = get_records_select('event', "modulename = 'quiz' and instance = '$quiz->id'")) {
foreach($events as $event) {
delete_event($event->id);
}
}
quiz_grade_item_delete($quiz);
return $result;
}
/**
* Get the best current grade for a particular user in a quiz.
*
* @param object $quiz the quiz object.
* @param integer $userid the id of the user.
* @return float the user's current grade for this quiz.
*/
function quiz_get_best_grade($quiz, $userid) {
$grade = get_field('quiz_grades', 'grade', 'quiz', $quiz->id, 'userid', $userid);
// Need to detect errors/no result, without catching 0 scores.
if (is_numeric($grade)) {
return round($grade, $quiz->decimalpoints);
} else {
return NULL;
}
}
function quiz_user_outline($course, $user, $mod, $quiz) {
/// Return a small object with summary information about what a
/// 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
$grade = quiz_get_best_grade($quiz, $user->id);
if (is_null($grade)) {
return NULL;
}
$result = new stdClass;
$result->info = get_string('grade') . ': ' . $grade . '/' . $quiz->grade;
$result->time = get_field('quiz_attempts', 'MAX(timefinish)', 'userid', $user->id, 'quiz', $quiz->id);
return $result;
}
function quiz_user_complete($course, $user, $mod, $quiz) {
/// Print a detailed representation of what a user has done with
/// a given particular instance of this module, for user activity reports.
if ($attempts = get_records_select('quiz_attempts', "userid='$user->id' AND quiz='$quiz->id'", 'attempt ASC')) {
if ($quiz->grade && $quiz->sumgrades && $grade = get_record('quiz_grades', 'userid', $user->id, 'quiz', $quiz->id)) {
echo get_string('grade').': '.round($grade->grade, $quiz->decimalpoints).'/'.$quiz->grade.'
';
}
foreach ($attempts as $attempt) {
echo get_string('attempt', 'quiz').' '.$attempt->attempt.': ';
if ($attempt->timefinish == 0) {
print_string('unfinished');
} else {
echo round($attempt->sumgrades, $quiz->decimalpoints).'/'.$quiz->sumgrades;
}
echo ' - '.userdate($attempt->timemodified).'
';
}
} else {
print_string('noattempts', 'quiz');
}
return true;
}
function quiz_cron() {
/// Function to be run periodically according to the moodle cron
/// This function searches for things that need to be done, such
/// as sending out mail, toggling flags etc ...
global $CFG;
return true;
}
/**
* @param integer $quizid the quiz id.
* @param integer $userid the userid.
* @param string $status 'all', 'finished' or 'unfinished' to control
* @return an array of all the user's attempts at this quiz. Returns an empty array if there are none.
*/
function quiz_get_user_attempts($quizid, $userid, $status = 'finished', $includepreviews = false) {
$status_condition = array(
'all' => '',
'finished' => ' AND timefinish > 0',
'unfinished' => ' AND timefinish = 0'
);
$previewclause = '';
if (!$includepreviews) {
$previewclause = ' AND preview = 0';
}
if ($attempts = get_records_select('quiz_attempts',
"quiz = '$quizid' AND userid = '$userid'" . $previewclause . $status_condition[$status],
'attempt ASC')) {
return $attempts;
} else {
return array();
}
}
/**
* Return grade for given user or all users.
*
* @param int $quizid id of quiz
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function quiz_get_user_grades($quiz, $userid=0) {
global $CFG;
$user = $userid ? "AND u.id = $userid" : "";
$sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade, g.timemodified AS dategraded, MAX(a.timefinish) AS datesubmitted
FROM {$CFG->prefix}user u, {$CFG->prefix}quiz_grades g, {$CFG->prefix}quiz_attempts a
WHERE u.id = g.userid AND g.quiz = {$quiz->id} AND a.quiz = g.quiz AND u.id = a.userid
$user
GROUP BY u.id, g.grade, g.timemodified";
return get_records_sql($sql);
}
/**
* Update grades in central gradebook
*
* @param object $quiz null means all quizs
* @param int $userid specific user only, 0 mean all
*/
function quiz_update_grades($quiz=null, $userid=0, $nullifnone=true) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
}
if ($quiz != null) {
if ($grades = quiz_get_user_grades($quiz, $userid)) {
quiz_grade_item_update($quiz, $grades);
} else if ($userid and $nullifnone) {
$grade = new object();
$grade->userid = $userid;
$grade->rawgrade = NULL;
quiz_grade_item_update($quiz, $grade);
} else {
quiz_grade_item_update($quiz);
}
} else {
$sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
FROM {$CFG->prefix}quiz a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=a.id";
if ($rs = get_recordset_sql($sql)) {
while ($quiz = rs_fetch_next_record($rs)) {
if ($quiz->grade != 0) {
quiz_update_grades($quiz, 0, false);
} else {
quiz_grade_item_update($quiz);
}
}
rs_close($rs);
}
}
}
/**
* Create grade item for given quiz
*
* @param object $quiz object with extra cmidnumber
* @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function quiz_grade_item_update($quiz, $grades=NULL) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
}
if (array_key_exists('cmidnumber', $quiz)) { //it may not be always present
$params = array('itemname'=>$quiz->name, 'idnumber'=>$quiz->cmidnumber);
} else {
$params = array('itemname'=>$quiz->name);
}
if ($quiz->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $quiz->grade;
$params['grademin'] = 0;
} else {
$params['gradetype'] = GRADE_TYPE_NONE;
}
/* description by TJ:
1/ If the quiz is set to not show scores while the quiz is still open, and is set to show scores after
the quiz is closed, then create the grade_item with a show-after date that is the quiz close date.
2/ If the quiz is set to not show scores at either of those times, create the grade_item as hidden.
3/ If the quiz is set to show scores, create the grade_item visible.
*/
if (!($quiz->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED)
and !($quiz->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN)) {
$params['hidden'] = 1;
} else if ( ($quiz->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_CLOSED)
and !($quiz->review & QUIZ_REVIEW_SCORES & QUIZ_REVIEW_OPEN)) {
if ($quiz->timeclose) {
$params['hidden'] = $quiz->timeclose;
} else {
$params['hidden'] = 1;
}
} else {
// a) both open and closed enabled
// b) open enabled, closed disabled - we can not "hide after", grades are kept visible even after closing
$params['hidden'] = 0;
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = NULL;
}
$gradebook_grades = grade_get_grades($quiz->course, 'mod', 'quiz', $quiz->id);
if (!empty($gradebook_grades->items)) {
$grade_item = $gradebook_grades->items[0];
if ($grade_item->locked) {
$confirm_regrade = optional_param('confirm_regrade', 0, PARAM_INT);
if (!$confirm_regrade) {
$message = get_string('gradeitemislocked', 'grades');
$back_link = $CFG->wwwroot . '/mod/quiz/report.php?q=' . $quiz->id . '&mode=overview';
$regrade_link = qualified_me() . '&confirm_regrade=1';
print_box_start('generalbox', 'notice');
echo '
'. $message .'
'; echo ' '; print_box_end(); return GRADE_UPDATE_ITEM_LOCKED; } } } return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, $grades, $params); } /** * Delete grade item for given quiz * * @param object $quiz object * @return object quiz */ function quiz_grade_item_delete($quiz) { global $CFG; require_once($CFG->libdir.'/gradelib.php'); return grade_update('mod/quiz', $quiz->course, 'mod', 'quiz', $quiz->id, 0, NULL, array('deleted'=>1)); } function quiz_get_participants($quizid) { /// Returns an array of users who have data in a given quiz /// (users with records in quiz_attempts and quiz_question_versions) global $CFG; //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, {$CFG->prefix}quiz_question_versions v 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); } 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. // If courseid = 0, then every quiz event in the site is checked, else // only quiz events belonging to the course specified are checked. // 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; } } $moduleid = get_field('modules', 'id', 'name', 'quiz'); foreach ($quizzes as $quiz) { $event = NULL; $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); } } } } $event->name = addslashes($quiz->name); $event->description = addslashes($quiz->intro); $event->courseid = $quiz->course; $event->groupid = 0; $event->userid = 0; $event->modulename = 'quiz'; $event->instance = $quiz->id; $event->visible = instance_is_visible('quiz', $quiz); $event->timestart = $quiz->timeopen; $event->eventtype = 'open'; $event->timeduration = ($quiz->timeclose - $quiz->timeopen); if ($event->timeduration > QUIZ_MAX_EVENT_LENGTH) { /// Set up two events $event2 = $event; $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($event2old->id)) { delete_event($event2old->id); } if (empty($event->id)) { if (!empty($event->timestart)) { add_event($event); } } else { update_event($event); } } return true; } /** * Returns all quiz graded users since a given time for specified quiz */ function quiz_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) { global $CFG, $COURSE, $USER; if ($COURSE->id == $courseid) { $course = $COURSE; } else { $course = get_record('course', 'id', $courseid); } $modinfo =& get_fast_modinfo($course); $cm = $modinfo->cms[$cmid]; if ($userid) { $userselect = "AND u.id = $userid"; } else { $userselect = ""; } if ($groupid) { $groupselect = "AND gm.groupid = $groupid"; $groupjoin = "JOIN {$CFG->prefix}groups_members gm ON gm.userid=u.id"; } else { $groupselect = ""; $groupjoin = ""; } if (!$attempts = get_records_sql("SELECT qa.*, q.sumgrades AS maxgrade, u.firstname, u.lastname, u.email, u.picture FROM {$CFG->prefix}quiz_attempts qa JOIN {$CFG->prefix}quiz q ON q.id = qa.quiz JOIN {$CFG->prefix}user u ON u.id = qa.userid $groupjoin WHERE qa.timefinish > $timestart AND q.id = $cm->instance $userselect $groupselect ORDER BY qa.timefinish ASC")) { return; } $cm_context = get_context_instance(CONTEXT_MODULE, $cm->id); $grader = has_capability('moodle/grade:viewall', $cm_context); $accessallgroups = has_capability('moodle/site:accessallgroups', $cm_context); $viewfullnames = has_capability('moodle/site:viewfullnames', $cm_context); $grader = has_capability('mod/quiz:grade', $cm_context); $groupmode = groups_get_activity_groupmode($cm, $course); if (is_null($modinfo->groups)) { $modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo } $aname = format_string($cm->name,true); foreach ($attempts as $attempt) { if ($attempt->userid != $USER->id) { if (!$grader) { // grade permission required continue; } if ($groupmode == SEPARATEGROUPS and !$accessallgroups) { $usersgroups = groups_get_all_groups($course->id, $attempt->userid, $cm->groupingid); if (!is_array($usersgroups)) { continue; } $usersgroups = array_keys($usersgroups); $interset = array_intersect($usersgroups, $modinfo->groups[$cm->id]); if (empty($intersect)) { continue; } } } $tmpactivity = new object(); $tmpactivity->type = 'quiz'; $tmpactivity->cmid = $cm->id; $tmpactivity->name = $aname; $tmpactivity->sectionnum= $cm->sectionnum; $tmpactivity->timestamp = $attempt->timefinish; $tmpactivity->content->attemptid = $attempt->id; $tmpactivity->content->sumgrades = $attempt->sumgrades; $tmpactivity->content->maxgrade = $attempt->maxgrade; $tmpactivity->content->attempt = $attempt->attempt; $tmpactivity->user->userid = $attempt->userid; $tmpactivity->user->fullname = fullname($attempt, $viewfullnames); $tmpactivity->user->picture = $attempt->picture; $activities[$index++] = $tmpactivity; } return; } function quiz_print_recent_mod_activity($activity, $courseid, $detail, $modnames) { global $CFG; echo '"; print_user_picture($activity->user->userid, $courseid, $activity->user->picture); echo " | ";
if ($detail) {
$modname = $modnames[$activity->type];
echo ' ';
echo " ';
}
echo '';
echo get_string("attempt", "quiz")." {$activity->content->attempt}: ";
$grades = "({$activity->content->sumgrades} / {$activity->content->maxgrade})";
echo "wwwroot/mod/quiz/review.php?attempt={$activity->content->attemptid}\">$grades";
echo ' ';
echo '';
echo "wwwroot/user/view.php?id={$activity->user->userid}&course=$courseid\">"
."{$activity->user->fullname} - ".userdate($activity->timestamp);
echo ' ';
echo " |