MDL-44640 enrol_meta: sync when enrol method is updated
authorMarina Glancy <marina@moodle.com>
Wed, 22 Apr 2015 13:16:52 +0000 (21:16 +0800)
committerMarina Glancy <marina@moodle.com>
Sun, 27 Sep 2015 09:52:20 +0000 (17:52 +0800)
enrol/meta/classes/observer.php
enrol/meta/db/events.php
enrol/meta/tests/plugin_test.php
enrol/meta/version.php

index dd62786..446fca5 100644 (file)
@@ -204,4 +204,30 @@ class enrol_meta_observer extends enrol_meta_handler {
 
         return true;
     }
+
+    /**
+     * Triggered via enrol_instance_updated event.
+     *
+     * @param \core\event\enrol_instance_updated $event
+     * @return boolean
+     */
+    public static function enrol_instance_updated(\core\event\enrol_instance_updated $event) {
+        global $DB;
+
+        if (!enrol_is_enabled('meta')) {
+            // This is slow, let enrol_meta_sync() deal with disabled plugin.
+            return true;
+        }
+
+        // Does anything want to sync with this parent?
+        $affectedcourses = $DB->get_fieldset_sql('SELECT DISTINCT courseid FROM {enrol} '.
+                'WHERE customint1 = ? AND enrol = ?',
+                array($event->courseid, 'meta'));
+
+        foreach ($affectedcourses as $courseid) {
+            enrol_meta_sync($courseid);
+        }
+
+        return true;
+    }
 }
index 8d2a590..560aefc 100644 (file)
@@ -52,4 +52,8 @@ $observers = array(
         'eventname'   => '\core\event\course_deleted',
         'callback'    => 'enrol_meta_observer::course_deleted',
     ),
+    array(
+        'eventname'   => '\core\event\enrol_instance_updated',
+        'callback'    => 'enrol_meta_observer::enrol_instance_updated',
+    ),
 );
index 4d3b549..9f0fb8a 100644 (file)
@@ -847,6 +847,16 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
         // Disable manual enrolment in course1 and make sure all user enrolments in course2 are suspended.
         $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
         $allsuspendedenrolemnts = array_combine(array_keys($expectedenrolments), array_fill(0, 5, ENROL_USER_SUSPENDED));
+        $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta2id), '', 'userid, status');
+        $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
+
+        $manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED);
+        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
+        $this->assertEquals($expectedenrolments, $enrolments);
+
+        // Disable events and repeat the same for course3 (testing sync):
+        $sink = $this->redirectEvents();
+        $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
         enrol_meta_sync($course3->id);
         $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta3id), '', 'userid, status');
         $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
@@ -855,5 +865,6 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
         enrol_meta_sync($course3->id);
         $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
         $this->assertEquals($expectedenrolments, $enrolments);
+        $sink->close();
     }
 }
index 3df94e1..83de31c 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015051100;        // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version   = 2015082400;        // The current plugin version (Date: YYYYMMDDXX)
 $plugin->requires  = 2015050500;        // Requires this Moodle version
 $plugin->component = 'enrol_meta';      // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 60*60;             // run cron every hour by default, it is not out-of-sync often