Exemplo n.º 1
0
function checklist_autoupdate($courseid, $module, $action, $cmid, $userid, $url)
{
    global $CFG;
    if ($userid == 0) {
        return 0;
    }
    if ($CFG->branch < 26) {
        require_once $CFG->dirroot . '/mod/checklist/classes/local/autoupdate.php';
    }
    $logactions = mod_checklist\local\autoupdate::get_log_actions_legacy($module);
    if (!in_array($action, $logactions)) {
        return 0;
    }
    if ($cmid == 0) {
        $matches = array();
        if (!preg_match('/id=(\\d+)/i', $url, $matches)) {
            return 0;
        }
        $cmid = $matches[1];
    }
    return checklist_autoupdate_internal($courseid, $module, $cmid, $userid);
}
Exemplo n.º 2
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
 **/
function checklist_cron()
{
    global $CFG, $DB;
    if ($CFG->branch >= 27) {
        // In Moodle 2.7+, all updates happen via events, so cron is no longer needed.
        return true;
    }
    $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 = array_keys($courses);
    if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
        mtrace("Looking for updates in courses: " . implode(', ', $courseids));
    }
    $logupdate = 0;
    $logs = mod_checklist\local\autoupdate::get_logs($courseids, $lastlogtime);
    if ($logs) {
        if (defined("DEBUG_CHECKLIST_AUTOUPDATE")) {
            mtrace("Found " . count($logs) . " log updates to check");
        }
        foreach ($logs as $log) {
            $logupdate += checklist_autoupdate_internal($log->course, $log->module, $log->cmid, $log->userid, $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;
}
Exemplo n.º 3
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);
     }
 }