From fad20e3c82b2ce84c8fb7c6904ae462e85f9e9eb Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Mon, 2 Mar 2015 20:20:10 -0500 Subject: [PATCH] MDL-45621 Portfolio: Allow portfolios to be uninstalled --- lib/classes/plugininfo/portfolio.php | 39 +++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/classes/plugininfo/portfolio.php b/lib/classes/plugininfo/portfolio.php index 4ae156d5463..78e94f40a96 100644 --- a/lib/classes/plugininfo/portfolio.php +++ b/lib/classes/plugininfo/portfolio.php @@ -54,4 +54,41 @@ class portfolio extends base { public static function get_manage_url() { return new moodle_url('/admin/portfolio.php'); } -} + + /** + * Defines if there should be a way to uninstall the plugin via the administration UI. + * @return boolean + */ + public function is_uninstall_allowed() { + return true; + } + + /** + * Pre-uninstall hook. + * This is intended for disabling of plugin, some DB table purging, etc. + */ + public function uninstall_cleanup() { + global $DB; + + // Get all instances of this portfolio. + $count = $DB->count_records('portfolio_instance', array('plugin' => $this->name)); + if ($count > 0) { + // This portfolio is in use, get the it's ID. + $rec = $DB->get_record('portfolio_instance', array('plugin' => $this->name)); + + // Remove all records from portfolio_instance_config. + $DB->delete_records('portfolio_instance_config', array('instance' => $rec->id)); + // Remove all records from portfolio_instance_user. + $DB->delete_records('portfolio_instance_user', array('instance' => $rec->id)); + // Remove all records from portfolio_log. + $DB->delete_records('portfolio_log', array('portfolio' => $rec->id)); + // Remove all records from portfolio_tempdata. + $DB->delete_records('portfolio_tempdata', array('instance' => $rec->id)); + + // Remove the record from the portfolio_instance table. + $DB->delete_records('portfolio_instance', array('id' => $rec->id)); + } + + parent::uninstall_cleanup(); + } +} \ No newline at end of file