Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [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->manage_defaultmessageoutputs($processors, $providers, $preferences);
  58  
  59          $output .= html_writer::start_tag('div', array('class' => 'form-buttons'));
  60          $output .= html_writer::empty_tag('input',
  61              array('type' => 'submit', 'value' => get_string('savechanges', 'admin'), 'class' => 'form-submit btn btn-primary')
  62          );
  63          $output .= html_writer::end_tag('div');
  64          $output .= html_writer::end_tag('form');
  65  
  66          return $output;
  67      }
  68  
  69      /**
  70       * Display the interface to manage message outputs
  71       *
  72       * @param  array  $processors array of objects containing message processors
  73       * @return string The text to render
  74       */
  75      public function manage_messageoutputs($processors) {
  76          // Display the current workflows
  77          $table = new html_table();
  78          $table->attributes['class'] = 'admintable generaltable';
  79          $table->data        = array();
  80          $table->head        = array(
  81              get_string('name'),
  82              get_string('enable'),
  83              get_string('settings'),
  84          );
  85          $table->colclasses = array(
  86              'displayname', 'availability text-center', 'settings',
  87          );
  88  
  89          foreach ($processors as $processor) {
  90              $row = new html_table_row();
  91              $row->attributes['class'] = 'messageoutputs';
  92  
  93              $name = new html_table_cell(get_string('pluginname', 'message_'.$processor->name));
  94              $enable = new html_table_cell();
  95              if (!$processor->available) {
  96                  $enable->text = html_writer::nonempty_tag('span', get_string('outputnotavailable', 'message'),
  97                      array('class' => 'error')
  98                  );
  99              } else {
 100                  $enable->text = html_writer::checkbox($processor->name, $processor->id, $processor->enabled, '',
 101                      array('id' => $processor->name)
 102                  );
 103              }
 104              // Settings
 105              $settings = new html_table_cell();
 106              if ($processor->available && $processor->hassettings) {
 107                  $settingsurl = new moodle_url('/admin/settings.php', array('section' => 'messagesetting'.$processor->name));
 108                  $settings->text = html_writer::link($settingsurl, get_string('settings', 'message'));
 109              }
 110  
 111              $row->cells = array($name, $enable, $settings);
 112              $table->data[] = $row;
 113          }
 114          return html_writer::table($table);
 115      }
 116  
 117      /**
 118       * Display the interface to manage default message outputs
 119       *
 120       * @param  array $processors  array of objects containing message processors
 121       * @param  array $providers   array of objects containing message providers
 122       * @param  stdClass $preferences object containing current preferences
 123       * @return string The text to render
 124       */
 125      public function manage_defaultmessageoutputs($processors, $providers, $preferences) {
 126          $context = [];
 127  
 128          foreach ($processors as $processor) {
 129              $processor->displayname = get_string('pluginname', 'message_'.$processor->name);
 130          }
 131  
 132          $activitycomponents = [];
 133          $othercomponents = [];
 134  
 135          foreach ($providers as $provider) {
 136              $provider->displayname = get_string('messageprovider:'.$provider->name, $provider->component);
 137              $providersettingprefix = $provider->component.'_'.$provider->name.'_';
 138              $provider->enabledsetting = $providersettingprefix.'disable';
 139              $provider->enabled = empty($preferences->{$provider->enabledsetting});
 140              $provider->enabledlabel = get_string('providerenabled', 'message', $provider->displayname);
 141              $provider->settings = [];
 142  
 143              // Settings for each processor
 144              foreach ($processors as $processor) {
 145                  $setting = new StdClass();
 146  
 147                  $setting->lockedsetting = $providersettingprefix.'locked['.$processor->name.']';
 148                  $preference = $processor->name.'_provider_'.$providersettingprefix.'locked';
 149  
 150                  $setting->locked = false;
 151                  if (property_exists($preferences, $preference)) {
 152                      $setting->locked = $preferences->{$preference} == 1;
 153                  }
 154  
 155                  $setting->enabledsetting = $providersettingprefix.'enabled['.$processor->name.']';
 156                  $preference = 'message_provider_'.$providersettingprefix.'enabled';
 157  
 158                  $setting->enabled = false;
 159                  if (property_exists($preferences, $preference)) {
 160                      $setting->enabled = (int)in_array($processor->name, explode(',', $preferences->{$preference}));
 161                  }
 162                  $labelparams = [
 163                      'provider'  => $provider->displayname,
 164                      'processor' => $processor->displayname,
 165                  ];
 166                  $setting->enabledlabel = get_string('sendingviaenabled', 'message', $labelparams);
 167                  $setting->lockedlabel = get_string('sendingvialocked', 'message', $labelparams);
 168  
 169                  $provider->settings[] = $setting;
 170              }
 171  
 172              // Order the components so that the activities appear first, followed
 173              // by the system and then anything else.
 174              if ($provider->component != 'moodle') {
 175                  if (substr($provider->component, 0, 4) == 'mod_') {
 176                      // Activities.
 177                      $activitycomponents[] = $provider->component;
 178                  } else {
 179                      // Other stuff.
 180                      $othercomponents[] = $provider->component;
 181                  }
 182              }
 183          }
 184  
 185          $activitycomponents = array_unique($activitycomponents);
 186          asort($activitycomponents);
 187          $othercomponents = array_unique($othercomponents);
 188          asort($othercomponents);
 189          $components = array_merge($activitycomponents, ['moodle'], $othercomponents);
 190          asort($providers);
 191  
 192          $colspan = count($processors) + 2;
 193          $componentsexport = [];
 194  
 195          foreach ($components as $component) {
 196              $componentexport = new StdClass();
 197              $componentexport->name = $component;
 198  
 199              if ($component != 'moodle') {
 200                  $componentexport->displayname = get_string('pluginname', $component);
 201              } else {
 202                  $componentexport->displayname = get_string('coresystem');
 203              }
 204  
 205              $componentexport->providers = [];
 206              foreach ($providers as $provider) {
 207                  if ($provider->component == $component) {
 208                      $componentexport->providers[] = $provider;
 209                  }
 210              }
 211              $componentexport->colspan = $colspan;
 212              $componentsexport[] = $componentexport;
 213          }
 214  
 215          $context['processors'] = array_values($processors);
 216          $context['components'] = $componentsexport;
 217  
 218          return $this->render_from_template('message/default_notification_preferences', $context);
 219      }
 220  
 221      /**
 222       * Display the interface for notification preferences
 223       *
 224       * @param object $user instance of a user
 225       * @return string The text to render
 226       */
 227      public function render_user_notification_preferences($user) {
 228          $processors = get_message_processors();
 229          $providers = message_get_providers_for_user($user->id);
 230  
 231          $preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user);
 232          $notificationlistoutput = new \core_message\output\preferences\notification_list($processors, $providers,
 233              $preferences, $user);
 234          return $this->render_from_template('message/notification_preferences',
 235              $notificationlistoutput->export_for_template($this));
 236      }
 237  
 238      /**
 239       * Display the interface for message preferences
 240       *
 241       * @param object $user instance of a user
 242       * @return string The text to render
 243       */
 244      public function render_user_message_preferences($user) {
 245          global $CFG;
 246  
 247          // Filter out enabled, available system_configured and user_configured processors only.
 248          $readyprocessors = array_filter(get_message_processors(), function($processor) {
 249              return $processor->enabled &&
 250                  $processor->configured &&
 251                  $processor->object->is_user_configured() &&
 252                  // Filter out processors that don't have and message preferences to configure.
 253                  $processor->object->has_message_preferences();
 254          });
 255  
 256          $providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
 257              return $provider->component === 'moodle';
 258          });
 259          $preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user);
 260          $notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors,
 261              $providers, $preferences, $user);
 262          $context = $notificationlistoutput->export_for_template($this);
 263  
 264          // Get the privacy settings options for being messaged.
 265          $privacysetting = \core_message\api::get_user_privacy_messaging_preference($user->id);
 266          $choices = array();
 267          $choices[] = [
 268              'value' => \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
 269              'text' => get_string('contactableprivacy_onlycontacts', 'message'),
 270              'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS)
 271          ];
 272          $choices[] = [
 273              'value' => \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
 274              'text' => get_string('contactableprivacy_coursemember', 'message'),
 275              'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER)
 276          ];
 277          if (!empty($CFG->messagingallusers)) {
 278              // Add the MESSAGE_PRIVACY_SITE option when site-wide messaging between users is enabled.
 279              $choices[] = [
 280                  'value' => \core_message\api::MESSAGE_PRIVACY_SITE,
 281                  'text' => get_string('contactableprivacy_site', 'message'),
 282                  'checked' => ($privacysetting == \core_message\api::MESSAGE_PRIVACY_SITE)
 283              ];
 284          }
 285          $context['privacychoices'] = $choices;
 286  
 287          return $this->render_from_template('message/message_preferences', $context);
 288      }
 289  }