MDL-48142 core_badge: bug fixes on badge awarded event class
authorSimey Lameze <simey@moodle.com>
Thu, 19 Feb 2015 04:32:36 +0000 (12:32 +0800)
committerSimey Lameze <simey@moodle.com>
Thu, 19 Feb 2015 04:32:36 +0000 (12:32 +0800)
lib/badgeslib.php
lib/classes/event/badge_awarded.php

index 9e348be..39f48d7 100644 (file)
@@ -399,10 +399,13 @@ class badge {
 
         if ($result) {
             // Trigger badge awarded event.
-            $context = context_system::instance();
-            $event = \core\event\badge_awarded::create(array('context' => $context, 'objectid' => $result,
-                'courseid' => $this->courseid ? $this->courseid : 0, 'relateduserid' => $userid));
-            $event->trigger();
+            $eventdata = array (
+                'context' => $this->get_context(),
+                'objectid' => $this->id,
+                'relateduserid' => $userid,
+                'other' => array('expiredate' => $issued->dateexpire, 'badgeissuedid' => $result)
+            );
+            \core\event\badge_awarded::create($eventdata)->trigger();
 
             // Lock the badge, so that its criteria could not be changed any more.
             if ($this->status == BADGE_STATUS_ACTIVE) {
index 271d6f1..b1a3f0c 100644 (file)
 /**
  * Badge awarded event.
  *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - int expiredate: Badge expire timestamp.
+ *      - int badgeissuedid: Badge issued ID.
+ * }
+ *
  * @package    core
- * @copyright  2014 James Ballard
+ * @copyright  2015 James Ballard
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
@@ -29,8 +36,8 @@ defined('MOODLE_INTERNAL') || die();
  * Event triggered after a badge is awarded to a user.
  *
  * @package    core
- * @since      Moodle 2.7
- * @copyright  2014 James Ballard
+ * @since      Moodle 2.9
+ * @copyright  2015 James Ballard
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 class badge_awarded extends base {
@@ -39,9 +46,9 @@ class badge_awarded extends base {
      * Set basic properties for the event.
      */
     protected function init() {
-        $this->data['objecttable'] = 'badge_issued';
+        $this->data['objecttable'] = 'badge';
         $this->data['crud'] = 'c';
-        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
+        $this->data['edulevel'] = self::LEVEL_TEACHING;
     }
 
     /**
@@ -59,7 +66,7 @@ class badge_awarded extends base {
      * @return string
      */
     public function get_description() {
-        return "The user with id '$this->userid' was awarded the badge with id '$this->objectid'.";
+        return "The user with the id '$this->relateduserid' has been awarded the badge with id '".$this->objectid."'.";
     }
 
     /**
@@ -67,6 +74,24 @@ class badge_awarded extends base {
      * @return \moodle_url
      */
     public function get_url() {
-        return null;
+        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->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+
+        if (!isset($this->objectid)) {
+            throw new \coding_exception('The \'objectid\' must be set.');
+        }
     }
 }