Merge branch 'MDL-32084_master' of git://github.com/lazydaisy/moodle
[moodle.git] / message / defaultoutputs.php
blob4d94cc4cd316485eea355e14e540b533cb761735
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(dirname(__FILE__) . '/../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', get_context_instance(CONTEXT_SYSTEM));
34 // Fetch processors
35 $processors = get_message_processors(true);
36 // Fetch message providers
37 $providers = $DB->get_records('message_providers', null, 'name');
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 foreach (array('permitted', 'loggedin', 'loggedoff') as $setting){
45 $value = null;
46 $componentprovidersetting = $componentproviderbase.'_'.$setting;
47 if ($setting == 'permitted') {
48 // if we deal with permitted select element, we need to create individual
49 // setting for each possible processor. Note that this block will
50 // always be processed first after entring parental foreach iteration
51 // so we can change form values on this stage.
52 foreach($processors as $processor) {
53 $value = '';
54 if (isset($form->{$componentprovidersetting}[$processor->name])) {
55 $value = $form->{$componentprovidersetting}[$processor->name];
57 // Ensure that loggedin loggedoff options are set correctly
58 // for this permission
59 if ($value == 'forced') {
60 $form->{$componentproviderbase.'_loggedin'}[$processor->name] = 1;
61 $form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 1;
62 } else if ($value == 'disallowed') {
63 // It might be better to unset them, but I can't figure out why that cause error
64 $form->{$componentproviderbase.'_loggedin'}[$processor->name] = 0;
65 $form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 0;
67 // record the site preference
68 $preferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
70 } else if (array_key_exists($componentprovidersetting, $form)) {
71 // we must be processing loggedin or loggedoff checkboxes. Store
72 // defained comma-separated processors as setting value.
73 // Using array_filter eliminates elements set to 0 above
74 $value = join(',', array_keys(array_filter($form->{$componentprovidersetting})));
75 if (empty($value)) {
76 $value = null;
79 if ($setting != 'permitted') {
80 // we have already recoded site preferences for 'permitted' type
81 $preferences['message_provider_'.$componentprovidersetting] = $value;
86 // Update database
87 $transaction = $DB->start_delegated_transaction();
88 foreach ($preferences as $name => $value) {
89 set_config($name, $value, 'message');
91 $transaction->allow_commit();
93 // Redirect
94 $url = new moodle_url('defaultoutputs.php');
95 redirect($url);
100 // Page settings
101 $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
102 $PAGE->requires->js_init_call('M.core_message.init_defaultoutputs');
104 // Grab the renderer
105 $renderer = $PAGE->get_renderer('core', 'message');
107 // Display the manage message outputs interface
108 $preferences = get_message_output_default_preferences();
109 $messageoutputs = $renderer->manage_defaultmessageoutputs($processors, $providers, $preferences);
111 // Display the page
112 echo $OUTPUT->header();
113 echo $OUTPUT->heading(get_string('defaultmessageoutputs', 'message'));
114 echo $messageoutputs;
115 echo $OUTPUT->footer();