Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / admin / message.php
blob1178a574f45dd6371cb0f19ca936c42edcb15bb7
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 * Message outputs configuration page
20 * @package message
21 * @copyright 2011 Lancaster University Network Services Limited
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once(__DIR__ . '/../config.php');
25 require_once($CFG->dirroot . '/message/lib.php');
26 require_once($CFG->libdir.'/adminlib.php');
28 // This is an admin page.
29 admin_externalpage_setup('managemessageoutputs');
31 // Fetch processors.
32 $allprocessors = get_message_processors();
33 $processors = array_filter($allprocessors, function($processor) {
34 return $processor->enabled;
35 });
36 $disabledprocessors = array_filter($allprocessors, function($processor) {
37 return !$processor->enabled;
38 });
40 // Fetch message providers.
41 $providers = get_message_providers();
42 // Fetch the manage message outputs interface.
43 $preferences = get_message_output_default_preferences();
45 if (($form = data_submitted()) && confirm_sesskey()) {
46 $newpreferences = array();
47 // Prepare default message outputs settings.
48 foreach ($providers as $provider) {
49 $componentproviderbase = $provider->component.'_'.$provider->name;
50 $disableprovidersetting = $componentproviderbase.'_disable';
51 if (!isset($form->$disableprovidersetting)) {
52 $newpreferences[$disableprovidersetting] = 1;
53 } else {
54 $newpreferences[$disableprovidersetting] = 0;
57 $componentprovidersetting = $componentproviderbase.'_locked';
58 foreach ($processors as $processor) {
59 $value = 0;
60 if (isset($form->{$componentprovidersetting}[$processor->name])) {
61 $value = $form->{$componentprovidersetting}[$processor->name];
62 if ($value == 'on') {
63 $value = 1;
67 // Record the site preference.
68 $newpreferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
71 $componentprovidersetting = $componentproviderbase.'_enabled';
72 $newsettings = [];
73 if (isset($form->$componentprovidersetting)) {
74 // Store defined comma-separated processors as setting value.
75 // Using array_filter eliminates elements set to 0 above.
76 $newsettings = array_keys(array_filter($form->{$componentprovidersetting}));
79 // Let's join existing setting values for disabled processors.
80 $property = 'message_provider_'.$componentprovidersetting;
81 if (property_exists($preferences, $property)) {
82 $existingsetting = $preferences->$property;
83 foreach ($disabledprocessors as $disable) {
84 if (strpos($existingsetting, $disable->name) > -1) {
85 $newsettings[] = $disable->name;
90 $value = join(',', $newsettings);
91 if (empty($value)) {
92 $value = null;
95 // Record the site preference.
96 $newpreferences['message_provider_'.$componentprovidersetting] = $value;
99 // Update database.
100 $transaction = $DB->start_delegated_transaction();
102 // Save processors enabled/disabled status.
103 foreach ($allprocessors as $processor) {
104 $enabled = isset($form->{$processor->name});
105 $class = \core_plugin_manager::resolve_plugininfo_class('message');
106 $class::enable_plugin($processor->name, $enabled);
109 foreach ($newpreferences as $name => $value) {
110 $old = isset($preferences->$name) ? $preferences->$name : '';
112 if ($old != $value) {
113 add_to_config_log($name, $old, $value, 'core');
116 set_config($name, $value, 'message');
118 $transaction->allow_commit();
120 core_plugin_manager::reset_caches();
122 $url = new moodle_url('message.php');
123 redirect($url);
126 // Page settings
127 $PAGE->set_context(context_system::instance());
128 $renderer = $PAGE->get_renderer('core', 'message');
130 // Display the page.
131 echo $OUTPUT->header();
132 echo $renderer->manage_messageoutput_settings($allprocessors, $processors, $providers, $preferences);
133 echo $OUTPUT->footer();