From a0e13871cf21b5e2c42529ecd4a89c3530ea8a2a Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Fri, 29 Jul 2016 21:51:05 -0400 Subject: [PATCH] MDL-45734 Badges: Add additional events --- badges/criteria/award_criteria.php | 24 +- badges/newbadge.php | 5 + badges/tests/events_test.php | 257 ++++++++++++++++++- lang/en/badges.php | 10 + lib/badgeslib.php | 35 ++- lib/classes/event/badge_archived.php | 98 +++++++ lib/classes/event/badge_created.php | 96 +++++++ lib/classes/event/badge_criteria_created.php | 116 +++++++++ lib/classes/event/badge_criteria_deleted.php | 117 +++++++++ lib/classes/event/badge_criteria_updated.php | 115 +++++++++ lib/classes/event/badge_deleted.php | 119 +++++++++ lib/classes/event/badge_disabled.php | 97 +++++++ lib/classes/event/badge_duplicated.php | 97 +++++++ lib/classes/event/badge_enabled.php | 97 +++++++ lib/classes/event/badge_updated.php | 96 +++++++ version.php | 2 +- 16 files changed, 1376 insertions(+), 5 deletions(-) create mode 100644 lib/classes/event/badge_archived.php create mode 100644 lib/classes/event/badge_created.php create mode 100644 lib/classes/event/badge_criteria_created.php create mode 100644 lib/classes/event/badge_criteria_deleted.php create mode 100644 lib/classes/event/badge_criteria_updated.php create mode 100644 lib/classes/event/badge_deleted.php create mode 100644 lib/classes/event/badge_disabled.php create mode 100644 lib/classes/event/badge_duplicated.php create mode 100644 lib/classes/event/badge_enabled.php create mode 100644 lib/classes/event/badge_updated.php diff --git a/badges/criteria/award_criteria.php b/badges/criteria/award_criteria.php index 58ccf8616fe..fdc9ee54980 100644 --- a/badges/criteria/award_criteria.php +++ b/badges/criteria/award_criteria.php @@ -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(); } diff --git a/badges/newbadge.php b/badges/newbadge.php index 6c3b74c523a..60f39518539 100644 --- a/badges/newbadge.php +++ b/badges/newbadge.php @@ -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. diff --git a/badges/tests/events_test.php b/badges/tests/events_test.php index 833cf097488..cfe5215b98d 100644 --- a/badges/tests/events_test.php +++ b/badges/tests/events_test.php @@ -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(); + + } +} diff --git a/lang/en/badges.php b/lang/en/badges.php index 1077607fa9f..aaf102ba5e8 100644 --- a/lang/en/badges.php +++ b/lang/en/badges.php @@ -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'; diff --git a/lib/badgeslib.php b/lib/badgeslib.php index 9103fcd081c..d77d739cc30 100644 --- a/lib/badgeslib.php +++ b/lib/badgeslib.php @@ -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 index 00000000000..4482e420997 --- /dev/null +++ b/lib/classes/event/badge_archived.php @@ -0,0 +1,98 @@ +. + +/** + * 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 index 00000000000..3cfc9dbc6a6 --- /dev/null +++ b/lib/classes/event/badge_created.php @@ -0,0 +1,96 @@ +. + +/** + * 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 index 00000000000..fd69497042d --- /dev/null +++ b/lib/classes/event/badge_criteria_created.php @@ -0,0 +1,116 @@ +. + +/** + * 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 index 00000000000..71c5e017c87 --- /dev/null +++ b/lib/classes/event/badge_criteria_deleted.php @@ -0,0 +1,117 @@ +. + +/** + * 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 index 00000000000..0406e6ed1a5 --- /dev/null +++ b/lib/classes/event/badge_criteria_updated.php @@ -0,0 +1,115 @@ +. + +/** + * 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 index 00000000000..0dd4fbaa624 --- /dev/null +++ b/lib/classes/event/badge_deleted.php @@ -0,0 +1,119 @@ +. + +/** + * 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 index 00000000000..bcd9f55b707 --- /dev/null +++ b/lib/classes/event/badge_disabled.php @@ -0,0 +1,97 @@ +. + +/** + * 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 index 00000000000..e159e9fac2a --- /dev/null +++ b/lib/classes/event/badge_duplicated.php @@ -0,0 +1,97 @@ +. + +/** + * 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 index 00000000000..774e4ad7d32 --- /dev/null +++ b/lib/classes/event/badge_enabled.php @@ -0,0 +1,97 @@ +. + +/** + * 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 index 00000000000..92377629510 --- /dev/null +++ b/lib/classes/event/badge_updated.php @@ -0,0 +1,96 @@ +. + +/** + * 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'); + } + +} + diff --git a/version.php b/version.php index 30ab917754e..c7460a0df8a 100644 --- a/version.php +++ b/version.php @@ -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.