Add idnumber to list of course students
[moodle.git] / admin / cron.php
blob6fbae20ff165bb6dead4cdbcc4cce70ea3fd7fce
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 require_once("../config.php");
20 $USER = get_admin();
22 echo "<pre>\n";
24 $timenow = time();
25 echo "Server Time: ".date('r',$timenow)."\n\n";
27 /// Run all cron jobs for each module
29 if ($mods = get_records_select("modules", "cron > 0 AND (($timenow - lastcron) > cron)")) {
30 foreach ($mods as $mod) {
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";
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.
50 srand ((double) microtime() * 10000000);
51 $random100 = rand(0,100);
53 if ($random100 < 20) { // Approximately 20% of the time.
54 echo "Running clean-up tasks...\n";
56 /// Unenrol users who haven't logged in for $CFG->longtimenosee
58 if ($CFG->longtimenosee) { // value in days
59 $longtime = $timenow - ($CFG->longtimenosee * 3600 * 24);
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";
70 /// Delete users who haven't confirmed within required period
72 $oneweek = $timenow - ($CFG->deleteunconfirmed * 3600);
73 if ($users = get_users_unconfirmed($oneweek)) {
74 foreach ($users as $user) {
75 if (delete_records("user", "id", $user->id)) {
76 echo "Deleted unconfirmed user for ".fullname($user, true)." ($user->id)\n";
82 /// Delete duplicate enrolments (don't know what causes these yet - expired sessions?)
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'");
93 /// Delete old logs to save space (this might need a timer to slow it down...)
95 if (!empty($CFG->loglifetime)) { // value in days
96 $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
97 delete_records_select("log", "time < '$loglifetime'");
100 /// Delete old cached texts
102 if (!empty($CFG->cachetext)) { // Defined in config.php
103 $cachelifetime = time() - $CFG->cachetext;
104 delete_records_select("cache_text", "timemodified < '$cachelifetime'");
108 if (file_exists("$CFG->dataroot/cronextra.php")) {
109 include("$CFG->dataroot/cronextra.php");
112 if (!isset($CFG->disablescheduledbackups)) { // Defined in config.php
113 //Execute backup's cron
114 //Perhaps a long time and memory could help in large sites
115 set_time_limit(0);
116 ini_set("memory_limit","56M");
117 if (file_exists("$CFG->dirroot/backup/backup_scheduled.php") and
118 file_exists("$CFG->dirroot/backup/backuplib.php") and
119 file_exists("$CFG->dirroot/backup/lib.php")) {
120 include_once("$CFG->dirroot/backup/backup_scheduled.php");
121 include_once("$CFG->dirroot/backup/backuplib.php");
122 include_once("$CFG->dirroot/backup/lib.php");
123 echo "Running backups if required...\n";
124 flush();
126 if (! schedule_backup_cron()) {
127 echo "Something went wrong while performing backup tasks!!!\n";
128 } else {
129 echo "Backup tasks finished\n";
134 echo "Cron script completed correctly\n";
136 $difftime = microtime_diff($starttime, microtime());
137 echo "Execution took ".$difftime." seconds\n";