MDL-45734 Badges: Add additional events
authorStephen Bourget <steve.bourget@sau19.org>
Sat, 30 Jul 2016 01:51:05 +0000 (21:51 -0400)
committerStephen Bourget <steve.bourget@sau19.org>
Sat, 30 Jul 2016 01:51:05 +0000 (21:51 -0400)
16 files changed:
badges/criteria/award_criteria.php
badges/newbadge.php
badges/tests/events_test.php
lang/en/badges.php
lib/badgeslib.php
lib/classes/event/badge_archived.php [new file with mode: 0644]
lib/classes/event/badge_created.php [new file with mode: 0644]
lib/classes/event/badge_criteria_created.php [new file with mode: 0644]
lib/classes/event/badge_criteria_deleted.php [new file with mode: 0644]
lib/classes/event/badge_criteria_updated.php [new file with mode: 0644]
lib/classes/event/badge_deleted.php [new file with mode: 0644]
lib/classes/event/badge_disabled.php [new file with mode: 0644]
lib/classes/event/badge_duplicated.php [new file with mode: 0644]
lib/classes/event/badge_enabled.php [new file with mode: 0644]
lib/classes/event/badge_updated.php [new file with mode: 0644]
version.php

index 58ccf86..fdc9ee5 100644 (file)
@@ -332,7 +332,7 @@ abstract class award_criteria {
      *
      */
     public function delete() {
-        global $DB;
+        global $DB, $PAGE;
 
         // Remove any records if it has already been met.
         $DB->delete_records('badge_criteria_met', array('critid' => $this->id));
@@ -342,6 +342,13 @@ abstract class award_criteria {
 
         // Finally remove criterion itself.
         $DB->delete_records('badge_criteria', array('id' => $this->id));
+
+        // Trigger event, badge criteria deleted.
+        $eventparams = array('objectid' => $this->id,
+            'context' => $PAGE->context,
+            'other' => array('badgeid' => $this->badgeid));
+        $event = \core\event\badge_criteria_deleted::create($eventparams);
+        $event->trigger();
     }
 
     /**
@@ -350,7 +357,7 @@ abstract class award_criteria {
      * @param array $params Values from the form or any other array.
      */
     public function save($params = array()) {
-        global $DB;
+        global $DB, $PAGE;
 
         // Figure out criteria description.
         // If it is coming from the form editor, it is an array(text, format).
@@ -386,6 +393,13 @@ abstract class award_criteria {
             $fordb->id = $cid;
             $DB->update_record('badge_criteria', $fordb, true);
 
+            // Trigger event: badge_criteria_updated.
+            $eventparams = array('objectid' => $this->id,
+                'context' => $PAGE->context,
+                'other' => array('badgeid' => $this->badgeid));
+            $event = \core\event\badge_criteria_updated::create($eventparams);
+            $event->trigger();
+
             $existing = $DB->get_fieldset_select('badge_criteria_param', 'name', 'critid = ?', array($cid));
             $todelete = array_diff($existing, $requiredkeys);
 
@@ -429,6 +443,12 @@ abstract class award_criteria {
                     $DB->insert_record('badge_criteria_param', $newp, false, true);
                 }
             }
+            // Trigger event: badge_criteria_created.
+            $eventparams = array('objectid' => $this->id,
+                'context' => $PAGE->context,
+                'other' => array('badgeid' => $this->badgeid));
+            $event = \core\event\badge_criteria_created::create($eventparams);
+            $event->trigger();
         }
         $t->allow_commit();
     }
index 6c3b74c..60f3951 100644 (file)
@@ -98,6 +98,11 @@ if ($form->is_cancelled()) {
 
     $newid = $DB->insert_record('badge', $fordb, true);
 
+    // Trigger event, badge created.
+    $eventparams = array('objectid' => $newid, 'context' => $PAGE->context);
+    $event = \core\event\badge_created::create($eventparams);
+    $event->trigger();
+
     $newbadge = new badge($newid);
     badges_process_badge_image($newbadge, $form->save_temp_file('image'));
     // If a user can configure badge criteria, they will be redirected to the criteria page.
index 833cf09..cfe5215 100644 (file)
@@ -55,4 +55,259 @@ class core_badges_events_testcase extends core_badges_badgeslib_testcase {
 
         $sink->close();
     }
-}
\ No newline at end of file
+
+    /**
+     * Test the badge created event.
+     *
+     * There is no external API for creating a badge, so the unit test will simply
+     * create and trigger the event and ensure data is returned as expected.
+     */
+    public function test_badge_created() {
+
+        $badge = new badge($this->badgeid);
+        // Trigger an event: badge created.
+        $eventparams = array(
+            'userid' => $badge->usercreated,
+            'objectid' => $badge->id,
+            'context' => $badge->get_context(),
+        );
+
+        $event = \core\event\badge_created::create($eventparams);
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $event->trigger();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\badge_created', $event);
+        $this->assertEquals($badge->usercreated, $event->userid);
+        $this->assertEquals($badge->id, $event->objectid);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+
+    /**
+     * Test the badge archived event.
+     *
+     */
+    public function test_badge_archived() {
+        $badge = new badge($this->badgeid);
+        $sink = $this->redirectEvents();
+
+        // Trigger and capture the event.
+        $badge->delete(true);
+        $events = $sink->get_events();
+        $this->assertCount(2, $events);
+        $event = $events[1];
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\badge_archived', $event);
+        $this->assertEquals($badge->id, $event->objectid);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+
+
+    /**
+     * Test the badge updated event.
+     *
+     */
+    public function test_badge_updated() {
+        $badge = new badge($this->badgeid);
+        $sink = $this->redirectEvents();
+
+        // Trigger and capture the event.
+        $badge->save();
+        $events = $sink->get_events();
+        $event = reset($events);
+        $this->assertCount(1, $events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\badge_updated', $event);
+        $this->assertEquals($badge->id, $event->objectid);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+    /**
+     * Test the badge deleted event.
+     */
+    public function test_badge_deleted() {
+        $badge = new badge($this->badgeid);
+        $sink = $this->redirectEvents();
+
+        // Trigger and capture the event.
+        $badge->delete(false);
+        $events = $sink->get_events();
+        $event = reset($events);
+        $this->assertCount(1, $events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\badge_deleted', $event);
+        $this->assertEquals($badge->id, $event->objectid);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+
+    /**
+     * Test the badge duplicated event.
+     *
+     */
+    public function test_badge_duplicated() {
+        $badge = new badge($this->badgeid);
+        $sink = $this->redirectEvents();
+
+        // Trigger and capture the event.
+        $newid = $badge->make_clone();
+        $events = $sink->get_events();
+        $event = reset($events);
+        $this->assertCount(1, $events);
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\badge_duplicated', $event);
+        $this->assertEquals($newid, $event->objectid);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+
+    /**
+     * Test the badge disabled event.
+     *
+     */
+    public function test_badge_disabled() {
+        $badge = new badge($this->badgeid);
+        $sink = $this->redirectEvents();
+
+        // Trigger and capture the event.
+        $badge->set_status(BADGE_STATUS_INACTIVE);
+        $events = $sink->get_events();
+        $event = reset($events);
+        $this->assertCount(2, $events);
+        $event = $events[1];
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\badge_disabled', $event);
+        $this->assertEquals($badge->id, $event->objectid);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+
+    /**
+     * Test the badge enabled event.
+     *
+     */
+    public function test_badge_enabled() {
+        $badge = new badge($this->badgeid);
+        $sink = $this->redirectEvents();
+
+        // Trigger and capture the event.
+        $badge->set_status(BADGE_STATUS_ACTIVE);
+        $events = $sink->get_events();
+        $event = reset($events);
+        $this->assertCount(2, $events);
+        $event = $events[1];
+
+        // Check that the event data is valid.
+        $this->assertInstanceOf('\core\event\badge_enabled', $event);
+        $this->assertEquals($badge->id, $event->objectid);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+
+    /**
+     * Test the badge criteria created event.
+     *
+     * There is no external API for this, so the unit test will simply
+     * create and trigger the event and ensure data is returned as expected.
+     */
+    public function test_badge_criteria_created() {
+
+        $badge = new badge($this->badgeid);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
+        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
+        $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
+        $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
+        $criteriaprofile->save($params);
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertCount(1, $events);
+        $this->assertInstanceOf('\core\event\badge_criteria_created', $event);
+        $this->assertEquals($criteriaprofile->id, $event->objectid);
+        $this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+
+    /**
+     * Test the badge criteria updated event.
+     *
+     * There is no external API for this, so the unit test will simply
+     * create and trigger the event and ensure data is returned as expected.
+     */
+    public function test_badge_criteria_updated() {
+
+        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
+        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
+        $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
+        $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
+        $criteriaprofile->save($params);
+        $badge = new badge($this->badgeid);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $criteria = $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE];
+        $params2 = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'id' => $criteria->id);
+        $criteria->save((array)$params2);
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertCount(1, $events);
+        $this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
+        $this->assertEquals($criteria->id, $event->objectid);
+        $this->assertEquals($this->badgeid, $event->other['badgeid']);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+
+    /**
+     * Test the badge criteria deleted event.
+     *
+     * There is no external API for this, so the unit test will simply
+     * create and trigger the event and ensure data is returned as expected.
+     */
+    public function test_badge_criteria_deleted() {
+
+        $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
+        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
+        $badge = new badge($this->badgeid);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+        $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        // Check that the event data is valid.
+        $this->assertCount(1, $events);
+        $this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
+        $this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
+        $this->assertDebuggingNotCalled();
+        $sink->close();
+
+    }
+}
index 1077607..aaf102b 100644 (file)
@@ -248,7 +248,17 @@ $string['error:requesttimeout'] = 'The connection request timed out before it co
 $string['error:requesterror'] = 'The connection request failed (error code {$a}).';
 $string['error:save'] = 'Cannot save the badge.';
 $string['error:userdeleted'] = '{$a->user} (This user no longer exists in {$a->site})';
+$string['eventbadgearchived'] = 'Badge archived';
 $string['eventbadgeawarded'] = 'Badge awarded';
+$string['eventbadgecreated'] = 'Badge created';
+$string['eventbadgecriteriacreated'] = 'Badge criteria created';
+$string['eventbadgecriteriadeleted'] = 'Badge criteria deleted';
+$string['eventbadgecriteriaupdated'] = 'Badge criteria updated';
+$string['eventbadgedeleted'] = 'Badge deleted';
+$string['eventbadgedisabled'] = 'Badge disabled';
+$string['eventbadgeduplicated'] = 'Badge duplicated';
+$string['eventbadgeenabled'] = 'Badge enabled';
+$string['eventbadgeupdated'] = 'Badge updated';
 $string['evidence'] = 'Evidence';
 $string['existingrecipients'] = 'Existing badge recipients';
 $string['expired'] = 'Expired';
index 9103fcd..d77d739 100644 (file)
@@ -222,6 +222,10 @@ class badge {
 
         $fordb->timemodified = time();
         if ($DB->update_record_raw('badge', $fordb)) {
+            // Trigger event, badge updated.
+            $eventparams = array('objectid' => $this->id, 'context' => $this->get_context());
+            $event = \core\event\badge_updated::create($eventparams);
+            $event->trigger();
             return true;
         } else {
             throw new moodle_exception('error:save', 'badges');
@@ -236,7 +240,7 @@ class badge {
      * @return int ID of new badge.
      */
     public function make_clone() {
-        global $DB, $USER;
+        global $DB, $USER, $PAGE;
 
         $fordb = new stdClass();
         foreach (get_object_vars($this) as $k => $v) {
@@ -274,6 +278,11 @@ class badge {
                 $crit->make_clone($new);
             }
 
+            // Trigger event, badge duplicated.
+            $eventparams = array('objectid' => $new, 'context' => $PAGE->context);
+            $event = \core\event\badge_duplicated::create($eventparams);
+            $event->trigger();
+
             return $new;
         } else {
             throw new moodle_exception('error:clone', 'badges');
@@ -312,6 +321,17 @@ class badge {
     public function set_status($status = 0) {
         $this->status = $status;
         $this->save();
+        if ($status == BADGE_STATUS_ACTIVE) {
+            // Trigger event, badge enabled.
+            $eventparams = array('objectid' => $this->id, 'context' => $this->get_context());
+            $event = \core\event\badge_enabled::create($eventparams);
+            $event->trigger();
+        } else if ($status == BADGE_STATUS_INACTIVE) {
+            // Trigger event, badge disabled.
+            $eventparams = array('objectid' => $this->id, 'context' => $this->get_context());
+            $event = \core\event\badge_disabled::create($eventparams);
+            $event->trigger();
+        }
     }
 
     /**
@@ -628,6 +648,11 @@ class badge {
         if ($archive) {
             $this->status = BADGE_STATUS_ARCHIVED;
             $this->save();
+
+            // Trigger event, badge archived.
+            $eventparams = array('objectid' => $this->id, 'context' => $this->get_context());
+            $event = \core\event\badge_archived::create($eventparams);
+            $event->trigger();
             return;
         }
 
@@ -654,6 +679,14 @@ class badge {
 
         // Finally, remove badge itself.
         $DB->delete_records('badge', array('id' => $this->id));
+
+        // Trigger event, badge deleted.
+        $eventparams = array('objectid' => $this->id,
+            'context' => $this->get_context(),
+            'other' => array('badgetype' => $this->type, 'courseid' => $this->courseid)
+            );
+        $event = \core\event\badge_deleted::create($eventparams);
+        $event->trigger();
     }
 }
 
diff --git a/lib/classes/event/badge_archived.php b/lib/classes/event/badge_archived.php
new file mode 100644 (file)
index 0000000..4482e42
--- /dev/null
@@ -0,0 +1,98 @@
+<?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/>.
+
+/**
+ * Badge archived event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after a badge is archived.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_archived extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge';
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgearchived', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has archived the badge with id '$this->objectid'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/overview.php', array('id' => $this->objectid));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge', 'restore' => 'badge');
+    }
+
+}
+
+
+
diff --git a/lib/classes/event/badge_created.php b/lib/classes/event/badge_created.php
new file mode 100644 (file)
index 0000000..3cfc9db
--- /dev/null
@@ -0,0 +1,96 @@
+<?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/>.
+
+/**
+ * Badge created event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after a badge is created.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_created extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge';
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgecreated', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has created the badge with id '$this->objectid'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/overview.php', array('id' => $this->objectid));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge', 'restore' => 'badge');
+    }
+
+}
+
diff --git a/lib/classes/event/badge_criteria_created.php b/lib/classes/event/badge_criteria_created.php
new file mode 100644 (file)
index 0000000..fd69497
--- /dev/null
@@ -0,0 +1,116 @@
+<?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/>.
+
+/**
+ * Badge criteria created event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after criteria is created for a badge.
+ *
+ * @property-read array $other {
+ *      Extra information about the event.
+ *
+ *      - int badgeid: The ID of the badge affected
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_criteria_created extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge_criteria';
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgecriteriacreated', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has created criteria to the badge with id '".$this->other['badgeid']."'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/criteria.php', array('id' => $this->other['badgeid']));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+        if (!isset($this->other['badgeid'])) {
+            throw new \coding_exception('The \'badgeid\' value must be set in other.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     *
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge_criteria', 'restore' => 'badge_criteria');
+    }
+
+    /**
+     * Used for maping events on restore
+     *
+     * @return bool
+     */
+    public static function get_other_mapping() {
+        $othermapped = array();
+        $othermapped['badgeid'] = array('db' => 'badge', 'restore' => 'badge');
+        return $othermapped;
+    }
+}
+
+
diff --git a/lib/classes/event/badge_criteria_deleted.php b/lib/classes/event/badge_criteria_deleted.php
new file mode 100644 (file)
index 0000000..71c5e01
--- /dev/null
@@ -0,0 +1,117 @@
+<?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/>.
+
+/**
+ * Badge criteria deleted event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after criteria is deleted from a badge.
+ *
+ * @property-read array $other {
+ *      Extra information about the event.
+ *
+ *      - int badgeid: The ID of the badge affected
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_criteria_deleted extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge_criteria';
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgecriteriadeleted', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has deleted criteria from the badge with id '".$this->other['badgeid']."'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/criteria.php', array('id' => $this->other['badgeid']));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+        if (!isset($this->other['badgeid'])) {
+            throw new \coding_exception('The \'badgeid\' value must be set in other.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     *
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge_criteria', 'restore' => 'badge_criteria');
+    }
+
+    /**
+     * Used for maping events on restore
+     *
+     * @return bool
+     */
+    public static function get_other_mapping() {
+        $othermapped = array();
+        $othermapped['badgeid'] = array('db' => 'badge', 'restore' => 'badge');
+        return $othermapped;
+    }
+}
+
+
+
diff --git a/lib/classes/event/badge_criteria_updated.php b/lib/classes/event/badge_criteria_updated.php
new file mode 100644 (file)
index 0000000..0406e6e
--- /dev/null
@@ -0,0 +1,115 @@
+<?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/>.
+
+/**
+ * Badge criteria updated event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after criteria is updated to a badge.
+ *
+ *
+ * @property-read array $other {
+ *      Extra information about the event.
+ *
+ *      - int badgeid: The ID of the badge affected
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_criteria_updated extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge_criteria';
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgecriteriaupdated', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has updated criteria to the badge with id '".$this->other['badgeid']."'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/criteria.php', array('id' => $this->other['badgeid']));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+        if (!isset($this->other['badgeid'])) {
+            throw new \coding_exception('The \'badgeid\' value must be set in other.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     *
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge_criteria', 'restore' => 'badge_criteria');
+    }
+
+    /**
+     * Used for maping events on restore
+     *
+     * @return bool
+     */
+    public static function get_other_mapping() {
+        $othermapped = array();
+        $othermapped['badgeid'] = array('db' => 'badge', 'restore' => 'badge');
+        return $othermapped;
+    }
+}
diff --git a/lib/classes/event/badge_deleted.php b/lib/classes/event/badge_deleted.php
new file mode 100644 (file)
index 0000000..0dd4fba
--- /dev/null
@@ -0,0 +1,119 @@
+<?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/>.
+
+/**
+ * Badge deleted event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+require_once($CFG->libdir . '/badgeslib.php');
+
+
+/**
+ * Event triggered after a badge is deleted.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_deleted extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge';
+        $this->data['crud'] = 'd';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgedeleted', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has deleted the badge with id '$this->objectid'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        if ($this->other['badgetype'] == BADGE_TYPE_COURSE) {
+            // Course badge.
+            $return = new \moodle_url('/badges/index.php',
+                    array('type' => BADGE_TYPE_COURSE, 'id' => $this->other['courseid']));
+        } else {
+            // Site badge.
+            $return = new \moodle_url('/badges/index.php', array('type' => BADGE_TYPE_SITE));
+        }
+        return $return;
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+        if (!isset($this->other['badgetype'])) {
+            throw new \coding_exception('The \'badgetype\' value must be set in other.');
+        } else {
+            if (($this->other['badgetype'] != BADGE_TYPE_COURSE) && ($this->other['badgetype'] != BADGE_TYPE_SITE)) {
+                throw new \coding_exception('Invalid \'badgetype\' value.');
+            }
+        }
+        if ($this->other['badgetype'] == BADGE_TYPE_COURSE) {
+            if (!isset($this->other['courseid'])) {
+                throw new \coding_exception('The \'courseid\' value must be set in other.');
+            }
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge', 'restore' => 'badge');
+    }
+
+}
+
+
diff --git a/lib/classes/event/badge_disabled.php b/lib/classes/event/badge_disabled.php
new file mode 100644 (file)
index 0000000..bcd9f55
--- /dev/null
@@ -0,0 +1,97 @@
+<?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/>.
+
+/**
+ * Badge disabled event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after a badge is disabled.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_disabled extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge';
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgedisabled', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has disabled access to the badge with id '$this->objectid'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/overview.php', array('id' => $this->objectid));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge', 'restore' => 'badge');
+    }
+
+}
+
+
diff --git a/lib/classes/event/badge_duplicated.php b/lib/classes/event/badge_duplicated.php
new file mode 100644 (file)
index 0000000..e159e9f
--- /dev/null
@@ -0,0 +1,97 @@
+<?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/>.
+
+/**
+ * Badge duplicated event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after a badge is duplicated.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_duplicated extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge';
+        $this->data['crud'] = 'c';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgeduplicated', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has duplicated the badge with id '$this->objectid'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/overview.php', array('id' => $this->objectid));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge', 'restore' => 'badge');
+    }
+
+}
+
+
diff --git a/lib/classes/event/badge_enabled.php b/lib/classes/event/badge_enabled.php
new file mode 100644 (file)
index 0000000..774e4ad
--- /dev/null
@@ -0,0 +1,97 @@
+<?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/>.
+
+/**
+ * Badge enabled event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after a badge is enabled.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_enabled extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge';
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgeenabled', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has enabled access to the badge with id '$this->objectid'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/overview.php', array('id' => $this->objectid));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge', 'restore' => 'badge');
+    }
+
+}
+
+
diff --git a/lib/classes/event/badge_updated.php b/lib/classes/event/badge_updated.php
new file mode 100644 (file)
index 0000000..9237762
--- /dev/null
@@ -0,0 +1,96 @@
+<?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/>.
+
+/**
+ * Badge updated event.
+ *
+ * @package    core
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\event;
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Event triggered after a badge is updated.
+ *
+ * @package    core
+ * @since      Moodle 3.2
+ * @copyright  2016 Stephen Bourget
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class badge_updated extends base {
+
+    /**
+     * Set basic properties for the event.
+     */
+    protected function init() {
+        $this->data['objecttable'] = 'badge';
+        $this->data['crud'] = 'u';
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
+    }
+
+    /**
+     * Returns localised general event name.
+     *
+     * @return string
+     */
+    public static function get_name() {
+        return get_string('eventbadgeupdated', 'badges');
+    }
+
+    /**
+     * Returns non-localised event description with id's for admin use only.
+     *
+     * @return string
+     */
+    public function get_description() {
+        return "The user with id '$this->userid' has updated the badge with id '$this->objectid'.";
+    }
+
+    /**
+     * Returns relevant URL.
+     * @return \moodle_url
+     */
+    public function get_url() {
+        return new \moodle_url('/badges/overview.php', array('id' => $this->objectid));
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
+    }
+
+    /**
+     * Used for maping events on restore
+     * @return array
+     */
+    public static function get_objectid_mapping() {
+        return array('db' => 'badge', 'restore' => 'badge');
+    }
+
+}
+
index 30ab917..c7460a0 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2016072800.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2016072800.02;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.