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); }
/** * 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; }
public function update_all_autoupdate_checks() { global $DB, $CFG; if (!$this->checklist->autoupdate) { return; } $users = get_users_by_capability($this->context, 'mod/checklist:updateown', 'u.id', '', '', '', '', '', false); if (!$users) { return; } $userids = array_keys($users); if ($CFG->branch < 26) { // No auto class loading before Moodle 2.6. require_once $CFG->dirroot . '/mod/checklist/classes/local/autoupdate.php'; } // Get a list of all the checklist items with a module linked to them (ignoring headings). $sql = "SELECT cm.id AS cmid, m.name AS mod_name, i.id AS itemid, cm.completion AS completion\n FROM {modules} m, {course_modules} cm, {checklist_item} i\n WHERE m.id = cm.module AND cm.id = i.moduleid AND i.moduleid > 0 AND i.checklist = ? AND i.itemoptional != 2"; $completion = new completion_info($this->course); $usingcompletion = $completion->is_enabled(); $items = $DB->get_records_sql($sql, array($this->checklist->id)); foreach ($items as $item) { if ($usingcompletion && $item->completion) { $fakecm = new stdClass(); $fakecm->id = $item->cmid; foreach ($users as $user) { $compdata = $completion->get_data($fakecm, false, $user->id); if ($compdata->completionstate == COMPLETION_COMPLETE || $compdata->completionstate == COMPLETION_COMPLETE_PASS) { $check = $DB->get_record('checklist_check', array('item' => $item->itemid, 'userid' => $user->id)); if ($check) { if ($check->usertimestamp) { continue; } $check->usertimestamp = time(); $DB->update_record('checklist_check', $check); } else { $check = new stdClass(); $check->item = $item->itemid; $check->userid = $user->id; $check->usertimestamp = time(); $check->teachertimestamp = 0; $check->teachermark = CHECKLIST_TEACHERMARK_UNDECIDED; $check->id = $DB->insert_record('checklist_check', $check); } } } continue; } $loguserids = mod_checklist\local\autoupdate::get_logged_userids($item->mod_name, $item->cmid, $userids); if (!$loguserids) { continue; } foreach ($loguserids as $loguserid) { $check = $DB->get_record('checklist_check', array('item' => $item->itemid, 'userid' => $loguserid)); if ($check) { if ($check->usertimestamp) { continue; } $check->usertimestamp = time(); $DB->update_record('checklist_check', $check); } else { $check = new stdClass(); $check->item = $item->itemid; $check->userid = $loguserid; $check->usertimestamp = time(); $check->teachertimestamp = 0; $check->teachermark = CHECKLIST_TEACHERMARK_UNDECIDED; $check->id = $DB->insert_record('checklist_check', $check); } } // Always update the grades. checklist_update_grades($this->checklist); } }