]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-13360 expire notification problems; merged from MOODLE_19_STABLE
authorskodak <skodak>
Tue, 4 Mar 2008 08:55:28 +0000 (08:55 +0000)
committerskodak <skodak>
Tue, 4 Mar 2008 08:55:28 +0000 (08:55 +0000)
enrol/manual/enrol.php
lang/en_utf8/moodle.php

index b25a2492f1c7e27d244a9541c2acacaea9353e4a..7596fc76a18993a3a973c27002fc2eb8615cb6eb 100644 (file)
@@ -240,61 +240,64 @@ function process_config($config) {
 
 
 /**
-* This function is run by admin/cron.php every time
-*
-* The cron function can perform regular checks for the current
-* enrollment plugin.  For example it can check a foreign database,
-* all look for a file to pull data in from
-*
+* Notify users about enrolments that are going to expire soon!
+* This function is run by admin/cron.php
+* @return void
 */
 function cron() {
-    global $CFG;
+    global $CFG, $USER, $SITE;
 
-    // Notify users about enrolments that are going to expire soon!
+    if (!isset($CFG->lastexpirynotify)) {
+        set_config('lastexpirynotify', 0);
+    }
 
-    if (empty($CFG->lastexpirynotify)) {
-        $CFG->lastexpirynotify = 0;
+    // notify once a day only - TODO: add some tz handling here, maybe use timestamps
+    if ($CFG->lastexpirynotify == date('Ymd')) {
+// return;
     }
 
-    if ($CFG->lastexpirynotify < date('Ymd') &&
-        ($courses = get_records_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0'))) {
+    if ($rs = get_recordset_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0')) {
+
+        $cronuser = clone($USER);
 
         $admin = get_admin();
 
-        $strexpirynotify = get_string('expirynotify');
-        foreach ($courses as $course) {
+        while($course = rs_fetch_next_record($rs)) {
             $a = new object();
-            $a->coursename = $course->shortname .'/'. $course->fullname;
-            $a->threshold = $course->expirythreshold / 86400;
-            $a->extendurl = $CFG->wwwroot . '/user/index.php?id=' . $course->id;
-            $a->current = array();
-            $a->past = array();
-            $a->current = $a->past = array();
+            $a->coursename = $course->shortname .'/'. $course->fullname; // must be processed by format_string later
+            $a->threshold  = $course->expirythreshold / 86400;
+            $a->extendurl  = $CFG->wwwroot . '/user/index.php?id=' . $course->id;
+            $a->current    = array();
+            $a->past       = array();
+
             $expiry = time() + $course->expirythreshold;
+            $cname  = $course->fullname;
 
-            /// Get all the role assignments for this course that have expired.
+            /// Get all the manual role assignments for this course that have expired.
 
             if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
                 continue;
             }
 
-            if ($oldenrolments = get_records_sql('
-                      SELECT u.*
-                        FROM '.$CFG->prefix.'role_assignments ra,
-                             '.$CFG->prefix.'user u
-                        WHERE ra.contextid = '.$context->id.'
-                          AND ra.timeend > 0 AND ra.timeend <= '.$expiry.'
-                          AND ra.userid = u.id ')) {
-
-
-                if (!$teacher = get_teacher($course->id)) {
-                    $teacher = get_admin();
+            if ($oldenrolments = get_records_sql("
+                      SELECT u.*, ra.timeend
+                        FROM {$CFG->prefix}user u
+                             JOIN {$CFG->prefix}role_assignments ra ON (ra.userid = u.id)
+                        WHERE ra.contextid = $context->id
+                              AND ra.timeend > 0 AND ra.timeend <= $expiry
+                              AND ra.enrol = 'manual'")) {
+
+                // inform user who can assign roles or admin
+                if ($teachers = get_users_by_capability($context, 'moodle/role:assign', '', '', '', '', '', '', false)) {
+                    $teachers = sort_by_roleassignment_authority($teachers, $context);
+                    $teacher  = reset($teachers);
+                } else {
+                    $teachers = array($admin);
+                    $teacher  = $admin;
                 }
 
                 $a->teacherstr = fullname($teacher, true);
 
-                $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
-
                 foreach ($oldenrolments as $user) {       /// Email all users about to expire
                     $a->studentstr = fullname($user, true);
                     if ($user->timeend < ($expiry - 86400)) {
@@ -302,30 +305,42 @@ function cron() {
                     } else {
                         $a->current[] = fullname($user) . " <$user->email>";
                         if ($course->notifystudents) {     // Send this guy notice
-                            email_to_user($user, $teacher, $SITE->fullname .' '. $strexpirynotify,
+                            // setup global $COURSE properly - needed for languages
+                            $USER = $user;
+                            course_setup($course);
+                            $a->coursename = format_string($cname);
+                            $a->course     = $a->coursename;
+                            $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
+                            $strexpirynotify              = get_string('expirynotify');
+
+                            email_to_user($user, $teacher, format_string($SITE->fullname) .' '. $strexpirynotify,
                                           $strexpirynotifystudentsemail);
                         }
                     }
                 }
 
                 $a->current = implode("\n", $a->current);
-                $a->past = implode("\n", $a->past);
-
-                $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
+                $a->past    = implode("\n", $a->past);
 
                 if ($a->current || $a->past) {
-                    if ($teachers = get_users_by_capability($context, 'moodle/course:update',
-                                                            'u.*', 'u.username ASC',
-                                                            '', '', '', '', false)) {
-                        foreach ($teachers as $teacher) {
-                            email_to_user($teacher, $admin, $a->coursename .' '. $strexpirynotify, $strexpirynotifyemail);
-                        }
+                    foreach ($teachers as $teacher) {
+                        // setup global $COURSE properly - needed for languages
+                        $USER = $teacher;
+                        course_setup($course);
+                        $a->coursename = format_string($cname);
+                        $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
+                        $strexpirynotify      = get_string('expirynotify');
+
+                        email_to_user($teacher, $admin, $a->coursename .' '. $strexpirynotify, $strexpirynotifyemail);
                     }
                 }
             }
-            set_config('lastexpirynotify', date('Ymd'));
         }
+        $USER = $cronuser;
+        course_setup($course);   // More environment
     }
+
+    set_config('lastexpirynotify', date('Ymd'));
 }
 
 
index bafcba78fe2ebac5b48481f2bf2731bd0906e006..4f20385430345b1741485294515ba7fa852f3c88 100644 (file)
@@ -605,7 +605,7 @@ $string['expirynotifystudentsemail'] = 'Dear $a->studentstr:
 
 This is a notification that your enrolment in the course $a->course will expire in $a->threshold days.
 
-Please contact your $a->teacherstr for any further enquiries.';
+Please contact $a->teacherstr for any further enquiries.';
 $string['expirythreshold'] = 'Threshold';
 $string['explanation'] = 'Explanation';
 $string['extendenrol'] = 'Extend enrolment';