Skip to content

Commit

Permalink
MDL-43557 mod_lesson: removed incorrect usage of snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Apr 30, 2014
1 parent f3d9818 commit 4d9c1b5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
44 changes: 37 additions & 7 deletions mod/lesson/classes/event/highscore_added.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@

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 <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/

class highscore_added extends \core\event\base {

/**
Expand Down Expand Up @@ -62,10 +78,8 @@ public function get_url() {
* @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'.";
}

/**
Expand All @@ -74,9 +88,25 @@ public function get_description() {
* @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.');
}
}
}
4 changes: 4 additions & 0 deletions mod/lesson/highscores.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@
'objectid' => $newhighscore->id,
'context' => $context,
'courseid' => $course->id,
'other' => array(
'lessonid' => $lesson->id,
'nickname' => $newhighscore->nickname
)
));
$event->trigger();

Expand Down
6 changes: 5 additions & 1 deletion mod/lesson/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public function test_highscore_added() {
$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.
Expand Down

0 comments on commit 4d9c1b5

Please sign in to comment.