Merge branch 'MDL-55980-master' of https://github.com/sammarshallou/moodle
[moodle.git] / message / defaultoutputs.php
blob398fe25fcef08558fd41699187685be933918583
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 * Default message outputs configuration page
20 * @package core_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('defaultmessageoutputs');
31 // Require site configuration capability
32 require_capability('moodle/site:config', context_system::instance());
34 // Fetch processors
35 $processors = get_message_processors(true);
36 // Fetch message providers
37 $providers = get_message_providers();
39 if (($form = data_submitted()) && confirm_sesskey()) {
40 $preferences = array();
41 // Prepare default message outputs settings
42 foreach ( $providers as $provider) {
43 $componentproviderbase = $provider->component.'_'.$provider->name;
44 $disableprovidersetting = $componentproviderbase.'_disable';
45 $providerdisabled = false;
46 if (!isset($form->$disableprovidersetting)) {
47 $providerdisabled = true;
48 $preferences[$disableprovidersetting] = 1;
49 } else {
50 $preferences[$disableprovidersetting] = 0;
53 foreach (array('permitted', 'loggedin', 'loggedoff') as $setting){
54 $value = null;
55 $componentprovidersetting = $componentproviderbase.'_'.$setting;
56 if ($setting == 'permitted') {
57 // if we deal with permitted select element, we need to create individual
58 // setting for each possible processor. Note that this block will
59 // always be processed first after entring parental foreach iteration
60 // so we can change form values on this stage.
61 foreach($processors as $processor) {
62 $value = '';
63 if (isset($form->{$componentprovidersetting}[$processor->name])) {
64 $value = $form->{$componentprovidersetting}[$processor->name];
66 // Ensure that loggedin loggedoff options are set correctly
67 // for this permission
68 if (($value == 'disallowed') || $providerdisabled) {
69 // It might be better to unset them, but I can't figure out why that cause error
70 $form->{$componentproviderbase.'_loggedin'}[$processor->name] = 0;
71 $form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 0;
72 } else if ($value == 'forced') {
73 $form->{$componentproviderbase.'_loggedin'}[$processor->name] = 1;
74 $form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 1;
76 // record the site preference
77 $preferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
79 } else if (array_key_exists($componentprovidersetting, $form)) {
80 // we must be processing loggedin or loggedoff checkboxes. Store
81 // defained comma-separated processors as setting value.
82 // Using array_filter eliminates elements set to 0 above
83 $value = join(',', array_keys(array_filter($form->{$componentprovidersetting})));
84 if (empty($value)) {
85 $value = null;
88 if ($setting != 'permitted') {
89 // we have already recoded site preferences for 'permitted' type
90 $preferences['message_provider_'.$componentprovidersetting] = $value;
95 // Update database
96 $transaction = $DB->start_delegated_transaction();
97 foreach ($preferences as $name => $value) {
98 set_config($name, $value, 'message');
100 $transaction->allow_commit();
102 // Redirect
103 $url = new moodle_url('defaultoutputs.php');
104 redirect($url);
109 // Page settings
110 $PAGE->set_context(context_system::instance());
111 $PAGE->requires->js_init_call('M.core_message.init_defaultoutputs');
113 // Grab the renderer
114 $renderer = $PAGE->get_renderer('core', 'message');
116 // Display the manage message outputs interface
117 $preferences = get_message_output_default_preferences();
118 $messageoutputs = $renderer->manage_defaultmessageoutputs($processors, $providers, $preferences);
120 // Display the page
121 echo $OUTPUT->header();
122 echo $OUTPUT->heading(get_string('defaultmessageoutputs', 'message'));
123 echo $messageoutputs;
124 echo $OUTPUT->footer();