Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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_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              'notifications' => [],
 131          ];
 132  
 133          foreach ($providers as $provider) {
 134              $preferencebase = $this->get_preference_base($provider);
 135              $preferencekey = 'message_provider_'.$preferencebase;
 136  
 137              // Hack to stop this one specific preference from showing up in the
 138              // notification list because it belongs to the message preferences page.
 139              if (!$this->should_show_preference_key($preferencekey)) {
 140                  continue;
 141              }
 142  
 143              // If provider component is not same or provider disabled then don't show.
 144              if (($provider->component != $component) ||
 145                      (!empty($defaultpreferences->{$preferencebase.'_disable'}))) {
 146                  continue;
 147              }
 148  
 149              $notificationcontext = [
 150                  'displayname' => $this->get_provider_display_name($provider),
 151                  'preferencekey' => $preferencekey,
 152                  'processors' => [],
 153              ];
 154  
 155              foreach ($processors as $processor) {
 156                  $notificationprocessor = new notification_list_processor($processor, $provider, $preferences);
 157                  $notificationcontext['processors'][] = $notificationprocessor->export_for_template($output);
 158              }
 159  
 160              $context['notifications'][] = $notificationcontext;
 161          }
 162  
 163          return $context;
 164      }
 165  }