Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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 function get_settings_section_name() {
  50          if (file_exists($this->full_path('settings.php'))) {
  51              return 'enrolsettings' . $this->name;
  52          } else {
  53              return null;
  54          }
  55      }
  56  
  57      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  58          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  59          $ADMIN = $adminroot; // May be used in settings.php.
  60          $plugininfo = $this; // Also can be used inside settings.php.
  61          $enrol = $this;      // Also can be used inside settings.php.
  62  
  63          if (!$this->is_installed_and_upgraded()) {
  64              return;
  65          }
  66  
  67          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
  68              return;
  69          }
  70  
  71          $section = $this->get_settings_section_name();
  72  
  73          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
  74  
  75          include($this->full_path('settings.php')); // This may also set $settings to null!
  76  
  77          if ($settings) {
  78              $ADMIN->add($parentnodename, $settings);
  79          }
  80      }
  81  
  82      public function is_uninstall_allowed() {
  83          if ($this->name === 'manual') {
  84              return false;
  85          }
  86          return true;
  87      }
  88  
  89      /**
  90       * Return URL used for management of plugins of this type.
  91       * @return moodle_url
  92       */
  93      public static function get_manage_url() {
  94          return new moodle_url('/admin/settings.php', array('section'=>'manageenrols'));
  95      }
  96  
  97      /**
  98       * Return warning with number of activities and number of affected courses.
  99       *
 100       * @return string
 101       */
 102      public function get_uninstall_extra_warning() {
 103          global $DB, $OUTPUT;
 104  
 105          $sql = "SELECT COUNT('x')
 106                    FROM {user_enrolments} ue
 107                    JOIN {enrol} e ON e.id = ue.enrolid
 108                   WHERE e.enrol = :plugin";
 109          $count = $DB->count_records_sql($sql, array('plugin'=>$this->name));
 110  
 111          if (!$count) {
 112              return '';
 113          }
 114  
 115          $migrateurl = new moodle_url('/admin/enrol.php', array('action'=>'migrate', 'enrol'=>$this->name, 'sesskey'=>sesskey()));
 116          $migrate = new \single_button($migrateurl, get_string('migratetomanual', 'core_enrol'));
 117          $button = $OUTPUT->render($migrate);
 118  
 119          $result = '<p>'.get_string('uninstallextraconfirmenrol', 'core_plugin', array('enrolments'=>$count)).'</p>';
 120          $result .= $button;
 121  
 122          return $result;
 123      }
 124  
 125      /**
 126       * Pre-uninstall hook.
 127       *
 128       * This is intended for disabling of plugin, some DB table purging, etc.
 129       *
 130       * NOTE: to be called from uninstall_plugin() only.
 131       * @private
 132       */
 133      public function uninstall_cleanup() {
 134          global $DB, $CFG;
 135  
 136          // NOTE: this is a bit brute force way - it will not trigger events and hooks properly.
 137  
 138          // Nuke all role assignments.
 139          role_unassign_all(array('component'=>'enrol_'.$this->name));
 140  
 141          // Purge participants.
 142          $DB->delete_records_select('user_enrolments', "enrolid IN (SELECT id FROM {enrol} WHERE enrol = ?)", array($this->name));
 143  
 144          // Purge enrol instances.
 145          $DB->delete_records('enrol', array('enrol'=>$this->name));
 146  
 147          // Tweak enrol settings.
 148          if (!empty($CFG->enrol_plugins_enabled)) {
 149              $enabledenrols = explode(',', $CFG->enrol_plugins_enabled);
 150              $enabledenrols = array_unique($enabledenrols);
 151              $enabledenrols = array_flip($enabledenrols);
 152              unset($enabledenrols[$this->name]);
 153              $enabledenrols = array_flip($enabledenrols);
 154              if (is_array($enabledenrols)) {
 155                  set_config('enrol_plugins_enabled', implode(',', $enabledenrols));
 156              }
 157          }
 158  
 159          parent::uninstall_cleanup();
 160      }
 161  }