Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Defines classes used for plugin info.
 *
 * @package    core
 * @copyright  2013 Petr Skoda {@link http://skodak.org}
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
namespace core\plugininfo;

use moodle_url, part_of_admin_tree, admin_settingpage, admin_externalpage;

defined('MOODLE_INTERNAL') || die();

/**
 * Class for enrolment plugins
 */
class enrol extends base {
    /**
     * Finds all enabled plugins, the result may include missing plugins.
     * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
     */
    public static function get_enabled_plugins() {
        global $CFG;

        $enabled = array();
        foreach (explode(',', $CFG->enrol_plugins_enabled) as $enrol) {
            $enabled[$enrol] = $enrol;
        }

        return $enabled;
    }

> public static function enable_plugin(string $pluginname, int $enabled): bool { public function get_settings_section_name() { > global $CFG; if (file_exists($this->full_path('settings.php'))) { > return 'enrolsettings' . $this->name; > $haschanged = false; } else { > $plugins = []; return null; > if (!empty($CFG->enrol_plugins_enabled)) { } > $plugins = array_flip(explode(',', $CFG->enrol_plugins_enabled)); } > } > // Only set visibility if it's different from the current value. public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) { > if ($enabled && !array_key_exists($pluginname, $plugins)) { global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them. > $plugins[$pluginname] = $pluginname; $ADMIN = $adminroot; // May be used in settings.php. > $haschanged = true; $plugininfo = $this; // Also can be used inside settings.php. > } else if (!$enabled && array_key_exists($pluginname, $plugins)) { $enrol = $this; // Also can be used inside settings.php. > unset($plugins[$pluginname]); > $haschanged = true; if (!$this->is_installed_and_upgraded()) { > } return; > } > if ($haschanged) { > $new = implode(',', array_flip($plugins)); if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) { > add_to_config_log('enrol_plugins_enabled', !$enabled, $enabled, $pluginname); return; > set_config('enrol_plugins_enabled', $new); } > // Reset caches. > \core_plugin_manager::reset_caches(); $section = $this->get_settings_section_name(); > // Resets all enrol caches. > $syscontext = \context_system::instance(); $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false); > $syscontext->mark_dirty(); > } include($this->full_path('settings.php')); // This may also set $settings to null! > > return $haschanged; if ($settings) { > } $ADMIN->add($parentnodename, $settings); >
} } public function is_uninstall_allowed() { if ($this->name === 'manual') { return false; } return true; } /** * Return URL used for management of plugins of this type. * @return moodle_url */ public static function get_manage_url() { return new moodle_url('/admin/settings.php', array('section'=>'manageenrols')); } /** * Return warning with number of activities and number of affected courses. * * @return string */ public function get_uninstall_extra_warning() { global $DB, $OUTPUT; $sql = "SELECT COUNT('x') FROM {user_enrolments} ue JOIN {enrol} e ON e.id = ue.enrolid WHERE e.enrol = :plugin"; $count = $DB->count_records_sql($sql, array('plugin'=>$this->name)); if (!$count) { return ''; } $migrateurl = new moodle_url('/admin/enrol.php', array('action'=>'migrate', 'enrol'=>$this->name, 'sesskey'=>sesskey())); $migrate = new \single_button($migrateurl, get_string('migratetomanual', 'core_enrol')); $button = $OUTPUT->render($migrate); $result = '<p>'.get_string('uninstallextraconfirmenrol', 'core_plugin', array('enrolments'=>$count)).'</p>'; $result .= $button; 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(); } }