Skip to content

Commit

Permalink
MDL-44403 events: added public method to get eventdata
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Mar 4, 2014
1 parent 76e4de3 commit 24c32bd
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 73 deletions.
6 changes: 3 additions & 3 deletions blog/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function add() {
'objectid' => $this->id,
'relateduserid' => $this->userid
));
$event->set_custom_data($this);
$event->set_blog_entry($this);
$event->trigger();
}

Expand Down Expand Up @@ -309,7 +309,7 @@ public function edit($params=array(), $form=null, $summaryoptions=array(), $atta
'objectid' => $entry->id,
'relateduserid' => $entry->userid
));
$event->set_custom_data($entry);
$event->set_blog_entry($entry);
$event->trigger();
}

Expand All @@ -334,7 +334,7 @@ public function delete() {
'relateduserid' => $this->userid
));
$event->add_record_snapshot("post", $record);
$event->set_custom_data($this);
$event->set_blog_entry($this);
$event->trigger();
}

Expand Down
2 changes: 0 additions & 2 deletions group/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ function groups_create_grouping($data, $editoroptions=null) {
'objectid' => $id
);
$event = \core\event\grouping_created::create($params);
$event->set_legacy_eventdata($data);
$event->trigger();

return $id;
Expand Down Expand Up @@ -442,7 +441,6 @@ function groups_update_grouping($data, $editoroptions=null) {
'objectid' => $data->id
);
$event = \core\event\grouping_updated::create($params);
$event->set_legacy_eventdata($data);
$event->trigger();

return true;
Expand Down
16 changes: 8 additions & 8 deletions group/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ public function test_grouping_created_event() {

$this->assertInstanceOf('\core\event\grouping_created', $event);

// 'Repairing' the object for comparison because of type of variables being wrong.
$group->id = (int) $group->id;
$group->timemodified = (int) $group->timemodified;
$group->timecreated = (int) $group->timecreated;
unset($group->idnumber);
unset($group->configdata);
$this->assertEventLegacyData($group, $event);
$this->assertSame('groups_grouping_created', $event->get_legacy_eventname());

Expand Down Expand Up @@ -187,10 +181,16 @@ public function test_grouping_updated_event() {

$this->assertInstanceOf('\core\event\grouping_updated', $event);

// 'Repairing' the object for comparison because of type of variables being wrong.
$data->id = (int) $grouping->id;
// Get the timemodified from DB for comparison with snapshot.
$data->timemodified = $DB->get_field('groupings', 'timemodified', array('id'=>$grouping->id));
$this->assertTimeCurrent($data->timemodified);
// Following fields were not updated so the snapshot should have them the same as in original group.
$data->description = $grouping->description;
$data->descriptionformat = $grouping->descriptionformat;
$data->configdata = $grouping->configdata;
$data->idnumber = $grouping->idnumber;
$data->timecreated = $grouping->timecreated;
// Assert legacy event data.
$this->assertEventLegacyData($data, $event);
$this->assertSame('groups_grouping_updated', $event->get_legacy_eventname());

Expand Down
26 changes: 19 additions & 7 deletions lib/classes/event/blog_entry_created.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
*/
class blog_entry_created extends \core\event\base {

/** @var \blog_entry A reference to the active blog_entry object. */
protected $customobject;
/** @var \blog_entry A reference to the active blog_entry object. */
protected $blogentry;

/**
* Set basic properties for the event.
Expand All @@ -51,12 +51,24 @@ protected function init() {
}

/**
* Set custom data of the event.
* Set blog_entry object to be used by observers.
*
* @param \blog_entry $data A reference to the active blog_entry object.
*/
public function set_custom_data(\blog_entry $data) {
$this->customobject = $data;
public function set_blog_entry(\blog_entry $blogentry) {
$this->blogentry = $blogentry;
}

/**
* Returns created blog_entry object for event observers.
*
* @return \blog_entry
*/
public function get_blog_entry() {
if ($this->is_restored()) {
throw new \coding_exception('Function get_blog_entry() can not be used on restored events.');
}
return $this->blogentry;
}

/**
Expand Down Expand Up @@ -100,7 +112,7 @@ public static function get_legacy_eventname() {
* @return \blog_entry
*/
protected function get_legacy_eventdata() {
return $this->customobject;
return $this->blogentry;
}

/**
Expand All @@ -110,6 +122,6 @@ protected function get_legacy_eventdata() {
*/
protected function get_legacy_logdata() {
return array (SITEID, 'blog', 'add', 'index.php?userid=' . $this->relateduserid . '&entryid=' . $this->objectid,
$this->customobject->subject);
$this->blogentry->subject);
}
}
22 changes: 17 additions & 5 deletions lib/classes/event/blog_entry_deleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
class blog_entry_deleted extends \core\event\base {

/** @var \blog_entry A reference to the active blog_entry object. */
protected $customobject;
protected $blogentry;

/**
* Set basic event properties.
Expand All @@ -59,12 +59,24 @@ public static function get_name() {
}

/**
* Set custom data of the event.
* Sets blog_entry object to be used by observers.
*
* @param \blog_entry $data A reference to the active blog_entry object.
*/
public function set_custom_data(\blog_entry $data) {
$this->customobject = $data;
public function set_blog_entry(\blog_entry $blogentry) {
$this->blogentry = $blogentry;
}

/**
* Returns deleted blog entry for event observers.
*
* @return \blog_entry
*/
public function get_blog_entry() {
if ($this->is_restored()) {
throw new \coding_exception('Function get_blog_entry() can not be used on restored events.');
}
return $this->blogentry;
}

/**
Expand All @@ -91,7 +103,7 @@ public static function get_legacy_eventname() {
* @return \blog_entry
*/
protected function get_legacy_eventdata() {
return $this->customobject;
return $this->blogentry;
}

/**
Expand Down
24 changes: 18 additions & 6 deletions lib/classes/event/blog_entry_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class blog_entry_updated extends base {

/** @var \blog_entry A reference to the active blog_entry object. */
protected $customobject;
protected $blogentry;

/**
* Set basic event properties.
Expand All @@ -49,12 +49,24 @@ protected function init() {
}

/**
* Set custom data of the event.
* Sets blog_entry object to be used by observers.
*
* @param \blog_entry $data A reference to the active blog_entry object.
*/
public function set_custom_data(\blog_entry $data) {
$this->customobject = $data;
public function set_blog_entry(\blog_entry $blogentry) {
$this->blogentry = $blogentry;
}

/**
* Returns updated blog entry for event observers.
*
* @return \blog_entry
*/
public function get_blog_entry() {
if ($this->is_restored()) {
throw new \coding_exception('Function get_blog_entry() can not be used on restored events.');
}
return $this->blogentry;
}

/**
Expand Down Expand Up @@ -89,7 +101,7 @@ public function get_url() {
* @return \blog_entry
*/
protected function get_legacy_eventdata() {
return $this->customobject;
return $this->blogentry;
}

/**
Expand All @@ -108,7 +120,7 @@ public static function get_legacy_eventname() {
*/
protected function get_legacy_logdata() {
return array(SITEID, 'blog', 'update', 'index.php?userid=' . $this->relateduserid . '&entryid=' . $this->objectid,
$this->customobject->subject);
$this->blogentry->subject);
}
}

20 changes: 16 additions & 4 deletions lib/classes/event/course_category_deleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,24 @@ protected function get_legacy_eventdata() {
}

/**
* Set the legacy event data.
* Set custom data of the event - deleted coursecat.
*
* @param coursecat $class instance of the coursecat class
* @param \coursecat $coursecat
*/
public function set_legacy_eventdata($class) {
$this->coursecat = $class;
public function set_coursecat(\coursecat $coursecat) {
$this->coursecat = $coursecat;
}

/**
* Returns deleted coursecat for event observers.
*
* @return \coursecat
*/
public function get_coursecat() {
if ($this->is_restored()) {
throw new \coding_exception('Function get_coursecat() can not be used on restored events.');
}
return $this->coursecat;
}

/**
Expand Down
19 changes: 1 addition & 18 deletions lib/classes/event/grouping_created.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@
*/
class grouping_created extends \core\event\base {

/**
* Legacy data.
*
* @var mixed
*/
protected $legacydata;

/**
* Returns description of what happened.
*
Expand All @@ -56,7 +49,7 @@ public function get_description() {
* @return stdClass
*/
protected function get_legacy_eventdata() {
return $this->legacydata;
return $this->get_record_snapshot('groupings', $this->objectid);
}

/**
Expand Down Expand Up @@ -97,14 +90,4 @@ protected function init() {
$this->data['objecttable'] = 'groupings';
}

/**
* Set legacy data.
*
* @param mixed $legacydata.
* @return void
*/
public function set_legacy_eventdata($legacydata) {
$this->legacydata = $legacydata;
}

}
19 changes: 1 addition & 18 deletions lib/classes/event/grouping_updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@
*/
class grouping_updated extends \core\event\base {

/**
* Legacy data.
*
* @var mixed
*/
protected $legacydata;

/**
* Returns description of what happened.
*
Expand All @@ -56,7 +49,7 @@ public function get_description() {
* @return stdClass
*/
protected function get_legacy_eventdata() {
return $this->legacydata;
return $this->get_record_snapshot('groupings', $this->objectid);
}

/**
Expand Down Expand Up @@ -97,14 +90,4 @@ protected function init() {
$this->data['objecttable'] = 'groupings';
}

/**
* Set legacy data.
*
* @param mixed $legacydata.
* @return void
*/
public function set_legacy_eventdata($legacydata) {
$this->legacydata = $legacydata;
}

}
4 changes: 2 additions & 2 deletions lib/coursecatlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ public function delete_full($showfeedback = true) {
'context' => $coursecatcontext,
'other' => array('name' => $this->name)
));
$event->set_legacy_eventdata($this);
$event->set_coursecat($this);
$event->trigger();

// If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
Expand Down Expand Up @@ -1798,7 +1798,7 @@ public function delete_move($newparentid, $showfeedback = false) {
'context' => $context,
'other' => array('name' => $this->name)
));
$event->set_legacy_eventdata($this);
$event->set_coursecat($this);
$event->trigger();

cache_helper::purge_by_event('changesincoursecat');
Expand Down

0 comments on commit 24c32bd

Please sign in to comment.