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
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.
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');
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));
}
}
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) {
'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.
}
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');
+ }
}
}
--- /dev/null
+<?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/>.
+
+/**
+ * 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));
+ }
+}
--- /dev/null
+<?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/>.
+
+/**
+ * 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',
+ ),
+
+);
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
$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)));
+ }
}
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;
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
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
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