From bd7cc14d6aa2966abdcd61f1499f42865015b40a Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Wed, 6 Jul 2016 23:11:31 +0800 Subject: [PATCH 1/2] MDL-54771 restore: Add deletesource to course object Since deleting a course now triggers the pre_course_delete hook it may be useful for hook implementations to know whether it was a "true" course deletion, or one originating from a temporary course created during a restore. --- backup/util/ui/restore_ui.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backup/util/ui/restore_ui.class.php b/backup/util/ui/restore_ui.class.php index d0e2237dfee..7c0de1bc27b 100644 --- a/backup/util/ui/restore_ui.class.php +++ b/backup/util/ui/restore_ui.class.php @@ -231,9 +231,11 @@ class restore_ui extends base_ui { * Delete course which is created by restore process */ public function cleanup() { + global $DB; $courseid = $this->controller->get_courseid(); - if ($this->is_temporary_course_created($courseid)) { - delete_course($courseid, false); + if ($this->is_temporary_course_created($courseid) && $course = $DB->get_record('course', array('id' => $courseid))) { + $course->deletesource = 'restore'; + delete_course($course, false); } } From c7010e82aeaa39ff80bfc14e1813a6743c41d47d Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Wed, 6 Jul 2016 23:13:46 +0800 Subject: [PATCH 2/2] MDL-54771 tool_recyclebin: Don't add temp course to recycle bin --- admin/tool/recyclebin/lib.php | 6 ++++++ admin/tool/recyclebin/tests/category_bin_test.php | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/admin/tool/recyclebin/lib.php b/admin/tool/recyclebin/lib.php index 39d451bef27..ee2eb748633 100644 --- a/admin/tool/recyclebin/lib.php +++ b/admin/tool/recyclebin/lib.php @@ -158,6 +158,12 @@ function tool_recyclebin_pre_course_module_delete($cm) { * @param \stdClass $course The course record. */ function tool_recyclebin_pre_course_delete($course) { + // It is possible that the course deletion which triggered this hook + // was from an in progress course restore. In that case we do not want + // it in the recycle bin. + if (isset($course->deletesource) && $course->deletesource == 'restore') { + return; + } // Delete all the items in the course recycle bin, regardless if it enabled or not. // It may have been enabled, then disabled later on, so may still have content. $coursebin = new \tool_recyclebin\course_bin($course->id); diff --git a/admin/tool/recyclebin/tests/category_bin_test.php b/admin/tool/recyclebin/tests/category_bin_test.php index 5e3f85d0495..22c7f216c35 100644 --- a/admin/tool/recyclebin/tests/category_bin_test.php +++ b/admin/tool/recyclebin/tests/category_bin_test.php @@ -38,6 +38,11 @@ class tool_recyclebin_category_bin_tests extends advanced_testcase { */ protected $course; + /** + * @var stdClass $coursebeingrestored + */ + protected $coursebeingrestored; + /** * Setup for each test. */ @@ -57,10 +62,16 @@ class tool_recyclebin_category_bin_tests extends advanced_testcase { public function test_pre_course_delete_hook() { global $DB; + // This simulates a temporary course being cleaned up by a course restore. + $this->coursebeingrestored = $this->getDataGenerator()->create_course(); + $this->coursebeingrestored->deletesource = 'restore'; + // Should have nothing in the recycle bin. $this->assertEquals(0, $DB->count_records('tool_recyclebin_category')); delete_course($this->course, false); + // This should not be added to the recycle bin. + delete_course($this->coursebeingrestored, false); // Check the course is now in the recycle bin. $this->assertEquals(1, $DB->count_records('tool_recyclebin_category'));