From 8fef061da48fbb4c361bea6b49ab938ef241cda1 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Tue, 1 Mar 2016 10:51:37 +0800 Subject: [PATCH] MDL-53292 core: deprecate callback delete_course --- course/format/README.txt | 5 ---- course/format/upgrade.txt | 3 ++ lib/moodlelib.php | 46 +++++++++++++++++++----------- mod/feedback/classes/observer.php | 46 ++++++++++++++++++++++++++++++ mod/feedback/db/events.php | 34 ++++++++++++++++++++++ mod/feedback/lib.php | 15 ---------- mod/feedback/tests/events_test.php | 13 +++++++++ mod/feedback/version.php | 2 +- mod/lesson/lib.php | 13 --------- mod/upgrade.txt | 4 +++ report/upgrade.txt | 3 ++ 11 files changed, 134 insertions(+), 50 deletions(-) create mode 100644 mod/feedback/classes/observer.php create mode 100644 mod/feedback/db/events.php diff --git a/course/format/README.txt b/course/format/README.txt index 9bb62ab0c7e..6705b5b6ca8 100644 --- a/course/format/README.txt +++ b/course/format/README.txt @@ -139,8 +139,3 @@ Optional file (styles) If this file exists it will be included in the CSS Moodle generates. - -Optional delete course hook ---------------------------- - -* in your yourformat/lib.php add function format_yourformat_delete_course($courseid) \ No newline at end of file diff --git a/course/format/upgrade.txt b/course/format/upgrade.txt index 8d82f27fe9e..8bdcfe0dbf3 100644 --- a/course/format/upgrade.txt +++ b/course/format/upgrade.txt @@ -2,6 +2,9 @@ This files describes API changes for course formats Overview of this plugin type at http://docs.moodle.org/dev/Course_formats +=== 3.2 === +* Callback delete_course is deprecated and should be replaced with observer for event \core\event\course_content_deleted + === 3.1 === * Course format may use the inplace_editable template to allow quick editing of section names, see https://docs.moodle.org/dev/Inplace_editable and MDL-51802 for example implementation. diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 8160f89c542..bdcea2394f3 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -4798,6 +4798,9 @@ function remove_course_contents($courseid, $showfeedback = true, array $options echo $OUTPUT->notification($strdeleted.get_string('type_block_plural', 'plugin'), 'notifysuccess'); } + // Get the list of all modules that are properly installed. + $allmodules = $DB->get_records_menu('modules', array(), '', 'name, id'); + // Delete every instance of every module, // this has to be done before deleting of course level stuff. $locations = core_component::get_plugin_list('mod'); @@ -4805,30 +4808,36 @@ function remove_course_contents($courseid, $showfeedback = true, array $options if ($modname === 'NEWMODULE') { continue; } - if ($module = $DB->get_record('modules', array('name' => $modname))) { + if (array_key_exists($modname, $allmodules)) { + $sql = "SELECT cm.*, m.id AS modinstance, m.name, '$modname' AS modname + FROM {".$modname."} m + LEFT JOIN {course_modules} cm ON cm.instance = m.id AND cm.module = :moduleid + WHERE m.course = :courseid"; + $instances = $DB->get_records_sql($sql, array('courseid' => $course->id, + 'modulename' => $modname, 'moduleid' => $allmodules[$modname])); + include_once("$moddir/lib.php"); // Shows php warning only if plugin defective. $moddelete = $modname .'_delete_instance'; // Delete everything connected to an instance. $moddeletecourse = $modname .'_delete_course'; // Delete other stray stuff (uncommon). - if ($instances = $DB->get_records($modname, array('course' => $course->id))) { - foreach ($instances as $instance) { - if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) { + if ($instances) { + foreach ($instances as $cm) { + if ($cm->id) { // Delete activity context questions and question categories. question_delete_activity($cm, $showfeedback); - // Notify the competency subsystem. \core_competency\api::hook_course_module_deleted($cm); } if (function_exists($moddelete)) { // This purges all module data in related tables, extra user prefs, settings, etc. - $moddelete($instance->id); + $moddelete($cm->modinstance); } else { // NOTE: we should not allow installation of modules with missing delete support! debugging("Defective module '$modname' detected when deleting course contents: missing function $moddelete()!"); - $DB->delete_records($modname, array('id' => $instance->id)); + $DB->delete_records($modname, array('id' => $cm->modinstance)); } - if ($cm) { + if ($cm->id) { // Delete cm and its context - orphaned contexts are purged in cron in case of any race condition. context_helper::delete_instance(CONTEXT_MODULE, $cm->id); $DB->delete_records('course_modules', array('id' => $cm->id)); @@ -4836,7 +4845,9 @@ function remove_course_contents($courseid, $showfeedback = true, array $options } } if (function_exists($moddeletecourse)) { - // Execute ptional course cleanup callback. + // Execute optional course cleanup callback. Deprecated since Moodle 3.2. TODO MDL-53297 remove in 3.6. + debugging("Callback delete_course is deprecated. Function $moddeletecourse should be converted " . + 'to observer of event \core\event\course_content_deleted', DEBUG_DEVELOPER); $moddeletecourse($course, $showfeedback); } if ($instances and $showfeedback) { @@ -4856,12 +4867,13 @@ function remove_course_contents($courseid, $showfeedback = true, array $options 'coursemoduleid IN (SELECT id from {course_modules} WHERE course=?)', array($courseid)); - // Remove course-module data. + // Remove course-module data that has not been removed in modules' _delete_instance callbacks. $cms = $DB->get_records('course_modules', array('course' => $course->id)); + $allmodulesbyid = array_flip($allmodules); foreach ($cms as $cm) { - if ($module = $DB->get_record('modules', array('id' => $cm->module))) { + if (array_key_exists($cm->module, $allmodulesbyid)) { try { - $DB->delete_records($module->name, array('id' => $cm->instance)); + $DB->delete_records($allmodulesbyid[$cm->module], array('id' => $cm->instance)); } catch (Exception $e) { // Ignore weird or missing table problems. } @@ -4874,17 +4886,19 @@ function remove_course_contents($courseid, $showfeedback = true, array $options echo $OUTPUT->notification($strdeleted.get_string('type_mod_plural', 'plugin'), 'notifysuccess'); } - // Cleanup the rest of plugins. + // Cleanup the rest of plugins. Deprecated since Moodle 3.2. TODO MDL-53297 remove in 3.6. $cleanuplugintypes = array('report', 'coursereport', 'format'); $callbacks = get_plugins_with_function('delete_course', 'lib.php'); foreach ($cleanuplugintypes as $type) { if (!empty($callbacks[$type])) { foreach ($callbacks[$type] as $pluginfunction) { + debugging("Callback delete_course is deprecated. Function $pluginfunction should be converted " . + 'to observer of event \core\event\course_content_deleted', DEBUG_DEVELOPER); $pluginfunction($course->id, $showfeedback); } - } - if ($showfeedback) { - echo $OUTPUT->notification($strdeleted.get_string('type_'.$type.'_plural', 'plugin'), 'notifysuccess'); + if ($showfeedback) { + echo $OUTPUT->notification($strdeleted.get_string('type_'.$type.'_plural', 'plugin'), 'notifysuccess'); + } } } diff --git a/mod/feedback/classes/observer.php b/mod/feedback/classes/observer.php new file mode 100644 index 00000000000..5d5b22be017 --- /dev/null +++ b/mod/feedback/classes/observer.php @@ -0,0 +1,46 @@ +. + +/** + * Event observers supported by this module + * + * @package mod_feedback + * @copyright 2016 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Event observers supported by this module + * + * @package mod_feedback + * @copyright 2016 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class mod_feedback_observer { + + /** + * Observer for the even course_content_deleted - delete all course templates. + * + * @param \core\event\course_content_deleted $event + */ + public static function course_content_deleted(\core\event\course_content_deleted $event) { + global $DB; + // Delete all templates of given course. + $DB->delete_records('feedback_template', array('course' => $event->objectid)); + } +} diff --git a/mod/feedback/db/events.php b/mod/feedback/db/events.php new file mode 100644 index 00000000000..68baaa85901 --- /dev/null +++ b/mod/feedback/db/events.php @@ -0,0 +1,34 @@ +. + +/** + * Feedback event handler definition. + * + * @package mod_feedback + * @category event + * @copyright 2016 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// List of observers. +$observers = array( + + array( + 'eventname' => '\core\event\course_content_deleted', + 'callback' => 'mod_feedback_observer::course_content_deleted', + ), + +); diff --git a/mod/feedback/lib.php b/mod/feedback/lib.php index 272b6d5d60a..eff359aed24 100644 --- a/mod/feedback/lib.php +++ b/mod/feedback/lib.php @@ -312,21 +312,6 @@ function feedback_delete_instance($id) { return $DB->delete_records("feedback", array("id"=>$id)); } -/** - * this is called after deleting all instances if the course will be deleted. - * only templates have to be deleted - * - * @global object - * @param object $course - * @return boolean - */ -function feedback_delete_course($course) { - global $DB; - - //delete all templates of given course - return $DB->delete_records('feedback_template', array('course'=>$course->id)); -} - /** * Return a small object with summary information about what a * user has done with a given particular instance of this module diff --git a/mod/feedback/tests/events_test.php b/mod/feedback/tests/events_test.php index a7dcf4c90f8..4d6fe2318af 100644 --- a/mod/feedback/tests/events_test.php +++ b/mod/feedback/tests/events_test.php @@ -329,5 +329,18 @@ class mod_feedback_events_testcase extends advanced_testcase { $this->assertContains("The 'anonymous' value must be set in other.", $e->getMessage()); } } + + /** + * Test that event observer is executed on course deletion and the templates are removed. + */ + public function test_delete_course() { + global $DB; + $this->resetAfterTest(); + feedback_save_as_template($this->eventfeedback, 'my template', 0); + $courseid = $this->eventcourse->id; + $this->assertNotEmpty($DB->get_records('feedback_template', array('course' => $courseid))); + delete_course($this->eventcourse, false); + $this->assertEmpty($DB->get_records('feedback_template', array('course' => $courseid))); + } } diff --git a/mod/feedback/version.php b/mod/feedback/version.php index 5e98b6bd07f..6b44cf0bcc7 100644 --- a/mod/feedback/version.php +++ b/mod/feedback/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016052300; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2016061300; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2016051900; // Requires this Moodle version $plugin->component = 'mod_feedback'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0; diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index 1e105c46156..7590b83e961 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -274,19 +274,6 @@ function lesson_delete_instance($id) { return $lesson->delete(); } -/** - * Given a course object, this function will clean up anything that - * would be leftover after all the instances were deleted - * - * @global object - * @param object $course an object representing the course that is being deleted - * @param boolean $feedback to specify if the process must output a summary of its work - * @return boolean - */ -function lesson_delete_course($course, $feedback=true) { - return true; -} - /** * Return a small object with summary information about what a * user has done with a given particular instance of this module diff --git a/mod/upgrade.txt b/mod/upgrade.txt index ad0c75de3b9..74e8d008859 100644 --- a/mod/upgrade.txt +++ b/mod/upgrade.txt @@ -1,6 +1,10 @@ This files describes API changes in /mod/* - activity modules, information provided here is intended especially for developers. +=== 3.2 === + +* Callback delete_course is deprecated and should be replaced with observer for event \core\event\course_content_deleted + === 3.1 === * Old /mod/MODULENAME/pix/icon.gif and enrol/paypal/pix/icon.gif GIF icons have been removed. Please use pix_icon diff --git a/report/upgrade.txt b/report/upgrade.txt index e6f06a74bf1..c40886cb43a 100644 --- a/report/upgrade.txt +++ b/report/upgrade.txt @@ -1,6 +1,9 @@ This files describes API changes in /report/* - plugins, information provided here is intended especially for developers. +=== 3.2 === +* Callback delete_course is deprecated and should be replaced with observer for event \core\event\course_content_deleted + === 2.7 === * How to migrate reports accessing table 'log': http://docs.moodle.org/dev/Migrating_log_access_in_reports