Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Defines classes used for plugin info.
  19   *
  20   * @package    core
  21   * @copyright  2013 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\plugininfo;
  25  
  26  use admin_settingpage;
  27  use moodle_url;
  28  use part_of_admin_tree;
  29  
  30  /**
  31   * Class for enrolment plugins
  32   */
  33  class enrol extends base {
  34  
  35      public static function plugintype_supports_disabling(): bool {
  36          return true;
  37      }
  38  
  39      /**
  40       *
  41       * Finds all enabled plugins, the result may include missing plugins.
  42       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  43       */
  44      public static function get_enabled_plugins() {
  45          global $CFG;
  46  
  47          $enabled = array();
  48          foreach (explode(',', $CFG->enrol_plugins_enabled) as $enrol) {
  49              $enabled[$enrol] = $enrol;
  50          }
  51  
  52          return $enabled;
  53      }
  54  
  55      public static function enable_plugin(string $pluginname, int $enabled): bool {
  56          global $CFG;
  57  
  58          $haschanged = false;
  59          $plugins = [];
  60          if (!empty($CFG->enrol_plugins_enabled)) {
  61              $plugins = array_flip(explode(',', $CFG->enrol_plugins_enabled));
  62          }
  63          // Only set visibility if it's different from the current value.
  64          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  65              $plugins[$pluginname] = $pluginname;
  66              $haschanged = true;
  67          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
  68              unset($plugins[$pluginname]);
  69              $haschanged = true;
  70          }
  71  
  72          if ($haschanged) {
  73              $new = implode(',', array_flip($plugins));
  74              add_to_config_log('enrol_plugins_enabled', !$enabled, $enabled, $pluginname);
  75              set_config('enrol_plugins_enabled', $new);
  76              // Reset caches.
  77              \core_plugin_manager::reset_caches();
  78              // Resets all enrol caches.
  79              $syscontext = \context_system::instance();
  80              $syscontext->mark_dirty();
  81          }
  82  
  83          return $haschanged;
  84      }
  85  
  86      public function get_settings_section_name() {
  87          if (file_exists($this->full_path('settings.php'))) {
  88              return 'enrolsettings' . $this->name;
  89          } else {
  90              return null;
  91          }
  92      }
  93  
  94      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  95          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  96          /** @var \admin_root $ADMIN */
  97          $ADMIN = $adminroot; // May be used in settings.php.
  98          $plugininfo = $this; // Also can be used inside settings.php.
  99          $enrol = $this;      // Also can be used inside settings.php.
 100  
 101          if (!$this->is_installed_and_upgraded()) {
 102              return;
 103          }
 104  
 105          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
 106              return;
 107          }
 108  
 109          $section = $this->get_settings_section_name();
 110  
 111          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 112  
 113          include($this->full_path('settings.php')); // This may also set $settings to null!
 114  
 115          if ($settings) {
 116              $ADMIN->add($parentnodename, $settings);
 117          }
 118      }
 119  
 120      public function is_uninstall_allowed() {
 121          if ($this->name === 'manual') {
 122              return false;
 123          }
 124          return true;
 125      }
 126  
 127      /**
 128       * Return URL used for management of plugins of this type.
 129       * @return moodle_url
 130       */
 131      public static function get_manage_url() {
 132          return new moodle_url('/admin/settings.php', array('section'=>'manageenrols'));
 133      }
 134  
 135      /**
 136       * Return warning with number of activities and number of affected courses.
 137       *
 138       * @return string
 139       */
 140      public function get_uninstall_extra_warning() {
 141          global $DB, $OUTPUT;
 142  
 143          $sql = "SELECT COUNT('x')
 144                    FROM {user_enrolments} ue
 145                    JOIN {enrol} e ON e.id = ue.enrolid
 146                   WHERE e.enrol = :plugin";
 147          $count = $DB->count_records_sql($sql, array('plugin'=>$this->name));
 148  
 149          if (!$count) {
 150              return '';
 151          }
 152  
 153          $migrateurl = new moodle_url('/admin/enrol.php', array('action'=>'migrate', 'enrol'=>$this->name, 'sesskey'=>sesskey()));
 154          $migrate = new \single_button($migrateurl, get_string('migratetomanual', 'core_enrol'));
 155          $button = $OUTPUT->render($migrate);
 156  
 157          $result = '<p>'.get_string('uninstallextraconfirmenrol', 'core_plugin', array('enrolments'=>$count)).'</p>';
 158          $result .= $button;
 159  
 160          return $result;
 161      }
 162  
 163      /**
 164       * Pre-uninstall hook.
 165       *
 166       * This is intended for disabling of plugin, some DB table purging, etc.
 167       *
 168       * NOTE: to be called from uninstall_plugin() only.
 169       * @private
 170       */
 171      public function uninstall_cleanup() {
 172          global $DB, $CFG;
 173  
 174          // NOTE: this is a bit brute force way - it will not trigger events and hooks properly.
 175  
 176          // Nuke all role assignments.
 177          role_unassign_all(array('component'=>'enrol_'.$this->name));
 178  
 179          // Purge participants.
 180          $DB->delete_records_select('user_enrolments', "enrolid IN (SELECT id FROM {enrol} WHERE enrol = ?)", array($this->name));
 181  
 182          // Purge enrol instances.
 183          $DB->delete_records('enrol', array('enrol'=>$this->name));
 184  
 185          // Tweak enrol settings.
 186          if (!empty($CFG->enrol_plugins_enabled)) {
 187              $enabledenrols = explode(',', $CFG->enrol_plugins_enabled);
 188              $enabledenrols = array_unique($enabledenrols);
 189              $enabledenrols = array_flip($enabledenrols);
 190              unset($enabledenrols[$this->name]);
 191              $enabledenrols = array_flip($enabledenrols);
 192              if (is_array($enabledenrols)) {
 193                  set_config('enrol_plugins_enabled', implode(',', $enabledenrols));
 194              }
 195          }
 196  
 197          parent::uninstall_cleanup();
 198      }
 199  }