MDL-43557 mod_lesson: removed incorrect usage of snapshots
authorMark Nelson <markn@moodle.com>
Thu, 24 Apr 2014 00:13:14 +0000 (17:13 -0700)
committerMark Nelson <markn@moodle.com>
Wed, 30 Apr 2014 02:15:47 +0000 (19:15 -0700)
mod/lesson/classes/event/highscore_added.php
mod/lesson/highscores.php
mod/lesson/tests/events_test.php

index b3ce792..b8cd4aa 100644 (file)
@@ -27,6 +27,22 @@ namespace mod_lesson\event;
 
 defined('MOODLE_INTERNAL') || die();
 
+/**
+ * Event to be triggered when a highscore is added.
+ *
+ * @property-read array $other {
+ *      Extra information about event.
+ *
+ *      - int lessonid: the id of the lesson in the lesson table.
+ *      - string nickname: the user's nickname.
+ * }
+ *
+ * @package    mod_lesson
+ * @since      Moodle 2.7
+ * @copyright  2013 Mark Nelson <markn@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
+ */
+
 class highscore_added extends \core\event\base {
 
     /**
@@ -62,10 +78,8 @@ class highscore_added extends \core\event\base {
      * @return string
      */
     public function get_description() {
-        $highscore = $this->get_record_snapshot('lesson_high_scores', $this->objectid);
-
-        return 'A new highscore was added to the lesson with the id ' . $highscore->lessonid .
-            ' for user with the id ' . $this->userid;
+        return "The user with the id '$this->userid' added a new highscore to the lesson activity with the course module " .
+            "id '$this->contextinstanceid'.";
     }
 
     /**
@@ -74,9 +88,25 @@ class highscore_added extends \core\event\base {
      * @return array of parameters to be passed to legacy add_to_log() function.
      */
     protected function get_legacy_logdata() {
-        $highscore = $this->get_record_snapshot('lesson_high_scores', $this->objectid);
-
         return array($this->courseid, 'lesson', 'update highscores', 'highscores.php?id=' . $this->contextinstanceid,
-            $highscore->nickname, $this->contextinstanceid);
+            $this->other['nickname'], $this->contextinstanceid);
+    }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception when validation fails.
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->other['lessonid'])) {
+            throw new \coding_exception('The \'lessonid\' value must be set in other.');
+        }
+
+        if (!isset($this->other['nickname'])) {
+            throw new \coding_exception('The \'nickname\' value must be set in other.');
+        }
     }
 }
index d1c3596..5cbedbb 100644 (file)
@@ -149,6 +149,10 @@ switch ($mode) {
                 'objectid' => $newhighscore->id,
                 'context' => $context,
                 'courseid' => $course->id,
+                'other' => array(
+                    'lessonid' => $lesson->id,
+                    'nickname' => $newhighscore->nickname
+                )
             ));
             $event->trigger();
 
index 820ebed..dda3ee9 100644 (file)
@@ -105,7 +105,11 @@ class mod_lesson_events_testcase extends advanced_testcase {
         $event = \mod_lesson\event\highscore_added::create(array(
             'objectid' => $newhighscore->id,
             'context' => context_module::instance($this->lesson->properties()->cmid),
-            'courseid' => $this->course->id
+            'courseid' => $this->course->id,
+            'other' => array(
+                'lessonid' => $this->lesson->id,
+                'nickname' => 'noob'
+            )
         ));
 
         // Trigger and capture the event.