Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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_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       * @return bool
  85       */
  86      private function is_preference_enabled($name) {
  87          $processor = $this->processor;
  88          $preferences = $this->preferences;
  89          $defaultpreferences = get_message_output_default_preferences();
  90  
  91          $checked = false;
  92          // See if user has touched this preference.
  93          if (isset($preferences->{$name})) {
  94              // User has some preferences for this state in the database.
  95              $checked = isset($preferences->{$name}[$processor->name]);
  96          } else {
  97              // User has not set this preference yet, using site default preferences set by admin.
  98              $defaultpreference = 'message_provider_'.$name;
  99              if (isset($defaultpreferences->{$defaultpreference})) {
 100                  $checked = (int)in_array($processor->name, explode(',', $defaultpreferences->{$defaultpreference}));
 101              }
 102          }
 103  
 104          return $checked;
 105      }
 106  
 107      public function export_for_template(\renderer_base $output) {
 108          $processor = $this->processor;
 109          $preferencebase = $this->get_preference_base();
 110          $permitted = MESSAGE_DEFAULT_PERMITTED;
 111          $defaultpreferences = get_message_output_default_preferences();
 112          $defaultpreference = $processor->name.'_provider_'.$preferencebase.'_permitted';
 113          $context = [
 114              'displayname' => get_string('pluginname', 'message_'.$processor->name),
 115              'name' => $processor->name,
 116              'locked' => false,
 117              'userconfigured' => $processor->object->is_user_configured(),
 118              'loggedin' => [
 119                  'name' => 'loggedin',
 120                  'displayname' => get_string('loggedindescription', 'message'),
 121                  'checked' => $this->is_preference_enabled($preferencebase.'_loggedin'),
 122              ],
 123              'loggedoff' => [
 124                  'name' => 'loggedoff',
 125                  'displayname' => get_string('loggedoffdescription', 'message'),
 126                  'checked' => $this->is_preference_enabled($preferencebase.'_loggedoff'),
 127              ],
 128          ];
 129  
 130          // Determine the default setting.
 131          if (isset($defaultpreferences->{$defaultpreference})) {
 132              $permitted = $defaultpreferences->{$defaultpreference};
 133          }
 134          // If settings are disallowed or forced, just display the corresponding message, if not use user settings.
 135          if ($permitted == 'disallowed') {
 136              $context['locked'] = true;
 137              $context['lockedmessage'] = get_string('disallowed', 'message');
 138          } else if ($permitted == 'forced') {
 139              $context['locked'] = true;
 140              $context['lockedmessage'] = get_string('forced', 'message');
 141          }
 142  
 143          return $context;
 144      }
 145  }