MDL-44640 enrol: trigger events on instances actions
authorMarina Glancy <marina@moodle.com>
Mon, 24 Aug 2015 08:28:57 +0000 (16:28 +0800)
committerMarina Glancy <marina@moodle.com>
Sun, 27 Sep 2015 09:52:20 +0000 (17:52 +0800)
12 files changed:
enrol/cohort/edit.php
enrol/guest/lib.php
enrol/manual/edit.php
enrol/paypal/edit.php
enrol/self/edit.php
enrol/tests/enrollib_test.php
enrol/upgrade.txt
lang/en/enrol.php
lib/classes/event/enrol_instance_created.php [new file with mode: 0644]
lib/classes/event/enrol_instance_deleted.php [new file with mode: 0644]
lib/classes/event/enrol_instance_updated.php [new file with mode: 0644]
lib/enrollib.php

index b921f15..3affaa9 100644 (file)
@@ -96,6 +96,7 @@ if ($mform->is_cancelled()) {
             $instance->customint2 = $groupid;
         }
         $DB->update_record('enrol', $instance);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
     }  else {
         // Create a new group for the cohort if requested.
         if ($data->customint2 == COHORT_CREATE_GROUP) {
index 9336e1c..f224f0a 100644 (file)
@@ -332,6 +332,7 @@ class enrol_guest_plugin extends enrol_plugin {
                         $instance->password = $data->{'enrol_guest_password_'.$i};
                     }
                     $DB->update_record('enrol', $instance);
+                    \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
 
                     if ($reset) {
                         $context = context_course::instance($course->id);
index d6d01e2..27e1de7 100644 (file)
@@ -92,13 +92,13 @@ if ($mform->is_cancelled()) {
         $instance->notifyall       = $data->notifyall;
         $instance->expirythreshold = $data->expirythreshold;
         $instance->timemodified    = time();
+        $markdirty = ($instance->status != $data->status);
+        $instance->status = $data->status;
 
         $DB->update_record('enrol', $instance);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
 
-        // Use standard API to update instance status.
-        if ($instance->status != $data->status) {
-            $instance = $DB->get_record('enrol', array('id'=>$instance->id));
-            $plugin->update_status($instance, $data->status);
+        if ($markdirty) {
             $context->mark_dirty();
         }
 
index 6175b3b..9c0541a 100644 (file)
@@ -76,6 +76,7 @@ if ($mform->is_cancelled()) {
         $instance->enrolenddate   = $data->enrolenddate;
         $instance->timemodified   = time();
         $DB->update_record('enrol', $instance);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
 
         if ($reset) {
             $context->mark_dirty();
index a339b8e..f429c39 100644 (file)
@@ -109,6 +109,7 @@ if ($mform->is_cancelled()) {
         $instance->enrolenddate   = $data->enrolenddate;
         $instance->timemodified   = time();
         $DB->update_record('enrol', $instance);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
 
         if ($reset) {
             $context->mark_dirty();
index 61a4392..1a88c50 100644 (file)
@@ -362,4 +362,64 @@ class core_enrollib_testcase extends advanced_testcase {
         $this->assertEventLegacyLogData($expected, $event);
         $this->assertEventContextNotUsed($event);
     }
+
+    /**
+     * Test enrol_instance_created, enrol_instance_updated and enrol_instance_deleted events.
+     */
+    public function test_instance_events() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $selfplugin = enrol_get_plugin('self');
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+
+        $course = $this->getDataGenerator()->create_course();
+
+        // Creating enrol instance.
+        $sink = $this->redirectEvents();
+        $instanceid = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
+                                                                'name' => 'Test instance 1',
+                                                                'customint6' => 1,
+                                                                'roleid' => $studentrole->id));
+        $events = $sink->get_events();
+        $sink->close();
+
+        $this->assertCount(1, $events);
+        $event = array_pop($events);
+        $this->assertInstanceOf('\core\event\enrol_instance_created', $event);
+        $this->assertEquals(context_course::instance($course->id), $event->get_context());
+        $this->assertEquals('self', $event->other['enrol']);
+        $this->assertEventContextNotUsed($event);
+
+        // Updating enrol instance.
+        $instance = $DB->get_record('enrol', array('id' => $instanceid));
+        $sink = $this->redirectEvents();
+        $selfplugin->update_status($instance, ENROL_INSTANCE_DISABLED);
+
+        $events = $sink->get_events();
+        $sink->close();
+
+        $this->assertCount(1, $events);
+        $event = array_pop($events);
+        $this->assertInstanceOf('\core\event\enrol_instance_updated', $event);
+        $this->assertEquals(context_course::instance($course->id), $event->get_context());
+        $this->assertEquals('self', $event->other['enrol']);
+        $this->assertEventContextNotUsed($event);
+
+        // Deleting enrol instance.
+        $instance = $DB->get_record('enrol', array('id' => $instanceid));
+        $sink = $this->redirectEvents();
+        $selfplugin->delete_instance($instance);
+
+        $events = $sink->get_events();
+        $sink->close();
+
+        $this->assertCount(1, $events);
+        $event = array_pop($events);
+        $this->assertInstanceOf('\core\event\enrol_instance_deleted', $event);
+        $this->assertEquals(context_course::instance($course->id), $event->get_context());
+        $this->assertEquals('self', $event->other['enrol']);
+        $this->assertEventContextNotUsed($event);
+    }
 }
index 40f9682..d409025 100644 (file)
@@ -1,6 +1,12 @@
 This files describes API changes in /enrol/* - plugins,
 information provided here is intended especially for developers.
 
+=== 3.0 ===
+
+* Added new events enrol_instance_created, enrol_instance_updated and
+  enrol_instance_deleted . Always trigger them when changing records in the
+  DB table 'enrol'.
+
 === 2.9 ===
 
 * External function core_enrol_external::get_users_courses now returns additional optional fields:
index 1ca0a6e..ec115e2 100644 (file)
@@ -48,6 +48,9 @@ $string['enrolcandidates'] = 'Not enrolled users';
 $string['enrolcandidatesmatching'] = 'Matching not enrolled users';
 $string['enrolcohort'] = 'Enrol cohort';
 $string['enrolcohortusers'] = 'Enrol users';
+$string['eventenrolinstancecreated'] = 'Enrolment instance created';
+$string['eventenrolinstancedeleted'] = 'Enrolment instance deleted';
+$string['eventenrolinstanceupdated'] = 'Enrolment instance updated';
 $string['enrollednewusers'] = 'Successfully enrolled {$a} new users';
 $string['enrolledusers'] = 'Enrolled users';
 $string['enrolledusersmatching'] = 'Matching enrolled users';
diff --git a/lib/classes/event/enrol_instance_created.php b/lib/classes/event/enrol_instance_created.php
new file mode 100644 (file)
index 0000000..ba03ea4
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Enrol instance created event.
+ *
+ * @package    core
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Enrol instance created event class.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string enrol: name of enrol method
+ * }
+ *
+ * @package    core
+ * @since      Moodle 2.9
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class enrol_instance_created extends base {
+
+    /**
+     * Api to Create new event from enrol object.
+     *
+     * @param \stdClass $enrol record from DB table 'enrol'
+     * @return \core\event\base returns instance of new event
+     */
+    public static final function create_from_record($enrol) {
+        $event = static::create(array(
+            'context'  => \context_course::instance($enrol->courseid),
+            'objectid' => $enrol->id,
+            'other'    => array('enrol' => $enrol->enrol)
+        ));
+        return $event;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' created the instance of enrolment method '" .
+                $this->other['enrol'] . "' with id '$this->objectid'.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventenrolinstancecreated', 'enrol');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+        $this->data['objecttable'] = 'enrol';
+    }
+
+    /**
+     * custom validations
+     *
+     * Throw \coding_exception notice in case of any problems.
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['enrol'])) {
+            throw new \coding_exception('The \'enrol\' value must be set in other.');
+        }
+    }
+}
diff --git a/lib/classes/event/enrol_instance_deleted.php b/lib/classes/event/enrol_instance_deleted.php
new file mode 100644 (file)
index 0000000..9c1bf95
--- /dev/null
@@ -0,0 +1,110 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Enrol instance deleted event.
+ *
+ * @package    core
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Enrol instance deleted event class.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string enrol: name of enrol method
+ * }
+ *
+ * @package    core
+ * @since      Moodle 2.9
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class enrol_instance_deleted extends base {
+
+    /**
+     * Api to Create new event from enrol object.
+     *
+     * @param \stdClass $enrol record from DB table 'enrol'
+     * @return \core\event\base returns instance of new event
+     */
+    public static final function create_from_record($enrol) {
+        $event = static::create(array(
+            'context'  => \context_course::instance($enrol->courseid),
+            'objectid' => $enrol->id,
+            'other'    => array('enrol' => $enrol->enrol)
+        ));
+        $event->add_record_snapshot('enrol', $enrol);
+        return $event;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' deleted the instance of enrolment method '" .
+                $this->other['enrol'] . "' with id '$this->objectid'.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventgroupingdeleted', 'group');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+        $this->data['objecttable'] = 'enrol';
+    }
+
+    /**
+     * custom validations
+     *
+     * Throw \coding_exception notice in case of any problems.
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['enrol'])) {
+            throw new \coding_exception('The \'enrol\' value must be set in other.');
+        }
+    }
+}
diff --git a/lib/classes/event/enrol_instance_updated.php b/lib/classes/event/enrol_instance_updated.php
new file mode 100644 (file)
index 0000000..98732d7
--- /dev/null
@@ -0,0 +1,110 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Enrol instance updated event.
+ *
+ * @package    core
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Enrol instance updated event class.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - string enrol: name of enrol method
+ * }
+ *
+ * @package    core
+ * @since      Moodle 2.9
+ * @copyright  2015 Marina Glancy
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class enrol_instance_updated extends base {
+
+    /**
+     * Api to Create new event from enrol object.
+     *
+     * @param \stdClass $enrol record from DB table 'enrol'
+     * @return \core\event\base returns instance of new event
+     */
+    public static final function create_from_record($enrol) {
+        $event = static::create(array(
+            'context'  => \context_course::instance($enrol->courseid),
+            'objectid' => $enrol->id,
+            'other'    => array('enrol' => $enrol->enrol)
+        ));
+        $event->add_record_snapshot('enrol', $enrol);
+        return $event;
+    }
+
+    /**
+     * Returns description of what happened.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' updated the instance of enrolment method '" .
+                $this->other['enrol'] . "' with id '$this->objectid'.";
+    }
+
+    /**
+     * Return localised event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventenrolinstanceupdated', 'enrol');
+    }
+
+    /**
+     * Get URL related to the action
+     *
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/enrol/instances.php', array('id' => $this->courseid));
+    }
+
+    /**
+     * Init method.
+     *
+     * @return void
+     */
+    protected function init() {
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_OTHER;
+        $this->data['objecttable'] = 'enrol';
+    }
+
+    /**
+     * custom validations
+     *
+     * Throw \coding_exception notice in case of any problems.
+     */
+    protected function validate_data() {
+        parent::validate_data();
+        if (!isset($this->other['enrol'])) {
+            throw new \coding_exception('The \'enrol\' value must be set in other.');
+        }
+    }
+}
index 67a1cfd..593cafa 100644 (file)
@@ -1692,7 +1692,11 @@ abstract class enrol_plugin {
             $instance->$field = $value;
         }
 
-        return $DB->insert_record('enrol', $instance);
+        $instance->id = $DB->insert_record('enrol', $instance);
+
+        \core\event\enrol_instance_created::create_from_record($instance)->trigger();
+
+        return $instance->id;
     }
 
     /**
@@ -1723,8 +1727,10 @@ abstract class enrol_plugin {
         $instance->status = $newstatus;
         $DB->update_record('enrol', $instance);
 
-        // invalidate all enrol caches
         $context = context_course::instance($instance->courseid);
+        \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
+
+        // Invalidate all enrol caches.
         $context->mark_dirty();
     }
 
@@ -1756,8 +1762,10 @@ abstract class enrol_plugin {
         // finally drop the enrol row
         $DB->delete_records('enrol', array('id'=>$instance->id));
 
-        // invalidate all enrol caches
         $context = context_course::instance($instance->courseid);
+        \core\event\enrol_instance_deleted::create_from_record($instance)->trigger();
+
+        // Invalidate all enrol caches.
         $context->mark_dirty();
     }