2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
30 * Class for rendering various message objects
33 * @copyright 2011 Lancaster University Network Services Limited
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_message_renderer
extends plugin_renderer_base
{
39 * Display the interface to manage message outputs
41 * @param mixed $processors array of objects containing message processors
42 * @return string The text to render
44 public function manage_messageoutputs($processors) {
46 // Display the current workflows
47 $table = new html_table();
48 $table->attributes
['class'] = 'generaltable';
49 $table->data
= array();
53 get_string('settings'),
55 $table->colclasses
= array(
56 'displayname', 'availability', 'settings',
59 foreach ($processors as $processor) {
60 $row = new html_table_row();
61 $row->attributes
['class'] = 'messageoutputs';
64 $name = new html_table_cell(get_string('pluginname', 'message_'.$processor->name
));
67 $enable = new html_table_cell();
68 $enable->attributes
['class'] = 'mdl-align';
69 if (!$processor->available
) {
70 $enable->text
= html_writer
::nonempty_tag('span', get_string('outputnotavailable', 'message'), array('class' => 'error'));
71 } else if (!$processor->configured
) {
72 $enable->text
= html_writer
::nonempty_tag('span', get_string('outputnotconfigured', 'message'), array('class' => 'error'));
73 } else if ($processor->enabled
) {
74 $url = new moodle_url('/admin/message.php', array('disable' => $processor->id
, 'sesskey' => sesskey()));
75 $enable->text
= html_writer
::link($url, html_writer
::empty_tag('img',
76 array('src' => $this->output
->pix_url('i/hide'),
78 'title' => get_string('outputenabled', 'message'),
79 'alt' => get_string('outputenabled', 'message'),
83 $name->attributes
['class'] = 'dimmed_text';
84 $url = new moodle_url('/admin/message.php', array('enable' => $processor->id
, 'sesskey' => sesskey()));
85 $enable->text
= html_writer
::link($url, html_writer
::empty_tag('img',
86 array('src' => $this->output
->pix_url('i/show'),
88 'title' => get_string('outputdisabled', 'message'),
89 'alt' => get_string('outputdisabled', 'message'),
94 $settings = new html_table_cell();
95 if ($processor->available
&& $processor->hassettings
) {
96 $settingsurl = new moodle_url('settings.php', array('section' => 'messagesetting'.$processor->name
));
97 $settings->text
= html_writer
::link($settingsurl, get_string('settings', 'message'));
100 $row->cells
= array($name, $enable, $settings);
101 $table->data
[] = $row;
103 return html_writer
::table($table);
107 * Display the interface to manage default message outputs
109 * @param mixed $processors array of objects containing message processors
110 * @param mixed $providers array of objects containing message providers
111 * @param mixed $preferences array of objects containing current preferences
112 * @return string The text to render
114 public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
117 // Prepare list of options for dropdown menu
119 foreach (array('disallowed', 'permitted', 'forced') as $setting) {
120 $options[$setting] = get_string($setting, 'message');
123 $output = html_writer
::start_tag('form', array('id'=>'defaultmessageoutputs', 'method'=>'post'));
124 $output .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
126 // Display users outputs table
127 $table = new html_table();
128 $table->attributes
['class'] = 'generaltable';
129 $table->data
= array();
130 $table->head
= array('');
132 // Populate the header row
133 foreach ($processors as $processor) {
134 $table->head
[] = get_string('pluginname', 'message_'.$processor->name
);
136 // Generate the matrix of settings for each provider and processor
137 foreach ($providers as $provider) {
138 $row = new html_table_row();
139 $row->attributes
['class'] = 'defaultmessageoutputs';
140 $row->cells
= array();
143 $providername = get_string('messageprovider:'.$provider->name
, $provider->component
);
144 $row->cells
[] = new html_table_cell($providername);
146 // Settings for each processor
147 foreach ($processors as $processor) {
149 foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
150 // pepare element and preference names
151 $elementname = $provider->component
.'_'.$provider->name
.'_'.$setting.'['.$processor->name
.']';
152 $preferencebase = $provider->component
.'_'.$provider->name
.'_'.$setting;
153 // prepare language bits
154 $processorname = get_string('pluginname', 'message_'.$processor->name
);
155 $statename = get_string($setting, 'message');
156 $labelparams = array(
157 'provider' => $providername,
158 'processor' => $processorname,
159 'state' => $statename
161 if ($setting == 'permitted') {
162 $label = get_string('sendingvia', 'message', $labelparams);
163 // determine the current setting or use default
164 $select = MESSAGE_DEFAULT_PERMITTED
;
165 $preference = $processor->name
.'_provider_'.$preferencebase;
166 if (array_key_exists($preference, $preferences)) {
167 $select = $preferences->{$preference};
170 $cellcontent = html_writer
::label($label, $elementname, true, array('class' => 'accesshide'));
171 $cellcontent .= html_writer
::select($options, $elementname, $select, false, array('id' => $elementname));
172 $cellcontent .= html_writer
::tag('div', get_string('defaults', 'message'));
174 $label = get_string('sendingviawhen', 'message', $labelparams);
175 // determine the current setting based on the 'permitted' setting above
177 if ($select == 'forced') {
179 } else if ($select == 'permitted') {
180 $preference = 'message_provider_'.$preferencebase;
181 if (array_key_exists($preference, $preferences)) {
182 $checked = (int)in_array($processor->name
, explode(',', $preferences->{$preference}));
186 $cellcontent .= html_writer
::start_tag('div');
187 $cellcontent .= html_writer
::label($label, $elementname, true, array('class' => 'accesshide'));
188 $cellcontent .= html_writer
::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
189 $cellcontent .= $statename;
190 $cellcontent .= html_writer
::end_tag('div');
193 $row->cells
[] = new html_table_cell($cellcontent);
195 $table->data
[] = $row;
198 $output .= html_writer
::table($table);
199 $output .= html_writer
::start_tag('div', array('class' => 'form-buttons'));
200 $output .= html_writer
::empty_tag('input', array('type' => 'submit', 'value' => get_string('savechanges','admin'), 'class' => 'form-submit'));
201 $output .= html_writer
::end_tag('div');
202 $output .= html_writer
::end_tag('form');
207 * Display the interface for messaging options
209 * @param mixed $processors array of objects containing message processors
210 * @param mixed $providers array of objects containing message providers
211 * @param mixed $preferences array of objects containing current preferences
212 * @param mixed $defaultpreferences array of objects containing site default preferences
213 * @return string The text to render
215 public function manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences) {
216 // Filter out enabled, available system_configured and user_configured processors only.
217 $readyprocessors = array_filter($processors, create_function('$a', 'return $a->enabled && $a->configured && $a->object->is_user_configured();'));
219 // Start the form. We're not using mform here because of our special formatting needs ...
220 $output = html_writer
::start_tag('form', array('method'=>'post', 'class' => 'mform'));
221 $output .= html_writer
::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
223 /// Settings table...
224 $output .= html_writer
::start_tag('fieldset', array('id' => 'providers', 'class' => 'clearfix'));
225 $output .= html_writer
::nonempty_tag('legend', get_string('providers_config', 'message'), array('class' => 'ftoggler'));
227 // Display the messging options table
228 $table = new html_table();
229 $table->attributes
['class'] = 'generaltable';
230 $table->data
= array();
231 $table->head
= array('');
233 foreach ($readyprocessors as $processor) {
234 $table->head
[] = get_string('pluginname', 'message_'.$processor->name
);
237 $number_procs = count($processors);
238 // Populate the table with rows
239 foreach ( $providers as $provider) {
240 $preferencebase = $provider->component
.'_'.$provider->name
;
242 $headerrow = new html_table_row();
243 $providername = get_string('messageprovider:'.$provider->name
, $provider->component
);
244 $providercell = new html_table_cell($providername);
245 $providercell->header
= true;
246 $providercell->colspan
= $number_procs +
1;
247 $providercell->attributes
['class'] = 'c0';
248 $headerrow->cells
= array($providercell);
249 $table->data
[] = $headerrow;
251 foreach (array('loggedin', 'loggedoff') as $state) {
252 $optionrow = new html_table_row();
253 $optionname = new html_table_cell(get_string($state.'description', 'message'));
254 $optionname->attributes
['class'] = 'c0';
255 $optionrow->cells
= array($optionname);
256 foreach ($readyprocessors as $processor) {
257 // determine the default setting
258 $permitted = MESSAGE_DEFAULT_PERMITTED
;
259 $defaultpreference = $processor->name
.'_provider_'.$preferencebase.'_permitted';
260 if (isset($defaultpreferences->{$defaultpreference})) {
261 $permitted = $defaultpreferences->{$defaultpreference};
263 // If settings are disallowed, just display the message that
264 // the setting is not permitted, if not use user settings or
266 if ($permitted == 'disallowed') {
267 if ($state == 'loggedoff') {
268 // skip if we are rendering the second line
271 $cellcontent = html_writer
::nonempty_tag('div', get_string('notpermitted', 'message'), array('class' => 'dimmed_text'));
272 $optioncell = new html_table_cell($cellcontent);
273 $optioncell->rowspan
= 2;
274 $optioncell->attributes
['class'] = 'disallowed';
276 // determine user preferences and use then unless we force
279 if ($permitted == 'forced') {
281 $disabled['disabled'] = 1;
284 // See if hser has touched this preference
285 if (isset($preferences->{$preferencebase.'_'.$state})) {
286 // User have some preferneces for this state in the database, use them
287 $checked = isset($preferences->{$preferencebase.'_'.$state}[$processor->name
]);
289 // User has not set this preference yet, using site default preferences set by admin
290 $defaultpreference = 'message_provider_'.$preferencebase.'_'.$state;
291 if (isset($defaultpreferences->{$defaultpreference})) {
292 $checked = (int)in_array($processor->name
, explode(',', $defaultpreferences->{$defaultpreference}));
296 $elementname = $preferencebase.'_'.$state.'['.$processor->name
.']';
297 // prepare language bits
298 $processorname = get_string('pluginname', 'message_'.$processor->name
);
299 $statename = get_string($state, 'message');
300 $labelparams = array(
301 'provider' => $providername,
302 'processor' => $processorname,
303 'state' => $statename
305 $label = get_string('sendingviawhen', 'message', $labelparams);
306 $cellcontent = html_writer
::label($label, $elementname, true, array('class' => 'accesshide'));
307 $cellcontent .= html_writer
::checkbox($elementname, 1, $checked, '', array_merge(array('id' => $elementname), $disabled));
308 $optioncell = new html_table_cell($cellcontent);
309 $optioncell->attributes
['class'] = 'mdl-align';
311 $optionrow->cells
[] = $optioncell;
313 $table->data
[] = $optionrow;
316 $output .= html_writer
::start_tag('div');
317 $output .= html_writer
::table($table);
318 $output .= html_writer
::end_tag('div');
319 $output .= html_writer
::end_tag('fieldset');
321 foreach ($processors as $processor) {
322 if (($processorconfigform = $processor->object->config_form($preferences)) && $processor->enabled
) {
323 $output .= html_writer
::start_tag('fieldset', array('id' => 'messageprocessor_'.$processor->name
, 'class' => 'clearfix'));
324 $output .= html_writer
::nonempty_tag('legend', get_string('pluginname', 'message_'.$processor->name
), array('class' => 'ftoggler'));
325 $output .= html_writer
::start_tag('div');
326 $output .= $processorconfigform;
327 $output .= html_writer
::end_tag('div');
328 $output .= html_writer
::end_tag('fieldset');
332 $output .= html_writer
::start_tag('fieldset', array('id' => 'messageprocessor_general', 'class' => 'clearfix'));
333 $output .= html_writer
::nonempty_tag('legend', get_string('generalsettings','admin'), array('class' => 'ftoggler'));
334 $output .= html_writer
::start_tag('div');
335 $output .= get_string('blocknoncontacts', 'message').': ';
336 $output .= html_writer
::checkbox('blocknoncontacts', 1, $preferences->blocknoncontacts
, '');
337 $output .= html_writer
::end_tag('div');
338 $output .= html_writer
::end_tag('fieldset');
339 $output .= html_writer
::start_tag('div', array('class' => 'mdl-align'));
340 $output .= html_writer
::empty_tag('input', array('type' => 'submit', 'value' => get_string('updatemyprofile'), 'class' => 'form-submit'));
341 $output .= html_writer
::end_tag('div');
342 $output .= html_writer
::end_tag('form');