From 74762da37f96c2eeb377feb756f0ffb544fed0a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Wed, 25 Sep 2013 10:12:11 +0200 Subject: [PATCH] MDL-41437 implement new block, enrol and course format pluginfo uninstall methods --- lib/adminlib.php | 50 ------------------------- lib/pluginlib.php | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 50 deletions(-) diff --git a/lib/adminlib.php b/lib/adminlib.php index 861a9ef6eed..b7ba77ff91f 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -231,56 +231,6 @@ function uninstall_plugin($type, $name) { } } } - - } else if ($type === 'enrol') { - // NOTE: this is a bit brute force way - it will not trigger events and hooks properly - // nuke all role assignments - role_unassign_all(array('component'=>$component)); - // purge participants - $DB->delete_records_select('user_enrolments', "enrolid IN (SELECT id FROM {enrol} WHERE enrol = ?)", array($name)); - // purge enrol instances - $DB->delete_records('enrol', array('enrol'=>$name)); - // tweak enrol settings - if (!empty($CFG->enrol_plugins_enabled)) { - $enabledenrols = explode(',', $CFG->enrol_plugins_enabled); - $enabledenrols = array_unique($enabledenrols); - $enabledenrols = array_flip($enabledenrols); - unset($enabledenrols[$name]); - $enabledenrols = array_flip($enabledenrols); - if (is_array($enabledenrols)) { - set_config('enrol_plugins_enabled', implode(',', $enabledenrols)); - } - } - - } else if ($type === 'block') { - if ($block = $DB->get_record('block', array('name'=>$name))) { - // Inform block it's about to be deleted - if (file_exists("$CFG->dirroot/blocks/$block->name/block_$block->name.php")) { - $blockobject = block_instance($block->name); - if ($blockobject) { - $blockobject->before_delete(); //only if we can create instance, block might have been already removed - } - } - - // First delete instances and related contexts - $instances = $DB->get_records('block_instances', array('blockname' => $block->name)); - foreach($instances as $instance) { - blocks_delete_instance($instance); - } - - // Delete block - $DB->delete_records('block', array('id'=>$block->id)); - } - } else if ($type === 'format') { - if (($defaultformat = get_config('moodlecourse', 'format')) && $defaultformat !== $name) { - $courses = $DB->get_records('course', array('format' => $name), 'id'); - $data = (object)array('id' => null, 'format' => $defaultformat); - foreach ($courses as $record) { - $data->id = $record->id; - update_course($data); - } - } - $DB->delete_records('course_format_options', array('format' => $name)); } // Specific plugin type cleanup. diff --git a/lib/pluginlib.php b/lib/pluginlib.php index 3dc18ae8879..a8afd85823f 100644 --- a/lib/pluginlib.php +++ b/lib/pluginlib.php @@ -3267,6 +3267,39 @@ class plugininfo_block extends plugininfo_base { return '

'.get_string('uninstallextraconfirmblock', 'core_plugin', array('instances'=>$count)).'

'; } + + /** + * Pre-uninstall hook. + * + * This is intended for disabling of plugin, some DB table purging, etc. + * + * NOTE: to be called from uninstall_plugin() only. + * @private + */ + public function uninstall_cleanup() { + global $DB, $CFG; + + if ($block = $DB->get_record('block', array('name'=>$this->name))) { + // Inform block it's about to be deleted + if (file_exists("$CFG->dirroot/blocks/$block->name/block_$block->name.php")) { + $blockobject = block_instance($block->name); + if ($blockobject) { + $blockobject->before_delete(); //only if we can create instance, block might have been already removed + } + } + + // First delete instances and related contexts + $instances = $DB->get_records('block_instances', array('blockname' => $block->name)); + foreach($instances as $instance) { + blocks_delete_instance($instance); + } + + // Delete block + $DB->delete_records('block', array('id'=>$block->id)); + } + + parent::uninstall_cleanup(); + } } @@ -3660,6 +3693,43 @@ class plugininfo_enrol extends plugininfo_base { return $result; } + + /** + * Pre-uninstall hook. + * + * This is intended for disabling of plugin, some DB table purging, etc. + * + * NOTE: to be called from uninstall_plugin() only. + * @private + */ + public function uninstall_cleanup() { + global $DB, $CFG; + + // NOTE: this is a bit brute force way - it will not trigger events and hooks properly. + + // Nuke all role assignments. + role_unassign_all(array('component'=>'enrol_'.$this->name)); + + // Purge participants. + $DB->delete_records_select('user_enrolments', "enrolid IN (SELECT id FROM {enrol} WHERE enrol = ?)", array($this->name)); + + // Purge enrol instances. + $DB->delete_records('enrol', array('enrol'=>$this->name)); + + // Tweak enrol settings. + if (!empty($CFG->enrol_plugins_enabled)) { + $enabledenrols = explode(',', $CFG->enrol_plugins_enabled); + $enabledenrols = array_unique($enabledenrols); + $enabledenrols = array_flip($enabledenrols); + unset($enabledenrols[$this->name]); + $enabledenrols = array_flip($enabledenrols); + if (is_array($enabledenrols)) { + set_config('enrol_plugins_enabled', implode(',', $enabledenrols)); + } + } + + parent::uninstall_cleanup(); + } } @@ -4113,4 +4183,29 @@ class plugininfo_format extends plugininfo_base { return $message; } + + /** + * Pre-uninstall hook. + * + * This is intended for disabling of plugin, some DB table purging, etc. + * + * NOTE: to be called from uninstall_plugin() only. + * @private + */ + public function uninstall_cleanup() { + global $DB; + + if (($defaultformat = get_config('moodlecourse', 'format')) && $defaultformat !== $this->name) { + $courses = $DB->get_records('course', array('format' => $this->name), 'id'); + $data = (object)array('id' => null, 'format' => $defaultformat); + foreach ($courses as $record) { + $data->id = $record->id; + update_course($data); + } + } + + $DB->delete_records('course_format_options', array('format' => $this->name)); + + parent::uninstall_cleanup(); + } }