Don't lowercase the module name
[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
afabdb14 20 $USER = get_admin();
21
b2ce450a 22 echo "<pre>\n";
f9903ed0 23
f9903ed0 24 $timenow = time();
a8502a18 25 echo "Server Time: ".date('r',$timenow)."\n\n";
f9903ed0 26
b2ce450a 27/// Run all cron jobs for each module
c88f014a 28
9fa49e22 29 if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron)")) {
f9903ed0 30 foreach ($mods as $mod) {
673de292 31 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
32 if (file_exists($libfile)) {
33 include_once($libfile);
34 $cron_function = $mod->name."_cron";
35 if (function_exists($cron_function)) {
36 if ($cron_function()) {
37 if (! set_field("modules", "lastcron", $timenow, "id", $mod->id)) {
38 echo "Error: could not update timestamp for $mod->fullname\n";
39 }
40 }
f9903ed0 41 }
42 }
43 }
44 }
c88f014a 45
b2ce450a 46/// Run all core cron jobs, but not every time since they aren't too important.
47/// These don't have a timer to reduce load, so we'll use a random number
48/// to randomly choose the percentage of times we should run these jobs.
c88f014a 49
b2ce450a 50 srand ((double) microtime() * 10000000);
51 $random100 = rand(0,100);
c88f014a 52
b2ce450a 53 if ($random100 < 20) { // Approximately 20% of the time.
e561d3c2 54 echo "Running clean-up tasks...\n";
b2ce450a 55
56 /// Unenrol users who haven't logged in for $CFG->longtimenosee
57
58 if ($CFG->longtimenosee) { // value in days
59 $longtime = $timenow - ($CFG->longtimenosee * 3600 * 24);
937ae59c 60 if ($students = get_users_longtimenosee($longtime)) {
61 foreach ($students as $student) {
62 if (unenrol_student($student->userid, $student->course)) {
63 echo "Deleted student enrolment for user $student->userid from course $student->course\n";
b2ce450a 64 }
65 }
66 }
67 }
68
69
70 /// Delete users who haven't confirmed within seven days
71
72 $oneweek = $timenow - (7 * 24 * 3600);
73 if ($users = get_users_unconfirmed($oneweek)) {
6cb86b39 74 foreach ($users as $user) {
b2ce450a 75 if (delete_records("user", "id", $user->id)) {
5fde0ca6 76 echo "Deleted unconfirmed user for ".fullname($user, true)." ($user->id)\n";
f282e631 77 }
6cb86b39 78 }
79 }
b2ce450a 80
81
82 /// Delete duplicate enrolments (don't know what causes these yet - expired sessions?)
83
84 if ($users = get_records_select("user_students", "userid > 0 GROUP BY course, userid ".
85 "HAVING count(*) > 1", "", "*,count(*)")) {
86 foreach ($users as $user) {
87 delete_records_select("user_students", "userid = '$user->userid' ".
88 "AND course = '$user->course' AND id <> '$user->id'");
f282e631 89 }
6cb86b39 90 }
b2ce450a 91
92
93 /// Delete old logs to save space (this might need a timer to slow it down...)
94
95 if (!empty($CFG->loglifetime)) { // value in days
96 $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
97 delete_records_select("log", "time < '$loglifetime'");
98 }
540bab47 99 }
100
750eeaeb 101 if (file_exists("$CFG->dataroot/cronextra.php")) {
102 include("$CFG->dataroot/cronextra.php");
103 }
104
d3159192 105 //Execute backup's cron
106 //Perhaps a long time and memory could help in large sites
9bc586e0 107 ini_set("max_execution_time","3000");
108 ini_set("memory_limit","56M");
f5aa1c37 109 if (file_exists("$CFG->dirroot/backup/backup_scheduled.php") and
110 file_exists("$CFG->dirroot/backup/backuplib.php") and
111 file_exists("$CFG->dirroot/backup/lib.php")) {
112 include_once("$CFG->dirroot/backup/backup_scheduled.php");
113 include_once("$CFG->dirroot/backup/backuplib.php");
114 include_once("$CFG->dirroot/backup/lib.php");
115 echo "Running backups if required...\n";
5e04a344 116 flush();
117
f5aa1c37 118 if (! schedule_backup_cron()) {
119 echo "Something went wrong while performing backup tasks!!!\n";
e6d273eb 120 } else {
121 echo "Backup tasks finished\n";
122 }
d3159192 123 }
124
f9903ed0 125 echo "Cron script completed correctly\n";
126
498d4032 127 $difftime = microtime_diff($starttime, microtime());
128 echo "Execution took ".$difftime." seconds\n";
129
f9903ed0 130?>