Slightly changed the shortcut method of disabling email so that it's
[moodle.git] / admin / cron.php
blob9972b2918fd9d7e48da00bb5bc743dbdd59fb5f6
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 $FULLME = "cron";
16 $starttime = microtime();
18 /// The current directory in PHP version 4.3.0 and above isn't necessarily the
19 /// directory of the script when run from the command line. The require_once()
20 /// would fail, so we'll have to chdir()
22 if (!isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['argv'][0])) {
23 chdir(dirname($_SERVER['argv'][0]));
26 require_once("../config.php");
28 if (!$alreadyadmin = isadmin()) {
29 unset($_SESSION['USER']);
30 unset($USER);
31 unset($_SESSION['SESSION']);
32 unset($SESSION);
33 $USER = get_admin(); /// Temporarily, to provide environment for this script
36 /// Start output log
38 $timenow = time();
40 mtrace("<pre>");
41 mtrace("Server Time: ".date('r',$timenow)."\n\n");
43 /// Run all cron jobs for each module
45 mtrace("Starting activity modules");
46 if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron)")) {
47 foreach ($mods as $mod) {
48 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
49 if (file_exists($libfile)) {
50 include_once($libfile);
51 $cron_function = $mod->name."_cron";
52 if (function_exists($cron_function)) {
53 mtrace("Processing module function $cron_function ...", '');
54 if ($cron_function()) {
55 if (! set_field("modules", "lastcron", $timenow, "id", $mod->id)) {
56 mtrace("Error: could not update timestamp for $mod->fullname");
59 mtrace("done.");
64 mtrace("Finished activity modules");
66 /// Run all core cron jobs, but not every time since they aren't too important.
67 /// These don't have a timer to reduce load, so we'll use a random number
68 /// to randomly choose the percentage of times we should run these jobs.
70 srand ((double) microtime() * 10000000);
71 $random100 = rand(0,100);
73 if ($random100 < 20) { // Approximately 20% of the time.
74 mtrace("Running clean-up tasks...");
76 /// Unenrol users who haven't logged in for $CFG->longtimenosee
78 if ($CFG->longtimenosee) { // value in days
79 $longtime = $timenow - ($CFG->longtimenosee * 3600 * 24);
80 if ($students = get_users_longtimenosee($longtime)) {
81 foreach ($students as $student) {
82 if (unenrol_student($student->userid, $student->course)) {
83 mtrace("Deleted student enrolment for user $student->userid from course $student->course");
90 /// Delete users who haven't confirmed within required period
92 $oneweek = $timenow - ($CFG->deleteunconfirmed * 3600);
93 if ($users = get_users_unconfirmed($oneweek)) {
94 foreach ($users as $user) {
95 if (delete_records("user", "id", $user->id)) {
96 mtrace("Deleted unconfirmed user for ".fullname($user, true)." ($user->id)");
100 flush();
103 /// Delete duplicate enrolments (don't know what causes these yet - expired sessions?)
105 if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ".
106 "HAVING count(*) > 1", "", "*,count(*)")) {
107 foreach ($users as $user) {
108 delete_records_select("user_students", "userid = '$user->userid' ".
109 "AND course = '$user->course' AND id <> '$user->id'");
112 flush();
115 /// Delete old logs to save space (this might need a timer to slow it down...)
117 if (!empty($CFG->loglifetime)) { // value in days
118 $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
119 delete_records_select("log", "time < '$loglifetime'");
121 flush();
123 /// Delete old cached texts
125 if (!empty($CFG->cachetext)) { // Defined in config.php
126 $cachelifetime = time() - $CFG->cachetext;
127 delete_records_select("cache_text", "timemodified < '$cachelifetime'");
129 flush();
131 if (!empty($CFG->notifyloginfailures)) {
132 notify_login_failures();
134 flush();
136 } // End of occasional clean-up tasks
138 if (file_exists("$CFG->dataroot/cronextra.php")) {
139 mtrace("Running extra commands in $CFG->dataroot/cronextra.php ...");
140 include("$CFG->dataroot/cronextra.php");
143 if (!isset($CFG->disablescheduledbackups)) { // Defined in config.php
144 //Execute backup's cron
145 //Perhaps a long time and memory could help in large sites
146 @set_time_limit(0);
147 @ini_set("memory_limit","128M");
148 if (file_exists("$CFG->dirroot/backup/backup_scheduled.php") and
149 file_exists("$CFG->dirroot/backup/backuplib.php") and
150 file_exists("$CFG->dirroot/backup/lib.php") and
151 file_exists("$CFG->libdir/blocklib.php")) {
152 include_once("$CFG->dirroot/backup/backup_scheduled.php");
153 include_once("$CFG->dirroot/backup/backuplib.php");
154 include_once("$CFG->dirroot/backup/lib.php");
155 require_once ("$CFG->libdir/blocklib.php");
156 mtrace("Running backups if required...");
158 if (! schedule_backup_cron()) {
159 mtrace("ERORR: Something went wrong while performing backup tasks!!!");
160 } else {
161 mtrace("Backup tasks finished.");
166 if (!empty($CFG->enablerssfeeds)) { //Defined in admin/variables page
167 if (file_exists("$CFG->dirroot/rss/rsslib.php")) {
168 include_once("$CFG->dirroot/rss/rsslib.php");
169 mtrace("Running rssfeeds if required...");
171 if ( ! cron_rss_feeds()) {
172 mtrace("Something went wrong while generating rssfeeds!!!");
173 } else {
174 mtrace("Rssfeeds finished");
179 /// Run the enrolment cron, if any
180 require_once("$CFG->dirroot/enrol/$CFG->enrol/enrol.php");
181 $enrol = new enrolment_plugin();
182 $enrol->cron();
183 if (!empty($enrol->log)) {
184 mtrace($enrol->log);
187 //Unset session variables and destroy it
188 @session_unset();
189 @session_destroy();
191 mtrace("Cron script completed correctly");
193 $difftime = microtime_diff($starttime, microtime());
194 mtrace("Execution took ".$difftime." seconds");