Moodle release 4.2beta
[moodle.git] / message / renderer.php
blob3afe2c82fe337e33162024cb5589722e56f1d4e5
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 * Contains renderer objects for messaging
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
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * message Renderer
30 * Class for rendering various message objects
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
37 class core_message_renderer extends plugin_renderer_base {
39 /**
40 * Display the interface to manage both message outputs and default message outputs
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
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()));
52 // Add message output processors enabled/disabled and settings.
53 $output .= $this->heading(get_string('messageoutputs', 'message'));
54 $output .= $this->manage_messageoutputs($allprocessors);
56 // Add active message output processors settings.
57 $output .= $this->manage_defaultmessageoutputs($processors, $providers, $preferences);
59 $output .= html_writer::start_tag('div', array('class' => 'form-buttons'));
60 $output .= html_writer::empty_tag('input',
61 array('type' => 'submit', 'value' => get_string('savechanges', 'admin'), 'class' => 'form-submit btn btn-primary')
63 $output .= html_writer::end_tag('div');
64 $output .= html_writer::end_tag('form');
66 return $output;
69 /**
70 * Display the interface to manage message outputs
72 * @param array $processors array of objects containing message processors
73 * @return string The text to render
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'),
85 $table->colclasses = array(
86 'displayname', 'availability text-center', 'settings',
89 foreach ($processors as $processor) {
90 $row = new html_table_row();
91 $row->attributes['class'] = 'messageoutputs';
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')
99 } else {
100 $enable->text = html_writer::checkbox($processor->name, $processor->id, $processor->enabled, '',
101 array('id' => $processor->name)
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'));
111 $row->cells = array($name, $enable, $settings);
112 $table->data[] = $row;
114 return html_writer::table($table);
118 * Display the interface to manage default message outputs
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
125 public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
126 $context = [];
128 foreach ($processors as $processor) {
129 $processor->displayname = get_string('pluginname', 'message_'.$processor->name);
132 $activitycomponents = [];
133 $othercomponents = [];
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 = [];
143 // Settings for each processor
144 foreach ($processors as $processor) {
145 $setting = new StdClass();
147 $setting->lockedsetting = $providersettingprefix.'locked['.$processor->name.']';
148 $preference = $processor->name.'_provider_'.$providersettingprefix.'locked';
150 $setting->locked = false;
151 if (property_exists($preferences, $preference)) {
152 $setting->locked = $preferences->{$preference} == 1;
155 $setting->enabledsetting = $providersettingprefix.'enabled['.$processor->name.']';
156 $preference = 'message_provider_'.$providersettingprefix.'enabled';
158 $setting->enabled = false;
159 if (property_exists($preferences, $preference)) {
160 $setting->enabled = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
162 $labelparams = [
163 'provider' => $provider->displayname,
164 'processor' => $processor->displayname,
166 $setting->enabledlabel = get_string('sendingviaenabled', 'message', $labelparams);
167 $setting->lockedlabel = get_string('sendingvialocked', 'message', $labelparams);
169 $provider->settings[] = $setting;
172 // Order the components so that the activities appear first, followed
173 // by the system and then anything else.
174 if ($provider->component != 'moodle') {
175 if (substr($provider->component, 0, 4) == 'mod_') {
176 // Activities.
177 $activitycomponents[] = $provider->component;
178 } else {
179 // Other stuff.
180 $othercomponents[] = $provider->component;
185 $activitycomponents = array_unique($activitycomponents);
186 asort($activitycomponents);
187 $othercomponents = array_unique($othercomponents);
188 asort($othercomponents);
189 $components = array_merge($activitycomponents, ['moodle'], $othercomponents);
190 asort($providers);
192 $colspan = count($processors) + 2;
193 $componentsexport = [];
195 foreach ($components as $component) {
196 $componentexport = new StdClass();
197 $componentexport->name = $component;
199 if ($component != 'moodle') {
200 $componentexport->displayname = get_string('pluginname', $component);
201 } else {
202 $componentexport->displayname = get_string('coresystem');
205 $componentexport->providers = [];
206 foreach ($providers as $provider) {
207 if ($provider->component == $component) {
208 $componentexport->providers[] = $provider;
211 $componentexport->colspan = $colspan;
212 $componentsexport[] = $componentexport;
215 $context['processors'] = array_values($processors);
216 $context['components'] = $componentsexport;
218 return $this->render_from_template('message/default_notification_preferences', $context);
222 * Display the interface for notification preferences
224 * @param object $user instance of a user
225 * @return string The text to render
227 public function render_user_notification_preferences($user) {
228 $processors = get_message_processors();
229 $providers = message_get_providers_for_user($user->id);
231 $preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user);
232 $notificationlistoutput = new \core_message\output\preferences\notification_list($processors, $providers,
233 $preferences, $user);
234 return $this->render_from_template('message/notification_preferences',
235 $notificationlistoutput->export_for_template($this));
239 * Display the interface for message preferences
241 * @param object $user instance of a user
242 * @return string The text to render
244 public function render_user_message_preferences($user) {
245 global $CFG;
247 // Filter out enabled, available system_configured and user_configured processors only.
248 $readyprocessors = array_filter(get_message_processors(), function($processor) {
249 return $processor->enabled &&
250 $processor->configured &&
251 $processor->object->is_user_configured() &&
252 // Filter out processors that don't have and message preferences to configure.
253 $processor->object->has_message_preferences();
256 $providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
257 return $provider->component === 'moodle';
259 $preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user);
260 $notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors,
261 $providers, $preferences, $user);
262 $context = $notificationlistoutput->export_for_template($this);
264 // Get the privacy settings options for being messaged.
265 $privacysetting = \core_message\api::get_user_privacy_messaging_preference($user->id);
266 $choices = array();
267 $choices[] = [
268 'value' => \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
269 'text' => get_string('contactableprivacy_onlycontacts', 'message'),
270 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS)
272 $choices[] = [
273 'value' => \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
274 'text' => get_string('contactableprivacy_coursemember', 'message'),
275 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER)
277 if (!empty($CFG->messagingallusers)) {
278 // Add the MESSAGE_PRIVACY_SITE option when site-wide messaging between users is enabled.
279 $choices[] = [
280 'value' => \core_message\api::MESSAGE_PRIVACY_SITE,
281 'text' => get_string('contactableprivacy_site', 'message'),
282 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_SITE)
285 $context['privacychoices'] = $choices;
287 return $this->render_from_template('message/message_preferences', $context);