2007-10-03 18:39:38 +00:00
|
|
|
<?php // $Id$
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-04-27 06:21:15 +00:00
|
|
|
/// This script looks through all the module directories for cron.php files
|
|
|
|
/// and runs them. These files can contain cleanup functions, email functions
|
|
|
|
/// or anything that needs to be run on a regular basis.
|
|
|
|
///
|
|
|
|
/// This file is best run from cron on the host system (ie outside PHP).
|
|
|
|
/// The script can either be invoked via the web server or via a standalone
|
|
|
|
/// version of PHP compiled for CGI.
|
|
|
|
///
|
|
|
|
/// eg wget -q -O /dev/null 'http://moodle.somewhere.edu/admin/cron.php'
|
|
|
|
/// or php /web/moodle/admin/cron.php
|
2007-05-23 06:02:59 +00:00
|
|
|
set_time_limit(0);
|
2003-02-01 01:36:31 +00:00
|
|
|
$starttime = microtime();
|
2004-12-12 06:49:26 +00:00
|
|
|
|
|
|
|
/// The following is a hack necessary to allow this script to work well
|
|
|
|
/// from the command line.
|
|
|
|
|
|
|
|
define('FULLME', 'cron');
|
2007-01-28 21:18:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Do not set moodle cookie because we do not need it here, it is better to emulate session
|
2008-06-25 17:31:23 +00:00
|
|
|
define('NO_MOODLE_COOKIES', true);
|
2007-01-28 21:18:08 +00:00
|
|
|
|
2004-05-18 13:38:58 +00:00
|
|
|
/// The current directory in PHP version 4.3.0 and above isn't necessarily the
|
|
|
|
/// directory of the script when run from the command line. The require_once()
|
|
|
|
/// would fail, so we'll have to chdir()
|
|
|
|
|
|
|
|
if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
|
|
|
|
chdir(dirname($_SERVER['argv'][0]));
|
|
|
|
}
|
2003-02-01 01:36:31 +00:00
|
|
|
|
2006-06-06 14:12:04 +00:00
|
|
|
require_once(dirname(__FILE__) . '/../config.php');
|
2006-09-02 13:14:57 +00:00
|
|
|
require_once($CFG->libdir.'/adminlib.php');
|
2007-08-09 09:38:14 +00:00
|
|
|
require_once($CFG->libdir.'/gradelib.php');
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2007-10-07 14:52:29 +00:00
|
|
|
/// Extra debugging (set in config.php)
|
|
|
|
if (!empty($CFG->showcronsql)) {
|
2008-05-15 21:40:00 +00:00
|
|
|
$DB->set_debug(true);
|
2007-10-07 14:52:29 +00:00
|
|
|
}
|
|
|
|
if (!empty($CFG->showcrondebugging)) {
|
|
|
|
$CFG->debug = DEBUG_DEVELOPER;
|
|
|
|
$CFG->displaydebug = true;
|
|
|
|
}
|
|
|
|
|
2007-01-28 21:18:08 +00:00
|
|
|
/// extra safety
|
|
|
|
@session_write_close();
|
|
|
|
|
|
|
|
/// check if execution allowed
|
2006-11-15 08:29:24 +00:00
|
|
|
if (isset($_SERVER['REMOTE_ADDR'])) { // if the script is accessed via the web.
|
|
|
|
if (!empty($CFG->cronclionly)) {
|
|
|
|
// This script can only be run via the cli.
|
|
|
|
print_error('cronerrorclionly', 'admin');
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
// This script is being called via the web, so check the password if there is one.
|
|
|
|
if (!empty($CFG->cronremotepassword)) {
|
|
|
|
$pass = optional_param('password', '', PARAM_RAW);
|
|
|
|
if($pass != $CFG->cronremotepassword) {
|
|
|
|
// wrong password.
|
|
|
|
print_error('cronerrorpassword', 'admin');
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-25 14:51:04 +00:00
|
|
|
|
2007-01-28 21:18:08 +00:00
|
|
|
/// emulate normal session
|
|
|
|
$USER = get_admin(); /// Temporarily, to provide environment for this script
|
2005-04-18 20:13:36 +00:00
|
|
|
|
2007-01-28 21:18:08 +00:00
|
|
|
/// ignore admins timezone, language and locale - use site deafult instead!
|
|
|
|
$USER->timezone = $CFG->timezone;
|
2006-07-20 06:36:33 +00:00
|
|
|
$USER->lang = '';
|
2007-01-28 21:18:08 +00:00
|
|
|
$USER->theme = '';
|
|
|
|
course_setup(SITEID);
|
2006-07-20 06:36:33 +00:00
|
|
|
|
|
|
|
/// send mime type and encoding
|
2006-10-26 08:55:56 +00:00
|
|
|
if (check_browser_version('MSIE')) {
|
|
|
|
//ugly IE hack to work around downloading instead of viewing
|
2006-11-11 17:23:20 +00:00
|
|
|
@header('Content-Type: text/html; charset=utf-8');
|
2006-10-26 08:55:56 +00:00
|
|
|
echo "<xmp>"; //<pre> is not good enough for us here
|
|
|
|
} else {
|
|
|
|
//send proper plaintext header
|
2006-11-11 17:23:20 +00:00
|
|
|
@header('Content-Type: text/plain; charset=utf-8');
|
2006-10-26 08:55:56 +00:00
|
|
|
}
|
2006-07-20 06:36:33 +00:00
|
|
|
|
2007-01-28 21:18:08 +00:00
|
|
|
/// no more headers and buffers
|
|
|
|
while(@ob_end_flush());
|
|
|
|
|
2007-02-14 20:46:38 +00:00
|
|
|
/// increase memory limit (PHP 5.2 does different calculation, we need more memory now)
|
|
|
|
@raise_memory_limit('128M');
|
|
|
|
|
2004-08-07 05:56:03 +00:00
|
|
|
/// Start output log
|
2001-11-22 06:23:56 +00:00
|
|
|
|
|
|
|
$timenow = time();
|
2004-08-07 05:56:03 +00:00
|
|
|
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Server Time: ".date('r',$timenow)."\n\n");
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-07-27 12:20:29 +00:00
|
|
|
/// Run all cron jobs for each module
|
2001-12-04 14:02:36 +00:00
|
|
|
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Starting activity modules");
|
2008-02-01 07:05:58 +00:00
|
|
|
get_mailer('buffer');
|
2008-05-30 21:16:16 +00:00
|
|
|
if ($mods = $DB->get_records_select("modules", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
|
2001-11-22 06:23:56 +00:00
|
|
|
foreach ($mods as $mod) {
|
2002-07-27 06:09:54 +00:00
|
|
|
$libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
|
|
|
|
if (file_exists($libfile)) {
|
|
|
|
include_once($libfile);
|
|
|
|
$cron_function = $mod->name."_cron";
|
|
|
|
if (function_exists($cron_function)) {
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Processing module function $cron_function ...", '');
|
2008-02-26 10:34:51 +00:00
|
|
|
$pre_dbqueries = null;
|
2008-06-18 08:26:41 +00:00
|
|
|
$pre_dbqueries = $DB->perf_get_queries();
|
2008-06-16 21:01:54 +00:00
|
|
|
$pre_time = microtime(1);
|
2002-07-27 06:09:54 +00:00
|
|
|
if ($cron_function()) {
|
2008-05-30 21:16:16 +00:00
|
|
|
if (!$DB->set_field("modules", "lastcron", $timenow, array("id"=>$mod->id))) {
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Error: could not update timestamp for $mod->fullname");
|
2002-07-27 06:09:54 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-12 02:57:58 +00:00
|
|
|
if (isset($pre_dbqueries)) {
|
2008-06-18 08:26:41 +00:00
|
|
|
mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
|
2007-09-12 02:57:58 +00:00
|
|
|
mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
|
|
|
|
}
|
2007-10-04 17:42:30 +00:00
|
|
|
/// Reset possible changes by modules to time_limit. MDL-11597
|
|
|
|
@set_time_limit(0);
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("done.");
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-01 07:05:58 +00:00
|
|
|
get_mailer('close');
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Finished activity modules");
|
2001-12-04 14:02:36 +00:00
|
|
|
|
2006-02-14 03:24:08 +00:00
|
|
|
mtrace("Starting blocks");
|
2008-05-30 21:16:16 +00:00
|
|
|
if ($blocks = $DB->get_records_select("block", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
|
2006-02-14 03:24:08 +00:00
|
|
|
// we will need the base class.
|
|
|
|
require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
|
|
|
|
foreach ($blocks as $block) {
|
|
|
|
$blockfile = $CFG->dirroot.'/blocks/'.$block->name.'/block_'.$block->name.'.php';
|
|
|
|
if (file_exists($blockfile)) {
|
|
|
|
require_once($blockfile);
|
|
|
|
$classname = 'block_'.$block->name;
|
|
|
|
$blockobj = new $classname;
|
|
|
|
if (method_exists($blockobj,'cron')) {
|
|
|
|
mtrace("Processing cron function for ".$block->name.'....','');
|
|
|
|
if ($blockobj->cron()) {
|
2008-05-30 21:16:16 +00:00
|
|
|
if (!$DB->set_field('block', 'lastcron', $timenow, array('id'=>$block->id))) {
|
2006-02-14 03:24:08 +00:00
|
|
|
mtrace('Error: could not update timestamp for '.$block->name);
|
|
|
|
}
|
|
|
|
}
|
2007-10-04 17:55:59 +00:00
|
|
|
/// Reset possible changes by blocks to time_limit. MDL-11597
|
|
|
|
@set_time_limit(0);
|
2006-02-14 03:24:08 +00:00
|
|
|
mtrace('done.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2007-03-07 04:56:07 +00:00
|
|
|
mtrace('Finished blocks');
|
2006-02-14 03:24:08 +00:00
|
|
|
|
2008-07-24 17:36:33 +00:00
|
|
|
mtrace("Starting quiz reports");
|
|
|
|
if ($reports = $DB->get_records_select('quiz_report', "cron > 0 AND ((? - lastcron) > cron)", array($timenow))) {
|
|
|
|
foreach ($reports as $report) {
|
|
|
|
$cronfile = "$CFG->dirroot/mod/quiz/report/$report->name/cron.php";
|
|
|
|
if (file_exists($cronfile)) {
|
|
|
|
include_once($cronfile);
|
|
|
|
$cron_function = 'quiz_report_'.$report->name."_cron";
|
|
|
|
if (function_exists($cron_function)) {
|
|
|
|
mtrace("Processing quiz report cron function $cron_function ...", '');
|
|
|
|
$pre_dbqueries = null;
|
|
|
|
$pre_dbqueries = $DB->perf_get_queries();
|
|
|
|
$pre_time = microtime(1);
|
|
|
|
if ($cron_function()) {
|
|
|
|
if (!$DB->set_field('quiz_report', "lastcron", $timenow, array("id"=>$report->id))) {
|
|
|
|
mtrace("Error: could not update timestamp for $report->name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($pre_dbqueries)) {
|
|
|
|
mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
|
|
|
|
mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
|
|
|
|
}
|
|
|
|
mtrace("done.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtrace("Finished quiz reports");
|
|
|
|
|
2008-02-26 10:34:51 +00:00
|
|
|
mtrace('Starting admin reports');
|
|
|
|
// Admin reports do not have a database table that lists them. Instead a
|
|
|
|
// report includes cron.php with function report_reportname_cron() if it wishes
|
|
|
|
// to be cronned. It is up to cron.php to handle e.g. if it only needs to
|
|
|
|
// actually do anything occasionally.
|
|
|
|
$reports = get_list_of_plugins($CFG->admin.'/report');
|
|
|
|
foreach($reports as $report) {
|
|
|
|
$cronfile = $CFG->dirroot.'/'.$CFG->admin.'/report/'.$report.'/cron.php';
|
|
|
|
if (file_exists($cronfile)) {
|
|
|
|
require_once($cronfile);
|
2008-07-25 16:18:28 +00:00
|
|
|
$cronfunction = 'report_'.$report.'_cron';
|
2008-02-26 10:34:51 +00:00
|
|
|
mtrace('Processing cron function for '.$report.'...', '');
|
|
|
|
$pre_dbqueries = null;
|
2008-06-18 08:26:41 +00:00
|
|
|
$pre_dbqueries = $DB->perf_get_queries();
|
2008-06-16 21:01:54 +00:00
|
|
|
$pre_time = microtime(true);
|
2008-02-26 10:34:51 +00:00
|
|
|
$cronfunction();
|
|
|
|
if (isset($pre_dbqueries)) {
|
2008-06-18 08:26:41 +00:00
|
|
|
mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
|
2008-02-26 10:34:51 +00:00
|
|
|
mtrace("... used " . round(microtime(true) - $pre_time, 2) . " seconds");
|
|
|
|
}
|
|
|
|
mtrace('done.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mtrace('Finished admin reports');
|
|
|
|
|
2005-04-20 07:29:28 +00:00
|
|
|
if (!empty($CFG->langcache)) {
|
|
|
|
mtrace('Updating languages cache');
|
2007-04-24 16:14:16 +00:00
|
|
|
get_list_of_languages(true);
|
2005-04-20 07:29:28 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 04:56:07 +00:00
|
|
|
mtrace('Removing expired enrolments ...', ''); // See MDL-8785
|
|
|
|
$timenow = time();
|
2007-07-04 02:24:05 +00:00
|
|
|
$somefound = false;
|
2008-06-15 11:35:25 +00:00
|
|
|
// The preferred way saves memory, datablib
|
2007-07-04 02:24:05 +00:00
|
|
|
// find courses where limited enrolment is enabled
|
2008-05-30 21:16:16 +00:00
|
|
|
$sql = "SELECT ra.roleid, ra.userid, ra.contextid
|
|
|
|
FROM {course} c
|
|
|
|
JOIN {context} cx ON cx.instanceid = c.id
|
|
|
|
JOIN {role_assignments} ra ON ra.contextid = cx.id
|
|
|
|
WHERE cx.contextlevel = '".CONTEXT_COURSE."'
|
|
|
|
AND ra.timeend > 0
|
|
|
|
AND ra.timeend < ?
|
|
|
|
AND c.enrolperiod > 0";
|
|
|
|
if ($rs = $DB->get_recordset_sql($sql, array($timenow))) {
|
|
|
|
foreach ($rs as $oldenrolment) {
|
|
|
|
role_unassign($oldenrolment->roleid, $oldenrolment->userid, 0, $oldenrolment->contextid);
|
|
|
|
$somefound = true;
|
|
|
|
}
|
|
|
|
$rs->close();
|
2007-07-04 02:24:05 +00:00
|
|
|
}
|
2008-05-30 21:16:16 +00:00
|
|
|
if ($somefound) {
|
2007-03-07 04:56:07 +00:00
|
|
|
mtrace('Done');
|
|
|
|
} else {
|
|
|
|
mtrace('none found');
|
|
|
|
}
|
|
|
|
|
2005-04-20 07:29:28 +00:00
|
|
|
|
2008-02-26 10:34:51 +00:00
|
|
|
|
2007-11-08 08:59:26 +00:00
|
|
|
mtrace('Starting main gradebook job ...');
|
2007-08-09 09:38:14 +00:00
|
|
|
grade_cron();
|
2007-11-08 08:59:26 +00:00
|
|
|
mtrace('done.');
|
2007-08-09 09:38:14 +00:00
|
|
|
|
|
|
|
|
2008-07-21 16:04:14 +00:00
|
|
|
mtrace('Starting processing the event queue...');
|
|
|
|
events_cron();
|
|
|
|
mtrace('done.');
|
|
|
|
|
2008-08-26 05:45:07 +00:00
|
|
|
if ($CFG->enableportfolios) {
|
2008-08-11 12:50:26 +00:00
|
|
|
// Portfolio cron
|
|
|
|
mtrace('Starting the portfolio cron...');
|
|
|
|
require_once($CFG->libdir . '/portfoliolib.php');
|
|
|
|
portfolio_cron();
|
|
|
|
mtrace('done');
|
|
|
|
}
|
2008-07-21 16:04:14 +00:00
|
|
|
|
2003-07-27 12:20:29 +00:00
|
|
|
/// Run all core cron jobs, but not every time since they aren't too important.
|
|
|
|
/// These don't have a timer to reduce load, so we'll use a random number
|
|
|
|
/// to randomly choose the percentage of times we should run these jobs.
|
2001-12-04 14:02:36 +00:00
|
|
|
|
2003-07-27 12:20:29 +00:00
|
|
|
srand ((double) microtime() * 10000000);
|
|
|
|
$random100 = rand(0,100);
|
2001-12-04 14:02:36 +00:00
|
|
|
|
2003-07-27 12:20:29 +00:00
|
|
|
if ($random100 < 20) { // Approximately 20% of the time.
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Running clean-up tasks...");
|
2003-07-27 12:20:29 +00:00
|
|
|
|
|
|
|
/// Unenrol users who haven't logged in for $CFG->longtimenosee
|
|
|
|
|
|
|
|
if ($CFG->longtimenosee) { // value in days
|
2007-10-03 18:39:38 +00:00
|
|
|
$cuttime = $timenow - ($CFG->longtimenosee * 3600 * 24);
|
2008-05-30 21:16:16 +00:00
|
|
|
$rs = $DB->get_recordset_sql ("SELECT id, userid, courseid
|
|
|
|
FROM {user_lastaccess}
|
|
|
|
WHERE courseid != ".SITEID."
|
|
|
|
AND timeaccess < ?", array($cuttime));
|
|
|
|
foreach ($rs as $assign) {
|
2007-10-03 18:39:38 +00:00
|
|
|
if ($context = get_context_instance(CONTEXT_COURSE, $assign->courseid)) {
|
|
|
|
if (role_unassign(0, $assign->userid, 0, $context->id)) {
|
|
|
|
mtrace("Deleted assignment for user $assign->userid from course $assign->courseid");
|
2003-07-27 12:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 21:16:16 +00:00
|
|
|
$rs->close();
|
2007-10-05 22:27:55 +00:00
|
|
|
/// Execute the same query again, looking for remaining records and deleting them
|
|
|
|
/// if the user hasn't moodle/course:view in the CONTEXT_COURSE context (orphan records)
|
2008-05-30 21:16:16 +00:00
|
|
|
$rs = $DB->get_recordset_sql ("SELECT id, userid, courseid
|
|
|
|
FROM {user_lastaccess}
|
|
|
|
WHERE courseid != ".SITEID."
|
|
|
|
AND timeaccess < ?", array($cuttime));
|
|
|
|
foreach ($rs as $assign) {
|
2007-10-05 22:27:55 +00:00
|
|
|
if ($context = get_context_instance(CONTEXT_COURSE, $assign->courseid)) {
|
|
|
|
if (!has_capability('moodle/course:view', $context, $assign->userid)) {
|
2008-05-30 21:16:16 +00:00
|
|
|
$DB->delete_records('user_lastaccess', array('userid'=>$assign->userid, 'courseid'=>$assign->courseid));
|
2007-10-05 22:27:55 +00:00
|
|
|
mtrace("Deleted orphan user_lastaccess for user $assign->userid from course $assign->courseid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-30 21:16:16 +00:00
|
|
|
$rs->close();
|
2003-07-27 12:20:29 +00:00
|
|
|
}
|
2007-10-03 18:39:38 +00:00
|
|
|
flush();
|
|
|
|
|
|
|
|
|
2004-03-16 10:59:42 +00:00
|
|
|
/// Delete users who haven't confirmed within required period
|
2004-03-01 22:53:58 +00:00
|
|
|
|
2006-10-17 20:47:12 +00:00
|
|
|
if (!empty($CFG->deleteunconfirmed)) {
|
2007-10-03 18:39:38 +00:00
|
|
|
$cuttime = $timenow - ($CFG->deleteunconfirmed * 3600);
|
2008-05-30 21:16:16 +00:00
|
|
|
$rs = $DB->get_recordset_sql ("SELECT id, firstname, lastname
|
|
|
|
FROM {user}
|
|
|
|
WHERE confirmed = 0 AND firstaccess > 0
|
|
|
|
AND firstaccess < ?", array($cuttime));
|
|
|
|
foreach ($rs as $user) {
|
|
|
|
if ($DB->delete_records('user', array('id'=>$user->id))) {
|
2007-10-03 18:39:38 +00:00
|
|
|
mtrace("Deleted unconfirmed user for ".fullname($user, true)." ($user->id)");
|
2002-08-05 08:40:00 +00:00
|
|
|
}
|
2002-08-05 07:59:11 +00:00
|
|
|
}
|
2008-05-30 21:16:16 +00:00
|
|
|
$rs->close();
|
2002-08-05 07:59:11 +00:00
|
|
|
}
|
2004-08-05 17:03:19 +00:00
|
|
|
flush();
|
2005-11-08 07:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Delete users who haven't completed profile within required period
|
|
|
|
|
2008-07-03 14:09:27 +00:00
|
|
|
if (!empty($CFG->deleteincompleteusers)) {
|
|
|
|
$cuttime = $timenow - ($CFG->deleteincompleteusers * 3600);
|
2008-05-30 21:16:16 +00:00
|
|
|
$rs = $DB->get_recordset_sql ("SELECT id, username
|
|
|
|
FROM {user}
|
|
|
|
WHERE confirmed = 1 AND lastaccess > 0
|
|
|
|
AND lastaccess < ? AND deleted = 0
|
|
|
|
AND (lastname = '' OR firstname = '' OR email = '')",
|
|
|
|
array($cuttime));
|
|
|
|
foreach ($rs as $user) {
|
2008-07-03 14:09:27 +00:00
|
|
|
if (delete_user($user)) {
|
2007-10-03 18:39:38 +00:00
|
|
|
mtrace("Deleted not fully setup user $user->username ($user->id)");
|
2005-11-08 07:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-30 21:16:16 +00:00
|
|
|
$rs->close();
|
2005-11-08 07:24:11 +00:00
|
|
|
}
|
|
|
|
flush();
|
|
|
|
|
|
|
|
|
2003-07-27 12:20:29 +00:00
|
|
|
/// Delete old logs to save space (this might need a timer to slow it down...)
|
2007-10-03 18:39:38 +00:00
|
|
|
|
2003-07-27 12:20:29 +00:00
|
|
|
if (!empty($CFG->loglifetime)) { // value in days
|
|
|
|
$loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
|
2008-05-30 21:16:16 +00:00
|
|
|
if ($DB->delete_records_select("log", "time < ?", array($loglifetime))) {
|
2007-10-03 18:39:38 +00:00
|
|
|
mtrace("Deleted old log records");
|
|
|
|
}
|
2003-07-27 12:20:29 +00:00
|
|
|
}
|
2004-08-05 17:03:19 +00:00
|
|
|
flush();
|
2004-02-09 12:33:02 +00:00
|
|
|
|
2007-10-03 18:39:38 +00:00
|
|
|
|
2004-02-09 12:33:02 +00:00
|
|
|
/// Delete old cached texts
|
|
|
|
|
|
|
|
if (!empty($CFG->cachetext)) { // Defined in config.php
|
2006-03-20 02:33:41 +00:00
|
|
|
$cachelifetime = time() - $CFG->cachetext - 60; // Add an extra minute to allow for really heavy sites
|
2008-05-30 21:16:16 +00:00
|
|
|
if ($DB->delete_records_select('cache_text', "timemodified < ?", array($cachelifetime))) {
|
2007-10-03 18:39:38 +00:00
|
|
|
mtrace("Deleted old cache_text records");
|
|
|
|
}
|
2004-02-09 12:33:02 +00:00
|
|
|
}
|
2004-08-05 17:03:19 +00:00
|
|
|
flush();
|
2004-07-25 14:14:56 +00:00
|
|
|
|
|
|
|
if (!empty($CFG->notifyloginfailures)) {
|
|
|
|
notify_login_failures();
|
2007-10-07 17:28:46 +00:00
|
|
|
mtrace('Notified login failured');
|
2004-07-25 14:14:56 +00:00
|
|
|
}
|
2004-08-05 17:03:19 +00:00
|
|
|
flush();
|
2004-07-25 14:14:56 +00:00
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
sync_metacourses();
|
2007-10-07 17:28:46 +00:00
|
|
|
mtrace('Synchronised metacourses');
|
2005-01-24 22:21:28 +00:00
|
|
|
|
2006-01-16 02:53:19 +00:00
|
|
|
//
|
|
|
|
// generate new password emails for users
|
|
|
|
//
|
|
|
|
mtrace('checking for create_password');
|
2008-05-30 21:16:16 +00:00
|
|
|
if ($DB->count_records('user_preferences', array('name'=>'create_password', 'value'=>'1'))) {
|
2006-01-16 02:53:19 +00:00
|
|
|
mtrace('creating passwords for new users');
|
2008-05-30 21:16:16 +00:00
|
|
|
$newusers = $DB->get_records_sql("SELECT u.id as id, u.email, u.firstname,
|
|
|
|
u.lastname, u.username,
|
|
|
|
p.id as prefid
|
|
|
|
FROM {user} u
|
|
|
|
JOIN {user_preferences} p ON u.id=p.userid
|
|
|
|
WHERE p.name='create_password' AND p.value=1 AND u.email !='' ");
|
2006-01-16 02:53:19 +00:00
|
|
|
|
|
|
|
foreach ($newusers as $newuserid => $newuser) {
|
|
|
|
$newuser->emailstop = 0; // send email regardless
|
|
|
|
// email user
|
|
|
|
if (setnew_password_and_mail($newuser)) {
|
|
|
|
// remove user pref
|
2008-05-30 21:16:16 +00:00
|
|
|
$DB->delete_records('user_preferences', array('id'=>$newuser->prefid));
|
2006-01-16 02:53:19 +00:00
|
|
|
} else {
|
|
|
|
trigger_error("Could not create and mail new user password!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-09 06:14:09 +00:00
|
|
|
|
2008-05-01 05:49:35 +00:00
|
|
|
if (!empty($CFG->usetags)) {
|
2007-08-04 01:51:18 +00:00
|
|
|
require_once($CFG->dirroot.'/tag/lib.php');
|
|
|
|
tag_cron();
|
2007-10-07 17:28:46 +00:00
|
|
|
mtrace ('Executed tag cron');
|
2007-08-04 01:51:18 +00:00
|
|
|
}
|
|
|
|
|
2007-09-19 07:29:20 +00:00
|
|
|
// Accesslib stuff
|
|
|
|
cleanup_contexts();
|
2007-10-07 17:28:46 +00:00
|
|
|
mtrace ('Cleaned up contexts');
|
2007-10-03 18:39:38 +00:00
|
|
|
gc_cache_flags();
|
2007-10-07 17:28:46 +00:00
|
|
|
mtrace ('Cleaned cache flags');
|
2007-09-19 07:29:20 +00:00
|
|
|
// If you suspect that the context paths are somehow corrupt
|
|
|
|
// replace the line below with: build_context_path(true);
|
|
|
|
build_context_path();
|
2007-10-07 17:28:46 +00:00
|
|
|
mtrace ('Built context paths');
|
|
|
|
|
|
|
|
mtrace("Finished clean-up tasks...");
|
2007-09-19 07:29:20 +00:00
|
|
|
|
2004-07-25 14:14:56 +00:00
|
|
|
} // End of occasional clean-up tasks
|
2003-04-27 06:21:15 +00:00
|
|
|
|
2003-12-01 08:01:41 +00:00
|
|
|
|
2006-09-03 13:10:56 +00:00
|
|
|
if (empty($CFG->disablescheduledbackups)) { // Defined in config.php
|
2004-02-07 05:41:58 +00:00
|
|
|
//Execute backup's cron
|
|
|
|
//Perhaps a long time and memory could help in large sites
|
2004-05-18 14:12:21 +00:00
|
|
|
@set_time_limit(0);
|
2007-02-14 20:58:31 +00:00
|
|
|
@raise_memory_limit("192M");
|
2006-01-16 03:15:32 +00:00
|
|
|
if (function_exists('apache_child_terminate')) {
|
|
|
|
// if we are running from Apache, give httpd a hint that
|
|
|
|
// it can recycle the process after it's done. Apache's
|
|
|
|
// memory management is truly awful but we can help it.
|
|
|
|
@apache_child_terminate();
|
|
|
|
}
|
2004-02-07 05:41:58 +00:00
|
|
|
if (file_exists("$CFG->dirroot/backup/backup_scheduled.php") and
|
|
|
|
file_exists("$CFG->dirroot/backup/backuplib.php") and
|
2004-04-22 13:49:45 +00:00
|
|
|
file_exists("$CFG->dirroot/backup/lib.php") and
|
|
|
|
file_exists("$CFG->libdir/blocklib.php")) {
|
2004-02-07 05:41:58 +00:00
|
|
|
include_once("$CFG->dirroot/backup/backup_scheduled.php");
|
|
|
|
include_once("$CFG->dirroot/backup/backuplib.php");
|
|
|
|
include_once("$CFG->dirroot/backup/lib.php");
|
2004-04-22 13:49:45 +00:00
|
|
|
require_once ("$CFG->libdir/blocklib.php");
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Running backups if required...");
|
2004-02-07 05:41:58 +00:00
|
|
|
|
|
|
|
if (! schedule_backup_cron()) {
|
2006-04-18 21:22:04 +00:00
|
|
|
mtrace("ERROR: Something went wrong while performing backup tasks!!!");
|
2004-02-07 05:41:58 +00:00
|
|
|
} else {
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Backup tasks finished.");
|
2004-02-07 05:41:58 +00:00
|
|
|
}
|
2003-12-13 23:57:16 +00:00
|
|
|
}
|
2003-12-12 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
2004-05-02 23:08:19 +00:00
|
|
|
if (!empty($CFG->enablerssfeeds)) { //Defined in admin/variables page
|
2005-01-25 17:08:05 +00:00
|
|
|
include_once("$CFG->libdir/rsslib.php");
|
|
|
|
mtrace("Running rssfeeds if required...");
|
2004-05-02 23:08:19 +00:00
|
|
|
|
2005-01-25 17:08:05 +00:00
|
|
|
if ( ! cron_rss_feeds()) {
|
|
|
|
mtrace("Something went wrong while generating rssfeeds!!!");
|
|
|
|
} else {
|
|
|
|
mtrace("Rssfeeds finished");
|
2004-05-02 23:08:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-25 03:44:29 +00:00
|
|
|
/// Run the enrolment cron, if any
|
2006-03-09 02:48:29 +00:00
|
|
|
if (!($plugins = explode(',', $CFG->enrol_plugins_enabled))) {
|
|
|
|
$plugins = array($CFG->enrol);
|
|
|
|
}
|
|
|
|
require_once($CFG->dirroot .'/enrol/enrol.class.php');
|
|
|
|
foreach ($plugins as $p) {
|
|
|
|
$enrol = enrolment_factory::factory($p);
|
|
|
|
if (method_exists($enrol, 'cron')) {
|
|
|
|
$enrol->cron();
|
|
|
|
}
|
|
|
|
if (!empty($enrol->log)) {
|
|
|
|
mtrace($enrol->log);
|
|
|
|
}
|
|
|
|
unset($enrol);
|
2004-07-29 14:53:48 +00:00
|
|
|
}
|
2004-06-25 03:44:29 +00:00
|
|
|
|
2007-01-04 03:19:49 +00:00
|
|
|
/// Run the auth cron, if any
|
2007-03-27 20:26:05 +00:00
|
|
|
$auths = get_enabled_auth_plugins();
|
|
|
|
|
2007-01-04 03:19:49 +00:00
|
|
|
mtrace("Running auth crons if required...");
|
|
|
|
foreach ($auths as $auth) {
|
|
|
|
$authplugin = get_auth_plugin($auth);
|
|
|
|
if (method_exists($authplugin, 'cron')) {
|
|
|
|
mtrace("Running cron for auth/$auth...");
|
|
|
|
$authplugin->cron();
|
|
|
|
if (!empty($authplugin->log)) {
|
|
|
|
mtrace($authplugin->log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($authplugin);
|
|
|
|
}
|
|
|
|
|
2006-09-03 15:46:21 +00:00
|
|
|
if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) {
|
2008-02-16 18:33:50 +00:00
|
|
|
require_once($CFG->dirroot.'/lib/statslib.php');
|
2005-10-10 00:46:44 +00:00
|
|
|
// check we're not before our runtime
|
2008-02-16 18:33:50 +00:00
|
|
|
$timetocheck = stats_get_base_daily() + $CFG->statsruntimestarthour*60*60 + $CFG->statsruntimestartminute*60;
|
2005-10-10 00:46:44 +00:00
|
|
|
|
|
|
|
if (time() > $timetocheck) {
|
2008-10-13 21:53:49 +00:00
|
|
|
// process configured number of days as max (defaulting to 31)
|
|
|
|
$maxdays = empty($CFG->statsruntimedays) ? 31 : abs($CFG->statsruntimedays);
|
|
|
|
if (stats_cron_daily($maxdays)) {
|
2008-02-16 18:33:50 +00:00
|
|
|
if (stats_cron_weekly()) {
|
|
|
|
if (stats_cron_monthly()) {
|
|
|
|
stats_clean_old();
|
|
|
|
}
|
2005-10-10 00:46:44 +00:00
|
|
|
}
|
2005-09-01 04:14:31 +00:00
|
|
|
}
|
2008-02-16 18:33:50 +00:00
|
|
|
@set_time_limit(0);
|
2008-02-18 03:13:46 +00:00
|
|
|
} else {
|
2008-02-24 13:29:55 +00:00
|
|
|
mtrace('Next stats run after:'. userdate($timetocheck));
|
2005-09-01 04:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-08 08:59:26 +00:00
|
|
|
|
2007-05-24 02:16:31 +00:00
|
|
|
// run gradebook import/export/report cron
|
|
|
|
if ($gradeimports = get_list_of_plugins('grade/import')) {
|
|
|
|
foreach ($gradeimports as $gradeimport) {
|
2007-11-08 08:59:26 +00:00
|
|
|
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';
|
2007-05-24 02:16:31 +00:00
|
|
|
if (function_exists($cron_function)) {
|
|
|
|
mtrace("Processing gradebook import function $cron_function ...", '');
|
|
|
|
$cron_function;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($gradeexports = get_list_of_plugins('grade/export')) {
|
|
|
|
foreach ($gradeexports as $gradeexport) {
|
2007-11-08 08:59:26 +00:00
|
|
|
if (file_exists($CFG->dirroot.'/grade/export/'.$gradeexport.'/lib.php')) {
|
|
|
|
require_once($CFG->dirroot.'/grade/export/'.$gradeexport.'/lib.php');
|
|
|
|
$cron_function = 'grade_export_'.$gradeexport.'_cron';
|
2007-05-24 02:16:31 +00:00
|
|
|
if (function_exists($cron_function)) {
|
|
|
|
mtrace("Processing gradebook export function $cron_function ...", '');
|
|
|
|
$cron_function;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-08 08:59:26 +00:00
|
|
|
|
2007-05-24 02:16:31 +00:00
|
|
|
if ($gradereports = get_list_of_plugins('grade/report')) {
|
|
|
|
foreach ($gradereports as $gradereport) {
|
2007-11-08 08:59:26 +00:00
|
|
|
if (file_exists($CFG->dirroot.'/grade/report/'.$gradereport.'/lib.php')) {
|
|
|
|
require_once($CFG->dirroot.'/grade/report/'.$gradereport.'/lib.php');
|
|
|
|
$cron_function = 'grade_report_'.$gradereport.'_cron';
|
2007-05-24 02:16:31 +00:00
|
|
|
if (function_exists($cron_function)) {
|
|
|
|
mtrace("Processing gradebook report function $cron_function ...", '');
|
|
|
|
$cron_function;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-30 02:00:04 +00:00
|
|
|
|
|
|
|
// run any customized cronjobs, if any
|
|
|
|
// looking for functions in lib/local/cron.php
|
2007-12-07 04:39:40 +00:00
|
|
|
if (file_exists($CFG->dirroot.'/local/cron.php')) {
|
2007-11-30 02:00:04 +00:00
|
|
|
mtrace('Processing customized cron script ...', '');
|
2007-12-07 04:39:40 +00:00
|
|
|
include_once($CFG->dirroot.'/local/cron.php');
|
2007-11-30 02:00:04 +00:00
|
|
|
mtrace('done.');
|
|
|
|
}
|
|
|
|
|
2005-09-01 04:14:31 +00:00
|
|
|
|
2004-07-07 19:35:28 +00:00
|
|
|
//Unset session variables and destroy it
|
|
|
|
@session_unset();
|
|
|
|
@session_destroy();
|
|
|
|
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Cron script completed correctly");
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-02-01 01:36:31 +00:00
|
|
|
$difftime = microtime_diff($starttime, microtime());
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Execution took ".$difftime." seconds");
|
2003-02-01 01:36:31 +00:00
|
|
|
|
2007-11-21 16:28:45 +00:00
|
|
|
/// finish the IE hack
|
2006-10-26 08:55:56 +00:00
|
|
|
if (check_browser_version('MSIE')) {
|
|
|
|
echo "</xmp>";
|
|
|
|
}
|
|
|
|
|
2007-10-03 12:24:06 +00:00
|
|
|
?>
|