Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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  /**
  18   * Contains renderer objects for messaging
  19   *
  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
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * message Renderer
  29   *
  30   * Class for rendering various message objects
  31   *
  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
  36   */
  37  class core_message_renderer extends plugin_renderer_base {
  38  
  39      /**
  40       * Display the interface to manage both message outputs and default message outputs
  41       *
  42       * @param  array $allprocessors  array of objects containing all message processors
  43       * @param  array $processors  array of objects containing active message processors
  44       * @param  array $providers   array of objects containing message providers
  45       * @param  stdClass $preferences object containing current preferences
  46       * @return string The text to render
  47       */
  48      public function manage_messageoutput_settings($allprocessors, $processors, $providers, $preferences) {
  49          $output = html_writer::start_tag('form', array('id' => 'defaultmessageoutputs', 'method' => 'post'));
  50          $output .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
  51  
  52          // Add message output processors enabled/disabled and settings.
  53          $output .= $this->heading(get_string('messageoutputs', 'message'));
  54          $output .= $this->manage_messageoutputs($allprocessors);
  55  
  56          // Add active message output processors settings.
  57          $output .= $this->heading(get_string('managemessageoutputs', 'message'));
  58          $output .= $this->manage_defaultmessageoutputs($processors, $providers, $preferences);
  59  
  60          $output .= html_writer::start_tag('div', array('class' => 'form-buttons'));
  61          $output .= html_writer::empty_tag('input',
  62              array('type' => 'submit', 'value' => get_string('savechanges', 'admin'), 'class' => 'form-submit btn btn-primary')
  63          );
  64          $output .= html_writer::end_tag('div');
  65          $output .= html_writer::end_tag('form');
  66  
  67          return $output;
  68      }
  69  
  70      /**
  71       * Display the interface to manage message outputs
  72       *
  73       * @param  array  $processors array of objects containing message processors
  74       * @return string The text to render
  75       */
  76      public function manage_messageoutputs($processors) {
  77          // Display the current workflows
  78          $table = new html_table();
  79          $table->attributes['class'] = 'admintable generaltable';
  80          $table->data        = array();
  81          $table->head        = array(
  82              get_string('name'),
  83              get_string('enable'),
  84              get_string('settings'),
  85          );
  86          $table->colclasses = array(
  87              'displayname', 'availability', 'settings',
  88          );
  89  
  90          foreach ($processors as $processor) {
  91              $row = new html_table_row();
  92              $row->attributes['class'] = 'messageoutputs';
  93  
  94              $name = new html_table_cell(get_string('pluginname', 'message_'.$processor->name));
  95              $enable = new html_table_cell();
  96              if (!$processor->available) {
  97                  $enable->text = html_writer::nonempty_tag('span', get_string('outputnotavailable', 'message'),
  98                      array('class' => 'error')
  99                  );
 100              } else {
 101                  $enable->text = html_writer::checkbox($processor->name, $processor->id, $processor->enabled, '',
 102                      array('id' => $processor->name)
 103                  );
 104              }
 105              // Settings
 106              $settings = new html_table_cell();
 107              if ($processor->available && $processor->hassettings) {
 108                  $settingsurl = new moodle_url('/admin/settings.php', array('section' => 'messagesetting'.$processor->name));
 109                  $settings->text = html_writer::link($settingsurl, get_string('settings', 'message'));
 110              }
 111  
 112              $row->cells = array($name, $enable, $settings);
 113              $table->data[] = $row;
 114          }
 115          return html_writer::table($table);
 116      }
 117  
 118      /**
 119       * Display the interface to manage default message outputs
 120       *
 121       * @param  array $processors  array of objects containing message processors
 122       * @param  array $providers   array of objects containing message providers
 123       * @param  stdClass $preferences object containing current preferences
 124       * @return string The text to render
 125       */
 126      public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
 127          // Prepare list of options for dropdown menu
 128          $options = array();
 129          foreach (array('disallowed', 'permitted', 'forced') as $setting) {
 130              $options[$setting] = get_string($setting, 'message');
 131          }
 132  
 133          // Display users outputs table
 134          $table = new html_table();
 135          $table->attributes['class'] = 'generaltable';
 136          $table->data        = array();
 137          $table->head        = array('');
 138  
 139          // Populate the header row
 140          foreach ($processors as $processor) {
 141              $table->head[]  = get_string('pluginname', 'message_'.$processor->name);
 142          }
 143          // Add enable/disable to head
 144          $table->head[] = get_string('enabled', 'core_message');
 145  
 146          // Generate the matrix of settings for each provider and processor
 147          foreach ($providers as $provider) {
 148              $row = new html_table_row();
 149              $row->attributes['class'] = 'defaultmessageoutputs';
 150              $row->cells = array();
 151  
 152              // Provider Name
 153              $providername = get_string('messageprovider:'.$provider->name, $provider->component);
 154              $row->cells[] = new html_table_cell($providername);
 155              $providersettingprefix = $provider->component.'_'.$provider->name.'_';
 156              $disableprovidersetting = $providersettingprefix.'disable';
 157              $providerdisabled = !empty($preferences->$disableprovidersetting);
 158              // Settings for each processor
 159              foreach ($processors as $processor) {
 160                  $cellcontent = '';
 161                  foreach (array('permitted', 'loggedin', 'loggedoff') as $setting) {
 162                      // pepare element and preference names
 163                      $elementname = $providersettingprefix.$setting.'['.$processor->name.']';
 164                      $preferencebase = $providersettingprefix.$setting;
 165                      // prepare language bits
 166                      $processorname = get_string('pluginname', 'message_'.$processor->name);
 167                      $statename = get_string($setting, 'message');
 168                      $labelparams = array(
 169                          'provider'  => $providername,
 170                          'processor' => $processorname,
 171                          'state'     => $statename
 172                      );
 173                      if ($setting == 'permitted') {
 174                          $label = get_string('sendingvia', 'message', $labelparams);
 175                          // determine the current setting or use default
 176                          $select = MESSAGE_DEFAULT_PERMITTED;
 177                          $preference = $processor->name.'_provider_'.$preferencebase;
 178                          if ($providerdisabled) {
 179                              $select = MESSAGE_DISALLOWED;
 180                          } else if (property_exists($preferences, $preference)) {
 181                              $select = $preferences->{$preference};
 182                          }
 183                          // dropdown menu
 184                          $cellcontent = html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
 185                          $cellcontent .= html_writer::select($options, $elementname, $select, false, array('id' => $elementname));
 186                          $cellcontent .= html_writer::tag('div', get_string('defaults', 'message'));
 187                      } else {
 188                          $label = get_string('sendingviawhen', 'message', $labelparams);
 189                          // determine the current setting based on the 'permitted' setting above
 190                          $checked = false;
 191                          if ($select == 'forced') {
 192                              $checked = true;
 193                          } else if ($select == 'permitted') {
 194                              $preference = 'message_provider_'.$preferencebase;
 195                              if (property_exists($preferences, $preference)) {
 196                                  $checked = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
 197                              }
 198                          }
 199                          // generate content
 200                          $cellcontent .= html_writer::start_tag('div');
 201                          $cellcontent .= html_writer::label($label, $elementname, true, array('class' => 'accesshide'));
 202                          $cellcontent .= html_writer::checkbox($elementname, 1, $checked, '', array('id' => $elementname));
 203                          $cellcontent .= $statename;
 204                          $cellcontent .= html_writer::end_tag('div');
 205                      }
 206                  }
 207                  $row->cells[] = new html_table_cell($cellcontent);
 208              }
 209              $disableprovider = html_writer::checkbox($disableprovidersetting, 1, !$providerdisabled, '',
 210                      array('id' => $disableprovidersetting, 'class' => 'messagedisable'));
 211              $disableprovider = html_writer::tag('div', $disableprovider);
 212              $row->cells[] = new html_table_cell($disableprovider);
 213              $table->data[] = $row;
 214          }
 215  
 216          $output = html_writer::table($table);
 217          return $output;
 218      }
 219  
 220      /**
 221       * Display the interface for notification preferences
 222       *
 223       * @param object $user instance of a user
 224       * @return string The text to render
 225       */
 226      public function render_user_notification_preferences($user) {
 227          $processors = get_message_processors();
 228          $providers = message_get_providers_for_user($user->id);
 229  
 230          $preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user);
 231          $notificationlistoutput = new \core_message\output\preferences\notification_list($processors, $providers,
 232              $preferences, $user);
 233          return $this->render_from_template('message/notification_preferences',
 234              $notificationlistoutput->export_for_template($this));
 235      }
 236  
 237      /**
 238       * Display the interface for message preferences
 239       *
 240       * @param object $user instance of a user
 241       * @return string The text to render
 242       */
 243      public function render_user_message_preferences($user) {
 244          global $CFG;
 245  
 246          // Filter out enabled, available system_configured and user_configured processors only.
 247          $readyprocessors = array_filter(get_message_processors(), function($processor) {
 248              return $processor->enabled &&
 249                  $processor->configured &&
 250                  $processor->object->is_user_configured() &&
 251                  // Filter out processors that don't have and message preferences to configure.
 252                  $processor->object->has_message_preferences();
 253          });
 254  
 255          $providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
 256              return $provider->component === 'moodle';
 257          });
 258          $preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user);
 259          $notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors,
 260              $providers, $preferences, $user);
 261          $context = $notificationlistoutput->export_for_template($this);
 262  
 263          // Get the privacy settings options for being messaged.
 264          $privacysetting = \core_message\api::get_user_privacy_messaging_preference($user->id);
 265          $choices = array();
 266          $choices[] = [
 267              'value' => \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
 268              'text' => get_string('contactableprivacy_onlycontacts', 'message'),
 269              'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS)
 270          ];
 271          $choices[] = [
 272              'value' => \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
 273              'text' => get_string('contactableprivacy_coursemember', 'message'),
 274              'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER)
 275          ];
 276          if (!empty($CFG->messagingallusers)) {
 277              // Add the MESSAGE_PRIVACY_SITE option when site-wide messaging between users is enabled.
 278              $choices[] = [
 279                  'value' => \core_message\api::MESSAGE_PRIVACY_SITE,
 280                  'text' => get_string('contactableprivacy_site', 'message'),
 281                  'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_SITE)
 282              ];
 283          }
 284          $context['privacychoices'] = $choices;
 285  
 286          return $this->render_from_template('message/message_preferences', $context);
 287      }
 288  }