*** empty log message ***
[moodle.git] / admin / cron.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
540bab47 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
f9903ed0 13
9fa49e22 14 $FULLME = "cron";
d5f48b16 15
498d4032 16 $starttime = microtime();
17
652fd672 18 require_once("../config.php");
f9903ed0 19
20 echo "<PRE>\n";
21
f9903ed0 22 $timenow = time();
23
540bab47 24/// Run all cron jobs for each module
c88f014a 25
9fa49e22 26 if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron)")) {
f9903ed0 27 foreach ($mods as $mod) {
673de292 28 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
29 if (file_exists($libfile)) {
30 include_once($libfile);
31 $cron_function = $mod->name."_cron";
32 if (function_exists($cron_function)) {
33 if ($cron_function()) {
34 if (! set_field("modules", "lastcron", $timenow, "id", $mod->id)) {
35 echo "Error: could not update timestamp for $mod->fullname\n";
36 }
37 }
f9903ed0 38 }
39 }
40 }
41 }
c88f014a 42
43
540bab47 44/// Unenrol users who haven't logged in for $CFG->longtimenosee
c88f014a 45
f282e631 46 if ($CFG->longtimenosee) { // value in days
9fa49e22 47 $longtime = $timenow - ($CFG->longtimenosee * 3600 * 24);
48 if ($users = get_users_longtimenosee($longtime)) {
6cb86b39 49 foreach ($users as $user) {
45aa6d56 50 if (unenrol_student($user->id)) {
f282e631 51 echo "Deleted student enrolment for $user->firstname $user->lastname ($user->id)\n";
52 }
6cb86b39 53 }
54 }
55 }
c88f014a 56
6cb86b39 57
540bab47 58/// Delete users who haven't confirmed within seven days
d0daec36 59
9fa49e22 60 $oneweek = $timenow - (7 * 24 * 3600);
61 if ($users = get_users_unconfirmed($oneweek)) {
6cb86b39 62 foreach ($users as $user) {
f282e631 63 if (delete_records("user", "id", $user->id)) {
d0daec36 64 echo "Deleted unconfirmed user for $user->firstname $user->lastname ($user->id)\n";
f282e631 65 }
6cb86b39 66 }
67 }
c88f014a 68
d7c8a46b 69
70/// Delete old queued mail that didn't get sent within 30 minutes
71
72 $oldmailtime = $timenow - 1800;
73 delete_records_select("mail_queue", "timecreated < '$oldmailtime'");
74
75
540bab47 76/// Delete old logs to save space (this might need a timer to slow it down...)
77
78 if (!empty($CFG->loglifetime)) { // value in days
79 $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
d7c8a46b 80 delete_records_select("log", "time < '$loglifetime'");
540bab47 81 }
82
f9903ed0 83 echo "Cron script completed correctly\n";
84
498d4032 85 $difftime = microtime_diff($starttime, microtime());
86 echo "Execution took ".$difftime." seconds\n";
87
f9903ed0 88?>