Merge branch 'MDL-39003-master' of git://github.com/damyon/moodle
[moodle.git] / message / renderer.php
blob9cc07af4188366ed9c6fd96ecdb171acbb592a86
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'] = '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 $name->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 // Generate the matrix of settings for each provider and processor
138 foreach ($providers as $provider) {
139 $row = new html_table_row();
140 $row->attributes['class'] = 'defaultmessageoutputs';
141 $row->cells = array();
143 // Provider Name
144 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
145 $row->cells[] = new html_table_cell($providername);
147 // Settings for each processor
148 foreach ($processors as $processor) {
149 $cellcontent = '';
150 foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
151 // pepare element and preference names
152 $elementname = $provider->component.'_'.$provider->name.'_'.$setting.'['.$processor->name.']';
153 $preferencebase = $provider->component.'_'.$provider->name.'_'.$setting;
154 // prepare language bits
155 $processorname = get_string('pluginname', 'message_'.$processor->name);
156 $statename = get_string($setting, 'message');
157 $labelparams = array(
158 'provider' => $providername,
159 'processor' => $processorname,
160 'state' => $statename
162 if ($setting == 'permitted') {
163 $label = get_string('sendingvia', 'message', $labelparams);
164 // determine the current setting or use default
165 $select = MESSAGE_DEFAULT_PERMITTED;
166 $preference = $processor->name.'_provider_'.$preferencebase;
167 if (array_key_exists($preference, $preferences)) {
168 $select = $preferences->{$preference};
170 // dropdown menu
171 $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
172 $cellcontent .= html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
173 $cellcontent .= html_writer::tag('div', get_string('defaults', 'message'));
174 } else {
175 $label = get_string('sendingviawhen', 'message', $labelparams);
176 // determine the current setting based on the 'permitted' setting above
177 $checked = false;
178 if ($select == 'forced') {
179 $checked = true;
180 } else if ($select == 'permitted') {
181 $preference = 'message_provider_'.$preferencebase;
182 if (array_key_exists($preference, $preferences)) {
183 $checked = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
186 // generate content
187 $cellcontent .= html_writer::start_tag('div');
188 $cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
189 $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
190 $cellcontent .= $statename;
191 $cellcontent .= html_writer::end_tag('div');
194 $row->cells[] = new html_table_cell($cellcontent);
196 $table->data[] = $row;
199 $output .= html_writer::table($table);
200 $output .= html_writer::start_tag('div', array('class' => 'form-buttons'));
201 $output .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('savechanges','admin'), 'class' => 'form-submit'));
202 $output .= html_writer::end_tag('div');
203 $output .= html_writer::end_tag('form');
204 return $output;
208 * Display the interface for messaging options
210 * @param array $processors Array of objects containing message processors
211 * @param array $providers Array of objects containing message providers
212 * @param array $preferences Array of objects containing current preferences
213 * @param array $defaultpreferences Array of objects containing site default preferences
214 * @param bool $notificationsdisabled Indicate if the user's "emailstop" flag is set (shouldn't receive any non-forced notifications)
215 * @return string The text to render
217 public function manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences, $notificationsdisabled = false) {
218 // Filter out enabled, available system_configured and user_configured processors only.
219 $readyprocessors = array_filter($processors, create_function('$a', 'return $a->enabled && $a->configured && $a->object->is_user_configured();'));
221 // Start the form. We're not using mform here because of our special formatting needs ...
222 $output = html_writer::start_tag('form', array('method'=>'post', 'class' => 'mform'));
223 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
225 /// Settings table...
226 $output .= html_writer::start_tag('fieldset', array('id' => 'providers', 'class' => 'clearfix'));
227 $output .= html_writer::nonempty_tag('legend', get_string('providers_config', 'message'), array('class' => 'ftoggler'));
229 foreach($providers as $provider) {
230 if($provider->component != 'moodle') {
231 $components[] = $provider->component;
234 // Lets arrange by components so that core settings (moodle) appear as the first table.
235 $components = array_unique($components);
236 asort($components);
237 array_unshift($components, 'moodle'); // pop it in front! phew!
238 asort($providers);
240 $numprocs = count($processors);
241 // Display the messaging options table(s)
242 foreach ($components as $component) {
243 $table = new html_table();
244 $table->attributes['class'] = 'generaltable';
245 $table->data = array();
246 if ($component != 'moodle') {
247 $componentname = get_string('pluginname', $component);
248 } else {
249 $componentname = get_string('coresystem');
251 $table->head = array($componentname);
253 foreach ($readyprocessors as $processor) {
254 $table->head[] = get_string('pluginname', 'message_'.$processor->name);
257 // Populate the table with rows
258 foreach ( $providers as $provider) {
259 if( $provider->component != $component) {
260 continue;
262 $preferencebase = $provider->component.'_'.$provider->name;
264 $headerrow = new html_table_row();
265 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
266 $providercell = new html_table_cell($providername);
267 $providercell->header = true;
268 $providercell->colspan = $numprocs;
269 $providercell->attributes['class'] = 'c0';
270 $headerrow->cells = array($providercell);
271 $table->data[] = $headerrow;
273 foreach (array('loggedin', 'loggedoff') as $state) {
274 $optionrow = new html_table_row();
275 $optionname = new html_table_cell(get_string($state.'description', 'message'));
276 $optionname->attributes['class'] = 'c0';
277 $optionrow->cells = array($optionname);
278 foreach ($readyprocessors as $processor) {
279 // determine the default setting
280 $permitted = MESSAGE_DEFAULT_PERMITTED;
281 $defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
282 if (isset($defaultpreferences->{$defaultpreference})) {
283 $permitted = $defaultpreferences->{$defaultpreference};
285 // If settings are disallowed or forced, just display the
286 // corresponding message, if not use user settings.
287 if (in_array($permitted, array('disallowed', 'forced'))) {
288 if ($state == 'loggedoff') {
289 // skip if we are rendering the second line
290 continue;
292 $cellcontent = html_writer::nonempty_tag('div', get_string($permitted, 'message'), array('class' => 'dimmed_text'));
293 $optioncell = new html_table_cell($cellcontent);
294 $optioncell->rowspan = 2;
295 $optioncell->attributes['class'] = 'disallowed';
296 } else {
297 // determine user preferences and use them.
298 $disabled = array();
299 $checked = false;
300 if ($notificationsdisabled) {
301 $disabled['disabled'] = 1;
303 // See if user has touched this preference
304 if (isset($preferences->{$preferencebase.'_'.$state})) {
305 // User have some preferneces for this state in the database, use them
306 $checked = isset($preferences->{$preferencebase.'_'.$state}[$processor->name]);
307 } else {
308 // User has not set this preference yet, using site default preferences set by admin
309 $defaultpreference = 'message_provider_'.$preferencebase.'_'.$state;
310 if (isset($defaultpreferences->{$defaultpreference})) {
311 $checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
314 $elementname = $preferencebase.'_'.$state.'['.$processor->name.']';
315 // prepare language bits
316 $processorname = get_string('pluginname', 'message_'.$processor->name);
317 $statename = get_string($state, 'message');
318 $labelparams = array(
319 'provider' => $providername,
320 'processor' => $processorname,
321 'state' => $statename
323 $label = get_string('sendingviawhen', 'message', $labelparams);
324 $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
325 $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array_merge(array('id' => $elementname, 'class' => 'notificationpreference'), $disabled));
326 $optioncell = new html_table_cell($cellcontent);
327 $optioncell->attributes['class'] = 'mdl-align';
329 $optionrow->cells[] = $optioncell;
331 $table->data[] = $optionrow;
334 $output .= html_writer::start_tag('div', array('class' => 'messagesettingcomponent'));
335 $output .= html_writer::table($table);
336 $output .= html_writer::end_tag('div');
339 $output .= html_writer::end_tag('fieldset');
341 foreach ($processors as $processor) {
342 if (($processorconfigform = $processor->object->config_form($preferences)) && $processor->enabled) {
343 $output .= html_writer::start_tag('fieldset', array('id' => 'messageprocessor_'.$processor->name, 'class' => 'clearfix'));
344 $output .= html_writer::nonempty_tag('legend', get_string('pluginname', 'message_'.$processor->name), array('class' => 'ftoggler'));
345 $output .= html_writer::start_tag('div');
346 $output .= $processorconfigform;
347 $output .= html_writer::end_tag('div');
348 $output .= html_writer::end_tag('fieldset');
352 $output .= html_writer::start_tag('fieldset', array('id' => 'messageprocessor_general', 'class' => 'clearfix'));
353 $output .= html_writer::nonempty_tag('legend', get_string('generalsettings','admin'), array('class' => 'ftoggler'));
355 $output .= html_writer::start_tag('div');
356 $output .= html_writer::checkbox('blocknoncontacts', 1, $preferences->blocknoncontacts, get_string('blocknoncontacts', 'message'));
357 $output .= html_writer::end_tag('div');
359 $disableallcheckbox = html_writer::checkbox('disableall', 1, $notificationsdisabled, get_string('disableall', 'message'), array('class'=>'disableallcheckbox'));
360 $disableallcheckbox .= $this->output->help_icon('disableall', 'message');
361 $output .= html_writer::nonempty_tag('div', $disableallcheckbox, array('class'=>'disableall'));
363 $output .= html_writer::end_tag('fieldset');
364 $output .= html_writer::start_tag('div', array('class' => 'mdl-align'));
365 $output .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('updatemyprofile'), 'class' => 'form-submit'));
366 $output .= html_writer::end_tag('div');
367 $output .= html_writer::end_tag('form');
368 return $output;