Example #1
0
/**
 * 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 ...
 *
 * @return boolean
 * @todo Finish documenting this function
 **/
function checklist_cron()
{
    global $CFG, $DB;
    $lastcron = $DB->get_field('modules', 'lastcron', array('name' => 'checklist'));
    if (!$lastcron) {
        // First time run - checklists will take care of any updates before now
        return true;
    }
    require_once $CFG->dirroot . '/mod/checklist/autoupdate.php';
    if (!$CFG->checklist_autoupdate_use_cron) {
        mtrace("Checklist cron updates disabled");
        return true;
    }
    $lastlogtime = $lastcron - 5;
    // Subtract 5 seconds just in case a log slipped through during the last cron update
    // Find all autoupdating checklists
    $checklists = $DB->get_records_select('checklist', 'autopopulate > 0 AND autoupdate > 0');
    if (!$checklists) {
        // No checklists to update
        mtrace("No automatic update checklists found");
        return true;
    }
    // Match up these checklists with the courses they are in
    $courses = array();
    foreach ($checklists as $checklist) {
        if (array_key_exists($checklist->course, $courses)) {
            $courses[$checklist->course][$checklist->id] = $checklist;
        } else {
            $courses[$checklist->course] = array($checklist->id => $checklist);
        }
    }
    $courseids = implode(',', array_keys($courses));
    if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
        mtrace("Looking for updates in courses: {$courseids}");
    }
    // Process all logs since the last cron update
    $logupdate = 0;
    $totalcount = 0;
    $logs = get_logs("l.time >= ? AND l.course IN ({$courseids}) AND cmid > 0", array($lastlogtime), 'l.time ASC', '', '', $totalcount);
    if ($logs) {
        if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
            mtrace("Found " . count($logs) . " log updates to check");
        }
        foreach ($logs as $log) {
            $logupdate += checklist_autoupdate($log->course, $log->module, $log->action, $log->cmid, $log->userid, $log->url, $courses[$log->course]);
        }
    }
    if ($logupdate) {
        mtrace(" Updated {$logupdate} checkmark(s) from log changes");
    } else {
        mtrace(" No checkmarks need updating from log changes");
    }
    // Process all the completion changes since the last cron update
    // Need the cmid, userid and newstate
    $completionupdate = 0;
    list($msql, $mparam) = $DB->get_in_or_equal(array_keys($courses));
    $sql = 'SELECT c.id, c.coursemoduleid, c.userid, c.completionstate FROM {course_modules_completion} c ';
    $sql .= 'JOIN {course_modules} m ON c.coursemoduleid = m.id ';
    $sql .= "WHERE c.timemodified > ? AND m.course {$msql} ";
    $params = array_merge(array($lastlogtime), $mparam);
    $completions = $DB->get_records_sql($sql, $params);
    if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
        mtrace("Found " . count($completions) . " completion updates to check");
    }
    foreach ($completions as $completion) {
        $completionupdate += checklist_completion_autoupdate($completion->coursemoduleid, $completion->userid, $completion->completionstate);
    }
    if ($completionupdate) {
        mtrace(" Updated {$completionupdate} checkmark(s) from completion changes");
    } else {
        mtrace(" No checkmarks need updating from completion changes");
    }
    return true;
}
Example #2
0
 public static function update_from_event(\core\event\base $event)
 {
     global $CFG;
     require_once $CFG->dirroot . '/mod/checklist/autoupdate.php';
     if ($event->target == 'course_module_completion' && $event->action == 'updated') {
         // Update from a completion change event.
         $comp = $event->get_record_snapshot('course_modules_completion', $event->objectid);
         // Update any relevant checklists.
         checklist_completion_autoupdate($comp->coursemoduleid, $comp->userid, $comp->completionstate);
     } else {
         // Check if this is an action that counts as 'completing' an activity (when completion is off).
         $info = self::get_entry_info($event);
         if (!$info) {
             return;
         }
         // Update any relevant checklists.
         checklist_autoupdate_internal($info->course, $info->module, $info->cmid, $info->userid);
     }
 }