]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-17774 refactored USER switching in cron scripts - now standardised in cron_setup_...
authorskodak <skodak>
Sat, 3 Jan 2009 13:16:59 +0000 (13:16 +0000)
committerskodak <skodak>
Sat, 3 Jan 2009 13:16:59 +0000 (13:16 +0000)
admin/cron.php
enrol/manual/enrol.php
lib/moodlelib.php
lib/sessionlib.php
mod/assignment/lib.php
mod/forum/lib.php
repository/remotemoodle/repository.class.php

index 5382de471d84dbebca36186e458247d0d4df175d..62bae7105ca0dff6fe76314e1db35e2728c82cf4 100644 (file)
 
 
 /// emulate normal session
-    session_set_user(get_admin());      /// Temporarily, to provide environment for this script
-
-/// ignore admins timezone, language and locale - use site deafult instead!
-    $USER->timezone = $CFG->timezone;
-    $USER->lang = '';
-    $USER->theme = '';
-    course_setup(SITEID);
+    cron_setup_user();
 
 /// send mime type and encoding
     if (check_browser_version('MSIE')) {
index 1e785823b3c8f34552c7b07135914ca9ba695572..d20733df539b48abed62a82000cc10c5e5b17e31 100644 (file)
@@ -273,9 +273,6 @@ function cron() {
     }
 
     if ($rs = $DB->get_recordset_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0')) {
-
-        $cronuser = clone($USER);
-
         $admin = get_admin();
 
         foreach ($rs as $course) {
@@ -322,8 +319,7 @@ function cron() {
                         $a->current[] = fullname($user) . " <$user->email>";
                         if ($course->notifystudents) {     // Send this guy notice
                             // setup global $COURSE properly - needed for languages
-                            $USER = $user;
-                            course_setup($course);
+                            cron_setup_user($user, $course);
                             $a->coursename = format_string($cname);
                             $a->course     = $a->coursename;
                             $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
@@ -349,8 +345,7 @@ function cron() {
                 if ($a->current || $a->past) {
                     foreach ($teachers as $teacher) {
                         // setup global $COURSE properly - needed for languages
-                        $USER = $teacher;
-                        course_setup($course);
+                        cron_setup_user($teacher, $course);
                         $a->coursename = format_string($cname);
                         $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
                         $strexpirynotify      = get_string('expirynotify');
@@ -369,9 +364,8 @@ function cron() {
                 }
             }
         }
-        $USER = $cronuser;
-        course_setup($SITE);   // More environment
         $rs->close();
+        cron_setup_user();
     }
 
     set_config('lastexpirynotify', date('Ymd'));
index bc8740afc6a41ed9962f0214de479fb162fce7c7..2cef0895cad930fd34d64a5b137a12ece5e6bdf1 100644 (file)
@@ -1847,11 +1847,8 @@ function course_setup($courseorid=0) {
     } else if (is_object($courseorid)) {
         $COURSE = clone($courseorid);
     } else {
-        global $course; // used here only to prevent repeated fetching from DB - may be removed later
         if ($courseorid == SITEID) {
             $COURSE = clone($SITE);
-        } else if (!empty($course->id) and $course->id == $courseorid) {
-            $COURSE = clone($course);
         } else {
             if (!$COURSE = $DB->get_record('course', array('id'=>$courseorid))) {
                 print_error('invalidcourseid');
index 7b8f87ff906a451e6755cfe11ed9513316f90eb1..0bbf7333867c15305bbfd22712d35d254c550001 100644 (file)
@@ -391,6 +391,53 @@ function session_unloginas() {
     unset($_SESSION['REALUSER']);
 }
 
+/**
+ * Sets up current user and course enviroment (lang, etc.) in cron.
+ * Do not use outside of cron script!
+ *
+ * @param object $user full user object, null means default cron user (admin)
+ * @param $course full course record, null means $SITE
+ * @return void
+ */
+function cron_setup_user($user=null, $course=null) {
+    global $CFG, $SITE;
+
+    static $cronuser    = null;
+    static $cronsession = null;
+
+    if (empty($cronuser)) {
+        /// ignore admins timezone, language and locale - use site deafult instead!
+        $cronuser = get_admin();
+        $cronuser->timezone = $CFG->timezone;
+        $cronuser->lang = '';
+        $cronuser->theme = '';
+
+        $cronsession = array();
+    }
+
+    if (!$user) {
+        // cached default cron user (==modified admin for now)
+        session_set_user($cronuser);
+        $_SESSION['SESSION'] = $cronsession;
+
+    } else {
+        // emulate real user session - needed for caps in cron
+        if ($_SESSION['USER']->id != $user->id) {
+            session_set_user($user);
+            $_SESSION['SESSION'] = array();
+        }
+    }
+
+    if ($course) {
+        course_setup($course);
+    } else {
+        course_setup($SITE);
+    }
+
+    // TODO: it should be possible to improve perf by caching some limited number of users here ;-)
+
+}
+
 /**
 * Enable cookieless sessions by including $CFG->usesid=true;
 * in config.php.
index 6ac6fc1980e7c2b269f1f090018a26662a7ffb8d..c8d65126d933090e9e5df756f983bcf1afb738ea 100644 (file)
@@ -2118,8 +2118,7 @@ function assignment_cron () {
 
             /// Override the language and timezone of the "current" user, so that
             /// mail is customised for the receiver.
-            $USER = $user;
-            course_setup($course);
+            cron_setup_user($user, $course);
 
             if (!has_capability('moodle/course:view', get_context_instance(CONTEXT_COURSE, $submission->course), $user->id)) {
                 echo fullname($user)." not an active participant in " . format_string($course->shortname) . "\n";
@@ -2180,9 +2179,7 @@ function assignment_cron () {
             }
         }
 
-        $USER = $realuser;
-        course_setup(SITEID); // reset cron user language, theme and timezone settings
-
+        cron_setup_user();
     }
 
     return true;
index c56daeff3c4b1ee75a3f20206a2054cc6fb5abb6..2a3beae55a535256b85bc4c96a15513d7b0eb1b3 100644 (file)
@@ -323,7 +323,6 @@ WHERE
 function forum_cron() {
     global $CFG, $USER, $DB;
 
-    $cronuser = clone($USER);
     $site = get_site();
 
     // all users that are subscribed to any post that needs sending
@@ -439,7 +438,7 @@ function forum_cron() {
             @set_time_limit(120); // terminate if processing of any account takes longer than 2 minutes
 
             // set this so that the capabilities are cached, and environment matches receiving user
-            $USER = $userto;
+            cron_setup_user($userto);
 
             mtrace('Processing user '.$userto->id);
 
@@ -489,7 +488,7 @@ function forum_cron() {
                 }
 
                 // setup global $COURSE properly - needed for roles and languages
-                course_setup($course);   // More environment
+                cron_setup_user($userto, $course);
 
                 // Fill caches
                 if (!isset($userto->viewfullnames[$forum->id])) {
@@ -618,8 +617,7 @@ function forum_cron() {
     unset($mailcount);
     unset($errorcount);
 
-    $USER = clone($cronuser);
-    course_setup(SITEID);
+    cron_setup_user();
 
     $sitetimezone = $CFG->timezone;
 
@@ -722,8 +720,7 @@ function forum_cron() {
 
                 @set_time_limit(120); // terminate if processing of any account takes longer than 2 minutes
 
-                $USER = $cronuser;
-                course_setup(SITEID); // reset cron user language, theme and timezone settings
+                cron_setup_user();
 
                 mtrace(get_string('processingdigest', 'forum', $userid), '... ');
 
@@ -733,8 +730,7 @@ function forum_cron() {
 
                 // Override the language and timezone of the "current" user, so that
                 // mail is customised for the receiver.
-                $USER = $userto;
-                course_setup(SITEID);
+                cron_setup_user($userto);
 
                 // init caches
                 $userto->viewfullnames = array();
@@ -767,7 +763,7 @@ function forum_cron() {
                     $cm         = $coursemodules[$forum->id];
 
                     //override language
-                    course_setup($course);
+                    cron_setup_user($userto, $course);
 
                     // Fill caches
                     if (!isset($userto->viewfullnames[$forum->id])) {
@@ -894,8 +890,7 @@ function forum_cron() {
         set_config('digestmailtimelast', $timenow);
     }
 
-    $USER = $cronuser;
-    course_setup(SITEID); // reset cron user language, theme and timezone settings
+    cron_setup_user();
 
     if (!empty($usermailcount)) {
         mtrace(get_string('digestsentusers', 'forum', $usermailcount));
index d98af32188908e54d75f5513b6b1370bc23fac21..153930b2381a24f45dd8a41df66d88642c151439 100644 (file)
@@ -55,6 +55,7 @@ class repository_remotemoodle extends repository {
 
         ///check the the user is known
         ///he has to be previously connected to the server site in order to be in the database
+        //TODO: this seems weird - is it executed from cron or what? Please review
         $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $MNET_REMOTE_CLIENT->id));
         if (empty($USER)) {
             exit(mnet_server_fault(9016, get_string('usernotfound', 'repository_remotemoodle',  $username)));
@@ -96,6 +97,7 @@ class repository_remotemoodle extends repository {
 
         ///check the the user is known
         ///he has to be previously connected to the server site in order to be in the database
+        //TODO: this seems weird - is it executed from cron or what? Please review
         $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $MNET_REMOTE_CLIENT->id));
         if (empty($USER)) {
             exit(mnet_server_fault(9016, get_string('usernotfound', 'repository_remotemoodle',  $username)));