MDL-59308 calendar: pass module context when known

this will protect us from exceptions when context for removed instance can not be retrieved
This commit is contained in:
Marina Glancy
2017-06-22 16:01:33 +08:00
parent dec00f6a53
commit 3d8a2c5cd9
3 changed files with 30 additions and 1 deletions
+25
View File
@@ -285,6 +285,31 @@ class core_calendar_container_testcase extends advanced_testcase {
$this->assertInstanceOf(event_interface::class, $factory->create_instance($event));
}
/**
* Test that when course module is deleted all events are also deleted.
*/
public function test_delete_module_delete_events() {
global $DB;
$user = $this->getDataGenerator()->create_user();
// Create the course we will be using.
$course = $this->getDataGenerator()->create_course();
$group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
foreach (core_component::get_plugin_list('mod') as $modname => $unused) {
$module = $this->getDataGenerator()->create_module($modname, ['course' => $course->id]);
// Create bunch of events of different type (user override, group override, module event).
$this->create_event(['userid' => $user->id, 'modulename' => $modname, 'instance' => $module->id]);
$this->create_event(['groupid' => $group->id, 'modulename' => $modname, 'instance' => $module->id]);
$this->create_event(['modulename' => $modname, 'instance' => $module->id]);
$this->create_event(['modulename' => $modname, 'instance' => $module->id, 'courseid' => $course->id]);
// Delete module and make sure all events are deleted.
course_delete_module($module->cmid);
$this->assertEmpty($DB->get_record('event', ['modulename' => $modname, 'instance' => $module->id]));
}
}
/**
* Test getting the event mapper.
*/
+3 -1
View File
@@ -1204,8 +1204,10 @@ function course_delete_module($cmid, $async = false) {
// Delete events from calendar.
if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) {
$coursecontext = context_course::instance($cm->course);
foreach($events as $event) {
$calendarevent = calendar_event::load($event->id);
$event->context = $coursecontext;
$calendarevent = calendar_event::load($event);
$calendarevent->delete();
}
}
+2
View File
@@ -1574,7 +1574,9 @@ class lesson extends lesson_base {
$DB->delete_records("lesson_timer", array("lessonid"=>$this->properties->id));
$DB->delete_records("lesson_branch", array("lessonid"=>$this->properties->id));
if ($events = $DB->get_records('event', array("modulename"=>'lesson', "instance"=>$this->properties->id))) {
$coursecontext = context_course::instance($cm->course);
foreach($events as $event) {
$event->context = $coursecontext;
$event = calendar_event::load($event);
$event->delete();
}