weekly release 5.0dev
[moodle.git] / lib / classes / plugininfo / communication.php
blobe198301134cdeee2f0996c3d28ac843fd6251c8b
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.
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;
22 use moodle_url;
24 /**
25 * Class for communication provider.
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
31 class communication extends base {
33 public static function get_manage_url(): ?moodle_url {
34 if (!\core_communication\api::is_available()) {
35 return null;
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 {
46 $haschanged = false;
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;
60 if ($haschanged) {
61 add_to_config_log('disabled', $oldvalue, $disabled, $plugin);
62 core_plugin_manager::reset_caches();
65 return $haschanged;
68 public static function get_enabled_plugins(): ?array {
69 $pluginmanager = core_plugin_manager::instance();
70 $plugins = $pluginmanager->get_installed_plugins('communication');
72 if (!$plugins) {
73 return [];
76 $plugins = array_keys($plugins);
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;
86 return $enabled;
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()) {
95 return;
98 if (!$hassiteconfig) {
99 return;
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.
109 if ($settings) {
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'))) {
116 return false;
118 return true;
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
126 * @return bool
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;
134 $communicationavailable = $communicationinfo->get_status();
135 return !($communicationavailable === \core_plugin_manager::PLUGIN_STATUS_MISSING ||
136 !empty(get_config($fullpluginname, 'disabled')));