Merge branch 'MDL-51083-master' of git://github.com/xow/moodle
[moodle.git] / message / renderer.php
blob558287c28dec5ea64e51e1dc1c925a2cd23ce67e
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 message outputs
42 * @param array $processors array of objects containing message processors
43 * @return string The text to render
45 public function manage_messageoutputs($processors) {
46 global $CFG;
47 // Display the current workflows
48 $table = new html_table();
49 $table->attributes['class'] = 'admintable generaltable';
50 $table->data = array();
51 $table->head = array(
52 get_string('name'),
53 get_string('enable'),
54 get_string('settings'),
56 $table->colclasses = array(
57 'displayname', 'availability', 'settings',
60 foreach ($processors as $processor) {
61 $row = new html_table_row();
62 $row->attributes['class'] = 'messageoutputs';
64 // Name
65 $name = new html_table_cell(get_string('pluginname', 'message_'.$processor->name));
67 // Enable
68 $enable = new html_table_cell();
69 $enable->attributes['class'] = 'mdl-align';
70 if (!$processor->available) {
71 $enable->text = html_writer::nonempty_tag('span', get_string('outputnotavailable', 'message'), array('class' => 'error'));
72 } else if (!$processor->configured) {
73 $enable->text = html_writer::nonempty_tag('span', get_string('outputnotconfigured', 'message'), array('class' => 'error'));
74 } else if ($processor->enabled) {
75 $url = new moodle_url('/admin/message.php', array('disable' => $processor->id, 'sesskey' => sesskey()));
76 $enable->text = html_writer::link($url, html_writer::empty_tag('img',
77 array('src' => $this->output->pix_url('t/hide'),
78 'class' => 'iconsmall',
79 'title' => get_string('outputenabled', 'message'),
80 'alt' => get_string('outputenabled', 'message'),
82 ));
83 } else {
84 $row->attributes['class'] = 'dimmed_text';
85 $url = new moodle_url('/admin/message.php', array('enable' => $processor->id, 'sesskey' => sesskey()));
86 $enable->text = html_writer::link($url, html_writer::empty_tag('img',
87 array('src' => $this->output->pix_url('t/show'),
88 'class' => 'iconsmall',
89 'title' => get_string('outputdisabled', 'message'),
90 'alt' => get_string('outputdisabled', 'message'),
92 ));
94 // Settings
95 $settings = new html_table_cell();
96 if ($processor->available && $processor->hassettings) {
97 $settingsurl = new moodle_url('settings.php', array('section' => 'messagesetting'.$processor->name));
98 $settings->text = html_writer::link($settingsurl, get_string('settings', 'message'));
101 $row->cells = array($name, $enable, $settings);
102 $table->data[] = $row;
104 return html_writer::table($table);
108 * Display the interface to manage default message outputs
110 * @param array $processors array of objects containing message processors
111 * @param array $providers array of objects containing message providers
112 * @param array $preferences array of objects containing current preferences
113 * @return string The text to render
115 public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
116 global $CFG;
118 // Prepare list of options for dropdown menu
119 $options = array();
120 foreach (array('disallowed', 'permitted', 'forced') as $setting) {
121 $options[$setting] = get_string($setting, 'message');
124 $output = html_writer::start_tag('form', array('id'=>'defaultmessageoutputs', 'method'=>'post'));
125 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
127 // Display users outputs table
128 $table = new html_table();
129 $table->attributes['class'] = 'generaltable';
130 $table->data = array();
131 $table->head = array('');
133 // Populate the header row
134 foreach ($processors as $processor) {
135 $table->head[] = get_string('pluginname', 'message_'.$processor->name);
137 // Add enable/disable to head
138 $table->head[] = get_string('enabled', 'core_message');
140 // Generate the matrix of settings for each provider and processor
141 foreach ($providers as $provider) {
142 $row = new html_table_row();
143 $row->attributes['class'] = 'defaultmessageoutputs';
144 $row->cells = array();
146 // Provider Name
147 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
148 $row->cells[] = new html_table_cell($providername);
149 $providersettingprefix = $provider->component.'_'.$provider->name.'_';
150 $disableprovidersetting = $providersettingprefix.'disable';
151 $providerdisabled = !empty($preferences->$disableprovidersetting);
152 // Settings for each processor
153 foreach ($processors as $processor) {
154 $cellcontent = '';
155 foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
156 // pepare element and preference names
157 $elementname = $providersettingprefix.$setting.'['.$processor->name.']';
158 $preferencebase = $providersettingprefix.$setting;
159 // prepare language bits
160 $processorname = get_string('pluginname', 'message_'.$processor->name);
161 $statename = get_string($setting, 'message');
162 $labelparams = array(
163 'provider' => $providername,
164 'processor' => $processorname,
165 'state' => $statename
167 if ($setting == 'permitted') {
168 $label = get_string('sendingvia', 'message', $labelparams);
169 // determine the current setting or use default
170 $select = MESSAGE_DEFAULT_PERMITTED;
171 $preference = $processor->name.'_provider_'.$preferencebase;
172 if ($providerdisabled) {
173 $select = MESSAGE_DISALLOWED;
174 } else if (array_key_exists($preference, $preferences)) {
175 $select = $preferences->{$preference};
177 // dropdown menu
178 $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
179 $cellcontent .= html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
180 $cellcontent .= html_writer::tag('div', get_string('defaults', 'message'));
181 } else {
182 $label = get_string('sendingviawhen', 'message', $labelparams);
183 // determine the current setting based on the 'permitted' setting above
184 $checked = false;
185 if ($select == 'forced') {
186 $checked = true;
187 } else if ($select == 'permitted') {
188 $preference = 'message_provider_'.$preferencebase;
189 if (array_key_exists($preference, $preferences)) {
190 $checked = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
193 // generate content
194 $cellcontent .= html_writer::start_tag('div');
195 $cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
196 $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
197 $cellcontent .= $statename;
198 $cellcontent .= html_writer::end_tag('div');
201 $row->cells[] = new html_table_cell($cellcontent);
203 $disableprovider = html_writer::checkbox($disableprovidersetting, 1, !$providerdisabled, '',
204 array('id' => $disableprovidersetting, 'class' => 'messagedisable'));
205 $disableprovider = html_writer::tag('div', $disableprovider);
206 $row->cells[] = new html_table_cell($disableprovider);
207 $table->data[] = $row;
210 $output .= html_writer::table($table);
211 $output .= html_writer::start_tag('div', array('class' => 'form-buttons'));
212 $output .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('savechanges','admin'), 'class' => 'form-submit'));
213 $output .= html_writer::end_tag('div');
214 $output .= html_writer::end_tag('form');
215 return $output;
219 * Display the interface for messaging options
221 * @param array $processors Array of objects containing message processors
222 * @param array $providers Array of objects containing message providers
223 * @param array $preferences Array of objects containing current preferences
224 * @param array $defaultpreferences Array of objects containing site default preferences
225 * @param bool $notificationsdisabled Indicate if the user's "emailstop" flag is set (shouldn't receive any non-forced notifications)
226 * @param null|int $userid User id, or null if current user.
227 * @return string The text to render
229 public function manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences,
230 $notificationsdisabled = false, $userid = null) {
231 global $USER;
232 if (empty($userid)) {
233 $userid = $USER->id;
235 // Filter out enabled, available system_configured and user_configured processors only.
236 $readyprocessors = array_filter($processors, create_function('$a', 'return $a->enabled && $a->configured && $a->object->is_user_configured();'));
238 // Start the form. We're not using mform here because of our special formatting needs ...
239 $output = html_writer::start_tag('form', array('method'=>'post', 'class' => 'mform'));
240 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
242 /// Settings table...
243 $output .= html_writer::start_tag('fieldset', array('id' => 'providers', 'class' => 'clearfix'));
244 $output .= html_writer::nonempty_tag('legend', get_string('providers_config', 'message'), array('class' => 'ftoggler'));
246 foreach($providers as $provider) {
247 if($provider->component != 'moodle') {
248 $components[] = $provider->component;
251 // Lets arrange by components so that core settings (moodle) appear as the first table.
252 $components = array_unique($components);
253 asort($components);
254 array_unshift($components, 'moodle'); // pop it in front! phew!
255 asort($providers);
257 $numprocs = count($processors);
258 // Display the messaging options table(s)
259 foreach ($components as $component) {
260 $provideradded = false;
261 $table = new html_table();
262 $table->attributes['class'] = 'generaltable';
263 $table->data = array();
264 if ($component != 'moodle') {
265 $componentname = get_string('pluginname', $component);
266 } else {
267 $componentname = get_string('coresystem');
269 $table->head = array($componentname);
270 foreach ($readyprocessors as $processor) {
271 $table->head[] = get_string('pluginname', 'message_'.$processor->name);
273 // Populate the table with rows
274 foreach ($providers as $provider) {
275 $preferencebase = $provider->component.'_'.$provider->name;
276 // If provider component is not same or provider disabled then don't show.
277 if (($provider->component != $component) ||
278 (!empty($defaultpreferences->{$preferencebase.'_disable'}))) {
279 continue;
281 $provideradded = true;
282 $headerrow = new html_table_row();
283 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
284 $providercell = new html_table_cell($providername);
285 $providercell->header = true;
286 $providercell->colspan = $numprocs;
287 $providercell->attributes['class'] = 'c0';
288 $headerrow->cells = array($providercell);
289 $table->data[] = $headerrow;
291 foreach (array('loggedin', 'loggedoff') as $state) {
292 $optionrow = new html_table_row();
293 $optionname = new html_table_cell(get_string($state.'description', 'message'));
294 $optionname->attributes['class'] = 'c0';
295 $optionrow->cells = array($optionname);
296 foreach ($readyprocessors as $processor) {
297 // determine the default setting
298 $permitted = MESSAGE_DEFAULT_PERMITTED;
299 $defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
300 if (isset($defaultpreferences->{$defaultpreference})) {
301 $permitted = $defaultpreferences->{$defaultpreference};
303 // If settings are disallowed or forced, just display the
304 // corresponding message, if not use user settings.
305 if (in_array($permitted, array('disallowed', 'forced'))) {
306 if ($state == 'loggedoff') {
307 // skip if we are rendering the second line
308 continue;
310 $cellcontent = html_writer::nonempty_tag('div', get_string($permitted, 'message'), array('class' => 'dimmed_text'));
311 $optioncell = new html_table_cell($cellcontent);
312 $optioncell->rowspan = 2;
313 $optioncell->attributes['class'] = 'disallowed';
314 } else {
315 // determine user preferences and use them.
316 $disabled = array();
317 $checked = false;
318 if ($notificationsdisabled) {
319 $disabled['disabled'] = 1;
321 // See if user has touched this preference
322 if (isset($preferences->{$preferencebase.'_'.$state})) {
323 // User have some preferneces for this state in the database, use them
324 $checked = isset($preferences->{$preferencebase.'_'.$state}[$processor->name]);
325 } else {
326 // User has not set this preference yet, using site default preferences set by admin
327 $defaultpreference = 'message_provider_'.$preferencebase.'_'.$state;
328 if (isset($defaultpreferences->{$defaultpreference})) {
329 $checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
332 $elementname = $preferencebase.'_'.$state.'['.$processor->name.']';
333 // prepare language bits
334 $processorname = get_string('pluginname', 'message_'.$processor->name);
335 $statename = get_string($state, 'message');
336 $labelparams = array(
337 'provider' => $providername,
338 'processor' => $processorname,
339 'state' => $statename
341 $label = get_string('sendingviawhen', 'message', $labelparams);
342 $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
343 $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array_merge(array('id' => $elementname, 'class' => 'notificationpreference'), $disabled));
344 $optioncell = new html_table_cell($cellcontent);
345 $optioncell->attributes['class'] = 'mdl-align';
347 $optionrow->cells[] = $optioncell;
349 $table->data[] = $optionrow;
352 // Add settings only if provider added for component.
353 if ($provideradded) {
354 $output .= html_writer::start_tag('div', array('class' => 'messagesettingcomponent'));
355 $output .= html_writer::table($table);
356 $output .= html_writer::end_tag('div');
360 $output .= html_writer::end_tag('fieldset');
362 foreach ($processors as $processor) {
363 if (($processorconfigform = $processor->object->config_form($preferences)) && $processor->enabled) {
364 $output .= html_writer::start_tag('fieldset', array('id' => 'messageprocessor_'.$processor->name, 'class' => 'clearfix'));
365 $output .= html_writer::nonempty_tag('legend', get_string('pluginname', 'message_'.$processor->name), array('class' => 'ftoggler'));
366 $output .= html_writer::start_tag('div');
367 $output .= $processorconfigform;
368 $output .= html_writer::end_tag('div');
369 $output .= html_writer::end_tag('fieldset');
373 $output .= html_writer::start_tag('fieldset', array('id' => 'messageprocessor_general', 'class' => 'clearfix'));
374 $output .= html_writer::nonempty_tag('legend', get_string('generalsettings','admin'), array('class' => 'ftoggler'));
376 $output .= html_writer::start_tag('div');
377 $output .= html_writer::checkbox('beepnewmessage', 1, $preferences->beepnewmessage, get_string('beepnewmessage', 'message'));
378 $output .= html_writer::end_tag('div');
380 $output .= html_writer::start_tag('div');
381 $output .= html_writer::checkbox('blocknoncontacts', 1, $preferences->blocknoncontacts, get_string('blocknoncontacts', 'message'));
382 $output .= html_writer::end_tag('div');
384 $disableallcheckbox = html_writer::checkbox('disableall', 1, $notificationsdisabled, get_string('disableall', 'message'), array('class'=>'disableallcheckbox'));
385 $disableallcheckbox .= $this->output->help_icon('disableall', 'message');
386 $output .= html_writer::nonempty_tag('div', $disableallcheckbox, array('class'=>'disableall'));
388 $redirect = new moodle_url("/user/preferences.php", array('userid' => $userid));
389 $output .= html_writer::end_tag('fieldset');
390 $output .= html_writer::start_tag('div', array('class' => 'mdl-align'));
391 $output .= html_writer::empty_tag('input', array('type' => 'submit',
392 'value' => get_string('savechanges'), 'class' => 'form-submit'));
393 $output .= html_writer::link($redirect, html_writer::empty_tag('input', array('type' => 'button',
394 'value' => get_string('cancel'), 'class' => 'btn-cancel')));
395 $output .= html_writer::end_tag('div');
397 $output .= html_writer::end_tag('form');
398 return $output;