Example #1
0
/**
 * Automatically clean-up all plugin data and remove the plugin DB tables
 *
 * NOTE: do not call directly, use new /admin/plugins.php?uninstall=component instead!
 *
 * @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;
    // This may take a long time.
    core_php_time_limit::raise();
    // Recursively uninstall all subplugins first.
    $subplugintypes = core_component::get_plugin_types_with_subplugins();
    if (isset($subplugintypes[$type])) {
        $base = core_component::get_plugin_directory($type, $name);
        if (file_exists("{$base}/db/subplugins.php")) {
            $subplugins = array();
            include "{$base}/db/subplugins.php";
            foreach ($subplugins as $subplugintype => $dir) {
                $instances = core_component::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);
    // Delete all tag instances associated with this plugin.
    require_once $CFG->dirroot . '/tag/lib.php';
    tag_delete_instances($component);
    // Custom plugin uninstall.
    $plugindirectory = core_component::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)) {
            // Do not verify result, let plugin complain if necessary.
            $uninstallfunction();
        }
    }
    // Specific plugin type cleanup.
    $plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
    if ($plugininfo) {
        $plugininfo->uninstall_cleanup();
        core_plugin_manager::reset_caches();
    }
    $plugininfo = null;
    // 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 scheduled tasks.
    $DB->delete_records('task_scheduled', array('component' => $pluginname));
    // Delete Inbound Message datakeys.
    $DB->delete_records_select('messageinbound_datakeys', 'handler IN (SELECT id FROM {messageinbound_handlers} WHERE component = ?)', array($pluginname));
    // Delete Inbound Message handlers.
    $DB->delete_records('messageinbound_handlers', array('component' => $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($component);
    if ($type === 'mod') {
        unset_all_config_for_plugin($pluginname);
    }
    // delete message provider
    message_provider_uninstall($component);
    // 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);
    // Delete all remaining files in the filepool owned by the component.
    $fs = get_file_storage();
    $fs->delete_component_files($component);
    // Finally purge all caches.
    purge_all_caches();
    // Invalidate the hash used for upgrade detections.
    set_config('allversionshash', '');
    echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
}
Example #2
0
/**
 * Web service discovery function used during install and upgrade.
 * @param string $component name of component (moodle, mod_assignment, etc.)
 * @return void
 */
function external_update_descriptions($component) {
    global $DB, $CFG;

    $defpath = core_component::get_component_directory($component).'/db/services.php';

    if (!file_exists($defpath)) {
        require_once($CFG->dirroot.'/lib/externallib.php');
        external_delete_descriptions($component);
        return;
    }

    // load new info
    $functions = array();
    $services = array();
    include($defpath);

    // update all function fist
    $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
    foreach ($dbfunctions as $dbfunction) {
        if (empty($functions[$dbfunction->name])) {
            $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
            // do not delete functions from external_services_functions, beacuse
            // we want to notify admins when functions used in custom services disappear

            //TODO: this looks wrong, we have to delete it eventually (skodak)
            continue;
        }

        $function = $functions[$dbfunction->name];
        unset($functions[$dbfunction->name]);
        $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];

        $update = false;
        if ($dbfunction->classname != $function['classname']) {
            $dbfunction->classname = $function['classname'];
            $update = true;
        }
        if ($dbfunction->methodname != $function['methodname']) {
            $dbfunction->methodname = $function['methodname'];
            $update = true;
        }
        if ($dbfunction->classpath != $function['classpath']) {
            $dbfunction->classpath = $function['classpath'];
            $update = true;
        }
        $functioncapabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
        if ($dbfunction->capabilities != $functioncapabilities) {
            $dbfunction->capabilities = $functioncapabilities;
            $update = true;
        }
        if ($update) {
            $DB->update_record('external_functions', $dbfunction);
        }
    }
    foreach ($functions as $fname => $function) {
        $dbfunction = new stdClass();
        $dbfunction->name       = $fname;
        $dbfunction->classname  = $function['classname'];
        $dbfunction->methodname = $function['methodname'];
        $dbfunction->classpath  = empty($function['classpath']) ? null : $function['classpath'];
        $dbfunction->component  = $component;
        $dbfunction->capabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
        $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
    }
    unset($functions);

    // now deal with services
    $dbservices = $DB->get_records('external_services', array('component'=>$component));
    foreach ($dbservices as $dbservice) {
        if (empty($services[$dbservice->name])) {
            $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
            $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
            $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
            $DB->delete_records('external_services', array('id'=>$dbservice->id));
            continue;
        }
        $service = $services[$dbservice->name];
        unset($services[$dbservice->name]);
        $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
        $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
        $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
        $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
        $service['uploadfiles'] = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
        $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];

        $update = false;
        if ($dbservice->requiredcapability != $service['requiredcapability']) {
            $dbservice->requiredcapability = $service['requiredcapability'];
            $update = true;
        }
        if ($dbservice->restrictedusers != $service['restrictedusers']) {
            $dbservice->restrictedusers = $service['restrictedusers'];
            $update = true;
        }
        if ($dbservice->downloadfiles != $service['downloadfiles']) {
            $dbservice->downloadfiles = $service['downloadfiles'];
            $update = true;
        }
        if ($dbservice->uploadfiles != $service['uploadfiles']) {
            $dbservice->uploadfiles = $service['uploadfiles'];
            $update = true;
        }
        //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
        if (isset($service['shortname']) and
                (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
            throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
        }
        if ($dbservice->shortname != $service['shortname']) {
            //check that shortname is unique
            if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
                $existingservice = $DB->get_record('external_services',
                        array('shortname' => $service['shortname']));
                if (!empty($existingservice)) {
                    throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
                }
            }
            $dbservice->shortname = $service['shortname'];
            $update = true;
        }
        if ($update) {
            $DB->update_record('external_services', $dbservice);
        }

        $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
        foreach ($functions as $function) {
            $key = array_search($function->functionname, $service['functions']);
            if ($key === false) {
                $DB->delete_records('external_services_functions', array('id'=>$function->id));
            } else {
                unset($service['functions'][$key]);
            }
        }
        foreach ($service['functions'] as $fname) {
            $newf = new stdClass();
            $newf->externalserviceid = $dbservice->id;
            $newf->functionname      = $fname;
            $DB->insert_record('external_services_functions', $newf);
        }
        unset($functions);
    }
    foreach ($services as $name => $service) {
        //check that shortname is unique
        if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
            $existingservice = $DB->get_record('external_services',
                    array('shortname' => $service['shortname']));
            if (!empty($existingservice)) {
                throw new moodle_exception('installserviceshortnameerror', 'webservice');
            }
        }

        $dbservice = new stdClass();
        $dbservice->name               = $name;
        $dbservice->enabled            = empty($service['enabled']) ? 0 : $service['enabled'];
        $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
        $dbservice->restrictedusers    = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
        $dbservice->downloadfiles      = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
        $dbservice->uploadfiles        = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
        $dbservice->shortname          = !isset($service['shortname']) ? null : $service['shortname'];
        $dbservice->component          = $component;
        $dbservice->timecreated        = time();
        $dbservice->id = $DB->insert_record('external_services', $dbservice);
        foreach ($service['functions'] as $fname) {
            $newf = new stdClass();
            $newf->externalserviceid = $dbservice->id;
            $newf->functionname      = $fname;
            $DB->insert_record('external_services_functions', $newf);
        }
    }
}
Example #3
0
/**
 * 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;
    // This may take a long time.
    @set_time_limit(0);
    // 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 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);
    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');
}
Example #4
0
/**
 * Web service discovery function used during install and upgrade.
 * @param string $component name of component (moodle, mod_assignment, etc.)
 * @return void
 */
function external_update_descriptions($component)
{
    global $DB;
    $defpath = get_component_directory($component) . '/db/services.php';
    if (!file_exists($defpath)) {
        external_delete_descriptions($component);
        return;
    }
    // load new info
    $functions = array();
    $services = array();
    include $defpath;
    // update all function fist
    $dbfunctions = $DB->get_records('external_functions', array('component' => $component));
    foreach ($dbfunctions as $dbfunction) {
        if (empty($functions[$dbfunction->name])) {
            $DB->delete_records('external_functions', array('id' => $dbfunction->id));
            // do not delete functions from external_services_functions, beacuse
            // we want to notify admins when functions used in custom services disappear
            //TODO: this looks wrong, we have to delete it eventually (skodak)
            continue;
        }
        $function = $functions[$dbfunction->name];
        unset($functions[$dbfunction->name]);
        $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
        $update = false;
        if ($dbfunction->classname != $function['classname']) {
            $dbfunction->classname = $function['classname'];
            $update = true;
        }
        if ($dbfunction->methodname != $function['methodname']) {
            $dbfunction->methodname = $function['methodname'];
            $update = true;
        }
        if ($dbfunction->classpath != $function['classpath']) {
            $dbfunction->classpath = $function['classpath'];
            $update = true;
        }
        $functioncapabilities = key_exists('capabilities', $function) ? $function['capabilities'] : '';
        if ($dbfunction->capabilities != $functioncapabilities) {
            $dbfunction->capabilities = $functioncapabilities;
            $update = true;
        }
        if ($update) {
            $DB->update_record('external_functions', $dbfunction);
        }
    }
    foreach ($functions as $fname => $function) {
        $dbfunction = new stdClass();
        $dbfunction->name = $fname;
        $dbfunction->classname = $function['classname'];
        $dbfunction->methodname = $function['methodname'];
        $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
        $dbfunction->component = $component;
        $dbfunction->capabilities = key_exists('capabilities', $function) ? $function['capabilities'] : '';
        $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
    }
    unset($functions);
    // now deal with services
    $dbservices = $DB->get_records('external_services', array('component' => $component));
    foreach ($dbservices as $dbservice) {
        if (empty($services[$dbservice->name])) {
            $DB->delete_records('external_services_functions', array('externalserviceid' => $dbservice->id));
            $DB->delete_records('external_services_users', array('externalserviceid' => $dbservice->id));
            $DB->delete_records('external_services', array('id' => $dbservice->id));
            continue;
        }
        $service = $services[$dbservice->name];
        unset($services[$dbservice->name]);
        $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
        $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
        $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
        $update = false;
        if ($dbservice->enabled != $service['enabled']) {
            $dbservice->enabled = $service['enabled'];
            $update = true;
        }
        if ($dbservice->requiredcapability != $service['requiredcapability']) {
            $dbservice->requiredcapability = $service['requiredcapability'];
            $update = true;
        }
        if ($dbservice->restrictedusers != $service['restrictedusers']) {
            $dbservice->restrictedusers = $service['restrictedusers'];
            $update = true;
        }
        if ($update) {
            $DB->update_record('external_services', $dbservice);
        }
        $functions = $DB->get_records('external_services_functions', array('externalserviceid' => $dbservice->id));
        foreach ($functions as $function) {
            $key = array_search($function->functionname, $service['functions']);
            if ($key === false) {
                $DB->delete_records('external_services_functions', array('id' => $function->id));
            } else {
                unset($service['functions'][$key]);
            }
        }
        foreach ($service['functions'] as $fname) {
            $newf = new stdClass();
            $newf->externalserviceid = $dbservice->id;
            $newf->functionname = $fname;
            $DB->insert_record('external_services_functions', $newf);
        }
        unset($functions);
    }
    foreach ($services as $name => $service) {
        $dbservice = new stdClass();
        $dbservice->name = $name;
        $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
        $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
        $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
        $dbservice->component = $component;
        $dbservice->timecreated = time();
        $dbservice->id = $DB->insert_record('external_services', $dbservice);
        foreach ($service['functions'] as $fname) {
            $newf = new stdClass();
            $newf->externalserviceid = $dbservice->id;
            $newf->functionname = $fname;
            $DB->insert_record('external_services_functions', $newf);
        }
    }
}
/**
 * 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');
}
Example #6
0
/**
 * Reload the webservice descriptions for a single plugins
 *
 * @param string $component
 * @param bool $dir does this component name have the directory on it
 * @return bool true = success
 */
function external_reload_component($component, $dir = true)
{
    // are there web service plugins
    if ($dir) {
        $component .= ($component ? '/' : '') . WEBSERVICE_DIRECTORY;
    }
    $basepath = get_config('docroot') . $component;
    // is there a webservice directory with the right files
    if (!file_exists($basepath) || !file_exists($basepath . '/services.php')) {
        external_delete_descriptions($component);
        return false;
    }
    $defpath = $basepath . '/services.php';
    // load new info
    $functions = array();
    $services = array();
    include $defpath;
    // update all function first
    $dbfunctions = get_records_array('external_functions', 'component', $component);
    if (!empty($dbfunctions)) {
        foreach ($dbfunctions as $dbfunction) {
            if (empty($functions[$dbfunction->name])) {
                // the functions is nolonger available for use
                delete_records('external_services_functions', 'functionname', $dbfunction->name);
                delete_records('external_functions', 'id', $dbfunction->id);
                continue;
            }
            $function = $functions[$dbfunction->name];
            unset($functions[$dbfunction->name]);
            $function['classpath'] = empty($function['classpath']) ? $component : $function['classpath'];
            $update = false;
            if ($dbfunction->classname != $function['classname']) {
                $dbfunction->classname = $function['classname'];
                $update = true;
            }
            if ($dbfunction->methodname != $function['methodname']) {
                $dbfunction->methodname = $function['methodname'];
                $update = true;
            }
            if ($dbfunction->classpath != $function['classpath']) {
                $dbfunction->classpath = $function['classpath'];
                $update = true;
            }
            if ($update) {
                update_record('external_functions', $dbfunction);
            }
        }
    }
    foreach ($functions as $fname => $function) {
        $dbfunction = new stdClass();
        $dbfunction->name = $fname;
        $dbfunction->classname = $function['classname'];
        $dbfunction->methodname = $function['methodname'];
        $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
        $dbfunction->component = $component;
        $dbfunction->id = insert_record('external_functions', $dbfunction);
    }
    unset($functions);
    // now deal with services
    $dbservices = get_records_array('external_services', 'component', $component);
    if (!empty($dbservices)) {
        foreach ($dbservices as $dbservice) {
            if (empty($services[$dbservice->name])) {
                delete_records('external_services_functions', 'externalserviceid', $dbservice->id);
                delete_records('external_services_users', 'externalserviceid', $dbservice->id);
                delete_records('external_tokens', 'externalserviceid', $dbservice->id);
                delete_records_select('oauth_server_token', "osr_id_ref IN (SELECT id FROM {oauth_server_registry} WHERE externalserviceid = ?)", array($dbservice->id));
                delete_records_select('oauth_server_registry', "externalserviceid = ?", array($dbservice->id));
                delete_records('external_services', 'id', $dbservice->id);
                continue;
            }
            $service = $services[$dbservice->name];
            unset($services[$dbservice->name]);
            $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
            $service['restrictedusers'] = isset($service['restrictedusers']) && $service['restrictedusers'] == 1 ? 1 : 0;
            $service['tokenusers'] = isset($service['tokenusers']) && $service['tokenusers'] == 1 ? 1 : 0;
            $update = false;
            if ($dbservice->enabled != $service['enabled']) {
                $dbservice->enabled = $service['enabled'];
                $update = true;
            }
            if ($dbservice->restrictedusers != $service['restrictedusers']) {
                $dbservice->restrictedusers = $service['restrictedusers'];
                $update = true;
            }
            if ($dbservice->tokenusers != $service['tokenusers']) {
                $dbservice->tokenusers = $service['tokenusers'];
                $update = true;
            }
            if ($update) {
                update_record('external_services', $dbservice);
            }
            $functions = get_records_array('external_services_functions', 'externalserviceid', $dbservice->id);
            if (!empty($functions)) {
                foreach ($functions as $function) {
                    $key = array_search($function->functionname, $service['functions']);
                    if ($key === false) {
                        delete_records('external_services_functions', 'id', $function->id);
                    } else {
                        unset($service['functions'][$key]);
                    }
                }
            }
            foreach ($service['functions'] as $fname) {
                $newf = new stdClass();
                $newf->externalserviceid = $dbservice->id;
                $newf->functionname = $fname;
                insert_record('external_services_functions', $newf);
            }
            unset($functions);
        }
    }
    foreach ($services as $name => $service) {
        $dbservice = new stdClass();
        $dbservice->name = $name;
        $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
        $dbservice->restrictedusers = isset($service['restrictedusers']) && $service['restrictedusers'] == 1 ? 1 : 0;
        $dbservice->tokenusers = isset($service['tokenusers']) && $service['tokenusers'] == 1 ? 1 : 0;
        $dbservice->component = $component;
        $dbservice->ctime = db_format_timestamp(time());
        $dbservice->id = insert_record('external_services', $dbservice, 'id', true);
        foreach ($service['functions'] as $fname) {
            $newf = new stdClass();
            $newf->externalserviceid = $dbservice->id;
            $newf->functionname = $fname;
            insert_record('external_services_functions', $newf);
        }
    }
    return true;
}