Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [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 moodle_url, part_of_admin_tree, admin_settingpage, admin_externalpage;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class for enrolment plugins
  32   */
  33  class enrol extends base {
  34      /**
  35       * Finds all enabled plugins, the result may include missing plugins.
  36       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  37       */
  38      public static function get_enabled_plugins() {
  39          global $CFG;
  40  
  41          $enabled = array();
  42          foreach (explode(',', $CFG->enrol_plugins_enabled) as $enrol) {
  43              $enabled[$enrol] = $enrol;
  44          }
  45  
  46          return $enabled;
  47      }
  48  
  49      public static function enable_plugin(string $pluginname, int $enabled): bool {
  50          global $CFG;
  51  
  52          $haschanged = false;
  53          $plugins = [];
  54          if (!empty($CFG->enrol_plugins_enabled)) {
  55              $plugins = array_flip(explode(',', $CFG->enrol_plugins_enabled));
  56          }
  57          // Only set visibility if it's different from the current value.
  58          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  59              $plugins[$pluginname] = $pluginname;
  60              $haschanged = true;
  61          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
  62              unset($plugins[$pluginname]);
  63              $haschanged = true;
  64          }
  65  
  66          if ($haschanged) {
  67              $new = implode(',', array_flip($plugins));
  68              add_to_config_log('enrol_plugins_enabled', !$enabled, $enabled, $pluginname);
  69              set_config('enrol_plugins_enabled', $new);
  70              // Reset caches.
  71              \core_plugin_manager::reset_caches();
  72              // Resets all enrol caches.
  73              $syscontext = \context_system::instance();
  74              $syscontext->mark_dirty();
  75          }
  76  
  77          return $haschanged;
  78      }
  79  
  80      public function get_settings_section_name() {
  81          if (file_exists($this->full_path('settings.php'))) {
  82              return 'enrolsettings' . $this->name;
  83          } else {
  84              return null;
  85          }
  86      }
  87  
  88      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  89          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  90          $ADMIN = $adminroot; // May be used in settings.php.
  91          $plugininfo = $this; // Also can be used inside settings.php.
  92          $enrol = $this;      // Also can be used inside settings.php.
  93  
  94          if (!$this->is_installed_and_upgraded()) {
  95              return;
  96          }
  97  
  98          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
  99              return;
 100          }
 101  
 102          $section = $this->get_settings_section_name();
 103  
 104          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 105  
 106          include($this->full_path('settings.php')); // This may also set $settings to null!
 107  
 108          if ($settings) {
 109              $ADMIN->add($parentnodename, $settings);
 110          }
 111      }
 112  
 113      public function is_uninstall_allowed() {
 114          if ($this->name === 'manual') {
 115              return false;
 116          }
 117          return true;
 118      }
 119  
 120      /**
 121       * Return URL used for management of plugins of this type.
 122       * @return moodle_url
 123       */
 124      public static function get_manage_url() {
 125          return new moodle_url('/admin/settings.php', array('section'=>'manageenrols'));
 126      }
 127  
 128      /**
 129       * Return warning with number of activities and number of affected courses.
 130       *
 131       * @return string
 132       */
 133      public function get_uninstall_extra_warning() {
 134          global $DB, $OUTPUT;
 135  
 136          $sql = "SELECT COUNT('x')
 137                    FROM {user_enrolments} ue
 138                    JOIN {enrol} e ON e.id = ue.enrolid
 139                   WHERE e.enrol = :plugin";
 140          $count = $DB->count_records_sql($sql, array('plugin'=>$this->name));
 141  
 142          if (!$count) {
 143              return '';
 144          }
 145  
 146          $migrateurl = new moodle_url('/admin/enrol.php', array('action'=>'migrate', 'enrol'=>$this->name, 'sesskey'=>sesskey()));
 147          $migrate = new \single_button($migrateurl, get_string('migratetomanual', 'core_enrol'));
 148          $button = $OUTPUT->render($migrate);
 149  
 150          $result = '<p>'.get_string('uninstallextraconfirmenrol', 'core_plugin', array('enrolments'=>$count)).'</p>';
 151          $result .= $button;
 152  
 153          return $result;
 154      }
 155  
 156      /**
 157       * Pre-uninstall hook.
 158       *
 159       * This is intended for disabling of plugin, some DB table purging, etc.
 160       *
 161       * NOTE: to be called from uninstall_plugin() only.
 162       * @private
 163       */
 164      public function uninstall_cleanup() {
 165          global $DB, $CFG;
 166  
 167          // NOTE: this is a bit brute force way - it will not trigger events and hooks properly.
 168  
 169          // Nuke all role assignments.
 170          role_unassign_all(array('component'=>'enrol_'.$this->name));
 171  
 172          // Purge participants.
 173          $DB->delete_records_select('user_enrolments', "enrolid IN (SELECT id FROM {enrol} WHERE enrol = ?)", array($this->name));
 174  
 175          // Purge enrol instances.
 176          $DB->delete_records('enrol', array('enrol'=>$this->name));
 177  
 178          // Tweak enrol settings.
 179          if (!empty($CFG->enrol_plugins_enabled)) {
 180              $enabledenrols = explode(',', $CFG->enrol_plugins_enabled);
 181              $enabledenrols = array_unique($enabledenrols);
 182              $enabledenrols = array_flip($enabledenrols);
 183              unset($enabledenrols[$this->name]);
 184              $enabledenrols = array_flip($enabledenrols);
 185              if (is_array($enabledenrols)) {
 186                  set_config('enrol_plugins_enabled', implode(',', $enabledenrols));
 187              }
 188          }
 189  
 190          parent::uninstall_cleanup();
 191      }
 192  }