MDL-54708 message: notification popover respects message preferences
[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
39 /**
40 * Display the interface to manage message outputs
41 *
6fbd60ef
AD
42 * @param array $processors array of objects containing message processors
43 * @return string The text to render
75c34c23
RK
44 */
45 public function manage_messageoutputs($processors) {
2f2137fc 46 global $CFG;
75c34c23
RK
47 // Display the current workflows
48 $table = new html_table();
e87214bd 49 $table->attributes['class'] = 'admintable generaltable';
75c34c23
RK
50 $table->data = array();
51 $table->head = array(
52 get_string('name'),
53 get_string('enable'),
54 get_string('settings'),
55 );
56 $table->colclasses = array(
57 'displayname', 'availability', 'settings',
58 );
59
1d72e9d4 60 foreach ($processors as $processor) {
75c34c23 61 $row = new html_table_row();
2f2137fc 62 $row->attributes['class'] = 'messageoutputs';
75c34c23
RK
63
64 // Name
2fd7c451 65 $name = new html_table_cell(get_string('pluginname', 'message_'.$processor->name));
75c34c23
RK
66
67 // Enable
68 $enable = new html_table_cell();
2f2137fc 69 $enable->attributes['class'] = 'mdl-align';
75c34c23
RK
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) {
75c34c23
RK
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',
fa9c0aab
FM
77 array('src' => $this->output->pix_url('t/hide'),
78 'class' => 'iconsmall',
75c34c23
RK
79 'title' => get_string('outputenabled', 'message'),
80 'alt' => get_string('outputenabled', 'message'),
81 )
82 ));
83 } else {
e87214bd 84 $row->attributes['class'] = 'dimmed_text';
75c34c23
RK
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',
fa9c0aab
FM
87 array('src' => $this->output->pix_url('t/show'),
88 'class' => 'iconsmall',
75c34c23
RK
89 'title' => get_string('outputdisabled', 'message'),
90 'alt' => get_string('outputdisabled', 'message'),
91 )
92 ));
93 }
2f2137fc 94 // Settings
75c34c23 95 $settings = new html_table_cell();
1d72e9d4 96 if ($processor->available && $processor->hassettings) {
2f2137fc 97 $settingsurl = new moodle_url('settings.php', array('section' => 'messagesetting'.$processor->name));
75c34c23
RK
98 $settings->text = html_writer::link($settingsurl, get_string('settings', 'message'));
99 }
100
101 $row->cells = array($name, $enable, $settings);
102 $table->data[] = $row;
103 }
104 return html_writer::table($table);
105 }
1d72e9d4
RK
106
107 /**
108 * Display the interface to manage default message outputs
109 *
6fbd60ef
AD
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
1d72e9d4
RK
114 */
115 public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
116 global $CFG;
117
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');
122 }
123
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()));
126
127 // Display users outputs table
128 $table = new html_table();
129 $table->attributes['class'] = 'generaltable';
130 $table->data = array();
131 $table->head = array('');
132
133 // Populate the header row
134 foreach ($processors as $processor) {
135 $table->head[] = get_string('pluginname', 'message_'.$processor->name);
136 }
fd0f7ac5
RT
137 // Add enable/disable to head
138 $table->head[] = get_string('enabled', 'core_message');
139
1d72e9d4
RK
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();
145
146 // Provider Name
147 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
148 $row->cells[] = new html_table_cell($providername);
fd0f7ac5
RT
149 $providersettingprefix = $provider->component.'_'.$provider->name.'_';
150 $disableprovidersetting = $providersettingprefix.'disable';
151 $providerdisabled = !empty($preferences->$disableprovidersetting);
1d72e9d4
RK
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
fd0f7ac5
RT
157 $elementname = $providersettingprefix.$setting.'['.$processor->name.']';
158 $preferencebase = $providersettingprefix.$setting;
1d72e9d4
RK
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
166 );
167 if ($setting == 'permitted') {
168 $label = get_string('sendingvia', 'message', $labelparams);
169 // determine the current setting or use default
814e3735 170 $select = MESSAGE_DEFAULT_PERMITTED;
1d72e9d4 171 $preference = $processor->name.'_provider_'.$preferencebase;
fd0f7ac5
RT
172 if ($providerdisabled) {
173 $select = MESSAGE_DISALLOWED;
174 } else if (array_key_exists($preference, $preferences)) {
1d72e9d4
RK
175 $select = $preferences->{$preference};
176 }
177 // dropdown menu
814e3735
RK
178 $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
179 $cellcontent .= html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
1d72e9d4
RK
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}));
191 }
192 }
193 // generate content
194 $cellcontent .= html_writer::start_tag('div');
1d72e9d4 195 $cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
814e3735 196 $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
1d72e9d4
RK
197 $cellcontent .= $statename;
198 $cellcontent .= html_writer::end_tag('div');
199 }
200 }
201 $row->cells[] = new html_table_cell($cellcontent);
202 }
fd0f7ac5
RT
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);
1d72e9d4
RK
207 $table->data[] = $row;
208 }
209
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;
216 }
217
a0eabdd3
RW
218 /**
219 * Get the preferences for the given user.
220 *
221 * @param array $processors list of message processors
222 * @param array $providers list of message providers
223 * @param stdClass $user user
224 * @return stdClass
225 */
226 private function get_all_preferences($processors, $providers, $user) {
643b015d 227 $preferences = get_providers_preferences($providers, $user->id);
a0eabdd3
RW
228 $preferences->userdefaultemail = $user->email;//may be displayed by the email processor
229
a0eabdd3
RW
230 /// For every processors put its options on the form (need to get function from processor's lib.php)
231 foreach ($processors as $processor) {
232 $processor->object->load_data($preferences, $user->id);
233 }
234
235 //load general messaging preferences
5ee31614
RW
236 $preferences->blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user->id);
237 $preferences->mailformat = $user->mailformat;
238 $preferences->mailcharset = get_user_preferences('mailcharset', '', $user->id);
a0eabdd3
RW
239
240 return $preferences;
241 }
242
a0eabdd3
RW
243 /**
244 * Display the interface for messaging options
245 *
246 * @param object $user instance of a user
247 * @return string The text to render
248 */
249 public function render_user_preferences($user) {
250 // Filter out enabled, available system_configured and user_configured processors only.
5ee31614
RW
251 $readyprocessors = array_filter(get_message_processors(), function($processor) {
252 return $processor->enabled && $processor->configured && $processor->object->is_user_configured();
253 });
a0eabdd3
RW
254
255 $providers = message_get_providers_for_user($user->id);
256 $preferences = $this->get_all_preferences($readyprocessors, $providers, $user);
5ee31614 257 $notificationlistoutput = new \core_message\output\preferences\notification_list($readyprocessors, $providers, $preferences, $user);
5ee31614 258 $generalsettingsoutput = new \core_message\output\preferences\general_settings($preferences, $user);
2ca1f894 259
5ee31614 260 $output = $this->render_from_template('message/preferences_notifications_list', $notificationlistoutput->export_for_template($this));
5ee31614 261 $output .= $this->render_from_template('message/preferences_general_settings', $generalsettingsoutput->export_for_template($this));
9279cae6 262
814e3735
RK
263 return $output;
264 }
75c34c23 265}