Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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  2011 David Mudrak <david@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core\plugininfo;
  25  
  26  use admin_settingpage;
  27  use part_of_admin_tree;
  28  
  29  /**
  30   * Class for webservice protocols
  31   */
  32  class webservice extends base {
  33  
  34      public static function plugintype_supports_disabling(): bool {
  35          return true;
  36      }
  37  
  38      /**
  39       * Finds all enabled plugins, the result may include missing plugins.
  40       * @return array of enabled plugins $pluginname => $pluginname
  41       */
  42      public static function get_enabled_plugins() {
  43          global $CFG;
  44  
  45          if (empty($CFG->enablewebservices) or empty($CFG->webserviceprotocols)) {
  46              return array();
  47          }
  48  
  49          $enabled = array();
  50          foreach (explode(',', $CFG->webserviceprotocols) as $protocol) {
  51              $enabled[$protocol] = $protocol;
  52          }
  53  
  54          return $enabled;
  55      }
  56  
  57      public static function enable_plugin(string $pluginname, int $enabled): bool {
  58          global $CFG;
  59  
  60          $haschanged = false;
  61          $plugins = [];
  62          if (!empty($CFG->webserviceprotocols)) {
  63              $plugins = array_flip(explode(',', $CFG->webserviceprotocols));
  64          }
  65  
  66          // Remove plugins that are no longer available.
  67          $availablews = \core_component::get_plugin_list('webservice');
  68          foreach ($plugins as $key => $notused) {
  69              if (empty($availablews[$key])) {
  70                  unset($plugins[$key]);
  71              }
  72          }
  73  
  74          // Only set visibility if it's different from the current value.
  75          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  76              $plugins[$pluginname] = $pluginname;
  77              $haschanged = true;
  78          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
  79              unset($plugins[$pluginname]);
  80              $haschanged = true;
  81          }
  82  
  83          if ($haschanged) {
  84              $new = implode(',', array_flip($plugins));
  85              add_to_config_log('webserviceprotocols', $CFG->webserviceprotocols ?? '', $new, 'core');
  86              set_config('webserviceprotocols', $new);
  87              // Reset caches.
  88              \core_plugin_manager::reset_caches();
  89          }
  90  
  91          return $haschanged;
  92      }
  93  
  94      public function get_settings_section_name() {
  95          return 'webservicesetting' . $this->name;
  96      }
  97  
  98      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  99          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 100          /** @var \admin_root $ADMIN */
 101          $ADMIN = $adminroot; // May be used in settings.php.
 102          $plugininfo = $this; // Also can be used inside settings.php.
 103          $webservice = $this; // Also can be used inside settings.php.
 104  
 105          if (!$this->is_installed_and_upgraded()) {
 106              return;
 107          }
 108  
 109          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
 110              return;
 111          }
 112  
 113          $section = $this->get_settings_section_name();
 114  
 115          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 116          include($this->full_path('settings.php')); // This may also set $settings to null.
 117  
 118          if ($settings) {
 119              $ADMIN->add($parentnodename, $settings);
 120          }
 121      }
 122  
 123      public function is_uninstall_allowed() {
 124          return true;
 125      }
 126  }