renderer.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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.
  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/>.
  16. /**
  17. * Contains renderer objects for messaging
  18. *
  19. * @package core_message
  20. * @copyright 2011 Lancaster University Network Services Limited
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * message Renderer
  26. *
  27. * Class for rendering various message objects
  28. *
  29. * @package core_message
  30. * @subpackage message
  31. * @copyright 2011 Lancaster University Network Services Limited
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class core_message_renderer extends plugin_renderer_base {
  35. /**
  36. * Display the interface to manage message outputs
  37. *
  38. * @param array $processors array of objects containing message processors
  39. * @return string The text to render
  40. */
  41. public function manage_messageoutputs($processors) {
  42. global $CFG;
  43. // Display the current workflows
  44. $table = new html_table();
  45. $table->attributes['class'] = 'admintable generaltable';
  46. $table->data = array();
  47. $table->head = array(
  48. get_string('name'),
  49. get_string('enable'),
  50. get_string('settings'),
  51. );
  52. $table->colclasses = array(
  53. 'displayname', 'availability', 'settings',
  54. );
  55. foreach ($processors as $processor) {
  56. $row = new html_table_row();
  57. $row->attributes['class'] = 'messageoutputs';
  58. // Name
  59. $name = new html_table_cell(get_string('pluginname', 'message_'.$processor->name));
  60. // Enable
  61. $enable = new html_table_cell();
  62. $enable->attributes['class'] = 'mdl-align';
  63. if (!$processor->available) {
  64. $enable->text = html_writer::nonempty_tag('span', get_string('outputnotavailable', 'message'), array('class' => 'error'));
  65. } else if (!$processor->configured) {
  66. $enable->text = html_writer::nonempty_tag('span', get_string('outputnotconfigured', 'message'), array('class' => 'error'));
  67. } else if ($processor->enabled) {
  68. $url = new moodle_url('/admin/message.php', array('disable' => $processor->id, 'sesskey' => sesskey()));
  69. $enable->text = html_writer::link($url, html_writer::empty_tag('img',
  70. array('src' => $this->output->pix_url('t/hide'),
  71. 'class' => 'iconsmall',
  72. 'title' => get_string('outputenabled', 'message'),
  73. 'alt' => get_string('outputenabled', 'message'),
  74. )
  75. ));
  76. } else {
  77. $row->attributes['class'] = 'dimmed_text';
  78. $url = new moodle_url('/admin/message.php', array('enable' => $processor->id, 'sesskey' => sesskey()));
  79. $enable->text = html_writer::link($url, html_writer::empty_tag('img',
  80. array('src' => $this->output->pix_url('t/show'),
  81. 'class' => 'iconsmall',
  82. 'title' => get_string('outputdisabled', 'message'),
  83. 'alt' => get_string('outputdisabled', 'message'),
  84. )
  85. ));
  86. }
  87. // Settings
  88. $settings = new html_table_cell();
  89. if ($processor->available && $processor->hassettings) {
  90. $settingsurl = new moodle_url('settings.php', array('section' => 'messagesetting'.$processor->name));
  91. $settings->text = html_writer::link($settingsurl, get_string('settings', 'message'));
  92. }
  93. $row->cells = array($name, $enable, $settings);
  94. $table->data[] = $row;
  95. }
  96. return html_writer::table($table);
  97. }
  98. /**
  99. * Display the interface to manage default message outputs
  100. *
  101. * @param array $processors array of objects containing message processors
  102. * @param array $providers array of objects containing message providers
  103. * @param array $preferences array of objects containing current preferences
  104. * @return string The text to render
  105. */
  106. public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
  107. global $CFG;
  108. // Prepare list of options for dropdown menu
  109. $options = array();
  110. foreach (array('disallowed', 'permitted', 'forced') as $setting) {
  111. $options[$setting] = get_string($setting, 'message');
  112. }
  113. $output = html_writer::start_tag('form', array('id'=>'defaultmessageoutputs', 'method'=>'post'));
  114. $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('');
  120. // Populate the header row
  121. foreach ($processors as $processor) {
  122. $table->head[] = get_string('pluginname', 'message_'.$processor->name);
  123. }
  124. // Add enable/disable to head
  125. $table->head[] = get_string('enabled', 'core_message');
  126. // Generate the matrix of settings for each provider and processor
  127. foreach ($providers as $provider) {
  128. $row = new html_table_row();
  129. $row->attributes['class'] = 'defaultmessageoutputs';
  130. $row->cells = array();
  131. // Provider Name
  132. $providername = get_string('messageprovider:'.$provider->name, $provider->component);
  133. $row->cells[] = new html_table_cell($providername);
  134. $providersettingprefix = $provider->component.'_'.$provider->name.'_';
  135. $disableprovidersetting = $providersettingprefix.'disable';
  136. $providerdisabled = !empty($preferences->$disableprovidersetting);
  137. // Settings for each processor
  138. foreach ($processors as $processor) {
  139. $cellcontent = '';
  140. foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
  141. // pepare element and preference names
  142. $elementname = $providersettingprefix.$setting.'['.$processor->name.']';
  143. $preferencebase = $providersettingprefix.$setting;
  144. // prepare language bits
  145. $processorname = get_string('pluginname', 'message_'.$processor->name);
  146. $statename = get_string($setting, 'message');
  147. $labelparams = array(
  148. 'provider' => $providername,
  149. 'processor' => $processorname,
  150. 'state' => $statename
  151. );
  152. if ($setting == 'permitted') {
  153. $label = get_string('sendingvia', 'message', $labelparams);
  154. // determine the current setting or use default
  155. $select = MESSAGE_DEFAULT_PERMITTED;
  156. $preference = $processor->name.'_provider_'.$preferencebase;
  157. if ($providerdisabled) {
  158. $select = MESSAGE_DISALLOWED;
  159. } else if (array_key_exists($preference, $preferences)) {
  160. $select = $preferences->{$preference};
  161. }
  162. // dropdown menu
  163. $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
  164. $cellcontent .= html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
  165. $cellcontent .= html_writer::tag('div', get_string('defaults', 'message'));
  166. } else {
  167. $label = get_string('sendingviawhen', 'message', $labelparams);
  168. // determine the current setting based on the 'permitted' setting above
  169. $checked = false;
  170. if ($select == 'forced') {
  171. $checked = true;
  172. } else if ($select == 'permitted') {
  173. $preference = 'message_provider_'.$preferencebase;
  174. if (array_key_exists($preference, $preferences)) {
  175. $checked = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
  176. }
  177. }
  178. // generate content
  179. $cellcontent .= html_writer::start_tag('div');
  180. $cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
  181. $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
  182. $cellcontent .= $statename;
  183. $cellcontent .= html_writer::end_tag('div');
  184. }
  185. }
  186. $row->cells[] = new html_table_cell($cellcontent);
  187. }
  188. $disableprovider = html_writer::checkbox($disableprovidersetting, 1, !$providerdisabled, '',
  189. array('id' => $disableprovidersetting, 'class' => 'messagedisable'));
  190. $disableprovider = html_writer::tag('div', $disableprovider);
  191. $row->cells[] = new html_table_cell($disableprovider);
  192. $table->data[] = $row;
  193. }
  194. $output .= html_writer::table($table);
  195. $output .= html_writer::start_tag('div', array('class' => 'form-buttons'));
  196. $output .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('savechanges','admin'), 'class' => 'form-submit'));
  197. $output .= html_writer::end_tag('div');
  198. $output .= html_writer::end_tag('form');
  199. return $output;
  200. }
  201. /**
  202. * Display the interface for messaging options
  203. *
  204. * @param array $processors Array of objects containing message processors
  205. * @param array $providers Array of objects containing message providers
  206. * @param array $preferences Array of objects containing current preferences
  207. * @param array $defaultpreferences Array of objects containing site default preferences
  208. * @param bool $notificationsdisabled Indicate if the user's "emailstop" flag is set (shouldn't receive any non-forced notifications)
  209. * @param null|int $userid User id, or null if current user.
  210. * @return string The text to render
  211. */
  212. public function manage_messagingoptions($processors, $providers, $preferences, $defaultpreferences,
  213. $notificationsdisabled = false, $userid = null) {
  214. global $USER;
  215. if (empty($userid)) {
  216. $userid = $USER->id;
  217. }
  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();'));
  220. // Start the form. We're not using mform here because of our special formatting needs ...
  221. $output = html_writer::start_tag('form', array('method'=>'post', 'class' => 'mform'));
  222. $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'));
  226. foreach($providers as $provider) {
  227. if($provider->component != 'moodle') {
  228. $components[] = $provider->component;
  229. }
  230. }
  231. // Lets arrange by components so that core settings (moodle) appear as the first table.
  232. $components = array_unique($components);
  233. asort($components);
  234. array_unshift($components, 'moodle'); // pop it in front! phew!
  235. asort($providers);
  236. $numprocs = count($processors);
  237. // Display the messaging options table(s)
  238. foreach ($components as $component) {
  239. $provideradded = false;
  240. $table = new html_table();
  241. $table->attributes['class'] = 'generaltable';
  242. $table->data = array();
  243. if ($component != 'moodle') {
  244. $componentname = get_string('pluginname', $component);
  245. } else {
  246. $componentname = get_string('coresystem');
  247. }
  248. $table->head = array($componentname);
  249. foreach ($readyprocessors as $processor) {
  250. $table->head[] = get_string('pluginname', 'message_'.$processor->name);
  251. }
  252. // Populate the table with rows
  253. foreach ($providers as $provider) {
  254. $preferencebase = $provider->component.'_'.$provider->name;
  255. // If provider component is not same or provider disabled then don't show.
  256. if (($provider->component != $component) ||
  257. (!empty($defaultpreferences->{$preferencebase.'_disable'}))) {
  258. continue;
  259. }
  260. $provideradded = true;
  261. $headerrow = new html_table_row();
  262. $providername = get_string('messageprovider:'.$provider->name, $provider->component);
  263. $providercell = new html_table_cell($providername);
  264. $providercell->header = true;
  265. $providercell->colspan = $numprocs;
  266. $providercell->attributes['class'] = 'c0';
  267. $headerrow->cells = array($providercell);
  268. $table->data[] = $headerrow;
  269. foreach (array('loggedin', 'loggedoff') as $state) {
  270. $optionrow = new html_table_row();
  271. $optionname = new html_table_cell(get_string($state.'description', 'message'));
  272. $optionname->attributes['class'] = 'c0';
  273. $optionrow->cells = array($optionname);
  274. foreach ($readyprocessors as $processor) {
  275. // determine the default setting
  276. $permitted = MESSAGE_DEFAULT_PERMITTED;
  277. $defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
  278. if (isset($defaultpreferences->{$defaultpreference})) {
  279. $permitted = $defaultpreferences->{$defaultpreference};
  280. }
  281. // If settings are disallowed or forced, just display the
  282. // corresponding message, if not use user settings.
  283. if (in_array($permitted, array('disallowed', 'forced'))) {
  284. if ($state == 'loggedoff') {
  285. // skip if we are rendering the second line
  286. continue;
  287. }
  288. $cellcontent = html_writer::nonempty_tag('div', get_string($permitted, 'message'), array('class' => 'dimmed_text'));
  289. $optioncell = new html_table_cell($cellcontent);
  290. $optioncell->rowspan = 2;
  291. $optioncell->attributes['class'] = 'disallowed';
  292. } else {
  293. // determine user preferences and use them.
  294. $disabled = array();
  295. $checked = false;
  296. if ($notificationsdisabled) {
  297. $disabled['disabled'] = 1;
  298. }
  299. // See if user has touched this preference
  300. if (isset($preferences->{$preferencebase.'_'.$state})) {
  301. // User have some preferneces for this state in the database, use them
  302. $checked = isset($preferences->{$preferencebase.'_'.$state}[$processor->name]);
  303. } else {
  304. // User has not set this preference yet, using site default preferences set by admin
  305. $defaultpreference = 'message_provider_'.$preferencebase.'_'.$state;
  306. if (isset($defaultpreferences->{$defaultpreference})) {
  307. $checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
  308. }
  309. }
  310. $elementname = $preferencebase.'_'.$state.'['.$processor->name.']';
  311. // prepare language bits
  312. $processorname = get_string('pluginname', 'message_'.$processor->name);
  313. $statename = get_string($state, 'message');
  314. $labelparams = array(
  315. 'provider' => $providername,
  316. 'processor' => $processorname,
  317. 'state' => $statename
  318. );
  319. $label = get_string('sendingviawhen', 'message', $labelparams);
  320. $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
  321. $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array_merge(array('id' => $elementname, 'class' => 'notificationpreference'), $disabled));
  322. $optioncell = new html_table_cell($cellcontent);
  323. $optioncell->attributes['class'] = 'mdl-align';
  324. }
  325. $optionrow->cells[] = $optioncell;
  326. }
  327. $table->data[] = $optionrow;
  328. }
  329. }
  330. // Add settings only if provider added for component.
  331. if ($provideradded) {
  332. $output .= html_writer::start_tag('div', array('class' => 'messagesettingcomponent'));
  333. $output .= html_writer::table($table);
  334. $output .= html_writer::end_tag('div');
  335. }
  336. }
  337. $output .= html_writer::end_tag('fieldset');
  338. foreach ($processors as $processor) {
  339. if (($processorconfigform = $processor->object->config_form($preferences)) && $processor->enabled) {
  340. $output .= html_writer::start_tag('fieldset', array('id' => 'messageprocessor_'.$processor->name, 'class' => 'clearfix'));
  341. $output .= html_writer::nonempty_tag('legend', get_string('pluginname', 'message_'.$processor->name), array('class' => 'ftoggler'));
  342. $output .= html_writer::start_tag('div');
  343. $output .= $processorconfigform;
  344. $output .= html_writer::end_tag('div');
  345. $output .= html_writer::end_tag('fieldset');
  346. }
  347. }
  348. $output .= html_writer::start_tag('fieldset', array('id' => 'messageprocessor_general', 'class' => 'clearfix'));
  349. $output .= html_writer::nonempty_tag('legend', get_string('generalsettings','admin'), array('class' => 'ftoggler'));
  350. $output .= html_writer::start_tag('div');
  351. $output .= html_writer::checkbox('beepnewmessage', 1, $preferences->beepnewmessage, get_string('beepnewmessage', 'message'));
  352. $output .= html_writer::end_tag('div');
  353. $output .= html_writer::start_tag('div');
  354. $output .= html_writer::checkbox('blocknoncontacts', 1, $preferences->blocknoncontacts, get_string('blocknoncontacts', 'message'));
  355. $output .= html_writer::end_tag('div');
  356. $disableallcheckbox = html_writer::checkbox('disableall', 1, $notificationsdisabled, get_string('disableall', 'message'), array('class'=>'disableallcheckbox'));
  357. $disableallcheckbox .= $this->output->help_icon('disableall', 'message');
  358. $output .= html_writer::nonempty_tag('div', $disableallcheckbox, array('class'=>'disableall'));
  359. $redirect = new moodle_url("/user/preferences.php", array('userid' => $userid));
  360. $output .= html_writer::end_tag('fieldset');
  361. $output .= html_writer::start_tag('div', array('class' => 'mdl-align'));
  362. $output .= html_writer::empty_tag('input', array('type' => 'submit',
  363. 'value' => get_string('savechanges'), 'class' => 'form-submit'));
  364. $output .= html_writer::link($redirect, html_writer::empty_tag('input', array('type' => 'button',
  365. 'value' => get_string('cancel'), 'class' => 'btn-cancel')));
  366. $output .= html_writer::end_tag('div');
  367. $output .= html_writer::end_tag('form');
  368. return $output;
  369. }
  370. }