From: skodak Date: Sat, 3 Jan 2009 13:16:59 +0000 (+0000) Subject: MDL-17774 refactored USER switching in cron scripts - now standardised in cron_setup_... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e8b7114d5e2f6db4195783c61b810ad4fc1456dc;p=moodle.git MDL-17774 refactored USER switching in cron scripts - now standardised in cron_setup_user() --- diff --git a/admin/cron.php b/admin/cron.php index 5382de471d..62bae7105c 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -66,13 +66,7 @@ /// 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')) { diff --git a/enrol/manual/enrol.php b/enrol/manual/enrol.php index 1e785823b3..d20733df53 100644 --- a/enrol/manual/enrol.php +++ b/enrol/manual/enrol.php @@ -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')); diff --git a/lib/moodlelib.php b/lib/moodlelib.php index bc8740afc6..2cef0895ca 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -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'); diff --git a/lib/sessionlib.php b/lib/sessionlib.php index 7b8f87ff90..0bbf733386 100644 --- a/lib/sessionlib.php +++ b/lib/sessionlib.php @@ -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. diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 6ac6fc1980..c8d65126d9 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -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; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index c56daeff3c..2a3beae55a 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -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)); diff --git a/repository/remotemoodle/repository.class.php b/repository/remotemoodle/repository.class.php index d98af32188..153930b238 100644 --- a/repository/remotemoodle/repository.class.php +++ b/repository/remotemoodle/repository.class.php @@ -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)));