From a61997b2aa8e510c2a9d28f0f3ad477afc7f4e2d Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 11 Mar 2015 00:29:18 -0700 Subject: [PATCH] Check course time using new logging API --- locallib.php | 54 +++++++++++++++++++++++++++++++++++++++------------- view.php | 10 ++++++++++ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/locallib.php b/locallib.php index 7b42a4b..cafd8d0 100644 --- a/locallib.php +++ b/locallib.php @@ -326,30 +326,59 @@ function customcert_get_templates() { * @return int the total time spent in seconds */ 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; - $sql = "l.course = :courseid AND l.userid = :userid"; - if ($logs = get_logs($sql, array('courseid' => $courseid, 'userid' => $USER->id), 'l.time ASC', '', '', $totalcount)) { + if ($logs = $DB->get_recordset_sql($sql, $params)) { foreach ($logs as $log) { if (!isset($login)) { - // For the first time $login is not set so the first log is also the first login. - $login = $log->time; - $lasthit = $log->time; + // For the first time $login is not set so the first log is also the first login + $login = $log->$timefield; + $lasthit = $log->$timefield; $totaltime = 0; } - $delay = $log->time - $lasthit; + $delay = $log->$timefield - $lasthit; if ($delay > ($CFG->sessiontimeout * 60)) { // The difference between the last log and the current log is more than - // the timeout register session value meaning we have found a session. - $login = $log->time; + // the timeout Register session value so that we have found a session! + $login = $log->$timefield; } else { $totaltime += $delay; } - // Now the actual log became the previous log for the next cycle. - $lasthit = $log->time; + // Now the actual log became the previous log for the next cycle + $lasthit = $log->$timefield; } return $totaltime; @@ -358,7 +387,6 @@ function customcert_get_course_time($courseid) { return 0; } - /** * Returns a list of issued customcerts. * diff --git a/view.php b/view.php index c946d86..c7c2254 100644 --- a/view.php +++ b/view.php @@ -45,6 +45,16 @@ $PAGE->set_cm($cm); $PAGE->set_title(format_string($customcert->name)); $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. if (empty($action)) { // Get the current groups mode.