Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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      /**
  82       * Pre-uninstall hook.
  83       *
  84       * This is intended for disabling of plugin, some DB table purging, etc.
  85       *
  86       * NOTE: to be called from uninstall_plugin() only.
  87       */
  88      public function uninstall_cleanup() {
  89          global $DB;
  90          $DB->delete_records_select('customfield_data',
  91              'fieldid IN (SELECT f.id FROM {customfield_field} f WHERE f.type = ?)', [$this->name]);
  92          $DB->delete_records('customfield_field', ['type' => $this->name]);
  93          parent::uninstall_cleanup();
  94      }
  95  
  96      /**
  97       * Setting section name
  98       *
  99       * @return null|string
 100       */
 101      public function get_settings_section_name() {
 102          return 'customfieldsetting' . $this->name;
 103      }
 104  
 105      /**
 106       * Load the global settings for a particular availability plugin (if there are any)
 107       *
 108       * @param \part_of_admin_tree $adminroot
 109       * @param string $parentnodename
 110       * @param bool $hassiteconfig
 111       */
 112      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 113          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 114          $ADMIN = $adminroot; // May be used in settings.php.
 115          $plugininfo = $this; // Also can be used inside settings.php
 116          $availability = $this; // Also to be used inside settings.php.
 117  
 118          if (!$this->is_installed_and_upgraded()) {
 119              return;
 120          }
 121  
 122          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
 123              return;
 124          }
 125  
 126          $section = $this->get_settings_section_name();
 127  
 128          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 129          include($this->full_path('settings.php')); // This may also set $settings to null.
 130  
 131          if ($settings) {
 132              $ADMIN->add($parentnodename, $settings);
 133          }
 134      }
 135  }