From d86c7206aa47a53a04aecca0a98774094ad9f503 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 30 Dec 2013 18:04:40 -0800 Subject: [PATCH] MDL-40912 coursecat: replaced 'move' add_to_log calls with an event --- lib/classes/event/course_category_updated.php | 16 ++++++++++ lib/coursecatlib.php | 26 +++++++++++++++-- lib/tests/events_test.php | 42 ++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/lib/classes/event/course_category_updated.php b/lib/classes/event/course_category_updated.php index ff843ce53cc..978c9e43dfc 100644 --- a/lib/classes/event/course_category_updated.php +++ b/lib/classes/event/course_category_updated.php @@ -27,6 +27,9 @@ defined('MOODLE_INTERNAL') || die(); */ class course_category_updated extends base { + /** @var array The legacy log data. */ + private $legacylogdata; + /** * Initialise the event data. */ @@ -56,11 +59,24 @@ class course_category_updated extends base { } /** + * 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); } } diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index 0e4d49ced64..5cbf71cb27a 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -1752,7 +1752,13 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { 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(); } @@ -1920,7 +1926,13 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { 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(); } } @@ -2541,7 +2553,15 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { $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'); diff --git a/lib/tests/events_test.php b/lib/tests/events_test.php index 9f2187e63ac..6130bc829e1 100644 --- a/lib/tests/events_test.php +++ b/lib/tests/events_test.php @@ -64,7 +64,7 @@ class core_events_testcase extends advanced_testcase { $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(); @@ -75,5 +75,45 @@ class core_events_testcase extends advanced_testcase { $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); } } -- 2.11.4.GIT