} if (set_cron_lock('statsrunning', true, $time, $clobber)) { require_once $CFG->dirroot . '/lib/statslib.php'; $return = stats_cron_daily(); if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) { stats_cron_weekly(); } if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) { $return = $return && stats_cron_monthly(); } if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) { stats_clean_old(); } set_cron_lock('statsrunning', false); if (!empty($firsttime)) { set_cron_lock('statsfirstrunlock', false); } } } } // run gradebook import/export/report cron if ($gradeimports = get_list_of_plugins('grade/import')) { foreach ($gradeimports as $gradeimport) { if (file_exists($CFG->dirroot . '/grade/import/' . $gradeimport . '/lib.php')) { require_once $CFG->dirroot . '/grade/import/' . $gradeimport . '/lib.php'; $cron_function = 'grade_import_' . $gradeimport . '_cron'; if (function_exists($cron_function)) { mtrace("Processing gradebook import function {$cron_function} ...", ''); $cron_function; } }
/** * Execute monthly statistics gathering * @return boolean success */ function stats_cron_monthly() { global $CFG, $DB; $now = time(); // read last execution date from db if (!($timestart = get_config(NULL, 'statslastmonthly'))) { $timestart = stats_get_base_monthly(stats_get_start_from('monthly')); set_config('statslastmonthly', $timestart); } $nextstartmonth = stats_get_next_month_start($timestart); // are there any months that need to be processed? if ($now < $nextstartmonth) { return true; // everything ok and up-to-date } $timeout = empty($CFG->statsmaxruntime) ? 60 * 60 * 24 : $CFG->statsmaxruntime; if (!set_cron_lock('statsrunning', $now + $timeout)) { return false; } // fisr delete entries that should not be there yet $DB->delete_records_select('stats_monthly', "timeend > {$timestart}"); $DB->delete_records_select('stats_user_monthly', "timeend > {$timestart}"); $startmonth = stats_get_base_monthly($now); mtrace("Running monthly statistics gathering, starting at {$timestart}:"); $months = 0; while ($now > $nextstartmonth) { @set_time_limit($timeout - 200); $months++; if ($months > 1) { // move the lock set_cron_lock('statsrunning', time() + $timeout, true); } $logtimesql = "l.time >= {$timestart} AND l.time < {$nextstartmonth}"; $stattimesql = "timeend > {$timestart} AND timeend <= {$nextstartmonth}"; $monthstart = time(); stats_progress('init'); /// process login info first $sql = "INSERT INTO {stats_user_monthly} (stattype, timeend, courseid, userid, statsreads)\n\n SELECT 'logins', timeend, courseid, userid, COUNT(statsreads)\n FROM (\n SELECT {$nextstartmonth} AS timeend, " . SITEID . " as courseid, l.userid, l.id AS statsreads\n FROM {log} l\n WHERE action = 'login' AND {$logtimesql}\n ) inline_view\n GROUP BY timeend, courseid, userid"; $DB->execute($sql); stats_progress('1'); $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)\n\n SELECT 'logins' AS stattype, {$nextstartmonth} AS timeend, " . SITEID . " as courseid, 0,\n COALESCE((SELECT SUM(statsreads)\n FROM {stats_user_monthly} s1\n WHERE s1.stattype = 'logins' AND timeend = {$nextstartmonth}), 0) AS nstat1,\n (SELECT COUNT('x')\n FROM {stats_user_monthly} s2\n WHERE s2.stattype = 'logins' AND timeend = {$nextstartmonth}) AS nstat2" . $DB->sql_null_from_clause(); $DB->execute($sql); stats_progress('2'); /// now enrolments averages $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)\n\n SELECT 'enrolments', ntimeend, courseid, roleid, " . $DB->sql_ceil('AVG(stat1)') . ", " . $DB->sql_ceil('AVG(stat2)') . "\n FROM (\n SELECT {$nextstartmonth} AS ntimeend, courseid, roleid, stat1, stat2\n FROM {stats_daily} sd\n WHERE stattype = 'enrolments' AND {$stattimesql}\n ) inline_view\n GROUP BY ntimeend, courseid, roleid"; $DB->execute($sql); stats_progress('3'); /// activity read/write averages $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2)\n\n SELECT 'activity', ntimeend, courseid, roleid, SUM(stat1), SUM(stat2)\n FROM (\n SELECT {$nextstartmonth} AS ntimeend, courseid, roleid, stat1, stat2\n FROM {stats_daily}\n WHERE stattype = 'activity' AND {$stattimesql}\n ) inline_view\n GROUP BY ntimeend, courseid, roleid"; $DB->execute($sql); stats_progress('4'); /// user read/write averages $sql = "INSERT INTO {stats_user_monthly} (stattype, timeend, courseid, userid, statsreads, statswrites)\n\n SELECT 'activity', ntimeend, courseid, userid, SUM(statsreads), SUM(statswrites)\n FROM (\n SELECT {$nextstartmonth} AS ntimeend, courseid, userid, statsreads, statswrites\n FROM {stats_user_daily}\n WHERE stattype = 'activity' AND {$stattimesql}\n ) inline_view\n GROUP BY ntimeend, courseid, userid"; $DB->execute($sql); stats_progress('5'); set_config('statslastmonthly', $nextstartmonth); $elapsed = time() - $monthstart; mtrace(" finished until {$nextstartmonth}: " . userdate($nextstartmonth) . " (in {$elapsed} s)"); $timestart = $nextstartmonth; $nextstartmonth = stats_get_next_month_start($nextstartmonth); } set_cron_lock('statsrunning', null); mtrace("...completed {$months} months of statistics."); return true; }
/** * Get one unjudged submission and set it as judged * If all submissions have been judged, return false * The function can be reentranced */ function get_unjudged_submission() { global $CFG; while (!set_cron_lock('assignment_judging', time() + 10)) { } //set_cron_lock('assignment_judging', time()+10); $sql = 'SELECT sub.*, epsub.judged, epsub.submission, epsub.id AS epsubid ' . 'FROM ' . $CFG->prefix . 'assignment_submissions AS sub, ' . $CFG->prefix . 'assignment_oj_submissions AS epsub ' . 'WHERE ' . 'sub.id = epsub.submission ' . 'AND epsub.judged = 0 '; $submissions = get_records_sql($sql, '', 1); $submission = null; if ($submissions) { $submission = array_pop($submissions); // Set judged mark set_field('assignment_oj_submissions', 'judged', 1, 'id', $submission->epsubid); } set_cron_lock('assignment_judging', null); return $submission; }
/** * Execute monthly statistics gathering * @return boolean success */ function stats_cron_monthly() { global $CFG, $DB; $now = time(); // read last execution date from db if (!$timestart = get_config(NULL, 'statslastmonthly')) { $timestart = stats_get_base_monthly(stats_get_start_from('monthly')); set_config('statslastmonthly', $timestart); } $nextstartmonth = stats_get_next_month_start($timestart); // are there any months that need to be processed? if ($now < $nextstartmonth) { return true; // everything ok and up-to-date } $timeout = empty($CFG->statsmaxruntime) ? 60*60*24 : $CFG->statsmaxruntime; if (!set_cron_lock('statsrunning', $now + $timeout)) { return false; } // fisr delete entries that should not be there yet $DB->delete_records_select('stats_monthly', "timeend > $timestart"); $DB->delete_records_select('stats_user_monthly', "timeend > $timestart"); $startmonth = stats_get_base_monthly($now); mtrace("Running monthly statistics gathering, starting at $timestart:"); cron_trace_time_and_memory(); $months = 0; while ($now > $nextstartmonth) { core_php_time_limit::raise($timeout - 200); $months++; if ($months > 1) { // move the lock set_cron_lock('statsrunning', time() + $timeout, true); } $stattimesql = "timeend > $timestart AND timeend <= $nextstartmonth"; $monthstart = time(); stats_progress('init'); /// process login info first $sql = "INSERT INTO {stats_user_monthly} (stattype, timeend, courseid, userid, statsreads) SELECT 'logins', timeend, courseid, userid, SUM(statsreads) FROM ( SELECT $nextstartmonth AS timeend, courseid, userid, statsreads FROM {stats_user_daily} sd WHERE stattype = 'logins' AND $stattimesql ) inline_view GROUP BY timeend, courseid, userid HAVING SUM(statsreads) > 0"; $DB->execute($sql); stats_progress('1'); $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2) SELECT 'logins' AS stattype, $nextstartmonth AS timeend, ".SITEID." as courseid, 0, COALESCE((SELECT SUM(statsreads) FROM {stats_user_monthly} s1 WHERE s1.stattype = 'logins' AND timeend = $nextstartmonth), 0) AS nstat1, (SELECT COUNT('x') FROM {stats_user_monthly} s2 WHERE s2.stattype = 'logins' AND timeend = $nextstartmonth) AS nstat2" . $DB->sql_null_from_clause(); $DB->execute($sql); stats_progress('2'); /// now enrolments averages $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2) SELECT 'enrolments', ntimeend, courseid, roleid, " . $DB->sql_ceil('AVG(stat1)') . ", " . $DB->sql_ceil('AVG(stat2)') . " FROM ( SELECT $nextstartmonth AS ntimeend, courseid, roleid, stat1, stat2 FROM {stats_daily} sd WHERE stattype = 'enrolments' AND $stattimesql ) inline_view GROUP BY ntimeend, courseid, roleid"; $DB->execute($sql); stats_progress('3'); /// activity read/write averages $sql = "INSERT INTO {stats_monthly} (stattype, timeend, courseid, roleid, stat1, stat2) SELECT 'activity', ntimeend, courseid, roleid, SUM(stat1), SUM(stat2) FROM ( SELECT $nextstartmonth AS ntimeend, courseid, roleid, stat1, stat2 FROM {stats_daily} WHERE stattype = 'activity' AND $stattimesql ) inline_view GROUP BY ntimeend, courseid, roleid"; $DB->execute($sql); stats_progress('4'); /// user read/write averages $sql = "INSERT INTO {stats_user_monthly} (stattype, timeend, courseid, userid, statsreads, statswrites) SELECT 'activity', ntimeend, courseid, userid, SUM(statsreads), SUM(statswrites) FROM ( SELECT $nextstartmonth AS ntimeend, courseid, userid, statsreads, statswrites FROM {stats_user_daily} WHERE stattype = 'activity' AND $stattimesql ) inline_view GROUP BY ntimeend, courseid, userid"; $DB->execute($sql); stats_progress('5'); set_config('statslastmonthly', $nextstartmonth); $elapsed = time() - $monthstart; mtrace(" finished until $nextstartmonth: ".userdate($nextstartmonth) ." (in $elapsed s)"); $timestart = $nextstartmonth; $nextstartmonth = stats_get_next_month_start($nextstartmonth); } set_cron_lock('statsrunning', null); mtrace("...completed $months months of statistics."); return true; }