Skip to content

Commit

Permalink
MDL-53537 core_backup: Add a course_backup_created event
Browse files Browse the repository at this point in the history
  • Loading branch information
sbourget committed Aug 15, 2017
1 parent f3625f4 commit f5de948
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
2 changes: 2 additions & 0 deletions admin/tool/recyclebin/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function test_category_bin_item_created() {
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);
Expand Down
15 changes: 15 additions & 0 deletions backup/util/plan/backup_plan.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ public function execute() {
$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();
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions course/tests/courselib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,52 @@ public function test_course_category_deleted_event() {
$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.
*/
Expand Down
1 change: 1 addition & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@
$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';
Expand Down
118 changes: 118 additions & 0 deletions lib/classes/event/course_backup_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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;
}
}

0 comments on commit f5de948

Please sign in to comment.