2001-11-22 06:23:56 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
|
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
|
2001-11-22 06:23:56 +00:00
|
|
|
|
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');
|
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
|
|
|
|
2003-02-20 22:09:59 +00:00
|
|
|
require_once("../config.php");
|
2005-09-01 04:14:31 +00:00
|
|
|
require_once($CFG->dirroot.'/lib/adminlib.php');
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2004-03-22 01:58:40 +00:00
|
|
|
if (!$alreadyadmin = isadmin()) {
|
2004-03-22 16:39:40 +00:00
|
|
|
unset($_SESSION['USER']);
|
|
|
|
unset($USER);
|
|
|
|
unset($_SESSION['SESSION']);
|
|
|
|
unset($SESSION);
|
2004-03-22 01:58:40 +00:00
|
|
|
$USER = get_admin(); /// Temporarily, to provide environment for this script
|
|
|
|
}
|
2004-01-25 14:51:04 +00:00
|
|
|
|
2005-04-18 20:13:36 +00:00
|
|
|
//unset test cookie, user must login again anyway
|
|
|
|
setcookie('MoodleSessionTest'.$CFG->sessioncookie, '', time() - 3600, '/');
|
|
|
|
|
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
|
|
|
|
|
|
|
mtrace("<pre>");
|
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");
|
2002-12-20 14:44:14 +00:00
|
|
|
if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron)")) {
|
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 ...", '');
|
2002-07-27 06:09:54 +00:00
|
|
|
if ($cron_function()) {
|
|
|
|
if (! set_field("modules", "lastcron", $timenow, "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
|
|
|
}
|
|
|
|
}
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("done.");
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Finished activity modules");
|
2001-12-04 14:02:36 +00:00
|
|
|
|
2005-04-20 07:29:28 +00:00
|
|
|
if (!empty($CFG->langcache)) {
|
|
|
|
mtrace('Updating languages cache');
|
|
|
|
get_list_of_languages();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
$longtime = $timenow - ($CFG->longtimenosee * 3600 * 24);
|
2003-10-27 14:01:29 +00:00
|
|
|
if ($students = get_users_longtimenosee($longtime)) {
|
|
|
|
foreach ($students as $student) {
|
|
|
|
if (unenrol_student($student->userid, $student->course)) {
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("Deleted student enrolment for user $student->userid from course $student->course");
|
2003-07-27 12:20:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
$oneweek = $timenow - ($CFG->deleteunconfirmed * 3600);
|
2003-07-27 12:20:29 +00:00
|
|
|
if ($users = get_users_unconfirmed($oneweek)) {
|
2002-08-05 07:59:11 +00:00
|
|
|
foreach ($users as $user) {
|
2003-07-27 12:20:29 +00:00
|
|
|
if (delete_records("user", "id", $user->id)) {
|
2004-08-07 05:56:03 +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
|
|
|
}
|
|
|
|
}
|
2004-08-05 17:03:19 +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...)
|
|
|
|
|
|
|
|
if (!empty($CFG->loglifetime)) { // value in days
|
|
|
|
$loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
|
|
|
|
delete_records_select("log", "time < '$loglifetime'");
|
|
|
|
}
|
2004-08-05 17:03:19 +00:00
|
|
|
flush();
|
2004-02-09 12:33:02 +00:00
|
|
|
|
|
|
|
/// Delete old cached texts
|
|
|
|
|
|
|
|
if (!empty($CFG->cachetext)) { // Defined in config.php
|
|
|
|
$cachelifetime = time() - $CFG->cachetext;
|
2004-02-09 13:12:46 +00:00
|
|
|
delete_records_select("cache_text", "timemodified < '$cachelifetime'");
|
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();
|
|
|
|
}
|
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();
|
|
|
|
|
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
|
|
|
|
2004-02-08 14:21:23 +00:00
|
|
|
if (!isset($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);
|
2005-05-17 00:18:37 +00:00
|
|
|
@raise_memory_limit("128M");
|
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()) {
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace("ERORR: 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
|
|
|
|
require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
|
|
|
|
$enrol = new enrolment_plugin();
|
|
|
|
$enrol->cron();
|
2004-07-29 14:53:48 +00:00
|
|
|
if (!empty($enrol->log)) {
|
2004-08-05 17:08:46 +00:00
|
|
|
mtrace($enrol->log);
|
2004-07-29 14:53:48 +00:00
|
|
|
}
|
2004-06-25 03:44:29 +00:00
|
|
|
|
2005-09-01 04:14:31 +00:00
|
|
|
if (!empty($CFG->enablestats)) {
|
2005-10-04 20:25:31 +00:00
|
|
|
$time = 60*60*20; // set it to 20 here for first run... (overridden by $CFG)
|
|
|
|
$clobber = true;
|
|
|
|
if (!empty($CFG->statsmaxruntime)) {
|
|
|
|
$time = $CFG->statsmaxruntime+(60*30); // add on half an hour just to make sure (it could take that long to break out of the loop)
|
|
|
|
}
|
2005-09-01 04:14:31 +00:00
|
|
|
if (!get_field_sql('SELECT id FROM '.$CFG->prefix.'stats_daily LIMIT 1')) {
|
|
|
|
// first run, set another lock. we'll check for this in subsequent runs to set the timeout to later for the normal lock.
|
2005-10-04 20:25:31 +00:00
|
|
|
set_cron_lock('statsfirstrunlock',true,$time,true);
|
2005-09-01 04:14:31 +00:00
|
|
|
$firsttime = true;
|
|
|
|
}
|
2005-10-04 20:25:31 +00:00
|
|
|
$time = 60*60*2; // this time set to 2.. (overridden by $CFG)
|
|
|
|
if (!empty($CFG->statsmaxruntime)) {
|
|
|
|
$time = $CFG->statsmaxruntime+(60*30); // add on half an hour to make sure (it could take that long to break out of the loop)
|
|
|
|
}
|
2005-09-01 04:14:31 +00:00
|
|
|
if ($config = get_record('config','name','statsfirstrunlock')) {
|
|
|
|
if (!empty($config->value)) {
|
2005-10-04 20:25:31 +00:00
|
|
|
$clobber = false; // if we're on the first run, just don't clobber it.
|
2005-09-01 04:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (set_cron_lock('statsrunning',true,$time, $clobber)) {
|
|
|
|
require_once($CFG->dirroot.'/lib/statslib.php');
|
|
|
|
$return = stats_cron_daily();
|
2005-10-04 20:25:31 +00:00
|
|
|
if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
|
2005-09-28 02:36:44 +00:00
|
|
|
stats_cron_weekly();
|
2005-09-01 04:14:31 +00:00
|
|
|
}
|
2005-10-04 20:25:31 +00:00
|
|
|
if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
|
|
|
|
$return = $return && stats_cron_monthly();
|
2005-09-01 04:14:31 +00:00
|
|
|
}
|
2005-10-04 20:25:31 +00:00
|
|
|
if (stats_check_runtime() && $return == STATS_RUN_COMPLETE) {
|
2005-09-27 21:09:10 +00:00
|
|
|
stats_clean_old();
|
|
|
|
}
|
2005-09-01 04:14:31 +00:00
|
|
|
set_cron_lock('statsrunning',false);
|
|
|
|
if (!empty($firsttime)) {
|
|
|
|
set_cron_lock('statsfirstrunlock',false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2001-11-22 06:23:56 +00:00
|
|
|
?>
|