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 notification_list class for displaying on message preferences page.
  19   *
  20   * @package   core_message
  21   * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_message\output\preferences;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use renderable;
  30  use templatable;
  31  use context_user;
  32  
  33  /**
  34   * Class to create context for the list of notifications on the message preferences page.
  35   *
  36   * @package   core_message
  37   * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class notification_list implements templatable, renderable {
  41  
  42      /**
  43       * @var array A list of message processors.
  44       */
  45      protected $processors;
  46  
  47      /**
  48       * @var array A list of message providers.
  49       */
  50      protected $providers;
  51  
  52      /**
  53       * @var array A list of message preferences.
  54       */
  55      protected $preferences;
  56  
  57      /**
  58       * @var \stdClass A user.
  59       */
  60      protected $user;
  61  
  62      /**
  63       * Constructor.
  64       *
  65       * @param array $processors
  66       * @param array $providers
  67       * @param \stdClass $preferences
  68       * @param \stdClass $user
  69       */
  70      public function __construct($processors, $providers, $preferences, $user) {
  71          $this->processors = $processors;
  72          $this->providers = $providers;
  73          $this->preferences = $preferences;
  74          $this->user = $user;
  75      }
  76  
  77      /**
  78       * Create the list component output object.
  79       *
  80       * @param string $component
  81       * @param array $readyprocessors
  82       * @param array $providers
  83       * @param \stdClass $preferences
  84       * @param \stdClass $user
  85       * @return notification_list_component
  86       */
  87      protected function create_list_component($component, $readyprocessors, $providers, $preferences, $user) {
  88          return new notification_list_component($component, $readyprocessors, $providers, $preferences, $user);
  89      }
  90  
  91      public function export_for_template(\renderer_base $output) {
  92          $processors = $this->processors;
  93          $providers = $this->providers;
  94          $preferences = $this->preferences;
  95          $user = $this->user;
  96          $usercontext = context_user::instance($user->id);
  97          $activitycomponents = [];
  98          $othercomponents = [];
  99  
 100          // Order the components so that the activities appear first, followed
 101          // by the system and then anything else.
 102          foreach ($providers as $provider) {
 103              if ($provider->component != 'moodle') {
 104                  if (substr($provider->component, 0, 4) == 'mod_') {
 105                      // Activities.
 106                      $activitycomponents[] = $provider->component;
 107                  } else {
 108                      // Other stuff.
 109                      $othercomponents[] = $provider->component;
 110                  }
 111              }
 112          }
 113  
 114          $activitycomponents = array_unique($activitycomponents);
 115          asort($activitycomponents);
 116          $othercomponents = array_unique($othercomponents);
 117          asort($othercomponents);
 118          $components = array_merge($activitycomponents, ['moodle'], $othercomponents);
 119          asort($providers);
 120  
 121          $context = [
 122              'userid' => $user->id,
 123              'disableall' => $user->emailstop,
 124              'processors' => [],
 125          ];
 126  
 127          $readyprocessors = [];
 128          // Make the unconfigured processors appear last in the array.
 129          uasort($processors, function($a, $b) {
 130              $aconf = $a->object->is_user_configured();
 131              $bconf = $b->object->is_user_configured();
 132  
 133              if ($aconf == $bconf) {
 134                  return 0;
 135              }
 136  
 137              return ($aconf < $bconf) ? 1 : -1;
 138          });
 139  
 140          foreach ($processors as $processor) {
 141              if (!$processor->enabled || !$processor->configured) {
 142                  // If the processor isn't enabled and configured at the site
 143                  // level then we should ignore it.
 144                  continue;
 145              }
 146  
 147              $context['processors'][] = [
 148                  'displayname' => get_string('pluginname', 'message_'.$processor->name),
 149                  'name' => $processor->name,
 150                  'hassettings' => !empty($processor->object->config_form($preferences)),
 151                  'contextid' => $usercontext->id,
 152                  'userconfigured' => $processor->object->is_user_configured(),
 153              ];
 154  
 155              $readyprocessors[] = $processor;
 156          }
 157  
 158          foreach ($components as $component) {
 159              $notificationcomponent = $this->create_list_component($component, $readyprocessors,
 160                  $providers, $preferences, $user);
 161  
 162              $context['components'][] = $notificationcomponent->export_for_template($output);
 163          }
 164  
 165          return $context;
 166      }
 167  }