/** * Automatically clean-up all plugin data and remove the plugin DB tables * * @param string $type The plugin type, eg. 'mod', 'qtype', 'workshopgrading' etc. * @param string $name The plugin name, eg. 'forum', 'multichoice', 'accumulative' etc. * @uses global $OUTPUT to produce notices and other messages * @return void */ function uninstall_plugin($type, $name) { global $CFG, $DB, $OUTPUT; // recursively uninstall all module subplugins first if ($type === 'mod') { if (file_exists("{$CFG->dirroot}/mod/{$name}/db/subplugins.php")) { $subplugins = array(); include "{$CFG->dirroot}/mod/{$name}/db/subplugins.php"; foreach ($subplugins as $subplugintype => $dir) { $instances = get_plugin_list($subplugintype); foreach ($instances as $subpluginname => $notusedpluginpath) { uninstall_plugin($subplugintype, $subpluginname); } } } } $component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum' if ($type === 'mod') { $pluginname = $name; // eg. 'forum' if (get_string_manager()->string_exists('modulename', $component)) { $strpluginname = get_string('modulename', $component); } else { $strpluginname = $component; } } else { $pluginname = $component; if (get_string_manager()->string_exists('pluginname', $component)) { $strpluginname = get_string('pluginname', $component); } else { $strpluginname = $component; } } echo $OUTPUT->heading($pluginname); $plugindirectory = get_plugin_directory($type, $name); $uninstalllib = $plugindirectory . '/db/uninstall.php'; if (file_exists($uninstalllib)) { require_once $uninstalllib; $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()' if (function_exists($uninstallfunction)) { if (!$uninstallfunction()) { echo $OUTPUT->notification('Encountered a problem running uninstall function for ' . $pluginname); } } } if ($type === 'mod') { // perform cleanup tasks specific for activity modules if (!($module = $DB->get_record('modules', array('name' => $name)))) { print_error('moduledoesnotexist', 'error'); } // delete all the relevant instances from all course sections if ($coursemods = $DB->get_records('course_modules', array('module' => $module->id))) { foreach ($coursemods as $coursemod) { if (!delete_mod_from_section($coursemod->id, $coursemod->section)) { echo $OUTPUT->notification("Could not delete the {$strpluginname} with id = {$coursemod->id} from section {$coursemod->section}"); } } } // clear course.modinfo for courses that used this module $sql = "UPDATE {course}\n SET modinfo=''\n WHERE id IN (SELECT DISTINCT course\n FROM {course_modules}\n WHERE module=?)"; $DB->execute($sql, array($module->id)); // delete all the course module records $DB->delete_records('course_modules', array('module' => $module->id)); // delete module contexts if ($coursemods) { foreach ($coursemods as $coursemod) { if (!delete_context(CONTEXT_MODULE, $coursemod->id)) { echo $OUTPUT->notification("Could not delete the context for {$strpluginname} with id = {$coursemod->id}"); } } } // delete the module entry itself $DB->delete_records('modules', array('name' => $module->name)); // cleanup the gradebook require_once $CFG->libdir . '/gradelib.php'; grade_uninstalled_module($module->name); // Perform any custom uninstall tasks if (file_exists($CFG->dirroot . '/mod/' . $module->name . '/lib.php')) { require_once $CFG->dirroot . '/mod/' . $module->name . '/lib.php'; $uninstallfunction = $module->name . '_uninstall'; if (function_exists($uninstallfunction)) { debugging("{$uninstallfunction}() has been deprecated. Use the plugin's db/uninstall.php instead", DEBUG_DEVELOPER); if (!$uninstallfunction()) { echo $OUTPUT->notification('Encountered a problem running uninstall function for ' . $module->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)); } } } } // perform clean-up task common for all the plugin/subplugin types // delete calendar events $DB->delete_records('event', array('modulename' => $pluginname)); // delete all the logs $DB->delete_records('log', array('module' => $pluginname)); // delete log_display information $DB->delete_records('log_display', array('component' => $component)); // delete the module configuration records unset_all_config_for_plugin($pluginname); // delete message provider message_provider_uninstall($component); // delete message processor if ($type === 'message') { message_processor_uninstall($name); } // delete the plugin tables $xmldbfilepath = $plugindirectory . '/db/install.xml'; drop_plugin_tables($component, $xmldbfilepath, false); if ($type === 'mod' or $type === 'block') { // non-frankenstyle table prefixes drop_plugin_tables($name, $xmldbfilepath, false); } // delete the capabilities that were defined by this module capabilities_cleanup($component); // remove event handlers and dequeue pending events events_uninstall($component); echo $OUTPUT->notification(get_string('success'), 'notifysuccess'); }
$DB->set_field('message_processors', 'enabled', '1', array('id' => $processor->id)); // Enable output } if (!empty($uninstall) && confirm_sesskey()) { echo $OUTPUT->header(); echo $OUTPUT->heading($headingtitle); if (!($processor = $DB->get_record('message_processors', array('id' => $uninstall)))) { print_error('outputdoesnotexist', 'message'); } $processorname = get_string('pluginname', 'message_' . $processor->name); if (!$confirm) { echo $OUTPUT->confirm(get_string('processordeleteconfirm', 'message', $processorname), 'message.php?uninstall=' . $processor->id . '&confirm=1', 'message.php'); echo $OUTPUT->footer(); exit; } else { message_processor_uninstall($processor->name); $a = new stdClass(); $a->processor = $processorname; $a->directory = $CFG->dirroot . '/message/output/' . $processor->name; notice(get_string('processordeletefiles', 'message', $a), 'message.php'); } } if ($disable || $enable || $uninstall) { $url = new moodle_url('message.php'); redirect($url); } // Page settings $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); // Grab the renderer $renderer = $PAGE->get_renderer('core', 'message'); // Display the manage message outputs interface
/** * Automatically clean-up all plugin data and remove the plugin DB tables * * @param string $type The subplugin type, eg. 'vmoodleadminset' etc. * @param string $name The subplugin name, eg. 'example', 'generic', etc. * @uses global $OUTPUT to produce notices and other messages * @return void */ function vmoodle_uninstall_plugin($type, $name, $plugindirectory) { global $CFG, $DB, $OUTPUT; // This may take a long time. @set_time_limit(0); $component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum' $pluginname = $component; if (get_string_manager()->string_exists('pluginname', $component)) { $strpluginname = get_string('pluginname', $component); } else { $strpluginname = $component; } echo $OUTPUT->heading($pluginname); $uninstalllib = $plugindirectory . '/db/uninstall.php'; if (file_exists($uninstalllib)) { require_once $uninstalllib; $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()' if (function_exists($uninstallfunction)) { if (!$uninstallfunction()) { echo $OUTPUT->notification('Encountered a problem running uninstall function for ' . $pluginname); } } } // Perform clean-up task common for all the plugin/subplugin types. // Delete the web service functions and pre-built services. require_once $CFG->dirroot . '/lib/externallib.php'; external_delete_descriptions($component); // Delete calendar events. $DB->delete_records('event', array('modulename' => $pluginname)); // Delete all the logs. $DB->delete_records('log', array('module' => $pluginname)); // Delete log_display information. $DB->delete_records('log_display', array('component' => $component)); // Delete the module configuration records. unset_all_config_for_plugin($pluginname); // delete message provider message_provider_uninstall($component); // Delete message processor. if ($type === 'message') { message_processor_uninstall($name); } // Delete the plugin tables. $xmldbfilepath = $plugindirectory . '/db/install.xml'; drop_plugin_tables($component, $xmldbfilepath, false); // Delete the capabilities that were defined by this module. capabilities_cleanup($component); // Remove event handlers and dequeue pending events. events_uninstall($component); echo $OUTPUT->notification(get_string('success'), 'notifysuccess'); }
/** * 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 $CFG; require_once $CFG->libdir . '/messagelib.php'; message_processor_uninstall($this->name); parent::uninstall_cleanup(); }