From f5de9484e85117b0e6b9489d08febfe48e5efca0 Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Tue, 15 Aug 2017 14:10:38 -0400 Subject: [PATCH] MDL-53537 core_backup: Add a course_backup_created event --- admin/tool/recyclebin/tests/events_test.php | 2 + backup/util/plan/backup_plan.class.php | 15 +++ course/tests/courselib_test.php | 46 ++++++++ lang/en/moodle.php | 1 + lib/classes/event/course_backup_created.php | 118 ++++++++++++++++++++ 5 files changed, 182 insertions(+) create mode 100644 lib/classes/event/course_backup_created.php diff --git a/admin/tool/recyclebin/tests/events_test.php b/admin/tool/recyclebin/tests/events_test.php index 80ab6aa4352..d10fa932005 100644 --- a/admin/tool/recyclebin/tests/events_test.php +++ b/admin/tool/recyclebin/tests/events_test.php @@ -60,6 +60,8 @@ class tool_recyclebin_events_testcase extends advanced_testcase { delete_course($course, false); $events = $sink->get_events(); $event = reset($events); + // Need the second event here, the first is backup created. + $event = next($events); // Get the item from the recycle bin. $rb = new \tool_recyclebin\category_bin($course->category); diff --git a/backup/util/plan/backup_plan.class.php b/backup/util/plan/backup_plan.class.php index 2fcb4692ba3..954ecb06c5b 100644 --- a/backup/util/plan/backup_plan.class.php +++ b/backup/util/plan/backup_plan.class.php @@ -119,6 +119,21 @@ class backup_plan extends base_plan implements loggable { $this->controller->set_status(backup::STATUS_EXECUTING); parent::execute(); $this->controller->set_status(backup::STATUS_FINISHED_OK); + + if ($this->controller->get_type() === backup::TYPE_1COURSE) { + // Trigger a course_backup_created event. + $otherarray = array('format' => $this->controller->get_format(), + 'mode' => $this->controller->get_mode(), + 'interactive' => $this->controller->get_interactive(), + 'type' => $this->controller->get_type(), + ); + $event = \core\event\course_backup_created::create(array( + 'objectid' => $this->get_courseid(), + 'context' => context_course::instance($this->get_courseid()), + 'other' => $otherarray + )); + $event->trigger(); + } } } diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index c51f30cf788..fee1d43c44c 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -1900,6 +1900,52 @@ class core_course_courselib_testcase extends advanced_testcase { $this->assertEventContextNotUsed($event); } + /** + * Test that triggering a course_backup_created event works as expected. + */ + public function test_course_backup_created_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); + $sink = $this->redirectEvents(); + $bc->execute_plan(); + + // Capture the event. + $events = $sink->get_events(); + $sink->close(); + + // Validate the event. + $event = array_pop($events); + $this->assertInstanceOf('\core\event\course_backup_created', $event); + $this->assertEquals('course', $event->objecttable); + $this->assertEquals($bc->get_courseid(), $event->objectid); + $this->assertEquals(context_course::instance($bc->get_courseid())->id, $event->contextid); + + $url = new moodle_url('/course/view.php', array('id' => $event->objectid)); + $this->assertEquals($url, $event->get_url()); + $this->assertEventContextNotUsed($event); + + // Destroy the resource controller since we are done using it. + $bc->destroy(); + } + /** * Test that triggering a course_restored event works as expected. */ diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 72b71daac36..7cced0213a8 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -729,6 +729,7 @@ $string['eventcommentcreated'] = 'Comment created'; $string['eventcommentdeleted'] = 'Comment deleted'; $string['eventcommentsviewed'] = 'Comments viewed'; $string['eventconfiglogcreated'] = 'Config log created'; +$string['eventcoursebackupcreated'] = 'Course backup created'; $string['eventcoursecategorycreated'] = 'Category created'; $string['eventcoursecategorydeleted'] = 'Category deleted'; $string['eventcoursecategoryupdated'] = 'Category updated'; diff --git a/lib/classes/event/course_backup_created.php b/lib/classes/event/course_backup_created.php new file mode 100644 index 00000000000..46f810e33aa --- /dev/null +++ b/lib/classes/event/course_backup_created.php @@ -0,0 +1,118 @@ +. + +/** + * Course backup created event. + * + * @package core + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\event; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Course backup created event class. + * + * @property-read array $other { + * Extra information about event. + * + * - string format: Format of backup (moodle, imscc) + * - int mode: execution mode. + * - boolean interactive: Interactive mode (yes/no) + * - string type: backup type + * } + * + * @package core + * @since Moodle 3.4 + * @copyright 2017 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class course_backup_created extends base { + + /** + * Initialise the event data. + */ + protected function init() { + $this->data['objecttable'] = 'course'; + $this->data['crud'] = 'c'; + $this->data['edulevel'] = self::LEVEL_TEACHING; + } + + /** + * Returns localised general event name. + * + * @return string + */ + public static function get_name() { + return get_string('eventcoursebackupcreated'); + } + + /** + * Returns non-localised description of what happened. + * + * @return string + */ + public function get_description() { + return "The user with id '$this->userid' created a backup of the course with the id '$this->objectid'."; + } + + /** + * Returns relevant URL. + * + * @return \moodle_url + */ + public function get_url() { + return new \moodle_url('/course/view.php', array('id' => $this->objectid)); + } + + /** + * Custom validation. + * + * @throws \coding_exception + * @return void + */ + protected function validate_data() { + parent::validate_data(); + + if (!isset($this->other['format'])) { + throw new \coding_exception('The \'format\' value must be set in other.'); + } + + if (!isset($this->other['mode'])) { + throw new \coding_exception('The \'mode\' value must be set in other.'); + } + + if (!isset($this->other['interactive'])) { + throw new \coding_exception('The \'interactive\' value must be set in other.'); + } + + if (!isset($this->other['type'])) { + throw new \coding_exception('The \'type\' value must be set in other.'); + } + } + + public static function get_objectid_mapping() { + return array('db' => 'course', 'restore' => 'course'); + } + + public static function get_other_mapping() { + // No need to map anything. + return false; + } +}