weekly release 4.5dev
[moodle.git] / lib / classes / plugininfo / message.php
blob743b3adb07f45f58530c5540807cc27a23011e4f
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 /**
18 * Defines classes used for plugin info.
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
24 namespace core\plugininfo;
26 use admin_settingpage;
27 use moodle_url;
28 use part_of_admin_tree;
30 /**
31 * Class for messaging processors
33 class message extends base {
35 public static function plugintype_supports_disabling(): bool {
36 return true;
39 /**
40 * Finds all enabled plugins, the result may include missing plugins.
41 * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
43 public static function get_enabled_plugins() {
44 global $DB;
45 return $DB->get_records_menu('message_processors', array('enabled'=>1), 'name ASC', 'name, name AS val');
48 public static function enable_plugin(string $pluginname, int $enabled): bool {
49 global $DB;
51 if (!$plugin = $DB->get_record('message_processors', ['name' => $pluginname])) {
52 throw new \moodle_exception('invalidplugin', 'message', '', $pluginname);
55 $haschanged = false;
57 // Only set visibility if it's different from the current value.
58 if ($plugin->enabled != $enabled) {
59 $haschanged = true;
60 $processor = \core_message\api::get_processed_processor_object($plugin);
62 // Include this information into config changes table.
63 add_to_config_log($processor->name, $processor->enabled, $enabled, 'core');
65 // Save processor enabled/disabled status.
66 \core_message\api::update_processor_status($processor, $enabled);
69 return $haschanged;
72 public function get_settings_section_name() {
73 return 'messagesetting' . $this->name;
76 public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
77 global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
78 /** @var \admin_root $ADMIN */
79 $ADMIN = $adminroot; // May be used in settings.php.
80 $plugininfo = $this; // Also can be used inside settings.php.
82 if (!$this->is_installed_and_upgraded()) {
83 return;
86 if (!$hassiteconfig) {
87 return;
89 $section = $this->get_settings_section_name();
91 $settings = null;
92 $processors = get_message_processors();
93 if (isset($processors[$this->name])) {
94 $processor = $processors[$this->name];
95 if ($processor->available && $processor->hassettings) {
96 $settings = new admin_settingpage($section, $this->displayname,
97 'moodle/site:config', $this->is_enabled() === false);
98 include($this->full_path('settings.php')); // This may also set $settings to null.
101 if ($settings) {
102 $ADMIN->add($parentnodename, $settings);
107 * Return URL used for management of plugins of this type.
108 * @return moodle_url
110 public static function get_manage_url() {
111 return new moodle_url('/admin/message.php');
114 public function is_uninstall_allowed() {
115 return true;
119 * Pre-uninstall hook.
121 * This is intended for disabling of plugin, some DB table purging, etc.
123 * NOTE: to be called from uninstall_plugin() only.
124 * @private
126 public function uninstall_cleanup() {
127 global $CFG;
129 require_once($CFG->libdir.'/messagelib.php');
130 message_processor_uninstall($this->name);
132 parent::uninstall_cleanup();