Merge branch 'MDL-54742-master' of git://github.com/mihailges/moodle
[moodle.git] / message / renderer.php
blob1aac25975b4a80fe3695cd2392872ff63d5df980
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, $this->output->pix_icon('t/hide', get_string('outputenabled', 'message')));
77 } else {
78 $row->attributes['class'] = 'dimmed_text';
79 $url = new moodle_url('/admin/message.php', array('enable' => $processor->id, 'sesskey' => sesskey()));
80 $enable->text = html_writer::link($url, $this->output->pix_icon('t/show', get_string('outputdisabled', 'message')));
82 // Settings
83 $settings = new html_table_cell();
84 if ($processor->available && $processor->hassettings) {
85 $settingsurl = new moodle_url('settings.php', array('section' => 'messagesetting'.$processor->name));
86 $settings->text = html_writer::link($settingsurl, get_string('settings', 'message'));
89 $row->cells = array($name, $enable, $settings);
90 $table->data[] = $row;
92 return html_writer::table($table);
95 /**
96 * Display the interface to manage default message outputs
98 * @param array $processors array of objects containing message processors
99 * @param array $providers array of objects containing message providers
100 * @param array $preferences array of objects containing current preferences
101 * @return string The text to render
103 public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
104 global $CFG;
106 // Prepare list of options for dropdown menu
107 $options = array();
108 foreach (array('disallowed', 'permitted', 'forced') as $setting) {
109 $options[$setting] = get_string($setting, 'message');
112 $output = html_writer::start_tag('form', array('id'=>'defaultmessageoutputs', 'method'=>'post'));
113 $output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
115 // Display users outputs table
116 $table = new html_table();
117 $table->attributes['class'] = 'generaltable';
118 $table->data = array();
119 $table->head = array('');
121 // Populate the header row
122 foreach ($processors as $processor) {
123 $table->head[] = get_string('pluginname', 'message_'.$processor->name);
125 // Add enable/disable to head
126 $table->head[] = get_string('enabled', 'core_message');
128 // Generate the matrix of settings for each provider and processor
129 foreach ($providers as $provider) {
130 $row = new html_table_row();
131 $row->attributes['class'] = 'defaultmessageoutputs';
132 $row->cells = array();
134 // Provider Name
135 $providername = get_string('messageprovider:'.$provider->name, $provider->component);
136 $row->cells[] = new html_table_cell($providername);
137 $providersettingprefix = $provider->component.'_'.$provider->name.'_';
138 $disableprovidersetting = $providersettingprefix.'disable';
139 $providerdisabled = !empty($preferences->$disableprovidersetting);
140 // Settings for each processor
141 foreach ($processors as $processor) {
142 $cellcontent = '';
143 foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
144 // pepare element and preference names
145 $elementname = $providersettingprefix.$setting.'['.$processor->name.']';
146 $preferencebase = $providersettingprefix.$setting;
147 // prepare language bits
148 $processorname = get_string('pluginname', 'message_'.$processor->name);
149 $statename = get_string($setting, 'message');
150 $labelparams = array(
151 'provider' => $providername,
152 'processor' => $processorname,
153 'state' => $statename
155 if ($setting == 'permitted') {
156 $label = get_string('sendingvia', 'message', $labelparams);
157 // determine the current setting or use default
158 $select = MESSAGE_DEFAULT_PERMITTED;
159 $preference = $processor->name.'_provider_'.$preferencebase;
160 if ($providerdisabled) {
161 $select = MESSAGE_DISALLOWED;
162 } else if (array_key_exists($preference, $preferences)) {
163 $select = $preferences->{$preference};
165 // dropdown menu
166 $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
167 $cellcontent .= html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
168 $cellcontent .= html_writer::tag('div', get_string('defaults', 'message'));
169 } else {
170 $label = get_string('sendingviawhen', 'message', $labelparams);
171 // determine the current setting based on the 'permitted' setting above
172 $checked = false;
173 if ($select == 'forced') {
174 $checked = true;
175 } else if ($select == 'permitted') {
176 $preference = 'message_provider_'.$preferencebase;
177 if (array_key_exists($preference, $preferences)) {
178 $checked = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
181 // generate content
182 $cellcontent .= html_writer::start_tag('div');
183 $cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
184 $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
185 $cellcontent .= $statename;
186 $cellcontent .= html_writer::end_tag('div');
189 $row->cells[] = new html_table_cell($cellcontent);
191 $disableprovider = html_writer::checkbox($disableprovidersetting, 1, !$providerdisabled, '',
192 array('id' => $disableprovidersetting, 'class' => 'messagedisable'));
193 $disableprovider = html_writer::tag('div', $disableprovider);
194 $row->cells[] = new html_table_cell($disableprovider);
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');
203 return $output;
207 * Display the interface for notification preferences
209 * @param object $user instance of a user
210 * @return string The text to render
212 public function render_user_notification_preferences($user) {
213 $processors = get_message_processors();
214 $providers = message_get_providers_for_user($user->id);
216 $preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user);
217 $notificationlistoutput = new \core_message\output\preferences\notification_list($processors, $providers,
218 $preferences, $user);
219 return $this->render_from_template('message/notification_preferences',
220 $notificationlistoutput->export_for_template($this));
224 * Display the interface for message preferences
226 * @param object $user instance of a user
227 * @return string The text to render
229 public function render_user_message_preferences($user) {
230 // Filter out enabled, available system_configured and user_configured processors only.
231 $readyprocessors = array_filter(get_message_processors(), function($processor) {
232 return $processor->enabled &&
233 $processor->configured &&
234 $processor->object->is_user_configured() &&
235 // Filter out processors that don't have and message preferences to configure.
236 $processor->object->has_message_preferences();
239 $providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
240 return $provider->component === 'moodle';
242 $preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user);
243 $notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors,
244 $providers, $preferences, $user);
245 $context = $notificationlistoutput->export_for_template($this);
246 $context['blocknoncontacts'] = get_user_preferences('message_blocknoncontacts', '', $user->id) ? true : false;
248 return $this->render_from_template('message/message_preferences', $context);