Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 401 and 402]

   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  namespace editor_tiny\plugininfo;
  18  
  19  use moodle_url;
  20  
  21  /**
  22   * Subplugin info class.
  23   *
  24   * @package     editor_tiny
  25   * @copyright   2022 Andrew Lyons <andrew@nicols.co.uk>
  26   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class tiny extends \core\plugininfo\base {
  29  
  30      public static function plugintype_supports_disabling(): bool {
  31          return true;
  32      }
  33  
  34      /**
  35       * These subplugins can be uninstalled.
  36       *
  37       * @return bool
  38       */
  39      public function is_uninstall_allowed(): bool {
  40          return true;
  41      }
  42  
  43      /**
  44       * Return URL used for management of plugins of this type.
  45       *
  46       * @return moodle_url
  47       */
  48      public static function get_manage_url(): moodle_url {
  49          return new moodle_url('/admin/settings.php', [
  50              'section' => 'editorsettingstiny',
  51          ]);
  52      }
  53  
  54      /**
  55       * Include the settings.php file from subplugins if provided.
  56       *
  57       * This is a copy of very similar implementations from various other subplugin areas.
  58       *
  59       * @param \part_of_admin_tree $adminroot
  60       * @param string $parentnodename
  61       * @param bool $hassiteconfig whether the current user has moodle/site:config capability
  62       */
  63      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig): void {
  64          // In case settings.php wants to refer to them.
  65          global $CFG, $USER, $DB, $OUTPUT, $PAGE;
  66  
  67          /** @var \admin_root $ADMIN */
  68          $ADMIN = $adminroot; // May be used in settings.php.
  69          $plugininfo = $this; // Also can be used inside settings.php.
  70  
  71          if (!$this->is_installed_and_upgraded()) {
  72              return;
  73          }
  74  
  75          if (!$hassiteconfig || !file_exists($this->full_path('settings.php'))) {
  76              return;
  77          }
  78  
  79          $section = $this->get_settings_section_name();
  80          $settings = new \admin_settingpage(
  81              $section,
  82              $this->displayname,
  83              'moodle/site:config',
  84              $this->is_enabled() === false
  85          );
  86  
  87          // This may also set $settings to null.
  88          include($this->full_path('settings.php'));
  89  
  90          if ($settings) {
  91              $ADMIN->add($parentnodename, $settings);
  92          }
  93      }
  94  
  95      /**
  96       * Get the settings section name.
  97       * This is used to get the setting links in the Tiny sub-plugins table.
  98       *
  99       * @return null|string the settings section name.
 100       */
 101      public function get_settings_section_name(): ?string {
 102          if (!file_exists($this->full_path('settings.php'))) {
 103              return null;
 104          }
 105  
 106          return "tiny_{$this->name}_settings";
 107      }
 108  
 109      public static function get_enabled_plugins(): array {
 110          $pluginmanager = \core_plugin_manager::instance();
 111          $plugins = $pluginmanager->get_installed_plugins('tiny');
 112  
 113          if (!$plugins) {
 114              return [];
 115          }
 116  
 117          // Filter to return only enabled plugins.
 118          $enabled = [];
 119          foreach (array_keys($plugins) as $pluginname) {
 120              $disabled = get_config("tiny_{$pluginname}", 'disabled');
 121              if (empty($disabled)) {
 122                  $enabled[$pluginname] = $pluginname;
 123              }
 124          }
 125          return $enabled;
 126      }
 127  
 128      public static function enable_plugin(string $plugin, int $enabled): bool {
 129          $pluginname = "tiny_{$plugin}";
 130  
 131          $oldvalue = !empty(get_config($pluginname, 'disabled'));
 132          $disabled = empty($enabled);
 133          $haschanged = false;
 134  
 135          // Only set value if there is no config setting or if the value is different from the previous one.
 136          if (!$oldvalue && $disabled) {
 137              set_config('disabled', $disabled, $pluginname);
 138              $haschanged = true;
 139          } else if ($oldvalue && !$disabled) {
 140              unset_config('disabled', $pluginname);
 141              $haschanged = true;
 142          }
 143  
 144          if ($haschanged) {
 145              add_to_config_log('disabled', $oldvalue, $disabled, $pluginname);
 146              \core_plugin_manager::reset_caches();
 147          }
 148  
 149          return $haschanged;
 150      }
 151  }