Skip to content

Commit

Permalink
MDL-40912 coursecat: replaced 'move' add_to_log calls with an event
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Jan 15, 2014
1 parent c4cea8c commit d86c720
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
16 changes: 16 additions & 0 deletions lib/classes/event/course_category_updated.php
Expand Up @@ -27,6 +27,9 @@
*/
class course_category_updated extends base {

/** @var array The legacy log data. */
private $legacylogdata;

/**
* Initialise the event data.
*/
Expand Down Expand Up @@ -55,12 +58,25 @@ public function get_description() {
$this->userid;
}

/**
* Set the legacy data used for add_to_log().
*
* @param array $logdata
*/
public function set_legacy_logdata($logdata) {
$this->legacylogdata = $logdata;
}

/**
* Return legacy data for add_to_log().
*
* @return array
*/
protected function get_legacy_logdata() {
if (!empty($this->legacylogdata)) {
return $this->legacylogdata;
}

return array(SITEID, 'category', 'update', 'editcategory.php?id=' . $this->objectid, $this->objectid);
}
}
26 changes: 23 additions & 3 deletions lib/coursecatlib.php
Expand Up @@ -1752,7 +1752,13 @@ public function delete_move($newparentid, $showfeedback = false) {
foreach ($children as $childcat) {
$childcat->change_parent_raw($newparentcat);
// Log action.
add_to_log(SITEID, "category", "move", "editcategory.php?id=$childcat->id", $childcat->id);
$event = \core\event\course_category_updated::create(array(
'objectid' => $childcat->id,
'context' => $childcat->get_context()
));
$event->set_legacy_logdata(array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id,
$childcat->id));
$event->trigger();
}
fix_course_sortorder();
}
Expand Down Expand Up @@ -1920,7 +1926,13 @@ public function change_parent($newparentcat) {
fix_course_sortorder();
cache_helper::purge_by_event('changesincoursecat');
$this->restore();
add_to_log(SITEID, "category", "move", "editcategory.php?id=$this->id", $this->id);

$event = \core\event\course_category_updated::create(array(
'objectid' => $this->id,
'context' => $this->get_context()
));
$event->set_legacy_logdata(array(SITEID, 'category', 'move', 'editcategory.php?id=' . $this->id, $this->id));
$event->trigger();
}
}

Expand Down Expand Up @@ -2541,7 +2553,15 @@ public function change_sortorder_by_one($up) {
$DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id' => $this->id));
$DB->set_field('course_categories', 'sortorder', $this->sortorder, array('id' => $swapcategory->id));
$this->sortorder = $swapcategory->sortorder;
add_to_log(SITEID, "category", "move", "management.php?categoryid={$this->id}", $this->id);

$event = \core\event\course_category_updated::create(array(
'objectid' => $this->id,
'context' => $this->get_context()
));
$event->set_legacy_logdata(array(SITEID, 'category', 'move', 'management.php?categoryid=' . $this->id,
$this->id));
$event->trigger();

// Finally reorder courses.
fix_course_sortorder();
cache_helper::purge_by_event('changesincoursecat');
Expand Down
42 changes: 41 additions & 1 deletion lib/tests/events_test.php
Expand Up @@ -64,7 +64,7 @@ public function test_course_category_updated() {
$data = new stdClass();
$data->name = 'Category name change';

// Trigger and capture the event.
// Trigger and capture the event for updating a category.
$sink = $this->redirectEvents();
$category->update($data);
$events = $sink->get_events();
Expand All @@ -75,5 +75,45 @@ public function test_course_category_updated() {
$this->assertEquals(context_coursecat::instance($category->id), $event->get_context());
$expected = array(SITEID, 'category', 'update', 'editcategory.php?id=' . $category->id, $category->id);
$this->assertEventLegacyLogData($expected, $event);

// Create another category and a child category.
$category2 = $this->getDataGenerator()->create_category();
$childcat = $this->getDataGenerator()->create_category(array('parent' => $category2->id));

// Trigger and capture the event for changing the parent of a category.
$sink = $this->redirectEvents();
$childcat->change_parent($category);
$events = $sink->get_events();
$event = reset($events);

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\course_category_updated', $event);
$this->assertEquals(context_coursecat::instance($childcat->id), $event->get_context());
$expected = array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id, $childcat->id);
$this->assertEventLegacyLogData($expected, $event);

// Trigger and capture the event for changing the sortorder of a category.
$sink = $this->redirectEvents();
$category2->change_sortorder_by_one(true);
$events = $sink->get_events();
$event = reset($events);

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\course_category_updated', $event);
$this->assertEquals(context_coursecat::instance($category2->id), $event->get_context());
$expected = array(SITEID, 'category', 'move', 'management.php?categoryid=' . $category2->id, $category2->id);
$this->assertEventLegacyLogData($expected, $event);

// Trigger and capture the event for deleting a category and moving it's children to another.
$sink = $this->redirectEvents();
$category->delete_move($category->id);
$events = $sink->get_events();
$event = reset($events);

// Check that the event data is valid.
$this->assertInstanceOf('\core\event\course_category_updated', $event);
$this->assertEquals(context_coursecat::instance($childcat->id), $event->get_context());
$expected = array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id, $childcat->id);
$this->assertEventLegacyLogData($expected, $event);
}
}

0 comments on commit d86c720

Please sign in to comment.