Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   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_processor 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_processor implements templatable, renderable {
  42  
  43      /**
  44       * @var \stdClass A notification processor.
  45       */
  46      protected $processor;
  47  
  48      /**
  49       * @var \stdClass A notification provider.
  50       */
  51      protected $provider;
  52  
  53      /**
  54       * @var \stdClass A list of message preferences.
  55       */
  56      protected $preferences;
  57  
  58      /**
  59       * Constructor.
  60       *
  61       * @param \stdClass $processor
  62       * @param \stdClass $provider
  63       * @param \stdClass $preferences
  64       */
  65      public function __construct($processor, $provider, $preferences) {
  66          $this->processor = $processor;
  67          $this->provider = $provider;
  68          $this->preferences = $preferences;
  69      }
  70  
  71      /**
  72       * Get the base key prefix for the given provider.
  73       *
  74       * @return string
  75       */
  76      private function get_preference_base() {
  77          return $this->provider->component . '_' . $this->provider->name;
  78      }
  79  
  80      /**
  81       * Check if the given preference is enabled or not.
  82       *
  83       * @param string $name preference name
  84       * @param string $locked Wether the preference is locked by admin.
  85       * @return bool
  86       */
  87      private function is_preference_enabled($name, $locked) {
  88          $processor = $this->processor;
  89          $preferences = $this->preferences;
  90          $defaultpreferences = get_message_output_default_preferences();
  91  
  92          $checked = false;
  93          // See if user has touched this preference.
  94          if (!$locked && isset($preferences->{$name})) {
  95              // User has some preferences for this state in the database.
  96              $checked = isset($preferences->{$name}[$processor->name]);
  97          } else {
  98              // User has not set this preference yet, using site default preferences set by admin.
  99              $defaultpreference = 'message_provider_'.$name;
 100              if (isset($defaultpreferences->{$defaultpreference})) {
 101                  $checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
 102              }
 103          }
 104  
 105          return $checked;
 106      }
 107  
 108      /**
 109       * Export this data so it can be used as the context for a mustache template.
 110       * @todo Remove loggedin and loggedoff from context on MDL-73284.
 111       *
 112       * @param renderer_base $output
 113       * @return stdClass
 114       */
 115      public function export_for_template(\renderer_base $output) {
 116          $processor = $this->processor;
 117          $preferencebase = $this->get_preference_base();
 118          $defaultpreferences = get_message_output_default_preferences();
 119          $defaultpreference = $processor->name.'_provider_'.$preferencebase.'_locked';
 120          $providername = get_string('messageprovider:'.$this->provider->name, $this->provider->component);
 121          $processorname = get_string('pluginname', 'message_'.$processor->name);
 122          $labelparams = [
 123              'provider'  => $providername,
 124              'processor' => $processorname,
 125          ];
 126  
 127          $context = [
 128              'displayname' => $processorname,
 129              'name' => $processor->name,
 130              'locked' => false,
 131              'userconfigured' => $processor->object->is_user_configured(),
 132              // Backward compatibility, deprecated attribute.
 133              'loggedin' => [
 134                  'name' => 'loggedin',
 135                  'displayname' => 'loggedin',
 136                  'checked' => false,
 137              ],
 138              // Backward compatibility, deprecated attribute.
 139              'loggedoff' => [
 140                  'name' => 'loggedoff',
 141                  'displayname' => 'loggedoff',
 142                  'checked' => false,
 143              ],
 144              'enabled' => false,
 145              'enabledlabel' => get_string('sendingviaenabled', 'message', $labelparams),
 146          ];
 147  
 148          // Determine the default setting.
 149          if (isset($defaultpreferences->{$defaultpreference})) {
 150              $context['locked'] = $defaultpreferences->{$defaultpreference};
 151          }
 152  
 153          $context['enabled'] = $this->is_preference_enabled($preferencebase.'_enabled', $context['locked']);
 154          $context['loggedoff']['checked'] = $context['enabled']; // Backward compatibility, deprecated attribute.
 155          $context['loggedin']['checked'] = $context['enabled']; // Backward compatibility, deprecated attribute.
 156  
 157          // If settings are disallowed or forced, just display the corresponding message, if not use user settings.
 158          if ($context['locked']) {
 159              if ($context['enabled']) {
 160                  $context['lockedmessage'] = get_string('forcedmessage', 'message');
 161                  $context['lockedlabel'] = get_string('providerprocesorislocked', 'message', $labelparams);
 162              } else {
 163                  $context['lockedmessage'] = get_string('disallowed', 'message');
 164                  $context['lockedlabel'] = get_string('providerprocesorisdisallowed', 'message', $labelparams);
 165              }
 166          }
 167  
 168          return $context;
 169      }
 170  }