weekly release 3.10.1+
[moodle.git] / message / renderer.php
CommitLineData
75c34c23 1<?php
2f2137fc
RK
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/>.
75c34c23
RK
16
17/**
6fbd60ef 18 * Contains renderer objects for messaging
75c34c23 19 *
6fbd60ef 20 * @package core_message
75c34c23
RK
21 * @copyright 2011 Lancaster University Network Services Limited
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * message Renderer
29 *
30 * Class for rendering various message objects
31 *
6fbd60ef
AD
32 * @package core_message
33 * @subpackage message
75c34c23
RK
34 * @copyright 2011 Lancaster University Network Services Limited
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_message_renderer extends plugin_renderer_base {
38
a029ee00
AA
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
f4feabb8 45 * @param stdClass $preferences object containing current preferences
a029ee00
AA
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
56 // Add active message output processors settings.
57 $output .= $this->heading(get_string('managemessageoutputs', 'message'));
58 $output .= $this->manage_defaultmessageoutputs($processors, $providers, $preferences);
59
60 $output .= html_writer::start_tag('div', array('class' => 'form-buttons'));
61 $output .= html_writer::empty_tag('input',
62 array('type' => 'submit', 'value' => get_string('savechanges', 'admin'), 'class' => 'form-submit btn btn-primary')
63 );
64 $output .= html_writer::end_tag('div');
65 $output .= html_writer::end_tag('form');
66
67 return $output;
68 }
69
75c34c23
RK
70 /**
71 * Display the interface to manage message outputs
72 *
6fbd60ef
AD
73 * @param array $processors array of objects containing message processors
74 * @return string The text to render
75c34c23
RK
75 */
76 public function manage_messageoutputs($processors) {
77 // Display the current workflows
78 $table = new html_table();
e87214bd 79 $table->attributes['class'] = 'admintable generaltable';
75c34c23
RK
80 $table->data = array();
81 $table->head = array(
82 get_string('name'),
83 get_string('enable'),
84 get_string('settings'),
85 );
86 $table->colclasses = array(
87 'displayname', 'availability', 'settings',
88 );
89
1d72e9d4 90 foreach ($processors as $processor) {
75c34c23 91 $row = new html_table_row();
2f2137fc 92 $row->attributes['class'] = 'messageoutputs';
75c34c23 93
2fd7c451 94 $name = new html_table_cell(get_string('pluginname', 'message_'.$processor->name));
75c34c23
RK
95 $enable = new html_table_cell();
96 if (!$processor->available) {
a029ee00
AA
97 $enable->text = html_writer::nonempty_tag('span', get_string('outputnotavailable', 'message'),
98 array('class' => 'error')
99 );
75c34c23 100 } else {
a029ee00
AA
101 $enable->text = html_writer::checkbox($processor->name, $processor->id, $processor->enabled, '',
102 array('id' => $processor->name)
103 );
75c34c23 104 }
2f2137fc 105 // Settings
75c34c23 106 $settings = new html_table_cell();
1d72e9d4 107 if ($processor->available && $processor->hassettings) {
a029ee00 108 $settingsurl = new moodle_url('/admin/settings.php', array('section' => 'messagesetting'.$processor->name));
75c34c23
RK
109 $settings->text = html_writer::link($settingsurl, get_string('settings', 'message'));
110 }
111
112 $row->cells = array($name, $enable, $settings);
113 $table->data[] = $row;
114 }
115 return html_writer::table($table);
116 }
1d72e9d4
RK
117
118 /**
119 * Display the interface to manage default message outputs
120 *
6fbd60ef
AD
121 * @param array $processors array of objects containing message processors
122 * @param array $providers array of objects containing message providers
f4feabb8 123 * @param stdClass $preferences object containing current preferences
6fbd60ef 124 * @return string The text to render
1d72e9d4
RK
125 */
126 public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
1d72e9d4
RK
127 // Prepare list of options for dropdown menu
128 $options = array();
129 foreach (array('disallowed', 'permitted', 'forced') as $setting) {
130 $options[$setting] = get_string($setting, 'message');
131 }
132
1d72e9d4
RK
133 // Display users outputs table
134 $table = new html_table();
135 $table->attributes['class'] = 'generaltable';
136 $table->data = array();
137 $table->head = array('');
138
139 // Populate the header row
140 foreach ($processors as $processor) {
141 $table->head[] = get_string('pluginname', 'message_'.$processor->name);
142 }
fd0f7ac5
RT
143 // Add enable/disable to head
144 $table->head[] = get_string('enabled', 'core_message');
145
1d72e9d4
RK
146 // Generate the matrix of settings for each provider and processor
147 foreach ($providers as $provider) {
148 $row = new html_table_row();
149 $row->attributes['class'] = 'defaultmessageoutputs';
150 $row->cells = array();
151
152 // Provider Name
153 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
154 $row->cells[] = new html_table_cell($providername);
fd0f7ac5
RT
155 $providersettingprefix = $provider->component.'_'.$provider->name.'_';
156 $disableprovidersetting = $providersettingprefix.'disable';
157 $providerdisabled = !empty($preferences->$disableprovidersetting);
1d72e9d4
RK
158 // Settings for each processor
159 foreach ($processors as $processor) {
160 $cellcontent = '';
161 foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
162 // pepare element and preference names
fd0f7ac5
RT
163 $elementname = $providersettingprefix.$setting.'['.$processor->name.']';
164 $preferencebase = $providersettingprefix.$setting;
1d72e9d4
RK
165 // prepare language bits
166 $processorname = get_string('pluginname', 'message_'.$processor->name);
167 $statename = get_string($setting, 'message');
168 $labelparams = array(
169 'provider' => $providername,
170 'processor' => $processorname,
171 'state' => $statename
172 );
173 if ($setting == 'permitted') {
174 $label = get_string('sendingvia', 'message', $labelparams);
175 // determine the current setting or use default
814e3735 176 $select = MESSAGE_DEFAULT_PERMITTED;
1d72e9d4 177 $preference = $processor->name.'_provider_'.$preferencebase;
fd0f7ac5
RT
178 if ($providerdisabled) {
179 $select = MESSAGE_DISALLOWED;
f4feabb8 180 } else if (property_exists($preferences, $preference)) {
1d72e9d4
RK
181 $select = $preferences->{$preference};
182 }
183 // dropdown menu
814e3735
RK
184 $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
185 $cellcontent .= html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
1d72e9d4
RK
186 $cellcontent .= html_writer::tag('div', get_string('defaults', 'message'));
187 } else {
188 $label = get_string('sendingviawhen', 'message', $labelparams);
189 // determine the current setting based on the 'permitted' setting above
190 $checked = false;
191 if ($select == 'forced') {
192 $checked = true;
193 } else if ($select == 'permitted') {
194 $preference = 'message_provider_'.$preferencebase;
f4feabb8 195 if (property_exists($preferences, $preference)) {
1d72e9d4
RK
196 $checked = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
197 }
198 }
199 // generate content
200 $cellcontent .= html_writer::start_tag('div');
1d72e9d4 201 $cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
814e3735 202 $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
1d72e9d4
RK
203 $cellcontent .= $statename;
204 $cellcontent .= html_writer::end_tag('div');
205 }
206 }
207 $row->cells[] = new html_table_cell($cellcontent);
208 }
fd0f7ac5
RT
209 $disableprovider = html_writer::checkbox($disableprovidersetting, 1, !$providerdisabled, '',
210 array('id' => $disableprovidersetting, 'class' => 'messagedisable'));
211 $disableprovider = html_writer::tag('div', $disableprovider);
212 $row->cells[] = new html_table_cell($disableprovider);
1d72e9d4
RK
213 $table->data[] = $row;
214 }
215
a029ee00 216 $output = html_writer::table($table);
1d72e9d4
RK
217 return $output;
218 }
219
a0eabdd3 220 /**
8c125526 221 * Display the interface for notification preferences
a0eabdd3
RW
222 *
223 * @param object $user instance of a user
224 * @return string The text to render
225 */
8c125526
RW
226 public function render_user_notification_preferences($user) {
227 $processors = get_message_processors();
228 $providers = message_get_providers_for_user($user->id);
8c125526 229
7b55aaa1
MN
230 $preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user);
231 $notificationlistoutput = new \core_message\output\preferences\notification_list($processors, $providers,
232 $preferences, $user);
df9986c8 233 return $this->render_from_template('message/notification_preferences',
7b55aaa1 234 $notificationlistoutput->export_for_template($this));
8c125526
RW
235 }
236
237 /**
238 * Display the interface for message preferences
239 *
240 * @param object $user instance of a user
241 * @return string The text to render
242 */
243 public function render_user_message_preferences($user) {
f7dfa9ba
SA
244 global $CFG;
245
a0eabdd3 246 // Filter out enabled, available system_configured and user_configured processors only.
5ee31614 247 $readyprocessors = array_filter(get_message_processors(), function($processor) {
8c125526
RW
248 return $processor->enabled &&
249 $processor->configured &&
250 $processor->object->is_user_configured() &&
7d69958e
RW
251 // Filter out processors that don't have and message preferences to configure.
252 $processor->object->has_message_preferences();
5ee31614 253 });
a0eabdd3 254
8c125526
RW
255 $providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
256 return $provider->component === 'moodle';
257 });
79f6c36c 258 $preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user);
7b55aaa1
MN
259 $notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors,
260 $providers, $preferences, $user);
8c125526 261 $context = $notificationlistoutput->export_for_template($this);
f7dfa9ba
SA
262
263 // Get the privacy settings options for being messaged.
264 $privacysetting = \core_message\api::get_user_privacy_messaging_preference($user->id);
265 $choices = array();
266 $choices[] = [
267 'value' => \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
268 'text' => get_string('contactableprivacy_onlycontacts', 'message'),
269 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS)
270 ];
271 $choices[] = [
272 'value' => \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
273 'text' => get_string('contactableprivacy_coursemember', 'message'),
274 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER)
275 ];
276 if (!empty($CFG->messagingallusers)) {
277 // Add the MESSAGE_PRIVACY_SITE option when site-wide messaging between users is enabled.
278 $choices[] = [
279 'value' => \core_message\api::MESSAGE_PRIVACY_SITE,
280 'text' => get_string('contactableprivacy_site', 'message'),
281 'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_SITE)
282 ];
283 }
284 $context['privacychoices'] = $choices;
2ca1f894 285
7e120451 286 return $this->render_from_template('message/message_preferences', $context);
814e3735 287 }
75c34c23 288}