/// 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')) {
}
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) {
$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);
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');
}
}
}
- $USER = $cronuser;
- course_setup($SITE); // More environment
$rs->close();
+ cron_setup_user();
}
set_config('lastexpirynotify', date('Ymd'));
} 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');
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.
/// 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";
}
}
- $USER = $realuser;
- course_setup(SITEID); // reset cron user language, theme and timezone settings
-
+ cron_setup_user();
}
return true;
function forum_cron() {
global $CFG, $USER, $DB;
- $cronuser = clone($USER);
$site = get_site();
// all users that are subscribed to any post that needs sending
@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);
}
// 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])) {
unset($mailcount);
unset($errorcount);
- $USER = clone($cronuser);
- course_setup(SITEID);
+ cron_setup_user();
$sitetimezone = $CFG->timezone;
@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), '... ');
// 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();
$cm = $coursemodules[$forum->id];
//override language
- course_setup($course);
+ cron_setup_user($userto, $course);
// Fill caches
if (!isset($userto->viewfullnames[$forum->id])) {
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));
///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)));
///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)));