| 1 | efrain | 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.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 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/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | /**
 | 
        
           |  |  | 18 |  * Message outputs configuration page
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 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
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 | require_once(__DIR__ . '/../config.php');
 | 
        
           |  |  | 25 | require_once($CFG->dirroot . '/message/lib.php');
 | 
        
           |  |  | 26 | require_once($CFG->libdir.'/adminlib.php');
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | // This is an admin page.
 | 
        
           |  |  | 29 | admin_externalpage_setup('managemessageoutputs');
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 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 | });
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | // Fetch message providers.
 | 
        
           |  |  | 41 | $providers = get_message_providers();
 | 
        
           |  |  | 42 | // Fetch the manage message outputs interface.
 | 
        
           |  |  | 43 | $preferences = get_message_output_default_preferences();
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 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;
 | 
        
           |  |  | 55 |         }
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 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;
 | 
        
           |  |  | 64 |                 }
 | 
        
           |  |  | 65 |             }
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |             // Record the site preference.
 | 
        
           |  |  | 68 |             $newpreferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
 | 
        
           |  |  | 69 |         }
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 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}));
 | 
        
           |  |  | 77 |         }
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 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;
 | 
        
           |  |  | 86 |                 }
 | 
        
           |  |  | 87 |             }
 | 
        
           |  |  | 88 |         }
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |         $value = join(',', $newsettings);
 | 
        
           |  |  | 91 |         if (empty($value)) {
 | 
        
           |  |  | 92 |             $value = null;
 | 
        
           |  |  | 93 |         }
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |         // Record the site preference.
 | 
        
           |  |  | 96 |         $newpreferences['message_provider_'.$componentprovidersetting] = $value;
 | 
        
           |  |  | 97 |     }
 | 
        
           |  |  | 98 |   | 
        
           |  |  | 99 |     // Update database.
 | 
        
           |  |  | 100 |     $transaction = $DB->start_delegated_transaction();
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 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);
 | 
        
           |  |  | 107 |     }
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 |     foreach ($newpreferences as $name => $value) {
 | 
        
           |  |  | 110 |         $old = isset($preferences->$name) ? $preferences->$name : '';
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |         if ($old != $value) {
 | 
        
           |  |  | 113 |             add_to_config_log($name, $old, $value, 'core');
 | 
        
           |  |  | 114 |         }
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 |         set_config($name, $value, 'message');
 | 
        
           |  |  | 117 |     }
 | 
        
           |  |  | 118 |     $transaction->allow_commit();
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |     core_plugin_manager::reset_caches();
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |     $url = new moodle_url('message.php');
 | 
        
           |  |  | 123 |     redirect($url);
 | 
        
           |  |  | 124 | }
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 | // Page settings
 | 
        
           |  |  | 127 | $PAGE->set_context(context_system::instance());
 | 
        
           |  |  | 128 | $renderer = $PAGE->get_renderer('core', 'message');
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 | // Display the page.
 | 
        
           |  |  | 131 | echo $OUTPUT->header();
 | 
        
           |  |  | 132 | echo $renderer->manage_messageoutput_settings($allprocessors, $processors, $providers, $preferences);
 | 
        
           |  |  | 133 | echo $OUTPUT->footer();
 |