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   * 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       */
  89      public function __construct($message, $messagetype = null) {
  90          $this->message = $message;
  91  
  92          if (empty($messagetype)) {
  93              $messagetype = self::NOTIFY_ERROR;
  94          }
  95  
  96          $this->messagetype = $messagetype;
  97      }
  98  
  99      /**
 100       * Set whether this notification should be announced assertively to screen readers.
 101       *
 102       * @param bool $announce
 103       * @return $this
 104       */
 105      public function set_announce($announce = false) {
 106          $this->announce = (bool) $announce;
 107  
 108          return $this;
 109      }
 110  
 111      /**
 112       * Set whether this notification should include a button to disiss itself.
 113       *
 114       * @param bool $button
 115       * @return $this
 116       */
 117      public function set_show_closebutton($button = false) {
 118          $this->closebutton = (bool) $button;
 119  
 120          return $this;
 121      }
 122  
 123      /**
 124       * Add any extra classes that this notification requires.
 125       *
 126       * @param array $classes
 127       * @return $this
 128       */
 129      public function set_extra_classes($classes = array()) {
 130          $this->extraclasses = $classes;
 131  
 132          return $this;
 133      }
 134  
 135      /**
 136       * Get the message for this notification.
 137       *
 138       * @return string message
 139       */
 140      public function get_message() {
 141          return $this->message;
 142      }
 143  
 144      /**
 145       * Get the message type for this notification.
 146       *
 147       * @return string message type
 148       */
 149      public function get_message_type() {
 150          return $this->messagetype;
 151      }
 152  
 153      /**
 154       * Export this data so it can be used as the context for a mustache template.
 155       *
 156       * @param renderer_base $output typically, the renderer that's calling this function
 157       * @return stdClass data context for a mustache template
 158       */
 159      public function export_for_template(\renderer_base $output) {
 160          return array(
 161              'message'       => clean_text($this->message),
 162              'extraclasses'  => implode(' ', $this->extraclasses),
 163              'announce'      => $this->announce,
 164              'closebutton'   => $this->closebutton,
 165              'issuccess'         => $this->messagetype === 'success',
 166              'isinfo'            => $this->messagetype === 'info',
 167              'iswarning'         => $this->messagetype === 'warning',
 168              'iserror'           => $this->messagetype === 'error',
 169          );
 170      }
 171  
 172      public function get_template_name() {
 173          $templatemappings = [
 174              // Current types mapped to template names.
 175              'success'           => 'core/notification_success',
 176              'info'              => 'core/notification_info',
 177              'warning'           => 'core/notification_warning',
 178              'error'             => 'core/notification_error',
 179          ];
 180  
 181          if (isset($templatemappings[$this->messagetype])) {
 182              return $templatemappings[$this->messagetype];
 183          }
 184          return $templatemappings['error'];
 185      }
 186  }