| 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 |  * Contains renderer objects for messaging
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 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
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | /**
 | 
        
           |  |  | 28 |  * message Renderer
 | 
        
           |  |  | 29 |  *
 | 
        
           |  |  | 30 |  * Class for rendering various message objects
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @package    core_message
 | 
        
           |  |  | 33 |  * @subpackage message
 | 
        
           |  |  | 34 |  * @copyright  2011 Lancaster University Network Services Limited
 | 
        
           |  |  | 35 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 36 |  */
 | 
        
           |  |  | 37 | class core_message_renderer extends plugin_renderer_base {
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /**
 | 
        
           |  |  | 40 |      * Display the interface to manage both message outputs and default message outputs
 | 
        
           |  |  | 41 |      *
 | 
        
           |  |  | 42 |      * @param  array $allprocessors  array of objects containing all message processors
 | 
        
           |  |  | 43 |      * @param  array $processors  array of objects containing active message processors
 | 
        
           |  |  | 44 |      * @param  array $providers   array of objects containing message providers
 | 
        
           |  |  | 45 |      * @param  stdClass $preferences object containing current preferences
 | 
        
           |  |  | 46 |      * @return string The text to render
 | 
        
           |  |  | 47 |      */
 | 
        
           |  |  | 48 |     public function manage_messageoutput_settings($allprocessors, $processors, $providers, $preferences) {
 | 
        
           |  |  | 49 |         $output = html_writer::start_tag('form', array('id' => 'defaultmessageoutputs', 'method' => 'post'));
 | 
        
           |  |  | 50 |         $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |         // Add message output processors enabled/disabled and settings.
 | 
        
           |  |  | 53 |         $output .= $this->heading(get_string('messageoutputs', 'message'));
 | 
        
           |  |  | 54 |         $output .= $this->manage_messageoutputs($allprocessors);
 | 
        
           |  |  | 55 |   | 
        
           | 1441 | ariadna | 56 |         $output .= html_writer::start_tag('div', ['class' => 'form-buttons mb-3']);
 | 
        
           | 1 | efrain | 57 |         $output .= html_writer::empty_tag('input',
 | 
        
           |  |  | 58 |             array('type' => 'submit', 'value' => get_string('savechanges', 'admin'), 'class' => 'form-submit btn btn-primary')
 | 
        
           |  |  | 59 |         );
 | 
        
           |  |  | 60 |         $output .= html_writer::end_tag('div');
 | 
        
           |  |  | 61 |         $output .= html_writer::end_tag('form');
 | 
        
           |  |  | 62 |   | 
        
           | 1441 | ariadna | 63 |         // Add active message output processors settings.
 | 
        
           |  |  | 64 |         $output .= $this->manage_defaultmessageoutputs($processors, $providers, $preferences);
 | 
        
           |  |  | 65 |   | 
        
           | 1 | efrain | 66 |         return $output;
 | 
        
           |  |  | 67 |     }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |     /**
 | 
        
           |  |  | 70 |      * Display the interface to manage message outputs
 | 
        
           |  |  | 71 |      *
 | 
        
           |  |  | 72 |      * @param  array  $processors array of objects containing message processors
 | 
        
           |  |  | 73 |      * @return string The text to render
 | 
        
           |  |  | 74 |      */
 | 
        
           |  |  | 75 |     public function manage_messageoutputs($processors) {
 | 
        
           |  |  | 76 |         // Display the current workflows
 | 
        
           |  |  | 77 |         $table = new html_table();
 | 
        
           |  |  | 78 |         $table->attributes['class'] = 'admintable generaltable';
 | 
        
           |  |  | 79 |         $table->data        = array();
 | 
        
           |  |  | 80 |         $table->head        = array(
 | 
        
           |  |  | 81 |             get_string('name'),
 | 
        
           |  |  | 82 |             get_string('enable'),
 | 
        
           |  |  | 83 |             get_string('settings'),
 | 
        
           |  |  | 84 |         );
 | 
        
           |  |  | 85 |         $table->colclasses = array(
 | 
        
           |  |  | 86 |             'displayname', 'availability text-center', 'settings',
 | 
        
           |  |  | 87 |         );
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |         foreach ($processors as $processor) {
 | 
        
           |  |  | 90 |             $row = new html_table_row();
 | 
        
           |  |  | 91 |             $row->attributes['class'] = 'messageoutputs';
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |             $name = new html_table_cell(get_string('pluginname', 'message_'.$processor->name));
 | 
        
           |  |  | 94 |             $enable = new html_table_cell();
 | 
        
           |  |  | 95 |             if (!$processor->available) {
 | 
        
           |  |  | 96 |                 $enable->text = html_writer::nonempty_tag('span', get_string('outputnotavailable', 'message'),
 | 
        
           |  |  | 97 |                     array('class' => 'error')
 | 
        
           |  |  | 98 |                 );
 | 
        
           |  |  | 99 |             } else {
 | 
        
           |  |  | 100 |                 $enable->text = html_writer::checkbox($processor->name, $processor->id, $processor->enabled, '',
 | 
        
           |  |  | 101 |                     array('id' => $processor->name)
 | 
        
           |  |  | 102 |                 );
 | 
        
           |  |  | 103 |             }
 | 
        
           |  |  | 104 |             // Settings
 | 
        
           |  |  | 105 |             $settings = new html_table_cell();
 | 
        
           |  |  | 106 |             if ($processor->available && $processor->hassettings) {
 | 
        
           |  |  | 107 |                 $settingsurl = new moodle_url('/admin/settings.php', array('section' => 'messagesetting'.$processor->name));
 | 
        
           |  |  | 108 |                 $settings->text = html_writer::link($settingsurl, get_string('settings', 'message'));
 | 
        
           |  |  | 109 |             }
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |             $row->cells = array($name, $enable, $settings);
 | 
        
           |  |  | 112 |             $table->data[] = $row;
 | 
        
           |  |  | 113 |         }
 | 
        
           |  |  | 114 |         return html_writer::table($table);
 | 
        
           |  |  | 115 |     }
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |     /**
 | 
        
           |  |  | 118 |      * Display the interface to manage default message outputs
 | 
        
           |  |  | 119 |      *
 | 
        
           |  |  | 120 |      * @param  array $processors  array of objects containing message processors
 | 
        
           |  |  | 121 |      * @param  array $providers   array of objects containing message providers
 | 
        
           |  |  | 122 |      * @param  stdClass $preferences object containing current preferences
 | 
        
           |  |  | 123 |      * @return string The text to render
 | 
        
           |  |  | 124 |      */
 | 
        
           |  |  | 125 |     public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
 | 
        
           |  |  | 126 |         $context = [];
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |         foreach ($processors as $processor) {
 | 
        
           |  |  | 129 |             $processor->displayname = get_string('pluginname', 'message_'.$processor->name);
 | 
        
           |  |  | 130 |         }
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 |         $activitycomponents = [];
 | 
        
           |  |  | 133 |         $othercomponents = [];
 | 
        
           |  |  | 134 |   | 
        
           |  |  | 135 |         foreach ($providers as $provider) {
 | 
        
           |  |  | 136 |             $provider->displayname = get_string('messageprovider:'.$provider->name, $provider->component);
 | 
        
           |  |  | 137 |             $providersettingprefix = $provider->component.'_'.$provider->name.'_';
 | 
        
           |  |  | 138 |             $provider->enabledsetting = $providersettingprefix.'disable';
 | 
        
           |  |  | 139 |             $provider->enabled = empty($preferences->{$provider->enabledsetting});
 | 
        
           |  |  | 140 |             $provider->enabledlabel = get_string('providerenabled', 'message', $provider->displayname);
 | 
        
           |  |  | 141 |             $provider->settings = [];
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 |             // Settings for each processor
 | 
        
           |  |  | 144 |             foreach ($processors as $processor) {
 | 
        
           |  |  | 145 |                 $setting = new StdClass();
 | 
        
           |  |  | 146 |   | 
        
           | 1441 | ariadna | 147 |                 $supportsprocessor = true;
 | 
        
           |  |  | 148 |                 if ($processor->name === 'sms') {
 | 
        
           |  |  | 149 |                     $supportsprocessor = core_message\helper::supports_sms_notifications($provider);
 | 
        
           |  |  | 150 |                 }
 | 
        
           |  |  | 151 |                 $setting->supportsprocessor = $supportsprocessor;
 | 
        
           |  |  | 152 |   | 
        
           | 1 | efrain | 153 |                 $setting->lockedsetting = $providersettingprefix.'locked['.$processor->name.']';
 | 
        
           |  |  | 154 |                 $preference = $processor->name.'_provider_'.$providersettingprefix.'locked';
 | 
        
           |  |  | 155 |   | 
        
           |  |  | 156 |                 $setting->locked = false;
 | 
        
           |  |  | 157 |                 if (property_exists($preferences, $preference)) {
 | 
        
           |  |  | 158 |                     $setting->locked = $preferences->{$preference} == 1;
 | 
        
           |  |  | 159 |                 }
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |                 $setting->enabledsetting = $providersettingprefix.'enabled['.$processor->name.']';
 | 
        
           |  |  | 162 |                 $preference = 'message_provider_'.$providersettingprefix.'enabled';
 | 
        
           |  |  | 163 |   | 
        
           |  |  | 164 |                 $setting->enabled = false;
 | 
        
           |  |  | 165 |                 if (property_exists($preferences, $preference)) {
 | 
        
           |  |  | 166 |                     $setting->enabled = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
 | 
        
           |  |  | 167 |                 }
 | 
        
           |  |  | 168 |                 $labelparams = [
 | 
        
           |  |  | 169 |                     'provider'  => $provider->displayname,
 | 
        
           |  |  | 170 |                     'processor' => $processor->displayname,
 | 
        
           |  |  | 171 |                 ];
 | 
        
           |  |  | 172 |                 $setting->enabledlabel = get_string('sendingviaenabled', 'message', $labelparams);
 | 
        
           |  |  | 173 |                 $setting->lockedlabel = get_string('sendingvialocked', 'message', $labelparams);
 | 
        
           |  |  | 174 |   | 
        
           |  |  | 175 |                 $provider->settings[] = $setting;
 | 
        
           |  |  | 176 |             }
 | 
        
           |  |  | 177 |   | 
        
           |  |  | 178 |             // Order the components so that the activities appear first, followed
 | 
        
           |  |  | 179 |             // by the system and then anything else.
 | 
        
           |  |  | 180 |             if ($provider->component != 'moodle') {
 | 
        
           |  |  | 181 |                 if (substr($provider->component, 0, 4) == 'mod_') {
 | 
        
           |  |  | 182 |                     // Activities.
 | 
        
           |  |  | 183 |                     $activitycomponents[] = $provider->component;
 | 
        
           |  |  | 184 |                 } else {
 | 
        
           |  |  | 185 |                     // Other stuff.
 | 
        
           |  |  | 186 |                     $othercomponents[] = $provider->component;
 | 
        
           |  |  | 187 |                 }
 | 
        
           |  |  | 188 |             }
 | 
        
           |  |  | 189 |         }
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 |         $activitycomponents = array_unique($activitycomponents);
 | 
        
           |  |  | 192 |         asort($activitycomponents);
 | 
        
           |  |  | 193 |         $othercomponents = array_unique($othercomponents);
 | 
        
           |  |  | 194 |         asort($othercomponents);
 | 
        
           |  |  | 195 |         $components = array_merge($activitycomponents, ['moodle'], $othercomponents);
 | 
        
           |  |  | 196 |         asort($providers);
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 |         $colspan = count($processors) + 2;
 | 
        
           |  |  | 199 |         $componentsexport = [];
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |         foreach ($components as $component) {
 | 
        
           |  |  | 202 |             $componentexport = new StdClass();
 | 
        
           |  |  | 203 |             $componentexport->name = $component;
 | 
        
           |  |  | 204 |   | 
        
           |  |  | 205 |             if ($component != 'moodle') {
 | 
        
           |  |  | 206 |                 $componentexport->displayname = get_string('pluginname', $component);
 | 
        
           |  |  | 207 |             } else {
 | 
        
           |  |  | 208 |                 $componentexport->displayname = get_string('coresystem');
 | 
        
           |  |  | 209 |             }
 | 
        
           |  |  | 210 |   | 
        
           |  |  | 211 |             $componentexport->providers = [];
 | 
        
           |  |  | 212 |             foreach ($providers as $provider) {
 | 
        
           |  |  | 213 |                 if ($provider->component == $component) {
 | 
        
           |  |  | 214 |                     $componentexport->providers[] = $provider;
 | 
        
           |  |  | 215 |                 }
 | 
        
           |  |  | 216 |             }
 | 
        
           |  |  | 217 |             $componentexport->colspan = $colspan;
 | 
        
           |  |  | 218 |             $componentsexport[] = $componentexport;
 | 
        
           |  |  | 219 |         }
 | 
        
           |  |  | 220 |   | 
        
           |  |  | 221 |         $context['processors'] = array_values($processors);
 | 
        
           |  |  | 222 |         $context['components'] = $componentsexport;
 | 
        
           |  |  | 223 |   | 
        
           |  |  | 224 |         return $this->render_from_template('message/default_notification_preferences', $context);
 | 
        
           |  |  | 225 |     }
 | 
        
           |  |  | 226 |   | 
        
           |  |  | 227 |     /**
 | 
        
           |  |  | 228 |      * Display the interface for notification preferences
 | 
        
           |  |  | 229 |      *
 | 
        
           |  |  | 230 |      * @param object $user instance of a user
 | 
        
           |  |  | 231 |      * @return string The text to render
 | 
        
           |  |  | 232 |      */
 | 
        
           |  |  | 233 |     public function render_user_notification_preferences($user) {
 | 
        
           |  |  | 234 |         $processors = get_message_processors();
 | 
        
           |  |  | 235 |         $providers = message_get_providers_for_user($user->id);
 | 
        
           |  |  | 236 |   | 
        
           |  |  | 237 |         $preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user);
 | 
        
           |  |  | 238 |         $notificationlistoutput = new \core_message\output\preferences\notification_list($processors, $providers,
 | 
        
           |  |  | 239 |             $preferences, $user);
 | 
        
           |  |  | 240 |         return $this->render_from_template('message/notification_preferences',
 | 
        
           |  |  | 241 |             $notificationlistoutput->export_for_template($this));
 | 
        
           |  |  | 242 |     }
 | 
        
           |  |  | 243 |   | 
        
           |  |  | 244 |     /**
 | 
        
           |  |  | 245 |      * Display the interface for message preferences
 | 
        
           |  |  | 246 |      *
 | 
        
           |  |  | 247 |      * @param object $user instance of a user
 | 
        
           |  |  | 248 |      * @return string The text to render
 | 
        
           |  |  | 249 |      */
 | 
        
           |  |  | 250 |     public function render_user_message_preferences($user) {
 | 
        
           |  |  | 251 |         global $CFG;
 | 
        
           |  |  | 252 |   | 
        
           |  |  | 253 |         // Filter out enabled, available system_configured and user_configured processors only.
 | 
        
           |  |  | 254 |         $readyprocessors = array_filter(get_message_processors(), function($processor) {
 | 
        
           |  |  | 255 |             return $processor->enabled &&
 | 
        
           |  |  | 256 |                 $processor->configured &&
 | 
        
           |  |  | 257 |                 $processor->object->is_user_configured() &&
 | 
        
           |  |  | 258 |                 // Filter out processors that don't have and message preferences to configure.
 | 
        
           |  |  | 259 |                 $processor->object->has_message_preferences();
 | 
        
           |  |  | 260 |         });
 | 
        
           |  |  | 261 |   | 
        
           |  |  | 262 |         $providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
 | 
        
           |  |  | 263 |             return $provider->component === 'moodle';
 | 
        
           |  |  | 264 |         });
 | 
        
           |  |  | 265 |         $preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user);
 | 
        
           |  |  | 266 |         $notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors,
 | 
        
           |  |  | 267 |             $providers, $preferences, $user);
 | 
        
           |  |  | 268 |         $context = $notificationlistoutput->export_for_template($this);
 | 
        
           |  |  | 269 |   | 
        
           |  |  | 270 |         // Get the privacy settings options for being messaged.
 | 
        
           |  |  | 271 |         $privacysetting = \core_message\api::get_user_privacy_messaging_preference($user->id);
 | 
        
           |  |  | 272 |         $choices = array();
 | 
        
           |  |  | 273 |         $choices[] = [
 | 
        
           |  |  | 274 |             'value' => \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
 | 
        
           |  |  | 275 |             'text' => get_string('contactableprivacy_onlycontacts', 'message'),
 | 
        
           |  |  | 276 |             'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS)
 | 
        
           |  |  | 277 |         ];
 | 
        
           |  |  | 278 |         $choices[] = [
 | 
        
           |  |  | 279 |             'value' => \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
 | 
        
           |  |  | 280 |             'text' => get_string('contactableprivacy_coursemember', 'message'),
 | 
        
           |  |  | 281 |             'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER)
 | 
        
           |  |  | 282 |         ];
 | 
        
           |  |  | 283 |         if (!empty($CFG->messagingallusers)) {
 | 
        
           |  |  | 284 |             // Add the MESSAGE_PRIVACY_SITE option when site-wide messaging between users is enabled.
 | 
        
           |  |  | 285 |             $choices[] = [
 | 
        
           |  |  | 286 |                 'value' => \core_message\api::MESSAGE_PRIVACY_SITE,
 | 
        
           |  |  | 287 |                 'text' => get_string('contactableprivacy_site', 'message'),
 | 
        
           |  |  | 288 |                 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_SITE)
 | 
        
           |  |  | 289 |             ];
 | 
        
           |  |  | 290 |         }
 | 
        
           |  |  | 291 |         $context['privacychoices'] = $choices;
 | 
        
           |  |  | 292 |   | 
        
           |  |  | 293 |         return $this->render_from_template('message/message_preferences', $context);
 | 
        
           |  |  | 294 |     }
 | 
        
           |  |  | 295 | }
 |