New delete_records_select() function
[moodle.git] / admin / cron.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
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//
673de292 11// eg wget -q -O /dev/null 'http://moodle.somewhere.edu/admin/cron.php'
5710022c 12// or php /web/moodle/admin/cron.php
f9903ed0 13
9fa49e22 14 $FULLME = "cron";
d5f48b16 15
498d4032 16 function microtime_diff($a, $b) {
17 list($a_dec, $a_sec) = explode(" ", $a);
18 list($b_dec, $b_sec) = explode(" ", $b);
19 return $b_sec - $a_sec + $b_dec - $a_dec;
20 }
21
22 $starttime = microtime();
23
652fd672 24 require_once("../config.php");
f9903ed0 25
26 echo "<PRE>\n";
27
f9903ed0 28 $timenow = time();
29
c88f014a 30// Run all cron jobs for each module
31
9fa49e22 32 if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron)")) {
f9903ed0 33 foreach ($mods as $mod) {
673de292 34 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
35 if (file_exists($libfile)) {
36 include_once($libfile);
37 $cron_function = $mod->name."_cron";
38 if (function_exists($cron_function)) {
39 if ($cron_function()) {
40 if (! set_field("modules", "lastcron", $timenow, "id", $mod->id)) {
41 echo "Error: could not update timestamp for $mod->fullname\n";
42 }
43 }
f9903ed0 44 }
45 }
46 }
47 }
c88f014a 48
49
d0daec36 50// Unenrol users who haven't logged in for $CFG->longtimenosee
c88f014a 51
f282e631 52 if ($CFG->longtimenosee) { // value in days
9fa49e22 53 $longtime = $timenow - ($CFG->longtimenosee * 3600 * 24);
54 if ($users = get_users_longtimenosee($longtime)) {
6cb86b39 55 foreach ($users as $user) {
45aa6d56 56 if (unenrol_student($user->id)) {
f282e631 57 echo "Deleted student enrolment for $user->firstname $user->lastname ($user->id)\n";
58 }
6cb86b39 59 }
60 }
61 }
c88f014a 62
6cb86b39 63
d0daec36 64// Delete users who haven't confirmed within seven days
65
9fa49e22 66 $oneweek = $timenow - (7 * 24 * 3600);
67 if ($users = get_users_unconfirmed($oneweek)) {
6cb86b39 68 foreach ($users as $user) {
f282e631 69 if (delete_records("user", "id", $user->id)) {
d0daec36 70 echo "Deleted unconfirmed user for $user->firstname $user->lastname ($user->id)\n";
f282e631 71 }
6cb86b39 72 }
73 }
c88f014a 74
f9903ed0 75 echo "Cron script completed correctly\n";
76
498d4032 77 $difftime = microtime_diff($starttime, microtime());
78 echo "Execution took ".$difftime." seconds\n";
79
f9903ed0 80?>