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  2018 Toni Barbera {@link http://www.moodle.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;
  27  use admin_settingpage;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Class for admin tool plugins
  33   *
  34   * @package    core
  35   * @copyright  2018 Toni Barbera {@link http://www.moodle.org}
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class customfield extends base {
  39  
  40      /**
  41       * Allow uninstall
  42       * @return bool
  43       */
  44      public function is_uninstall_allowed() {
  45          return true;
  46      }
  47  
  48      /**
  49       * Return URL used for management of plugins of this type.
  50       * @return moodle_url
  51       */
  52      public static function get_manage_url() {
  53          return new moodle_url('/admin/settings.php', array('section' => 'managecustomfields'));
  54      }
  55  
  56      /**
  57       * Enabled plugins
  58       * @return array|null
  59       */
  60      public static function get_enabled_plugins() {
  61          global $DB;
  62  
  63          // Get all available plugins.
  64          $plugins = \core_plugin_manager::instance()->get_installed_plugins('customfield');
  65          if (!$plugins) {
  66              return array();
  67          }
  68  
  69          // Check they are enabled using get_config (which is cached and hopefully fast).
  70          $enabled = array();
  71          foreach ($plugins as $plugin => $version) {
  72              $disabled = get_config('customfield_' . $plugin, 'disabled');
  73              if (empty($disabled)) {
  74                  $enabled[$plugin] = $plugin;
  75              }
  76          }
  77  
  78          return $enabled;
  79      }
  80  
  81      public static function enable_plugin(string $pluginname, int $enabled): bool {
  82          $haschanged = false;
  83  
  84          $plugin = 'customfield_' . $pluginname;
  85          $oldvalue = get_config($plugin, 'disabled');
  86          $disabled = !$enabled;
  87          // Only set value if there is no config setting or if the value is different from the previous one.
  88          if ($oldvalue == false && $disabled) {
  89              set_config('disabled', $disabled, $plugin);
  90              $haschanged = true;
  91          } else if ($oldvalue != false && !$disabled) {
  92              unset_config('disabled', $plugin);
  93              $haschanged = true;
  94          }
  95  
  96          if ($haschanged) {
  97              add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
  98              \core_plugin_manager::reset_caches();
  99          }
 100  
 101          return $haschanged;
 102      }
 103  
 104      /**
 105       * Pre-uninstall hook.
 106       *
 107       * This is intended for disabling of plugin, some DB table purging, etc.
 108       *
 109       * NOTE: to be called from uninstall_plugin() only.
 110       */
 111      public function uninstall_cleanup() {
 112          global $DB;
 113          $DB->delete_records_select('customfield_data',
 114              'fieldid IN (SELECT f.id FROM {customfield_field} f WHERE f.type = ?)', [$this->name]);
 115          $DB->delete_records('customfield_field', ['type' => $this->name]);
 116          parent::uninstall_cleanup();
 117      }
 118  
 119      /**
 120       * Setting section name
 121       *
 122       * @return null|string
 123       */
 124      public function get_settings_section_name() {
 125          return 'customfieldsetting' . $this->name;
 126      }
 127  
 128      /**
 129       * Load the global settings for a particular availability plugin (if there are any)
 130       *
 131       * @param \part_of_admin_tree $adminroot
 132       * @param string $parentnodename
 133       * @param bool $hassiteconfig
 134       */
 135      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 136          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 137          $ADMIN = $adminroot; // May be used in settings.php.
 138          $plugininfo = $this; // Also can be used inside settings.php
 139          $availability = $this; // Also to be used inside settings.php.
 140  
 141          if (!$this->is_installed_and_upgraded()) {
 142              return;
 143          }
 144  
 145          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
 146              return;
 147          }
 148  
 149          $section = $this->get_settings_section_name();
 150  
 151          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 152          include($this->full_path('settings.php')); // This may also set $settings to null.
 153  
 154          if ($settings) {
 155              $ADMIN->add($parentnodename, $settings);
 156          }
 157      }
 158  }