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