From 3c2fab942b8d70d163bfebddd54c81f9cd961189 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:48:17 +0800 Subject: [PATCH 01/16] MDL-39957 course: replaced the course_created legacy event --- course/lib.php | 15 +++-- lang/en/moodle.php | 1 + lib/classes/event/course_created.php | 90 ++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 lib/classes/event/course_created.php diff --git a/course/lib.php b/course/lib.php index 31f539c0a18..e73403cc88c 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2229,7 +2229,7 @@ function course_overviewfiles_options($course) { * @return object new course instance */ function create_course($data, $editoroptions = NULL) { - global $CFG, $DB; + global $DB; //check the categoryid - must be given for all new courses $category = $DB->get_record('course_categories', array('id'=>$data->category), '*', MUST_EXIST); @@ -2304,10 +2304,15 @@ function create_course($data, $editoroptions = NULL) { // set up enrolments enrol_course_updated(true, $course, $data); - add_to_log(SITEID, 'course', 'new', 'view.php?id='.$course->id, $data->fullname.' (ID '.$course->id.')'); - - // Trigger events - events_trigger('course_created', $course); + // Trigger a course created event. + $event = \core\event\course_created::create(array( + 'objectid' => $course->id, + 'context' => context_course::instance($course->id), + 'other' => array('shortname' => $course->shortname, + 'fullname' => $course->fullname) + )); + $event->add_record_snapshot('course', $course); + $event->trigger(); return $course; } diff --git a/lang/en/moodle.php b/lang/en/moodle.php index fdbabdbea90..82e14b753fc 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -659,6 +659,7 @@ $string['errorcreatingactivity'] = 'Unable to create an instance of activity \'{ $string['errorfiletoobig'] = 'The file was bigger than the limit of {$a} bytes'; $string['errornouploadrepo'] = 'There is no upload repository enabled for this site'; $string['errorwhenconfirming'] = 'You are not confirmed yet because an error occurred. If you clicked on a link in an email to get here, make sure that the line in your email wasn\'t broken or wrapped. You may have to use cut and paste to reconstruct the link properly.'; +$string['eventcoursecreated'] = 'Course created'; $string['everybody'] = 'Everybody'; $string['executeat'] = 'Execute at'; $string['existing'] = 'Existing'; diff --git a/lib/classes/event/course_created.php b/lib/classes/event/course_created.php new file mode 100644 index 00000000000..73c4599e1ca --- /dev/null +++ b/lib/classes/event/course_created.php @@ -0,0 +1,90 @@ +. + +namespace core\event; + +/** + * Course created event. + * + * @package core + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_created extends base { + + /** + * Initialise the event data. + */ + protected function init() { + $this->data['objecttable'] = 'course'; + $this->data['crud'] = 'c'; + $this->data['level'] = 50; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcoursecreated'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return "Course {$this->objectid} was created by user {$this->userid}"; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/course/view.php', array('id' => $this->objectid)); + } + + /** + * Returns the name of the legacy event. + * + * @return string legacy event name + */ + public static function get_legacy_eventname() { + return 'course_created'; + } + + /** + * Returns the legacy event data. + * + * @return \stdClass the course that was created + */ + protected function get_legacy_eventdata() { + return $this->get_record_snapshot('course', $this->objectid); + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + return array(SITEID, 'course', 'new', 'view.php?id=' . $this->objectid, $this->other['fullname'] . ' (ID ' . $this->objectid . ')'); + } +} From 35ad79e20d263c3b22405a5f7b118d7ad69ca121 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:48:48 +0800 Subject: [PATCH 02/16] MDL-39957 course: added PHPUnit test for the new course_created event --- course/tests/courselib_test.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index b805289ca06..9e8fa4d4308 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1334,4 +1334,33 @@ class core_course_courselib_testcase extends advanced_testcase { $eventcount = $DB->count_records('event', array('instance' => $assign->id, 'modulename' => 'assign')); $this->assertEmpty($eventcount); } + + /** + * Test that triggering a course_created event works as expected. + */ + public function test_course_created_event() { + $this->resetAfterTest(); + + // Catch the events. + $sink = $this->redirectEvents(); + + // Create the course. + $course = $this->getDataGenerator()->create_course(); + + // Capture the event. + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_created', $event); + $this->assertEquals('course', $event->objecttable); + $this->assertEquals($course->id, $event->objectid); + $this->assertEquals(context_course::instance($course->id)->id, $event->contextid); + $this->assertEquals($course, $event->get_record_snapshot('course', $course->id)); + $this->assertEquals('course_created', $event->get_legacy_eventname()); + $this->assertEventLegacyData($course, $event); + $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')'); + $this->assertEventLegacyLogData($expectedlog, $event); + } } From 53a8e678bfb15ae3c087786938c476b7bd52b5a6 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:50:10 +0800 Subject: [PATCH 03/16] MDL-39957 course: replaced the course_updated legacy event --- course/lib.php | 15 +++-- lang/en/moodle.php | 1 + lib/classes/event/course_updated.php | 90 ++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 lib/classes/event/course_updated.php diff --git a/course/lib.php b/course/lib.php index e73403cc88c..c799a8baafb 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2328,7 +2328,7 @@ function create_course($data, $editoroptions = NULL) { * @return void */ function update_course($data, $editoroptions = NULL) { - global $CFG, $DB; + global $DB; $data->timemodified = time(); @@ -2398,10 +2398,15 @@ function update_course($data, $editoroptions = NULL) { // update enrol settings enrol_course_updated(false, $course, $data); - add_to_log($course->id, "course", "update", "edit.php?id=$course->id", $course->id); - - // Trigger events - events_trigger('course_updated', $course); + // Trigger a course updated event. + $event = \core\event\course_updated::create(array( + 'objectid' => $course->id, + 'context' => $context, + 'other' => array('shortname' => $course->shortname, + 'fullname' => $course->fullname) + )); + $event->add_record_snapshot('course', $course); + $event->trigger(); if ($oldcourse->format !== $course->format) { // Remove all options stored for the previous format diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 82e14b753fc..88f71587e55 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -660,6 +660,7 @@ $string['errorfiletoobig'] = 'The file was bigger than the limit of {$a} bytes'; $string['errornouploadrepo'] = 'There is no upload repository enabled for this site'; $string['errorwhenconfirming'] = 'You are not confirmed yet because an error occurred. If you clicked on a link in an email to get here, make sure that the line in your email wasn\'t broken or wrapped. You may have to use cut and paste to reconstruct the link properly.'; $string['eventcoursecreated'] = 'Course created'; +$string['eventcourseupdated'] = 'Course updated'; $string['everybody'] = 'Everybody'; $string['executeat'] = 'Execute at'; $string['existing'] = 'Existing'; diff --git a/lib/classes/event/course_updated.php b/lib/classes/event/course_updated.php new file mode 100644 index 00000000000..33cc8c10f08 --- /dev/null +++ b/lib/classes/event/course_updated.php @@ -0,0 +1,90 @@ +. + +namespace core\event; + +/** + * Course updated event. + * + * @package core + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_updated extends base { + + /** + * Initialise the event data. + */ + protected function init() { + $this->data['objecttable'] = 'course'; + $this->data['crud'] = 'u'; + $this->data['level'] = 50; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcourseupdated'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return "Course {$this->courseid} was updated by user {$this->userid}"; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/course/view.php', array('id' => $this->objectid)); + } + + /** + * Returns the name of the legacy event. + * + * @return string legacy event name + */ + public static function get_legacy_eventname() { + return 'course_updated'; + } + + /** + * Returns the legacy event data. + * + * @return \stdClass the course that was updated + */ + protected function get_legacy_eventdata() { + return $this->get_record_snapshot('course', $this->objectid); + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + return array($this->objectid, 'course', 'update', 'edit.php?id=' . $this->objectid, $this->objectid); + } +} From 4fd391d5157994fe8fe92e49fd7b9aa05dd7ca53 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:50:28 +0800 Subject: [PATCH 04/16] MDL-39957 course: added PHPUnit test for the new course_updated event --- course/tests/courselib_test.php | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 9e8fa4d4308..7a2eda03396 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1363,4 +1363,49 @@ class core_course_courselib_testcase extends advanced_testcase { $expectedlog = array(SITEID, 'course', 'new', 'view.php?id=' . $course->id, $course->fullname . ' (ID ' . $course->id . ')'); $this->assertEventLegacyLogData($expectedlog, $event); } + + /** + * Test that triggering a course_updated event works as expected. + */ + public function test_course_updated_event() { + global $DB; + + $this->resetAfterTest(); + + // Create the course. + $course = $this->getDataGenerator()->create_course(); + + // Catch the update event. + $sink = $this->redirectEvents(); + + // Keep track of the old sortorder. + $sortorder = $course->sortorder; + + // Call update_course which will trigger the course_updated event. + update_course($course); + + // Return the updated course information from the DB. + $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST); + + // Now we want to set the sortorder back to what it was. The reason for this is because update_course() + // calls fix_course_sortorder() which alters the sort order in the DB, but it does not then set the + // value of sortorder for the course object which was passed to the legacy event. + $course->sortorder = $sortorder; + + // Capture the event. + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_updated', $event); + $this->assertEquals('course', $event->objecttable); + $this->assertEquals($course->id, $event->objectid); + $this->assertEquals(context_course::instance($course->id)->id, $event->contextid); + $this->assertEquals($course, $event->get_record_snapshot('course', $course->id)); + $this->assertEquals('course_updated', $event->get_legacy_eventname()); + $this->assertEventLegacyData($course, $event); + $expectedlog = array($course->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id); + $this->assertEventLegacyLogData($expectedlog, $event); + } } From c347241ac8113a37e39a7fea395b66b3251db54a Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:51:02 +0800 Subject: [PATCH 05/16] MDL-39957 course: replaced the course_deleted legacy event --- course/delete.php | 4 -- lang/en/moodle.php | 1 + lib/classes/event/course_deleted.php | 84 ++++++++++++++++++++++++++++ lib/moodlelib.php | 13 +++-- 4 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 lib/classes/event/course_deleted.php diff --git a/course/delete.php b/course/delete.php index 71bafca20b9..26c3de29cd9 100644 --- a/course/delete.php +++ b/course/delete.php @@ -63,10 +63,6 @@ print_error('confirmsesskeybad', 'error'); } - // OK checks done, delete the course now. - - add_to_log(SITEID, "course", "delete", "view.php?id=$course->id", "$course->fullname (ID $course->id)"); - $strdeletingcourse = get_string("deletingcourse", "", $courseshortname); $PAGE->navbar->add($strdeletingcourse); diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 88f71587e55..213b717754b 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -660,6 +660,7 @@ $string['errorfiletoobig'] = 'The file was bigger than the limit of {$a} bytes'; $string['errornouploadrepo'] = 'There is no upload repository enabled for this site'; $string['errorwhenconfirming'] = 'You are not confirmed yet because an error occurred. If you clicked on a link in an email to get here, make sure that the line in your email wasn\'t broken or wrapped. You may have to use cut and paste to reconstruct the link properly.'; $string['eventcoursecreated'] = 'Course created'; +$string['eventcoursedeleted'] = 'Course deleted'; $string['eventcourseupdated'] = 'Course updated'; $string['everybody'] = 'Everybody'; $string['executeat'] = 'Execute at'; diff --git a/lib/classes/event/course_deleted.php b/lib/classes/event/course_deleted.php new file mode 100644 index 00000000000..7bdc477e56e --- /dev/null +++ b/lib/classes/event/course_deleted.php @@ -0,0 +1,84 @@ +. + +namespace core\event; + +/** + * Course deleted event. + * + * @package core + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_deleted extends base { + + /** + * Initialise the event data. + */ + protected function init() { + $this->data['objecttable'] = 'course'; + $this->data['crud'] = 'd'; + $this->data['level'] = 50; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcoursedeleted'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return "Course {$this->courseid} was deleted by user {$this->userid}"; + } + + /** + * Returns the name of the legacy event. + * + * @return string legacy event name + */ + public static function get_legacy_eventname() { + return 'course_deleted'; + } + + /** + * Returns the legacy event data. + * + * @return \stdClass the course that was deleted + */ + protected function get_legacy_eventdata() { + $course = $this->get_record_snapshot('course', $this->objectid); + $course->context = $this->context; + + return $course; + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + return array(SITEID, 'course', 'delete', 'view.php?id=' . $this->objectid, $this->other['fullname'] . '(ID ' . $this->objectid . ')'); + } +} diff --git a/lib/moodlelib.php b/lib/moodlelib.php index cfab5d8cbe1..001289c7ae3 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -4859,10 +4859,15 @@ function delete_course($courseorid, $showfeedback = true) { $DB->delete_records("course", array("id" => $courseid)); $DB->delete_records("course_format_options", array("courseid" => $courseid)); - // Trigger events. - $course->context = $context; - // You can not fetch context in the event because it was already deleted. - events_trigger('course_deleted', $course); + // Trigger a course deleted event. + $event = \core\event\course_deleted::create(array( + 'objectid' => $course->id, + 'context' => $context, + 'other' => array('shortname' => $course->shortname, + 'fullname' => $course->fullname) + )); + $event->add_record_snapshot('course', $course); + $event->trigger(); return true; } From bc3c5b222a93975bef4cd5366c90323aff2083c2 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:51:36 +0800 Subject: [PATCH 06/16] MDL-39957 course: added PHPUnit test for the new course_deleted event --- course/tests/courselib_test.php | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 7a2eda03396..19d0554f9f3 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1408,4 +1408,45 @@ class core_course_courselib_testcase extends advanced_testcase { $expectedlog = array($course->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id); $this->assertEventLegacyLogData($expectedlog, $event); } + + /** + * Test that triggering a course_deleted event works as expected. + */ + public function test_course_deleted_event() { + $this->resetAfterTest(); + + // Create the course. + $course = $this->getDataGenerator()->create_course(); + + // Save the course context before we delete the course. + $coursecontext = context_course::instance($course->id); + + // Catch the update event. + $sink = $this->redirectEvents(); + + // Call delete_course() which will trigger the course_deleted event and the course_content_deleted + // event. This function prints out data to the screen, which we do not want during a PHPUnit test, + // so use ob_start and ob_end_clean to prevent this. + ob_start(); + delete_course($course); + ob_end_clean(); + + // Capture the event. + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_deleted', $event); + $this->assertEquals('course', $event->objecttable); + $this->assertEquals($course->id, $event->objectid); + $this->assertEquals($coursecontext->id, $event->contextid); + $this->assertEquals($course, $event->get_record_snapshot('course', $course->id)); + $this->assertEquals('course_deleted', $event->get_legacy_eventname()); + // The legacy data also passed the context in the course object. + $course->context = $coursecontext; + $this->assertEventLegacyData($course, $event); + $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')'); + $this->assertEventLegacyLogData($expectedlog, $event); + } } From db53f7fe28c024ce6d60b0c4511537005497d1c7 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:55:30 +0800 Subject: [PATCH 07/16] MDL-39957 course: replaced the course_content_deleted legacy event --- lang/en/moodle.php | 1 + lib/classes/event/course_content_deleted.php | 76 ++++++++++++++++++++ lib/moodlelib.php | 15 ++-- 3 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 lib/classes/event/course_content_deleted.php diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 213b717754b..61b401a8855 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -659,6 +659,7 @@ $string['errorcreatingactivity'] = 'Unable to create an instance of activity \'{ $string['errorfiletoobig'] = 'The file was bigger than the limit of {$a} bytes'; $string['errornouploadrepo'] = 'There is no upload repository enabled for this site'; $string['errorwhenconfirming'] = 'You are not confirmed yet because an error occurred. If you clicked on a link in an email to get here, make sure that the line in your email wasn\'t broken or wrapped. You may have to use cut and paste to reconstruct the link properly.'; +$string['eventcoursecontentdeleted'] = 'Course content deleted'; $string['eventcoursecreated'] = 'Course created'; $string['eventcoursedeleted'] = 'Course deleted'; $string['eventcourseupdated'] = 'Course updated'; diff --git a/lib/classes/event/course_content_deleted.php b/lib/classes/event/course_content_deleted.php new file mode 100644 index 00000000000..4587583621b --- /dev/null +++ b/lib/classes/event/course_content_deleted.php @@ -0,0 +1,76 @@ +. + +namespace core\event; + +/** + * Course content_deleted event. + * + * @package core + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_content_deleted extends base { + + /** + * Initialise the event data. + */ + protected function init() { + $this->data['objecttable'] = 'course'; + $this->data['crud'] = 'd'; + $this->data['level'] = 50; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcoursecontentdeleted'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return "Course content was deleted by user {$this->userid}"; + } + + /** + * Returns the name of the legacy event. + * + * @return string legacy event name + */ + public static function get_legacy_eventname() { + return 'course_content_removed'; + } + + /** + * Returns the legacy event data. + * + * @return \stdClass the course the content was deleted from + */ + protected function get_legacy_eventdata() { + $course = $this->get_record_snapshot('course', $this->objectid); + $course->context = $this->context; + $course->options = $this->other['options']; + + return $course; + } +} diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 001289c7ae3..8fd04b3581c 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -4893,6 +4893,7 @@ function delete_course($courseorid, $showfeedback = true) { */ function remove_course_contents($courseid, $showfeedback = true, array $options = null) { global $CFG, $DB, $OUTPUT; + require_once($CFG->libdir.'/badgeslib.php'); require_once($CFG->libdir.'/completionlib.php'); require_once($CFG->libdir.'/questionlib.php'); @@ -5132,10 +5133,16 @@ function remove_course_contents($courseid, $showfeedback = true, array $options // also some non-standard unsupported plugins may try to store something there. fulldelete($CFG->dataroot.'/'.$course->id); - // Finally trigger the event. - $course->context = $coursecontext; // You can not access context in cron event later after course is deleted. - $course->options = $options; // Not empty if we used any crazy hack. - events_trigger('course_content_removed', $course); + // Trigger a course content deleted event. + $event = \core\event\course_content_deleted::create(array( + 'objectid' => $course->id, + 'context' => $coursecontext, + 'other' => array('shortname' => $course->shortname, + 'fullname' => $course->fullname, + 'options' => $options) // Passing this for legacy reasons. + )); + $event->add_record_snapshot('course', $course); + $event->trigger(); return true; } From ec8f23de3b79077755ff1fc31d1dbffd4c036c80 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:55:48 +0800 Subject: [PATCH 08/16] MDL-39957 course: added PHPUnit test for the new course_content_deleted event --- course/tests/courselib_test.php | 48 ++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 19d0554f9f3..24ef609e889 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1436,7 +1436,7 @@ class core_course_courselib_testcase extends advanced_testcase { $sink->close(); // Validate the event. - $event = $events[0]; + $event = $events[1]; $this->assertInstanceOf('\core\event\course_deleted', $event); $this->assertEquals('course', $event->objecttable); $this->assertEquals($course->id, $event->objectid); @@ -1449,4 +1449,50 @@ class core_course_courselib_testcase extends advanced_testcase { $expectedlog = array(SITEID, 'course', 'delete', 'view.php?id=' . $course->id, $course->fullname . '(ID ' . $course->id . ')'); $this->assertEventLegacyLogData($expectedlog, $event); } + + /** + * Test that triggering a course_content_deleted event works as expected. + */ + public function test_course_content_deleted_event() { + global $DB; + + $this->resetAfterTest(); + + // Create the course. + $course = $this->getDataGenerator()->create_course(); + + // Get the course from the DB. The data generator adds some extra properties, such as + // numsections, to the course object which will fail the assertions later on. + $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST); + + // Save the course context before we delete the course. + $coursecontext = context_course::instance($course->id); + + // Catch the update event. + $sink = $this->redirectEvents(); + + // Call remove_course_contents() which will trigger the course_content_deleted event. + // This function prints out data to the screen, which we do not want during a PHPUnit + // test, so use ob_start and ob_end_clean to prevent this. + ob_start(); + remove_course_contents($course->id); + ob_end_clean(); + + // Capture the event. + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_content_deleted', $event); + $this->assertEquals('course', $event->objecttable); + $this->assertEquals($course->id, $event->objectid); + $this->assertEquals($coursecontext->id, $event->contextid); + $this->assertEquals($course, $event->get_record_snapshot('course', $course->id)); + $this->assertEquals('course_content_removed', $event->get_legacy_eventname()); + // The legacy data also passed the context and options in the course object. + $course->context = $coursecontext; + $course->options = array(); + $this->assertEventLegacyData($course, $event); + } } From f326c5b4f44dfa8e30816f1976e41f9431d3fe69 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:56:13 +0800 Subject: [PATCH 09/16] MDL-39957 course: replaced the course_category_deleted legacy event --- lang/en/moodle.php | 1 + lib/classes/event/course_category_deleted.php | 95 +++++++++++++++++++ lib/coursecatlib.php | 26 ++++- 3 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 lib/classes/event/course_category_deleted.php diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 61b401a8855..f4dc49bd40d 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -659,6 +659,7 @@ $string['errorcreatingactivity'] = 'Unable to create an instance of activity \'{ $string['errorfiletoobig'] = 'The file was bigger than the limit of {$a} bytes'; $string['errornouploadrepo'] = 'There is no upload repository enabled for this site'; $string['errorwhenconfirming'] = 'You are not confirmed yet because an error occurred. If you clicked on a link in an email to get here, make sure that the line in your email wasn\'t broken or wrapped. You may have to use cut and paste to reconstruct the link properly.'; +$string['eventcoursecategorydeleted'] = 'Category deleted'; $string['eventcoursecontentdeleted'] = 'Course content deleted'; $string['eventcoursecreated'] = 'Course created'; $string['eventcoursedeleted'] = 'Course deleted'; diff --git a/lib/classes/event/course_category_deleted.php b/lib/classes/event/course_category_deleted.php new file mode 100644 index 00000000000..1f8fde94fe1 --- /dev/null +++ b/lib/classes/event/course_category_deleted.php @@ -0,0 +1,95 @@ +. + +namespace core\event; + +/** + * category deleted event. + * + * @package core + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_category_deleted extends base { + + /** + * The course category class used for legacy reasons. + */ + private $coursecat; + + /** + * Initialise the event data. + */ + protected function init() { + $this->data['objecttable'] = 'course_categories'; + $this->data['crud'] = 'd'; + $this->data['level'] = 50; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcoursecategorydeleted'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return "Category {$this->objectid} was deleted by user {$this->userid}"; + } + + /** + * Returns the name of the legacy event. + * + * @return string legacy event name + */ + public static function get_legacy_eventname() { + return 'course_category_deleted'; + } + + /** + * Returns the legacy event data. + * + * @return coursecat the category that was deleted + */ + protected function get_legacy_eventdata() { + return $this->coursecat; + } + + /** + * Set the legacy event data. + * + * @param coursecat $class instance of the coursecat class + */ + public function set_legacy_eventdata($class) { + $this->coursecat = $class; + } + + /** + * Return legacy data for add_to_log(). + * + * @return array + */ + protected function get_legacy_logdata() { + return array(SITEID, 'category', 'delete', 'index.php', $this->other['name'] . '(ID ' . $this->objectid . ')'); + } +} diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index 816c001d9b2..0a5164939d9 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -1366,6 +1366,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { */ public function delete_full($showfeedback = true) { global $CFG, $DB; + require_once($CFG->libdir.'/gradelib.php'); require_once($CFG->libdir.'/questionlib.php'); require_once($CFG->dirroot.'/cohort/lib.php'); @@ -1400,12 +1401,20 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { // finally delete the category and it's context $DB->delete_records('course_categories', array('id' => $this->id)); - context_helper::delete_instance(CONTEXT_COURSECAT, $this->id); - add_to_log(SITEID, "category", "delete", "index.php", "$this->name (ID $this->id)"); + + $coursecatcontext = context_coursecat::instance($this->id); + $coursecatcontext->delete(); cache_helper::purge_by_event('changesincoursecat'); - events_trigger('course_category_deleted', $this); + // Trigger a course category deleted event. + $event = \core\event\course_category_deleted::create(array( + 'objectid' => $this->id, + 'context' => $coursecatcontext, + 'other' => array('name' => $this->name) + )); + $event->set_legacy_eventdata($this); + $event->trigger(); // If we deleted $CFG->defaultrequestcategory, make it point somewhere else. if ($this->id == $CFG->defaultrequestcategory) { @@ -1495,6 +1504,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { */ public function delete_move($newparentid, $showfeedback = false) { global $CFG, $DB, $OUTPUT; + require_once($CFG->libdir.'/gradelib.php'); require_once($CFG->libdir.'/questionlib.php'); require_once($CFG->dirroot.'/cohort/lib.php'); @@ -1543,9 +1553,15 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { // finally delete the category and it's context $DB->delete_records('course_categories', array('id' => $this->id)); $context->delete(); - add_to_log(SITEID, "category", "delete", "index.php", "$this->name (ID $this->id)"); - events_trigger('course_category_deleted', $this); + // Trigger a course category deleted event. + $event = \core\event\course_category_deleted::create(array( + 'objectid' => $this->id, + 'context' => $context, + 'other' => array('name' => $this->name) + )); + $event->set_legacy_eventdata($this); + $event->trigger(); cache_helper::purge_by_event('changesincoursecat'); From 7240cd92f9117a017f0f6cd08213d536a6329f03 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:56:23 +0800 Subject: [PATCH 10/16] MDL-39957 course: added PHPUnit test for the new course_category_deleted event --- course/tests/courselib_test.php | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 24ef609e889..18bc69ed757 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1495,4 +1495,65 @@ class core_course_courselib_testcase extends advanced_testcase { $course->options = array(); $this->assertEventLegacyData($course, $event); } + + /** + * Test that triggering a course_category_deleted event works as expected. + */ + public function test_course_category_deleted_event() { + $this->resetAfterTest(); + + // Create a category. + $category = $this->getDataGenerator()->create_category(); + + // Save the context before it is deleted. + $categorycontext = context_coursecat::instance($category->id); + + // Catch the update event. + $sink = $this->redirectEvents(); + + // Delete the category. + $category->delete_full(); + + // Capture the event. + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_category_deleted', $event); + $this->assertEquals('course_categories', $event->objecttable); + $this->assertEquals($category->id, $event->objectid); + $this->assertEquals($categorycontext->id, $event->contextid); + $this->assertEquals('course_category_deleted', $event->get_legacy_eventname()); + $this->assertEventLegacyData($category, $event); + $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category->name . '(ID ' . $category->id . ')'); + $this->assertEventLegacyLogData($expectedlog, $event); + + // Create two categories. + $category = $this->getDataGenerator()->create_category(); + $category2 = $this->getDataGenerator()->create_category(); + + // Save the context before it is moved and then deleted. + $category2context = context_coursecat::instance($category2->id); + + // Catch the update event. + $sink = $this->redirectEvents(); + + // Move the category. + $category2->delete_move($category->id); + + // Capture the event. + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_category_deleted', $event); + $this->assertEquals('course_categories', $event->objecttable); + $this->assertEquals($category2->id, $event->objectid); + $this->assertEquals($category2context->id, $event->contextid); + $this->assertEquals('course_category_deleted', $event->get_legacy_eventname()); + $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')'); + $this->assertEventLegacyLogData($expectedlog, $event); + } } From a4003c733af646db788aacd4f956d8ecf7520ad5 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:56:49 +0800 Subject: [PATCH 11/16] MDL-39957 course: replaced the course_restored legacy event --- backup/util/plan/restore_plan.class.php | 19 +++--- lang/en/moodle.php | 1 + lib/classes/event/course_restored.php | 89 +++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 lib/classes/event/course_restored.php diff --git a/backup/util/plan/restore_plan.class.php b/backup/util/plan/restore_plan.class.php index 9943e0bd0bc..c9dd8fb77ee 100644 --- a/backup/util/plan/restore_plan.class.php +++ b/backup/util/plan/restore_plan.class.php @@ -157,15 +157,18 @@ class restore_plan extends base_plan implements loggable { parent::execute(); $this->controller->set_status(backup::STATUS_FINISHED_OK); - events_trigger('course_restored', (object) array( - 'courseid' => $this->get_courseid(), // The new course - 'userid' => $this->get_userid(), // User doing the restore - 'type' => $this->controller->get_type(), // backup::TYPE_* constant - 'target' => $this->controller->get_target(), // backup::TARGET_* constant - 'mode' => $this->controller->get_mode(), // backup::MODE_* constant - 'operation' => $this->controller->get_operation(), // backup::OPERATION_* constant - 'samesite' => $this->controller->is_samesite(), + // Trigger a course restored event. + $event = \core\event\course_restored::create(array( + 'objectid' => $this->get_courseid(), + 'userid' => $this->get_userid(), + 'context' => context_course::instance($this->get_courseid()), + 'other' => array('type' => $this->controller->get_type(), + 'target' => $this->controller->get_target(), + 'mode' => $this->controller->get_mode(), + 'operation' => $this->controller->get_operation(), + 'samesite' => $this->controller->is_samesite()) )); + $event->trigger(); } /** diff --git a/lang/en/moodle.php b/lang/en/moodle.php index f4dc49bd40d..b6d20621297 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -663,6 +663,7 @@ $string['eventcoursecategorydeleted'] = 'Category deleted'; $string['eventcoursecontentdeleted'] = 'Course content deleted'; $string['eventcoursecreated'] = 'Course created'; $string['eventcoursedeleted'] = 'Course deleted'; +$string['eventcourserestored'] = 'Course restored'; $string['eventcourseupdated'] = 'Course updated'; $string['everybody'] = 'Everybody'; $string['executeat'] = 'Execute at'; diff --git a/lib/classes/event/course_restored.php b/lib/classes/event/course_restored.php new file mode 100644 index 00000000000..083ff893134 --- /dev/null +++ b/lib/classes/event/course_restored.php @@ -0,0 +1,89 @@ +. + +namespace core\event; + +/** + * Course restored event. + * + * @package core + * @copyright 2013 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_restored extends base { + + /** + * Initialise the event data. + */ + protected function init() { + $this->data['objecttable'] = 'course'; + $this->data['crud'] = 'c'; + $this->data['level'] = 50; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcourserestored'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return "Course {$this->objectid} was restored by user {$this->userid}"; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/course/view.php', array('id' => $this->objectid)); + } + + /** + * Returns the name of the legacy event. + * + * @return string legacy event name + */ + public static function get_legacy_eventname() { + return 'course_restored'; + } + + /** + * Returns the legacy event data. + * + * @return \stdClass the legacy event data + */ + protected function get_legacy_eventdata() { + return (object) array( + 'courseid' => $this->objectid, + 'userid' => $this->userid, + 'type' => $this->other['type'], + 'target' => $this->other['target'], + 'mode' => $this->other['mode'], + 'operation' => $this->other['operation'], + 'samesite' => $this->other['samesite'], + ); + } +} From 02cbb621d68a5ccf682d0e20de9803a0dec8b8cc Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 8 Aug 2013 16:57:01 +0800 Subject: [PATCH 12/16] MDL-39957 course: added PHPUnit test for the new course_restored event --- course/tests/courselib_test.php | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 18bc69ed757..5c892de7c0e 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1556,4 +1556,76 @@ class core_course_courselib_testcase extends advanced_testcase { $expectedlog = array(SITEID, 'category', 'delete', 'index.php', $category2->name . '(ID ' . $category2->id . ')'); $this->assertEventLegacyLogData($expectedlog, $event); } + + /** + * Test that triggering a course_restored event works as expected. + */ + public function test_course_restored_event() { + global $CFG; + + // Get the necessary files to perform backup and restore. + require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); + require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); + + $this->resetAfterTest(); + + // Set to admin user. + $this->setAdminUser(); + + // The user id is going to be 2 since we are the admin user. + $userid = 2; + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create backup file and save it to the backup location. + $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $userid); + $bc->execute_plan(); + $results = $bc->get_results(); + $file = $results['backup_destination']; + $fp = get_file_packer(); + $filepath = $CFG->dataroot . '/temp/backup/test-restore-course-event'; + $file->extract_to_pathname($fp, $filepath); + $bc->destroy(); + unset($bc); + + // Now we want to catch the restore course event. + $sink = $this->redirectEvents(); + + // Now restore the course to trigger the event. + $rc = new restore_controller('test-restore-course-event', $course->id, backup::INTERACTIVE_NO, + backup::MODE_GENERAL, $userid, backup::TARGET_NEW_COURSE); + $rc->execute_precheck(); + $rc->execute_plan(); + + // Capture the event. + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = $events[0]; + $this->assertInstanceOf('\core\event\course_restored', $event); + $this->assertEquals('course', $event->objecttable); + $this->assertEquals($rc->get_courseid(), $event->objectid); + $this->assertEquals(context_course::instance($rc->get_courseid())->id, $event->contextid); + $this->assertEquals('course_restored', $event->get_legacy_eventname()); + $legacydata = (object) array( + 'courseid' => $rc->get_courseid(), + 'userid' => $rc->get_userid(), + 'type' => $rc->get_type(), + 'target' => $rc->get_target(), + 'mode' => $rc->get_mode(), + 'operation' => $rc->get_operation(), + 'samesite' => $rc->is_samesite() + ); + $this->assertEventLegacyData($legacydata, $event); + + // Destroy the resource controller since we are done using it. + $rc->destroy(); + unset($rc); + + // Clear the time limit, otherwise PHPUnit complains. + set_time_limit(0); + } } From 2bf2f3599e0f0d85894af907c1c39f5fadf63988 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 9 Aug 2013 17:58:30 +0800 Subject: [PATCH 13/16] MDL-39957 course: altered the handling of legacy log data for the course_updated event --- course/lib.php | 1 + lib/classes/event/course_updated.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/course/lib.php b/course/lib.php index c799a8baafb..6df63a849d9 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2406,6 +2406,7 @@ function update_course($data, $editoroptions = NULL) { 'fullname' => $course->fullname) )); $event->add_record_snapshot('course', $course); + $event->set_legacy_logdata(array($course->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id)); $event->trigger(); if ($oldcourse->format !== $course->format) { diff --git a/lib/classes/event/course_updated.php b/lib/classes/event/course_updated.php index 33cc8c10f08..3020cc4886b 100644 --- a/lib/classes/event/course_updated.php +++ b/lib/classes/event/course_updated.php @@ -25,6 +25,9 @@ namespace core\event; */ class course_updated extends base { + /** @var array The legacy log data. */ + private $legacylogdata; + /** * Initialise the event data. */ @@ -79,12 +82,21 @@ class course_updated extends base { return $this->get_record_snapshot('course', $this->objectid); } + /** + * 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() { - return array($this->objectid, 'course', 'update', 'edit.php?id=' . $this->objectid, $this->objectid); + return $this->legacylogdata; } } From ed54cb4110eaedcb917407b0c47c3a5f0f76a004 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 9 Aug 2013 18:01:05 +0800 Subject: [PATCH 14/16] MDL-39957 course: replaced add_to_log for moving courses --- course/lib.php | 32 ++++++++++++++++++++++++-------- course/manage.php | 15 ++++++++++++++- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/course/lib.php b/course/lib.php index 6df63a849d9..ef51f2cc666 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2026,14 +2026,14 @@ function course_allowed_module($course, $modname) { * @return bool success */ function move_courses($courseids, $categoryid) { - global $CFG, $DB, $OUTPUT; + global $DB; if (empty($courseids)) { - // nothing to do + // Nothing to do. return; } - if (!$category = $DB->get_record('course_categories', array('id'=>$categoryid))) { + if (!$category = $DB->get_record('course_categories', array('id' => $categoryid))) { return false; } @@ -2042,21 +2042,37 @@ function move_courses($courseids, $categoryid) { $i = 1; foreach ($courseids as $courseid) { - if ($course = $DB->get_record('course', array('id'=>$courseid), 'id, category')) { + if ($dbcourse = $DB->get_record('course', array('id' => $courseid))) { $course = new stdClass(); $course->id = $courseid; $course->category = $category->id; $course->sortorder = $category->sortorder + MAX_COURSES_IN_CATEGORY - $i++; if ($category->visible == 0) { - // hide the course when moving into hidden category, - // do not update the visibleold flag - we want to get to previous state if somebody unhides the category + // Hide the course when moving into hidden category, do not update the visibleold flag - we want to get + // to previous state if somebody unhides the category. $course->visible = 0; } $DB->update_record('course', $course); - add_to_log($course->id, "course", "move", "edit.php?id=$course->id", $course->id); - $context = context_course::instance($course->id); + // Store the context. + $context = context_course::instance($course->id); + + // Update the course object we are passing to the event. + $dbcourse->category = $course->category; + $dbcourse->sortorder = $course->sortorder; + + // Trigger a course updated event. + $event = \core\event\course_updated::create(array( + 'objectid' => $course->id, + 'context' => $context, + 'other' => array('shortname' => $dbcourse->shortname, + 'fullname' => $dbcourse->fullname) + )); + $event->add_record_snapshot('course', $dbcourse); + $event->set_legacy_logdata(array($course->id, 'course', 'move', 'edit.php?id=' . $course->id, $course->id)); + $event->trigger(); + $context->update_moved($newparent); } } diff --git a/course/manage.php b/course/manage.php index d4a905455cd..ddf02ce4848 100644 --- a/course/manage.php +++ b/course/manage.php @@ -277,7 +277,20 @@ if ((!empty($moveup) or !empty($movedown)) && confirm_sesskey()) { $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id)); $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id)); cache_helper::purge_by_event('changesincourse'); - add_to_log($movecourse->id, "course", "move", "edit.php?id=$movecourse->id", $movecourse->id); + + // Update $movecourse's sortorder. + $movecourse->sortorder = $swapcourse->sortorder; + + // Trigger a course updated event. + $event = \core\event\course_updated::create(array( + 'objectid' => $movecourse->id, + 'context' => context_course::instance($movecourse->id), + 'other' => array('shortname' => $movecourse->shortname, + 'fullname' => $movecourse->fullname) + )); + $event->add_record_snapshot('course', $movecourse); + $event->set_legacy_logdata(array($movecourse->id, 'course', 'move', 'edit.php?id=' . $movecourse->id, $movecourse->id)); + $event->trigger(); } } From e5959771b2220eeb0fb5fbbfab331775b8e81767 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 12 Aug 2013 11:47:32 +0800 Subject: [PATCH 15/16] MDL-39957 course: added PHPUnit assertions for when a course_updated event is triggered by moving a course --- course/tests/courselib_test.php | 50 +++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 5c892de7c0e..77c857ff476 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1372,40 +1372,60 @@ class core_course_courselib_testcase extends advanced_testcase { $this->resetAfterTest(); - // Create the course. + // Create a course. $course = $this->getDataGenerator()->create_course(); - // Catch the update event. + // Create a category we are going to move this course to. + $category = $this->getDataGenerator()->create_category(); + + // Catch the update events. $sink = $this->redirectEvents(); // Keep track of the old sortorder. $sortorder = $course->sortorder; - // Call update_course which will trigger the course_updated event. + // Call update_course which will trigger a course_updated event. update_course($course); // Return the updated course information from the DB. - $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST); + $updatedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST); - // Now we want to set the sortorder back to what it was. The reason for this is because update_course() - // calls fix_course_sortorder() which alters the sort order in the DB, but it does not then set the - // value of sortorder for the course object which was passed to the legacy event. - $course->sortorder = $sortorder; + // Now move the course to the category, this will also trigger an event. + move_courses(array($course->id), $category->id); - // Capture the event. + // Return the moved course information from the DB. + $movedcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST); + + // Now we want to set the sortorder back to what it was before fix_course_sortorder() was called. The reason for + // this is because update_course() and move_courses() call fix_course_sortorder() which alters the sort order in + // the DB, but it does not set the value of the sortorder for the course object passed to the event. + $updatedcourse->sortorder = $sortorder; + $movedcourse->sortorder = $category->sortorder + MAX_COURSES_IN_CATEGORY - 1; + + // Capture the events. $events = $sink->get_events(); $sink->close(); - // Validate the event. + // Validate the events. $event = $events[0]; $this->assertInstanceOf('\core\event\course_updated', $event); $this->assertEquals('course', $event->objecttable); - $this->assertEquals($course->id, $event->objectid); - $this->assertEquals(context_course::instance($course->id)->id, $event->contextid); - $this->assertEquals($course, $event->get_record_snapshot('course', $course->id)); + $this->assertEquals($updatedcourse->id, $event->objectid); + $this->assertEquals(context_course::instance($updatedcourse->id)->id, $event->contextid); + $this->assertEquals($updatedcourse, $event->get_record_snapshot('course', $updatedcourse->id)); $this->assertEquals('course_updated', $event->get_legacy_eventname()); - $this->assertEventLegacyData($course, $event); - $expectedlog = array($course->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id); + $this->assertEventLegacyData($updatedcourse, $event); + $expectedlog = array($updatedcourse->id, 'course', 'update', 'edit.php?id=' . $course->id, $course->id); + $this->assertEventLegacyLogData($expectedlog, $event); + + $event = $events[1]; + $this->assertInstanceOf('\core\event\course_updated', $event); + $this->assertEquals('course', $event->objecttable); + $this->assertEquals($movedcourse->id, $event->objectid); + $this->assertEquals(context_course::instance($movedcourse->id)->id, $event->contextid); + $this->assertEquals($movedcourse, $event->get_record_snapshot('course', $movedcourse->id)); + $this->assertEquals('course_updated', $event->get_legacy_eventname()); + $expectedlog = array($movedcourse->id, 'course', 'move', 'edit.php?id=' . $movedcourse->id, $movedcourse->id); $this->assertEventLegacyLogData($expectedlog, $event); } From cd14c5cab85febca17362fd3d223f75abbd98db5 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 9 Aug 2013 18:01:48 +0800 Subject: [PATCH 16/16] MDL-39957 course: replaced add_to_log for hiding/showing courses --- course/manage.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/course/manage.php b/course/manage.php index ddf02ce4848..4888c0a4960 100644 --- a/course/manage.php +++ b/course/manage.php @@ -249,7 +249,22 @@ if ((!empty($hide) or !empty($show)) && confirm_sesskey()) { $params = array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time()); $DB->update_record('course', $params); cache_helper::purge_by_event('changesincourse'); - add_to_log($course->id, "course", ($visible ? 'show' : 'hide'), "edit.php?id=$course->id", $course->id); + + // Update the course object we pass to the event class. + $course->visible = $params['visible']; + $course->visibleold = $params['visibleold']; + $course->timemodified = $params['timemodified']; + + // Trigger a course updated event. + $event = \core\event\course_updated::create(array( + 'objectid' => $course->id, + 'context' => $coursecontext, + 'other' => array('shortname' => $course->shortname, + 'fullname' => $course->fullname) + )); + $event->add_record_snapshot('course', $course); + $event->set_legacy_logdata(array($course->id, 'course', ($visible ? 'show' : 'hide'), 'edit.php?id=' . $course->id, $course->id)); + $event->trigger(); } if ((!empty($moveup) or !empty($movedown)) && confirm_sesskey()) {