Merge branch 'MDL-69102-310' of https://github.com/martygilbert/moodle into MOODLE_31...
[moodle.git] / admin / message.php
blobb5c31d7cee706adcaa7d5df66857e8634df14d42
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 $providerdisabled = false;
52 if (!isset($form->$disableprovidersetting)) {
53 $providerdisabled = true;
54 $newpreferences[$disableprovidersetting] = 1;
55 } else {
56 $newpreferences[$disableprovidersetting] = 0;
59 foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
60 $value = null;
61 $componentprovidersetting = $componentproviderbase.'_'.$setting;
62 if ($setting == 'permitted') {
63 // If we deal with permitted select element, we need to create individual
64 // setting for each possible processor. Note that this block will
65 // always be processed first after entring parental foreach iteration
66 // so we can change form values on this stage.
67 foreach ($processors as $processor) {
68 $value = '';
69 if (isset($form->{$componentprovidersetting}[$processor->name])) {
70 $value = $form->{$componentprovidersetting}[$processor->name];
72 // Ensure that loggedin loggedoff options are set correctly for this permission.
73 if (($value == 'disallowed') || $providerdisabled) {
74 // It might be better to unset them, but I can't figure out why that cause error.
75 $form->{$componentproviderbase.'_loggedin'}[$processor->name] = 0;
76 $form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 0;
77 } else if ($value == 'forced') {
78 $form->{$componentproviderbase.'_loggedin'}[$processor->name] = 1;
79 $form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 1;
81 // Record the site preference.
82 $newpreferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
84 } else {
85 $newsettings = array();
86 if (property_exists($form, $componentprovidersetting)) {
87 // We must be processing loggedin or loggedoff checkboxes.
88 // Store defained comma-separated processors as setting value.
89 // Using array_filter eliminates elements set to 0 above.
90 $newsettings = array_keys(array_filter($form->{$componentprovidersetting}));
93 // Let's join existing setting values for disabled processors.
94 $property = 'message_provider_'.$componentprovidersetting;
95 if (property_exists($preferences, $property)) {
96 $existingsetting = $preferences->$property;
97 foreach ($disabledprocessors as $disable) {
98 if (strpos($existingsetting, $disable->name) > -1) {
99 $newsettings[] = $disable->name;
104 $value = join(',', $newsettings);
105 if (empty($value)) {
106 $value = null;
109 if ($setting != 'permitted') {
110 // We have already recoded site preferences for 'permitted' type.
111 $newpreferences['message_provider_'.$componentprovidersetting] = $value;
116 // Update database.
117 $transaction = $DB->start_delegated_transaction();
119 // Save processors enabled/disabled status.
120 foreach ($allprocessors as $processor) {
121 $enabled = isset($form->{$processor->name});
122 \core_message\api::update_processor_status($processor, $enabled);
125 foreach ($newpreferences as $name => $value) {
126 set_config($name, $value, 'message');
128 $transaction->allow_commit();
130 core_plugin_manager::reset_caches();
132 $url = new moodle_url('message.php');
133 redirect($url);
136 // Page settings
137 $PAGE->set_context(context_system::instance());
138 $PAGE->requires->js_init_call('M.core_message.init_defaultoutputs');
140 $renderer = $PAGE->get_renderer('core', 'message');
142 // Display the page.
143 echo $OUTPUT->header();
144 echo $renderer->manage_messageoutput_settings($allprocessors, $processors, $providers, $preferences);
145 echo $OUTPUT->footer();