Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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_component 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  require_once($CFG->dirroot . '/message/lib.php');
  30  
  31  use renderable;
  32  use templatable;
  33  
  34  /**
  35   * Class to create context for a notification component on the message preferences page.
  36   *
  37   * @package   core_message
  38   * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class notification_list_component implements templatable, renderable {
  42  
  43      /**
  44       * @var array A list of message processors.
  45       */
  46      protected $processors;
  47  
  48      /**
  49       * @var array A list of message providers.
  50       */
  51      protected $providers;
  52  
  53      /**
  54       * @var array A list of message preferences.
  55       */
  56      protected $preferences;
  57  
  58      /**
  59       * @var string The component name.
  60       */
  61      protected $component;
  62  
  63      /**
  64       * @var \stdClass A user.
  65       */
  66      protected $user;
  67  
  68      /**
  69       * Constructor.
  70       *
  71       * @param string $component
  72       * @param array $processors
  73       * @param array $providers
  74       * @param \stdClass $preferences
  75       * @param \stdClass $user
  76       */
  77      public function __construct($component, $processors, $providers, $preferences, $user) {
  78          $this->processors = $processors;
  79          $this->providers = $providers;
  80          $this->preferences = $preferences;
  81          $this->component = $component;
  82          $this->user = $user;
  83      }
  84  
  85      /**
  86       * Get the base key prefix for the given provider.
  87       *
  88       * @param \stdClass $provider The message provider
  89       * @return string
  90       */
  91      private function get_preference_base($provider) {
  92          return $provider->component.'_'.$provider->name;
  93      }
  94  
  95      /**
  96       * Get the display name for the given provider.
  97       *
  98       * @param \stdClass $provider The message provider
  99       * @return string
 100       */
 101      private function get_provider_display_name($provider) {
 102          return get_string('messageprovider:'.$provider->name, $provider->component);
 103      }
 104  
 105      /**
 106       * Determine if the preference should be displayed.
 107       *
 108       * @param string $preferencekey
 109       * @return bool
 110       */
 111      protected function should_show_preference_key($preferencekey) {
 112          return $preferencekey !== 'message_provider_moodle_instantmessage';
 113      }
 114  
 115      public function export_for_template(\renderer_base $output) {
 116          $processors = $this->processors;
 117          $providers = $this->providers;
 118          $preferences = $this->preferences;
 119          $component = $this->component;
 120          $defaultpreferences = get_message_output_default_preferences();
 121  
 122          if ($component != 'moodle') {
 123              $componentname = get_string('pluginname', $component);
 124          } else {
 125              $componentname = get_string('coresystem');
 126          }
 127  
 128          $context = [
 129              'displayname' => $componentname,
 130              'colspan' => count($processors) + 1,
 131              'notifications' => [],
 132          ];
 133  
 134          foreach ($providers as $provider) {
 135              $preferencebase = $this->get_preference_base($provider);
 136              $preferencekey = 'message_provider_'.$preferencebase;
 137  
 138              // Hack to stop this one specific preference from showing up in the
 139              // notification list because it belongs to the message preferences page.
 140              if (!$this->should_show_preference_key($preferencekey)) {
 141                  continue;
 142              }
 143  
 144              // If provider component is not same or provider disabled then don't show.
 145              if (($provider->component != $component) ||
 146                      (!empty($defaultpreferences->{$preferencebase.'_disable'}))) {
 147                  continue;
 148              }
 149  
 150              $notificationcontext = [
 151                  'displayname' => $this->get_provider_display_name($provider),
 152                  'preferencekey' => $preferencekey,
 153                  'processors' => [],
 154              ];
 155  
 156              foreach ($processors as $processor) {
 157                  $notificationprocessor = new notification_list_processor($processor, $provider, $preferences);
 158                  $notificationcontext['processors'][] = $notificationprocessor->export_for_template($output);
 159              }
 160  
 161              $context['notifications'][] = $notificationcontext;
 162          }
 163  
 164          $context['hasnotifications'] = (count($context['notifications']) > 0);
 165  
 166          return $context;
 167      }
 168  }