Check course time using new logging API

This commit is contained in:
Mark Nelson 2015-03-11 00:29:18 -07:00
parent 7a2675e889
commit a61997b2aa
2 changed files with 51 additions and 13 deletions

View file

@ -326,30 +326,59 @@ function customcert_get_templates() {
* @return int the total time spent in seconds * @return int the total time spent in seconds
*/ */
function customcert_get_course_time($courseid) { function customcert_get_course_time($courseid) {
global $CFG, $USER; global $CFG, $DB, $USER;
set_time_limit(0); $logmanager = get_log_manager();
$readers = $logmanager->get_readers();
$enabledreaders = get_config('tool_log', 'enabled_stores');
$enabledreaders = explode(',', $enabledreaders);
// Go through all the readers until we find one that we can use.
foreach ($enabledreaders as $enabledreader) {
$reader = $readers[$enabledreader];
if ($reader instanceof \logstore_legacy\log\store) {
$logtable = 'log';
$coursefield = 'course';
$timefield = 'time';
break;
} else if ($reader instanceof \core\log\sql_internal_reader) {
$logtable = $reader->get_internal_log_table_name();
$coursefield = 'courseid';
$timefield = 'timecreated';
break;
}
}
// If we didn't find a reader then return 0.
if (!isset($logtable)) {
return 0;
}
$sql = "SELECT id, $timefield
FROM {{$logtable}}
WHERE userid = :userid
AND $coursefield = :courseid
ORDER BY $timefield ASC";
$params = array('userid' => $USER->id, 'courseid' => $courseid);
$totaltime = 0; $totaltime = 0;
$sql = "l.course = :courseid AND l.userid = :userid"; if ($logs = $DB->get_recordset_sql($sql, $params)) {
if ($logs = get_logs($sql, array('courseid' => $courseid, 'userid' => $USER->id), 'l.time ASC', '', '', $totalcount)) {
foreach ($logs as $log) { foreach ($logs as $log) {
if (!isset($login)) { if (!isset($login)) {
// For the first time $login is not set so the first log is also the first login. // For the first time $login is not set so the first log is also the first login
$login = $log->time; $login = $log->$timefield;
$lasthit = $log->time; $lasthit = $log->$timefield;
$totaltime = 0; $totaltime = 0;
} }
$delay = $log->time - $lasthit; $delay = $log->$timefield - $lasthit;
if ($delay > ($CFG->sessiontimeout * 60)) { if ($delay > ($CFG->sessiontimeout * 60)) {
// The difference between the last log and the current log is more than // The difference between the last log and the current log is more than
// the timeout register session value meaning we have found a session. // the timeout Register session value so that we have found a session!
$login = $log->time; $login = $log->$timefield;
} else { } else {
$totaltime += $delay; $totaltime += $delay;
} }
// Now the actual log became the previous log for the next cycle. // Now the actual log became the previous log for the next cycle
$lasthit = $log->time; $lasthit = $log->$timefield;
} }
return $totaltime; return $totaltime;
@ -358,7 +387,6 @@ function customcert_get_course_time($courseid) {
return 0; return 0;
} }
/** /**
* Returns a list of issued customcerts. * Returns a list of issued customcerts.
* *

View file

@ -45,6 +45,16 @@ $PAGE->set_cm($cm);
$PAGE->set_title(format_string($customcert->name)); $PAGE->set_title(format_string($customcert->name));
$PAGE->set_heading(format_string($course->fullname)); $PAGE->set_heading(format_string($course->fullname));
// Check if the user can view the certificate based on time spent in course.
if ($customcert->requiredtime && !has_capability('mod/certificate:manage', $context)) {
if (customcert_get_course_time($course->id) < ($customcert->requiredtime * 60)) {
$a = new stdClass;
$a->requiredtime = $customcert->requiredtime;
notice(get_string('requiredtimenotmet', 'certificate', $a), "$CFG->wwwroot/course/view.php?id=$course->id");
die;
}
}
// Check that no action was passed, if so that means we are not outputting to PDF. // Check that no action was passed, if so that means we are not outputting to PDF.
if (empty($action)) { if (empty($action)) {
// Get the current groups mode. // Get the current groups mode.