<commit_before><?php  //
       // 

/**
 * adminlib.php - Contains functions that only administrators will ever need to use
 *
 * @author <NAME>
 * @version  $Id$
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package moodlecore
 */

/**
 * Upgrade plugins
 *
 * @uses $db
 * @uses $CFG
 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
 * @param string $dir  The directory where the plugins are located (e.g. 'question/questiontypes')
 * @param string $return The url to prompt the user to continue to
 */ 
function upgrade_plugins($type, $dir, $return) {
    global $CFG, $db;

    if (!$plugs = get_list_of_plugins($dir) ) {
        error('No '.$type.' plugins installed!');
    }

    $updated_plugins = false;
    $strpluginsetup  = get_string('pluginsetup');

    foreach ($plugs as $plug) {

        $fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;

        unset($plugin);

        if (is_readable($fullplug .'/version.php')) {
            include_once($fullplug .'/version.php');  // defines $plugin with version etc
        } else {
            continue;                              // Nothing to do.
        }

        if (is_readable($fullplug .'/db/'. $CFG->dbtype .'.php')) {
            include_once($fullplug .'/db/'. $CFG->dbtype .'.php');  // defines upgrading function
        } else {
            continue;
        }

        if (!isset($plugin)) {
            continue;
        }

        if (!empty($plugin->requires)) {
            if ($plugin->requires > $CFG->version) {
                $info->pluginname = $plug;
                $info->pluginversion  = $plugin->version;
                $info->currentmoodle = $CFG->version;
                $info->requiremoodle = $plugin->requires;
                if (!$updated_plugins) {
                    print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '', 
                        '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
                        false, '&nbsp;', '&nbsp;');
                }
                upgrade_log_start();
                notify(get_string('pluginrequirementsnotmet', 'error', $info));
                $updated_plugins = true;
                unset($info);
                continue;
            }
        }

        $plugin->name = $plug;   // The name MUST match the directory

        $pluginversion = $type.'_'.$plug.'_version';

        if (!isset($CFG->$pluginversion)) {
            set_config($pluginversion, 0);
        }
        
        if ($CFG->$pluginversion == $plugin->version) {
            // do nothing
        } else if ($CFG->$pluginversion < $plugin->version) {
            if (!$updated_plugins) {
                print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '', 
                        '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
                        false, '&nbsp;', '&nbsp;');
            }
            upgrade_log_start();
            print_heading($plugin->name .' plugin needs upgrading');
            if ($CFG->$pluginversion == 0) {    // It's a new install of this plugin
                if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
                    $db->debug = true;
                    @set_time_limit(0);  // To allow slow databases to complete the long SQL
                    if (modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
                        // OK so far, now update the plugins record
                        set_config($pluginversion, $plugin->version);
                        if (!update_capabilities($dir)) {
                            error('Could not set up the capabilities for '.$module->name.'!');
                        }
                        notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
                    } else {
                        notify('Installing '. $plugin->name .' FAILED!');
                    }
                    $db->debug = false;
                } else {    // We'll assume no tables are necessary
                    set_config($pluginversion, $plugin->version);
                    notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
                }
            } else {                            // Upgrade existing install
                $upgrade_function = $type.'_'.$plugin->name .'_upgrade';
                if (function_exists($upgrade_function)) {
                    $db->debug=true;
                    if ($upgrade_function($CFG->$pluginversion)) {
                        $db->debug=false;
                        // OK so far, now update the plugins record
                        set_config($pluginversion, $plugin->version);
                        if (!update_capabilities($dir)) {
                            error('Could not update '.$plugin->name.' capabilities!');
                        }
                        notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
                    } else {
                        $db->debug=false;
                        notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!');
                    }
                }
            }
            echo '<hr />';
            $updated_plugins = true;
        } else {
            upgrade_log_start();
            error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
        }
    }

    upgrade_log_finish();

    if ($updated_plugins) {
        print_continue($return);
        die;
    }
}

/**<commit_msg>Fixed bug in upgrade_plugins(). Capabilities were not being loaded correctly.<commit_after><?php  //
       // 

/**
 * adminlib.php - Contains functions that only administrators will ever need to use
 *
 * @author <NAME>
 * @version  $Id$
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package moodlecore
 */

/**
 * Upgrade plugins
 *
 * @uses $db
 * @uses $CFG
 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
 * @param string $dir  The directory where the plugins are located (e.g. 'question/questiontypes')
 * @param string $return The url to prompt the user to continue to
 */ 
function upgrade_plugins($type, $dir, $return) {
    global $CFG, $db;

    if (!$plugs = get_list_of_plugins($dir) ) {
        error('No '.$type.' plugins installed!');
    }

    $updated_plugins = false;
    $strpluginsetup  = get_string('pluginsetup');

    foreach ($plugs as $plug) {

        $fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;

        unset($plugin);

        if (is_readable($fullplug .'/version.php')) {
            include_once($fullplug .'/version.php');  // defines $plugin with version etc
        } else {
            continue;                              // Nothing to do.
        }

        if (is_readable($fullplug .'/db/'. $CFG->dbtype .'.php')) {
            include_once($fullplug .'/db/'. $CFG->dbtype .'.php');  // defines upgrading function
        } else {
            continue;
        }

        if (!isset($plugin)) {
            continue;
        }

        if (!empty($plugin->requires)) {
            if ($plugin->requires > $CFG->version) {
                $info->pluginname = $plug;
                $info->pluginversion  = $plugin->version;
                $info->currentmoodle = $CFG->version;
                $info->requiremoodle = $plugin->requires;
                if (!$updated_plugins) {
                    print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '', 
                        '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
                        false, '&nbsp;', '&nbsp;');
                }
                upgrade_log_start();
                notify(get_string('pluginrequirementsnotmet', 'error', $info));
                $updated_plugins = true;
                unset($info);
                continue;
            }
        }

        $plugin->name = $plug;   // The name MUST match the directory

        $pluginversion = $type.'_'.$plug.'_version';

        if (!isset($CFG->$pluginversion)) {
            set_config($pluginversion, 0);
        }
        
        if ($CFG->$pluginversion == $plugin->version) {
            // do nothing
        } else if ($CFG->$pluginversion < $plugin->version) {
            if (!$updated_plugins) {
                print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '', 
                        '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
                        false, '&nbsp;', '&nbsp;');
            }
            upgrade_log_start();
            print_heading($plugin->name .' plugin needs upgrading');
            if ($CFG->$pluginversion == 0) {    // It's a new install of this plugin
                if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
                    $db->debug = true;
                    @set_time_limit(0);  // To allow slow databases to complete the long SQL
                    if (modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
                        // OK so far, now update the plugins record
                        set_config($pluginversion, $plugin->version);
                        if (!update_capabilities($dir.'/'.$plug)) {
                            error('Could not set up the capabilities for '.$module->name.'!');
                        }
                        notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
                    } else {
                        notify('Installing '. $plugin->name .' FAILED!');
                    }
                    $db->debug = false;
                } else {    // We'll assume no tables are necessary
                    set_config($pluginversion, $plugin->version);
                    notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
                }
            } else {                            // Upgrade existing install
                $upgrade_function = $type.'_'.$plugin->name .'_upgrade';
                if (function_exists($upgrade_function)) {
                    $db->debug=true;
                    if ($upgrade_function($CFG->$pluginversion)) {
                        $db->debug=false;
                        // OK so far, now update the plugins record
                        set_config($pluginversion, $plugin->version);
                        if (!update_capabilities($dir.'/'.$plug)) {
                            error('Could not update '.$plugin->name.' capabilities!');
                        }
                        notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
                    } else {
                        $db->debug=false;
                        notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!');
                    }
                }
            }
            echo '<hr />';
            $updated_plugins = true;
        } else {
            upgrade_log_start();
            error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
        }
    }

    upgrade_log_finish();

    if ($updated_plugins) {
        print_continue($return);
        die;
    }
}

/**
{'fraction_non_alphanumeric': 0.14709453599306158, 'fraction_numerical': 0.0005203816131830009, 'mean_word_length': 1.5354001759014952, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3256757', 'n_tokens_mistral': 3142, 'n_tokens_neox': 2991, 'n_words': 677}