.
/**
* 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}
*
* @package mod-quiz
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/** Require {@link eventslib.php} */
require_once($CFG->libdir . '/eventslib.php');
/** Require {@link calendar/lib.php} */
require_once($CFG->dirroot . '/calendar/lib.php');
/// CONSTANTS ///////////////////////////////////////////////////////////////////
/**#@+
* Options determining how the grades from individual attempts are combined to give
* the overall grade for a user
*/
define('QUIZ_GRADEHIGHEST', 1);
define('QUIZ_GRADEAVERAGE', 2);
define('QUIZ_ATTEMPTFIRST', 3);
define('QUIZ_ATTEMPTLAST', 4);
/**#@-*/
define('QUIZ_MAX_ATTEMPT_OPTION', 10);
define('QUIZ_MAX_QPP_OPTION', 50);
define('QUIZ_MAX_DECIMAL_OPTION', 5);
define('QUIZ_MAX_Q_DECIMAL_OPTION', 7);
/**#@+
* 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_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @global object
* @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) {
global $DB;
// 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.
$quiz->id = $DB->insert_record('quiz', $quiz);
// 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_form.php) this function
* will update an existing instance with new data.
*
* @global stdClass
* @global object
* @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, $mform) {
global $CFG, $DB;
// Process the options from the form.
$result = quiz_process_options($quiz);
if ($result && is_string($result)) {
return $result;
}
// Repaginate, if asked to.
if (!$quiz->shufflequestions && !empty($quiz->repaginatenow)) {
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
$quiz->questions = $DB->get_field('quiz', 'questions', array('id' => $quiz->instance));
$quiz->questions = quiz_repaginate($quiz->questions, $quiz->questionsperpage);
}
unset($quiz->repaginatenow);
// Update the database.
$quiz->id = $quiz->instance;
$DB->update_record('quiz', $quiz);
// Do the processing required after an add or an update.
quiz_after_add_or_update($quiz);
// Delete any previous preview attempts
quiz_delete_previews($quiz);
return true;
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @global object
* @param int $id
* @return bool
*/
function quiz_delete_instance($id) {
global $DB;
if (!$quiz = $DB->get_record('quiz', array('id' => $id))) {
return false;
}
quiz_delete_all_attempts($quiz);
quiz_delete_all_overrides($quiz);
$DB->delete_records('quiz_question_instances', array('quiz' => $quiz->id));
$DB->delete_records('quiz_feedback', array('quizid' => $quiz->id));
$events = $DB->get_records('event', array('modulename' => 'quiz', 'instance' => $quiz->id));
foreach($events as $event) {
$event = calendar_event::load($event);
$event->delete();
}
quiz_grade_item_delete($quiz);
$DB->delete_records('quiz', array('id' => $quiz->id));
return true;
}
/**
* Deletes a quiz override from the database and clears any corresponding calendar events
*
* @param object $quiz The quiz object.
* @param integer $overrideid The id of the override being deleted
* @return bool true on success
*/
function quiz_delete_override($quiz, $overrideid) {
global $DB;
if (!$override = $DB->get_record('quiz_overrides', array('id' => $overrideid))) {
return false;
}
$groupid = empty($override->groupid)? 0 : $override->groupid;
$userid = empty($override->userid)? 0 : $override->userid;
// Delete the events
$events = $DB->get_records('event', array('modulename'=>'quiz', 'instance'=>$quiz->id, 'groupid'=>$groupid, 'userid'=>$userid));
foreach($events as $event) {
$eventold = calendar_event::load($event);
$eventold->delete();
}
$DB->delete_records('quiz_overrides', array('id' => $overrideid));
return true;
}
/**
* Deletes all quiz overrides from the database and clears any corresponding calendar events
*
* @param object $quiz The quiz object.
*/
function quiz_delete_all_overrides($quiz) {
global $DB;
$overrides = $DB->get_records('quiz_overrides', array('quiz' => $quiz->id), 'id');
foreach ($overrides as $override) {
quiz_delete_override($quiz, $override->id);
}
}
/**
* Updates a quiz object with override information for a user.
*
* Algorithm: For each quiz setting, if there is a matching user-specific override,
* then use that otherwise, if there are group-specific overrides, return the most
* lenient combination of them. If neither applies, leave the quiz setting unchanged.
*
* Special case: if there is more than one password that applies to the user, then
* quiz->extrapasswords will contain an array of strings giving the remaining
* passwords.
*
* @param object $quiz The quiz object.
* @param integer $userid The userid.
* @return object $quiz The updated quiz object.
*/
function quiz_update_effective_access($quiz, $userid) {
global $DB;
// check for user override
$override = $DB->get_record('quiz_overrides', array('quiz' => $quiz->id, 'userid' => $userid));
if (!$override) {
$override = new stdclass;
$override->timeopen = null;
$override->timeclose = null;
$override->timelimit = null;
$override->attempts = null;
$override->password = null;
}
// check for group overrides
$groupings = groups_get_user_groups($quiz->course, $userid);
if (!empty($groupings[0])) {
// Select all overrides that apply to the User's groups
list($extra, $params) = $DB->get_in_or_equal(array_values($groupings[0]));
$sql = "SELECT * FROM {quiz_overrides}
WHERE groupid $extra AND quiz = ?";
$params[] = $quiz->id;
$records = $DB->get_records_sql($sql, $params);
// Combine the overrides
$opens = array();
$closes = array();
$limits = array();
$attempts = array();
$passwords = array();
foreach ($records as $gpoverride) {
if (isset($gpoverride->timeopen)) {
$opens[] = $gpoverride->timeopen;
}
if (isset($gpoverride->timeclose)) {
$closes[] = $gpoverride->timeclose;
}
if (isset($gpoverride->timelimit)) {
$limits[] = $gpoverride->timelimit;
}
if (isset($gpoverride->attempts)) {
$attempts[] = $gpoverride->attempts;
}
if (isset($gpoverride->password)) {
$passwords[] = $gpoverride->password;
}
}
// If there is a user override for a setting, ignore the group override
if (is_null($override->timeopen) && count($opens)) {
$override->timeopen = min($opens);
}
if (is_null($override->timeclose) && count($closes)) {
$override->timeclose = max($closes);
}
if (is_null($override->timelimit) && count($limits)) {
$override->timelimit = max($limits);
}
if (is_null($override->attempts) && count($attempts)) {
$override->attempts = max($attempts);
}
if (is_null($override->password) && count($passwords)) {
$override->password = array_shift($passwords);
if (count($passwords)) {
$override->extrapasswords = $passwords;
}
}
}
// merge with quiz defaults
$keys = array('timeopen','timeclose', 'timelimit', 'attempts', 'password', 'extrapasswords');
foreach ($keys as $key) {
if (isset($override->{$key})) {
$quiz->{$key} = $override->{$key};
}
}
return $quiz;
}
/**
* Delete all the attempts belonging to a quiz.
*
* @global stdClass
* @global object
* @param object $quiz The quiz object.
*/
function quiz_delete_all_attempts($quiz) {
global $CFG, $DB;
require_once($CFG->libdir . '/questionlib.php');
$attempts = $DB->get_records('quiz_attempts', array('quiz' => $quiz->id));
foreach ($attempts as $attempt) {
delete_attempt($attempt->uniqueid);
}
$DB->delete_records('quiz_attempts', array('quiz' => $quiz->id));
$DB->delete_records('quiz_grades', array('quiz' => $quiz->id));
}
/**
* 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
*
* @global object
* @param object $course
* @param object $user
* @param object $mod
* @param object $quiz
* @return object|null
*/
function quiz_user_outline($course, $user, $mod, $quiz) {
global $DB, $CFG;
require_once("$CFG->libdir/gradelib.php");
$grades = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $user->id);
if (empty($grades->items[0]->grades)) {
return null;
} else {
$grade = reset($grades->items[0]->grades);
}
$result = new stdClass;
$result->info = get_string('grade') . ': ' . $grade->str_long_grade;
$result->time = $grade->dategraded;
return $result;
}
/**
* Is this a graded quiz? If this method returns true, you can assume that
* $quiz->grade and $quiz->sumgrades are non-zero (for example, if you want to
* divide by them).
*
* @param object $quiz a row from the quiz table.
* @return boolean whether this is a graded quiz.
*/
function quiz_has_grades($quiz) {
return $quiz->grade != 0 && $quiz->sumgrades != 0;
}
/**
* Get the best current grade for a particular user in a quiz.
*
* @param object $quiz the quiz settings.
* @param integer $userid the id of the user.
* @return float the user's current grade for this quiz, or NULL if this user does
* not have a grade on this quiz.
*/
function quiz_get_best_grade($quiz, $userid) {
global $DB;
$grade = $DB->get_field('quiz_grades', 'grade', array('quiz' => $quiz->id, 'userid' => $userid));
// Need to detect errors/no result, without catching 0 scores.
if ($grade === false) {
return null;
}
return $grade + 0; // Convert to number.
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @global object
* @param object $course
* @param object $user
* @param object $mod
* @param object $quiz
* @return bool
*/
function quiz_user_complete($course, $user, $mod, $quiz) {
global $DB, $CFG, $OUTPUT;
require_once("$CFG->libdir/gradelib.php");
$grades = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $user->id);
if (!empty($grades->items[0]->grades)) {
$grade = reset($grades->items[0]->grades);
echo $OUTPUT->container(get_string('grade').': '.$grade->str_long_grade);
if ($grade->str_feedback) {
echo $OUTPUT->container(get_string('feedback').': '.$grade->str_feedback);
}
}
if ($attempts = $DB->get_records('quiz_attempts', array('userid' => $user->id, 'quiz' => $quiz->id), 'attempt')) {
foreach ($attempts as $attempt) {
echo get_string('attempt', 'quiz').' '.$attempt->attempt.': ';
if ($attempt->timefinish == 0) {
print_string('unfinished');
} else {
echo quiz_format_grade($quiz, $attempt->sumgrades) . '/' . quiz_format_grade($quiz, $quiz->sumgrades);
}
echo ' - '.userdate($attempt->timemodified).'
';
}
} else {
print_string('noattempts', 'quiz');
}
return true;
}
/**
* 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 stdClass
* @return bool true
*/
function quiz_cron() {
global $CFG;
return true;
}
/**
* @global object
* @param integer $quizid the quiz id.
* @param integer $userid the userid.
* @param string $status 'all', 'finished' or 'unfinished' to control
* @param bool $includepreviews
* @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=0, $status = 'finished', $includepreviews = false) {
global $DB;
$status_condition = array(
'all' => '',
'finished' => ' AND timefinish > 0',
'unfinished' => ' AND timefinish = 0'
);
$previewclause = '';
if (!$includepreviews) {
$previewclause = ' AND preview = 0';
}
$params=array($quizid);
if ($userid){
$userclause = ' AND userid = ?';
$params[]=$userid;
} else {
$userclause = '';
}
if ($attempts = $DB->get_records_select('quiz_attempts',
"quiz = ?" .$userclause. $previewclause . $status_condition[$status], $params,
'attempt ASC')) {
return $attempts;
} else {
return array();
}
}
/**
* Return grade for given user or all users.
*
* @global stdClass
* @global object
* @param int $quizid id of quiz
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none. These are raw grades. They should
* be processed with quiz_format_grade for display.
*/
function quiz_get_user_grades($quiz, $userid=0) {
global $CFG, $DB;
$params = array($quiz->id);
$wheresql = '';
if ($userid) {
$params[] = $userid;
$wheresql = "AND u.id = ?";
}
$sql = "SELECT u.id, u.id AS userid, g.grade AS rawgrade, g.timemodified AS dategraded, MAX(a.timefinish) AS datesubmitted
FROM {user} u, {quiz_grades} g, {quiz_attempts} a
WHERE u.id = g.userid AND g.quiz = ? AND a.quiz = g.quiz AND u.id = a.userid $wheresql
GROUP BY u.id, g.grade, g.timemodified";
return $DB->get_records_sql($sql, $params);
}
/**
* Round a grade to to the correct number of decimal places, and format it for display.
*
* @param object $quiz The quiz table row, only $quiz->decimalpoints is used.
* @param float $grade The grade to round.
* @return float
*/
function quiz_format_grade($quiz, $grade) {
return format_float($grade, $quiz->decimalpoints);
}
/**
* Round a grade to to the correct number of decimal places, and format it for display.
*
* @param object $quiz The quiz table row, only $quiz->decimalpoints is used.
* @param float $grade The grade to round.
* @return float
*/
function quiz_format_question_grade($quiz, $grade) {
if ($quiz->questiondecimalpoints == -1) {
return format_float($grade, $quiz->decimalpoints);
} else {
return format_float($grade, $quiz->questiondecimalpoints);
}
}
/**
* Update grades in central gradebook
*
* @global stdClass
* @global object
* @param object $quiz
* @param int $userid specific user only, 0 means all
*/
function quiz_update_grades($quiz, $userid=0, $nullifnone=true) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
if ($quiz->grade == 0) {
quiz_grade_item_update($quiz);
} else 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);
}
}
/**
* Update all grades in gradebook.
*
* @global object
*/
function quiz_upgrade_grades() {
global $DB;
$sql = "SELECT COUNT('x')
FROM {quiz} a, {course_modules} cm, {modules} m
WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=a.id";
$count = $DB->count_records_sql($sql);
$sql = "SELECT a.*, cm.idnumber AS cmidnumber, a.course AS courseid
FROM {quiz} a, {course_modules} cm, {modules} m
WHERE m.name='quiz' AND m.id=cm.module AND cm.instance=a.id";
if ($rs = $DB->get_recordset_sql($sql)) {
$pbar = new progress_bar('quizupgradegrades', 500, true);
$i=0;
foreach ($rs as $quiz) {
$i++;
upgrade_set_timeout(60*5); // set up timeout, may also abort execution
quiz_update_grades($quiz, 0, false);
$pbar->update($i, $count, "Updating Quiz grades ($i/$count).");
}
$rs->close();
}
}
/**
* Create grade item for given quiz
*
* @global stdClass
* @uses GRADE_TYPE_VALUE
* @uses GRADE_TYPE_NONE
* @uses QUIZ_REVIEW_SCORES
* @uses QUIZ_REVIEW_CLOSED
* @uses QUIZ_REVIEW_OPEN
* @uses PARAM_INT
* @uses GRADE_UPDATE_ITEM_LOCKED
* @param object $quiz object with extra cmidnumber
* @param mixed $grades 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, $OUTPUT;
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';
echo $OUTPUT->box_start('generalbox', 'notice');
echo '
'. $message .'
'; echo $OUTPUT->container_start('buttons'); echo $OUTPUT->single_button($regrade_link, get_string('regradeanyway', 'grades')); echo $OUTPUT->single_button($back_link, get_string('cancel')); echo $OUTPUT->container_end(); echo $OUTPUT->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 * * @global stdClass * @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)); } /** * @return the options for calculating the quiz grade from the individual attempt grades. */ function quiz_get_grading_options() { return array ( QUIZ_GRADEHIGHEST => get_string('gradehighest', 'quiz'), QUIZ_GRADEAVERAGE => get_string('gradeaverage', 'quiz'), QUIZ_ATTEMPTFIRST => get_string('attemptfirst', 'quiz'), QUIZ_ATTEMPTLAST => get_string('attemptlast', 'quiz')); } /** * Returns an array of users who have data in a given quiz * * @global stdClass * @global object * @param int $quizid * @return array */ function quiz_get_participants($quizid) { global $CFG, $DB; //Get users from attempts $us_attempts = $DB->get_records_sql("SELECT DISTINCT u.id, u.id FROM {user} u, {quiz_attempts} a WHERE a.quiz = ? and u.id = a.userid", array($quizid)); //Return us_attempts array (it contains an array of unique users) return $us_attempts; } /** * 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() * * @global object * @uses QUIZ_MAX_EVENT_LENGTH * @param int $courseid * @return bool */ function quiz_refresh_events($courseid = 0) { global $DB; if ($courseid == 0) { if (! $quizzes = $DB->get_records('quiz')) { return true; } } else { if (! $quizzes = $DB->get_records('quiz', array('course' => $courseid))) { return true; } } foreach ($quizzes as $quiz) { quiz_update_events($quiz); } 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, $DB; require_once('locallib.php'); if ($COURSE->id == $courseid) { $course = $COURSE; } else { $course = $DB->get_record('course', array('id' => $courseid)); } $modinfo =& get_fast_modinfo($course); $cm = $modinfo->cms[$cmid]; $quiz = $DB->get_record('quiz', array('id' => $cm->instance)); if ($userid) { $userselect = "AND u.id = :userid"; $params['userid'] = $userid; } else { $userselect = ''; } if ($groupid) { $groupselect = 'AND gm.groupid = :groupid'; $groupjoin = 'JOIN {groups_members} gm ON gm.userid=u.id'; $params['groupid'] = $groupid; } else { $groupselect = ''; $groupjoin = ''; } $params = array( 'timestart' => $timestart, 'quizid' => $quiz->id ); if (!$attempts = $DB->get_records_sql(" SELECT qa.*, u.firstname, u.lastname, u.email, u.picture, u.imagealt FROM {quiz_attempts} qa JOIN {user} u ON u.id = qa.userid $groupjoin WHERE qa.timefinish > :timestart AND qa.quiz = :quizid AND qa.preview = 0 $userselect $groupselect ORDER BY qa.timefinish ASC", $params)) { return; } $context = get_context_instance(CONTEXT_MODULE, $cm->id); $accessallgroups = has_capability('moodle/site:accessallgroups', $context); $viewfullnames = has_capability('moodle/site:viewfullnames', $context); $grader = has_capability('mod/quiz:viewreports', $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 } $usersgroups = null; $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) { if (is_null($usersgroups)) { $usersgroups = groups_get_all_groups($course->id, $attempt->userid, $cm->groupingid); if (is_array($usersgroups)) { $usersgroups = array_keys($usersgroups); } else { $usersgroups = array(); } } if (!array_intersect($usersgroups, $modinfo->groups[$cm->id])) { continue; } } } $options = quiz_get_reviewoptions($quiz, $attempt, $context); $tmpactivity = new stdClass; $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->attempt = $attempt->attempt; if (quiz_has_grades($quiz) && $options->scores) { $tmpactivity->content->sumgrades = quiz_format_grade($quiz, $attempt->sumgrades); $tmpactivity->content->maxgrade = quiz_format_grade($quiz, $quiz->sumgrades); } else { $tmpactivity->content->sumgrades = null; $tmpactivity->content->maxgrade = null; } $tmpactivity->user->id = $attempt->id; $tmpactivity->user->firstname = $attempt->firstname; $tmpactivity->user->lastname = $attempt->lastname; $tmpactivity->user->fullname = fullname($attempt, $viewfullnames); $tmpactivity->user->picture = $attempt->picture; $tmpactivity->user->imagealt = $attempt->imagealt; $tmpactivity->user->email = $attempt->email; $activities[$index++] = $tmpactivity; } return; } function quiz_print_recent_mod_activity($activity, $courseid, $detail, $modnames) { global $CFG, $OUTPUT; echo ''; echo $OUTPUT->user_picture($activity->user, array('courseid' => $courseid)); echo ' | ';
if ($detail) {
$modname = $modnames[$activity->type];
echo ' ';
echo ' ';
}
echo '';
echo get_string('attempt', 'quiz', $activity->content->attempt);
if (isset($activity->content->maxgrade)) {
$grades = $activity->content->sumgrades . ' / ' . $activity->content->maxgrade;
echo ': (' . $grades . ')';
}
echo ' ';
echo '';
echo '' . $activity->user->fullname .
' - ' . userdate($activity->timestamp);
echo ' ';
echo ' |