defaultoutputs.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * Default message outputs configuration page
  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. require_once(__DIR__ . '/../config.php');
  24. require_once($CFG->dirroot . '/message/lib.php');
  25. require_once($CFG->libdir.'/adminlib.php');
  26. // This is an admin page
  27. admin_externalpage_setup('defaultmessageoutputs');
  28. // Require site configuration capability
  29. require_capability('moodle/site:config', context_system::instance());
  30. // Fetch processors
  31. $processors = get_message_processors(true);
  32. // Fetch message providers
  33. $providers = get_message_providers();
  34. if (($form = data_submitted()) && confirm_sesskey()) {
  35. $preferences = array();
  36. // Prepare default message outputs settings
  37. foreach ( $providers as $provider) {
  38. $componentproviderbase = $provider->component.'_'.$provider->name;
  39. $disableprovidersetting = $componentproviderbase.'_disable';
  40. $providerdisabled = false;
  41. if (!isset($form->$disableprovidersetting)) {
  42. $providerdisabled = true;
  43. $preferences[$disableprovidersetting] = 1;
  44. } else {
  45. $preferences[$disableprovidersetting] = 0;
  46. }
  47. foreach (array('permitted', 'loggedin', 'loggedoff') as $setting){
  48. $value = null;
  49. $componentprovidersetting = $componentproviderbase.'_'.$setting;
  50. if ($setting == 'permitted') {
  51. // if we deal with permitted select element, we need to create individual
  52. // setting for each possible processor. Note that this block will
  53. // always be processed first after entring parental foreach iteration
  54. // so we can change form values on this stage.
  55. foreach($processors as $processor) {
  56. $value = '';
  57. if (isset($form->{$componentprovidersetting}[$processor->name])) {
  58. $value = $form->{$componentprovidersetting}[$processor->name];
  59. }
  60. // Ensure that loggedin loggedoff options are set correctly
  61. // for this permission
  62. if (($value == 'disallowed') || $providerdisabled) {
  63. // It might be better to unset them, but I can't figure out why that cause error
  64. $form->{$componentproviderbase.'_loggedin'}[$processor->name] = 0;
  65. $form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 0;
  66. } else if ($value == 'forced') {
  67. $form->{$componentproviderbase.'_loggedin'}[$processor->name] = 1;
  68. $form->{$componentproviderbase.'_loggedoff'}[$processor->name] = 1;
  69. }
  70. // record the site preference
  71. $preferences[$processor->name.'_provider_'.$componentprovidersetting] = $value;
  72. }
  73. } else if (array_key_exists($componentprovidersetting, $form)) {
  74. // we must be processing loggedin or loggedoff checkboxes. Store
  75. // defained comma-separated processors as setting value.
  76. // Using array_filter eliminates elements set to 0 above
  77. $value = join(',', array_keys(array_filter($form->{$componentprovidersetting})));
  78. if (empty($value)) {
  79. $value = null;
  80. }
  81. }
  82. if ($setting != 'permitted') {
  83. // we have already recoded site preferences for 'permitted' type
  84. $preferences['message_provider_'.$componentprovidersetting] = $value;
  85. }
  86. }
  87. }
  88. // Update database
  89. $transaction = $DB->start_delegated_transaction();
  90. foreach ($preferences as $name => $value) {
  91. set_config($name, $value, 'message');
  92. }
  93. $transaction->allow_commit();
  94. // Redirect
  95. $url = new moodle_url('defaultoutputs.php');
  96. redirect($url);
  97. }
  98. // Page settings
  99. $PAGE->set_context(context_system::instance());
  100. $PAGE->requires->js_init_call('M.core_message.init_defaultoutputs');
  101. // Grab the renderer
  102. $renderer = $PAGE->get_renderer('core', 'message');
  103. // Display the manage message outputs interface
  104. $preferences = get_message_output_default_preferences();
  105. $messageoutputs = $renderer->manage_defaultmessageoutputs($processors, $providers, $preferences);
  106. // Display the page
  107. echo $OUTPUT->header();
  108. echo $OUTPUT->heading(get_string('defaultmessageoutputs', 'message'));
  109. echo $messageoutputs;
  110. echo $OUTPUT->footer();