if ($config = get_record('config', 'name', 'statsfirstrunlock')) { if (!empty($config->value)) { $clobber = false; // if we're on the first run, just don't clobber it. } } 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';
function stats_cron_monthly() { global $CFG; if (empty($CFG->enablestats)) { return STATS_RUN_ABORTED; } if (!($timestart = stats_get_start_from('monthly'))) { return STATS_RUN_ABORTED; } // check to make sure we're due to run, at least one month after last run $monthend = stats_get_base_monthly(); if (isset($CFG->statslastmonthly) and time() - 31 * 24 * 60 * 60 <= $CFG->statslastmonthly) { return STATS_RUN_ABORTED; } mtrace("Running monthly statistics gathering..."); set_config('statslastmonthly', time()); $return = STATS_RUN_COMPLETE; // optimistic static $monthly_modules; if (empty($monthly_modules)) { $monthly_modules = array(); $mods = get_records("modules"); foreach ($mods as $mod) { $file = $CFG->dirroot . '/mod/' . $mod->name . '/lib.php'; if (!is_readable($file)) { continue; } require_once $file; $fname = $mod->name . '_get_monthly_stats'; if (function_exists($fname)) { $monthly_modules[$mod] = $fname; } } } $nextmonthend = stats_get_next_monthend($timestart); if (!($courses = get_records('course', '', '', '', 'id,1'))) { return STATS_RUN_ABORTED; } $months = 0; mtrace("starting from {$timestart}"); while ($monthend > $nextmonthend && $timestart < $nextmonthend) { $timesql = " (timeend > {$timestart} AND timeend < {$nextmonthend}) "; begin_sql(); foreach ($courses as $course) { // enrolment first $sql = 'SELECT roleid, ceil(avg(stat1)) AS stat1, ceil(avg(stat2)) AS stat2 FROM ' . $CFG->prefix . 'stats_daily WHERE courseid = ' . $course->id . ' AND ' . $timesql . ' AND stattype = \'enrolments\' GROUP BY roleid'; if ($rolestats = get_records_sql($sql)) { foreach ($rolestats as $stat) { $stat->courseid = $course->id; $stat->timeend = $nextmonthend; $stat->stattype = 'enrolments'; insert_record('stats_monthly', $stat, false); // don't worry about the return id, we don't need it. } } // activity $sql = 'SELECT roleid, sum(stat1) AS stat1, sum(stat2) as stat2 FROM ' . $CFG->prefix . 'stats_daily WHERE courseid = ' . $course->id . ' AND ' . $timesql . ' AND stattype = \'activity\' GROUP BY roleid'; if ($rolestats = get_records_sql($sql)) { foreach ($rolestats as $stat) { $stat->courseid = $course->id; $stat->timeend = $nextmonthend; $stat->stattype = 'activity'; unset($stat->id); insert_record('stats_monthly', $stat, false); // don't worry about the return id, we don't need it. } } // logins if ($course->id == SITEID) { $sql = 'SELECT sum(stat1) AS stat1 FROM ' . $CFG->prefix . 'stats_daily WHERE courseid = ' . $course->id . ' AND ' . $timesql . ' AND stattype = \'logins\''; if ($stat = get_record_sql($sql)) { if (empty($stat->stat1)) { $stat->stat1 = 0; } $stat->courseid = $course->id; $stat->roleid = 0; $stat->timeend = $nextmonthend; $stat->stattype = 'logins'; $sql = 'SELECT COUNT(DISTINCT(l.userid)) FROM ' . $CFG->prefix . 'log l WHERE l.action = \'login\' AND ' . str_replace('timeend', 'time', $timesql); $stat->stat2 = count_records_sql($sql); insert_record('stats_monthly', $stat, false); // don't worry about the return id, we don't need it. } } $users = stats_get_course_users($course, $timesql); foreach ($users as $user) { stats_do_aggregate_user_cron($course, $user, $user->primaryrole, $timesql, $nextmonthend, 'monthly', $monthly_modules); } } stats_do_aggregate_user_login_cron($timesql, $nextmonthend, 'monthly'); commit_sql(); $timestart = $nextmonthend; $nextmonthend = stats_get_next_monthend($timestart); $months++; if (!stats_check_runtime()) { mtrace("Stopping early! reached maxruntime"); break; $return = STATS_RUN_ABORTED; } } mtrace("got up to {$timestart}"); mtrace("Completed {$months} months"); return $return; }