Incorrect variable name - not sure why this worked!
[moodle-linuxchix.git] / admin / cron.php
blob6fe5384f730a7adbe46d24ded421e43c8b17bc25
1 <?PHP // $Id$
3 /// This script looks through all the module directories for cron.php files
4 /// and runs them. These files can contain cleanup functions, email functions
5 /// or anything that needs to be run on a regular basis.
6 ///
7 /// This file is best run from cron on the host system (ie outside PHP).
8 /// The script can either be invoked via the web server or via a standalone
9 /// version of PHP compiled for CGI.
10 ///
11 /// eg wget -q -O /dev/null 'http://moodle.somewhere.edu/admin/cron.php'
12 /// or php /web/moodle/admin/cron.php
14 $starttime = microtime();
16 /// The following is a hack necessary to allow this script to work well
17 /// from the command line.
19 define('FULLME', 'cron');
21 /// The current directory in PHP version 4.3.0 and above isn't necessarily the
22 /// directory of the script when run from the command line. The require_once()
23 /// would fail, so we'll have to chdir()
25 if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
26 chdir(dirname($_SERVER['argv'][0]));
29 require_once("../config.php");
31 if (!$alreadyadmin = isadmin()) {
32 unset($_SESSION['USER']);
33 unset($USER);
34 unset($_SESSION['SESSION']);
35 unset($SESSION);
36 $USER = get_admin(); /// Temporarily, to provide environment for this script
39 //unset test cookie, user must login again anyway
40 setcookie('MoodleSessionTest'.$CFG->sessioncookie, '', time() - 3600, '/');
42 /// Start output log
44 $timenow = time();
46 mtrace("<pre>");
47 mtrace("Server Time: ".date('r',$timenow)."\n\n");
49 /// Run all cron jobs for each module
51 mtrace("Starting activity modules");
52 if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron)")) {
53 foreach ($mods as $mod) {
54 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
55 if (file_exists($libfile)) {
56 include_once($libfile);
57 $cron_function = $mod->name."_cron";
58 if (function_exists($cron_function)) {
59 mtrace("Processing module function $cron_function ...", '');
60 if ($cron_function()) {
61 if (! set_field("modules", "lastcron", $timenow, "id", $mod->id)) {
62 mtrace("Error: could not update timestamp for $mod->fullname");
65 mtrace("done.");
70 mtrace("Finished activity modules");
72 if (!empty($CFG->langcache)) {
73 mtrace('Updating languages cache');
74 get_list_of_languages();
78 /// Run all core cron jobs, but not every time since they aren't too important.
79 /// These don't have a timer to reduce load, so we'll use a random number
80 /// to randomly choose the percentage of times we should run these jobs.
82 srand ((double) microtime() * 10000000);
83 $random100 = rand(0,100);
85 if ($random100 < 20) { // Approximately 20% of the time.
86 mtrace("Running clean-up tasks...");
88 /// Unenrol users who haven't logged in for $CFG->longtimenosee
90 if ($CFG->longtimenosee) { // value in days
91 $longtime = $timenow - ($CFG->longtimenosee * 3600 * 24);
92 if ($students = get_users_longtimenosee($longtime)) {
93 foreach ($students as $student) {
94 if (unenrol_student($student->userid, $student->course)) {
95 mtrace("Deleted student enrolment for user $student->userid from course $student->course");
102 /// Delete users who haven't confirmed within required period
104 $oneweek = $timenow - ($CFG->deleteunconfirmed * 3600);
105 if ($users = get_users_unconfirmed($oneweek)) {
106 foreach ($users as $user) {
107 if (delete_records("user", "id", $user->id)) {
108 mtrace("Deleted unconfirmed user for ".fullname($user, true)." ($user->id)");
112 flush();
114 /// Delete old logs to save space (this might need a timer to slow it down...)
116 if (!empty($CFG->loglifetime)) { // value in days
117 $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
118 delete_records_select("log", "time < '$loglifetime'");
120 flush();
122 /// Delete old cached texts
124 if (!empty($CFG->cachetext)) { // Defined in config.php
125 $cachelifetime = time() - $CFG->cachetext;
126 delete_records_select("cache_text", "timemodified < '$cachelifetime'");
128 flush();
130 if (!empty($CFG->notifyloginfailures)) {
131 notify_login_failures();
133 flush();
135 sync_metacourses();
137 } // End of occasional clean-up tasks
140 if (!isset($CFG->disablescheduledbackups)) { // Defined in config.php
141 //Execute backup's cron
142 //Perhaps a long time and memory could help in large sites
143 @set_time_limit(0);
144 @raise_memory_limit("128M");
145 if (file_exists("$CFG->dirroot/backup/backup_scheduled.php") and
146 file_exists("$CFG->dirroot/backup/backuplib.php") and
147 file_exists("$CFG->dirroot/backup/lib.php") and
148 file_exists("$CFG->libdir/blocklib.php")) {
149 include_once("$CFG->dirroot/backup/backup_scheduled.php");
150 include_once("$CFG->dirroot/backup/backuplib.php");
151 include_once("$CFG->dirroot/backup/lib.php");
152 require_once ("$CFG->libdir/blocklib.php");
153 mtrace("Running backups if required...");
155 if (! schedule_backup_cron()) {
156 mtrace("ERORR: Something went wrong while performing backup tasks!!!");
157 } else {
158 mtrace("Backup tasks finished.");
163 if (!empty($CFG->enablerssfeeds)) { //Defined in admin/variables page
164 include_once("$CFG->libdir/rsslib.php");
165 mtrace("Running rssfeeds if required...");
167 if ( ! cron_rss_feeds()) {
168 mtrace("Something went wrong while generating rssfeeds!!!");
169 } else {
170 mtrace("Rssfeeds finished");
174 /// Run the enrolment cron, if any
175 require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
176 $enrol = new enrolment_plugin();
177 $enrol->cron();
178 if (!empty($enrol->log)) {
179 mtrace($enrol->log);
182 //Unset session variables and destroy it
183 @session_unset();
184 @session_destroy();
186 mtrace("Cron script completed correctly");
188 $difftime = microtime_diff($starttime, microtime());
189 mtrace("Execution took ".$difftime." seconds");