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.
   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 core\plugininfo;
  18  
  19  use admin_settingpage;
  20  use core_communication\processor;
  21  use core_plugin_manager;
  22  use moodle_url;
  23  
  24  /**
  25   * Class for communication provider.
  26   *
  27   * @package    core
  28   * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class communication extends base {
  32  
  33      public static function get_manage_url(): ?moodle_url {
  34          if (!\core_communication\api::is_available()) {
  35              return null;
  36          }
  37  
  38          return new moodle_url('/admin/settings.php', ['section' => 'managecommunicationproviders']);
  39      }
  40  
  41      public function get_settings_section_name(): string {
  42          return $this->type . '_' . $this->name;
  43      }
  44  
  45      public static function enable_plugin(string $pluginname, int $enabled): bool {
  46          $haschanged = false;
  47  
  48          $plugin = 'communication_' . $pluginname;
  49          $oldvalue = get_config($plugin, 'disabled');
  50          $disabled = !$enabled;
  51          // Only set value if there is no config setting or if the value is different from the previous one.
  52          if ($oldvalue == false && $disabled) {
  53              set_config('disabled', $disabled, $plugin);
  54              $haschanged = true;
  55          } else if ($oldvalue != false && !$disabled) {
  56              unset_config('disabled', $plugin);
  57              $haschanged = true;
  58          }
  59  
  60          if ($haschanged) {
  61              add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
  62              core_plugin_manager::reset_caches();
  63          }
  64  
  65          return $haschanged;
  66      }
  67  
  68      public static function get_enabled_plugins(): ?array {
  69          $pluginmanager = core_plugin_manager::instance();
  70          $plugins = $pluginmanager->get_installed_plugins('communication');
  71  
  72          if (!$plugins) {
  73              return [];
  74          }
  75  
  76          $plugins = array_keys($plugins);
  77  
  78          // Filter to return only enabled plugins.
  79          $enabled = [];
  80          foreach ($plugins as $plugin) {
  81              $disabled = get_config('communication_' . $plugin, 'disabled');
  82              if (empty($disabled)) {
  83                  $enabled[$plugin] = $plugin;
  84              }
  85          }
  86          return $enabled;
  87      }
  88  
  89      public function load_settings(\part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  90          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  91          $ADMIN = $adminroot; // May be used in settings.php.
  92          $plugininfo = $this;      // Also can be used inside settings.php.
  93  
  94          if (!$this->is_installed_and_upgraded()) {
  95              return;
  96          }
  97  
  98          if (!$hassiteconfig) {
  99              return;
 100          }
 101  
 102          $section = $this->get_settings_section_name();
 103          $settings = null;
 104          if (file_exists($this->full_path('settings.php'))) {
 105              $settings = new admin_settingpage($section, $this->displayname,
 106                  'moodle/site:config', $this->is_enabled() === false);
 107              include($this->full_path('settings.php')); // This may also set $settings to null.
 108          }
 109          if ($settings) {
 110              $ADMIN->add($parentnodename, $settings);
 111          }
 112      }
 113  
 114      public function is_uninstall_allowed(): bool {
 115          if (in_array($this->name, \core_plugin_manager::standard_plugins_list('communication'))) {
 116              return false;
 117          }
 118          return true;
 119      }
 120  
 121      /**
 122       * Checks if a communication plugin is ready to be used.
 123       * It checks the plugin status as well as the plugin is missing or not.
 124       *
 125       * @param string $fullpluginname the name of the plugin
 126       * @return bool
 127       */
 128      public static function is_plugin_enabled($fullpluginname): bool {
 129          $pluginmanager = \core_plugin_manager::instance();
 130          $communicationinfo = $pluginmanager->get_plugin_info($fullpluginname);
 131          if (empty($communicationinfo)) {
 132              return false;
 133          }
 134          $communicationavailable = $communicationinfo->get_status();
 135          return !($communicationavailable === \core_plugin_manager::PLUGIN_STATUS_MISSING ||
 136              !empty(get_config($fullpluginname, 'disabled')));
 137      }
 138  
 139  }