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] [Versions 400 and 403] [Versions 401 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   * Notification renderable component.
  19   *
  20   * @package    core
  21   * @copyright  2015 Jetha Chan
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\output;
  26  
  27  /**
  28   * Data structure representing a notification.
  29   *
  30   * @copyright 2015 Jetha Chan
  31   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   * @since Moodle 2.9
  33   * @package core
  34   * @category output
  35   */
  36  class notification implements \renderable, \templatable {
  37  
  38      /**
  39       * A notification of level 'success'.
  40       */
  41      const NOTIFY_SUCCESS = 'success';
  42  
  43      /**
  44       * A notification of level 'warning'.
  45       */
  46      const NOTIFY_WARNING = 'warning';
  47  
  48      /**
  49       * A notification of level 'info'.
  50       */
  51      const NOTIFY_INFO = 'info';
  52  
  53      /**
  54       * A notification of level 'error'.
  55       */
  56      const NOTIFY_ERROR = 'error';
  57  
  58      /**
  59       * @var string Message payload.
  60       */
  61      protected $message = '';
  62  
  63      /**
  64       * @var string Message type.
  65       */
  66      protected $messagetype = self::NOTIFY_WARNING;
  67  
  68      /**
  69       * @var bool $announce Whether this notification should be announced assertively to screen readers.
  70       */
  71      protected $announce = true;
  72  
  73      /**
  74       * @var bool $closebutton Whether this notification should inlcude a button to dismiss itself.
  75       */
  76      protected $closebutton = true;
  77  
  78      /**
  79       * @var array $extraclasses A list of any extra classes that may be required.
  80       */
  81      protected $extraclasses = array();
  82  
  83      /**
  84       * Notification constructor.
  85       *
  86       * @param string $message the message to print out
  87       * @param ?string $messagetype one of the NOTIFY_* constants..
  88       * @param bool $closebutton Whether to show a close icon to remove the notification (default true).
  89       */
  90      public function __construct($message, $messagetype = null, $closebutton = true) {
  91          $this->message = $message;
  92  
  93          if (empty($messagetype)) {
  94              $messagetype = self::NOTIFY_ERROR;
  95          }
  96  
  97          $this->messagetype = $messagetype;
  98  
  99          $this->closebutton = $closebutton;
 100      }
 101  
 102      /**
 103       * Set whether this notification should be announced assertively to screen readers.
 104       *
 105       * @param bool $announce
 106       * @return $this
 107       */
 108      public function set_announce($announce = false) {
 109          $this->announce = (bool) $announce;
 110  
 111          return $this;
 112      }
 113  
 114      /**
 115       * Set whether this notification should include a button to disiss itself.
 116       *
 117       * @param bool $button
 118       * @return $this
 119       */
 120      public function set_show_closebutton($button = false) {
 121          $this->closebutton = (bool) $button;
 122  
 123          return $this;
 124      }
 125  
 126      /**
 127       * Add any extra classes that this notification requires.
 128       *
 129       * @param array $classes
 130       * @return $this
 131       */
 132      public function set_extra_classes($classes = array()) {
 133          $this->extraclasses = $classes;
 134  
 135          return $this;
 136      }
 137  
 138      /**
 139       * Get the message for this notification.
 140       *
 141       * @return string message
 142       */
 143      public function get_message() {
 144          return $this->message;
 145      }
 146  
 147      /**
 148       * Get the message type for this notification.
 149       *
 150       * @return string message type
 151       */
 152      public function get_message_type() {
 153          return $this->messagetype;
 154      }
 155  
 156      /**
 157       * Export this data so it can be used as the context for a mustache template.
 158       *
 159       * @param \renderer_base $output typically, the renderer that's calling this function
 160       * @return array data context for a mustache template
 161       */
 162      public function export_for_template(\renderer_base $output) {
 163          return array(
 164              'message'       => clean_text($this->message),
 165              'extraclasses'  => implode(' ', $this->extraclasses),
 166              'announce'      => $this->announce,
 167              'closebutton'   => $this->closebutton,
 168              'issuccess'         => $this->messagetype === 'success',
 169              'isinfo'            => $this->messagetype === 'info',
 170              'iswarning'         => $this->messagetype === 'warning',
 171              'iserror'           => $this->messagetype === 'error',
 172          );
 173      }
 174  
 175      public function get_template_name() {
 176          $templatemappings = [
 177              // Current types mapped to template names.
 178              'success'           => 'core/notification_success',
 179              'info'              => 'core/notification_info',
 180              'warning'           => 'core/notification_warning',
 181              'error'             => 'core/notification_error',
 182          ];
 183  
 184          if (isset($templatemappings[$this->messagetype])) {
 185              return $templatemappings[$this->messagetype];
 186          }
 187          return $templatemappings['error'];
 188      }
 189  }