2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace core\plugininfo
;
19 use admin_settingpage
;
20 use core_communication\processor
;
21 use core_plugin_manager
;
25 * Class for communication provider.
28 * @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class communication
extends base
{
33 public static function get_manage_url(): ?moodle_url
{
34 if (!\core_communication\api
::is_available()) {
38 return new moodle_url('/admin/settings.php', ['section' => 'managecommunicationproviders']);
41 public function get_settings_section_name(): string {
42 return $this->type
. '_' . $this->name
;
45 public static function enable_plugin(string $pluginname, int $enabled): bool {
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);
55 } else if ($oldvalue != false && !$disabled) {
56 unset_config('disabled', $plugin);
61 add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
62 core_plugin_manager
::reset_caches();
68 public static function get_enabled_plugins(): ?
array {
69 $pluginmanager = core_plugin_manager
::instance();
70 $plugins = $pluginmanager->get_installed_plugins('communication');
76 $plugins = array_keys($plugins);
78 // Filter to return only enabled plugins.
80 foreach ($plugins as $plugin) {
81 $disabled = get_config('communication_' . $plugin, 'disabled');
82 if (empty($disabled)) {
83 $enabled[$plugin] = $plugin;
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.
94 if (!$this->is_installed_and_upgraded()) {
98 if (!$hassiteconfig) {
102 $section = $this->get_settings_section_name();
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.
110 $ADMIN->add($parentnodename, $settings);
114 public function is_uninstall_allowed(): bool {
115 if (in_array($this->name
, \core_plugin_manager
::standard_plugins_list('communication'))) {
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.
125 * @param string $fullpluginname the name of the plugin
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)) {
134 $communicationavailable = $communicationinfo->get_status();
135 return !($communicationavailable === \core_plugin_manager
::PLUGIN_STATUS_MISSING ||
136 !empty(get_config($fullpluginname, 'disabled')));