[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/classes/output/ -> notification.php (source)

   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       * @deprecated
  60       * A generic message.
  61       */
  62      const NOTIFY_MESSAGE = 'message';
  63  
  64      /**
  65       * @deprecated
  66       * A message notifying the user that a problem occurred.
  67       */
  68      const NOTIFY_PROBLEM = 'problem';
  69  
  70      /**
  71       * @deprecated
  72       * A notification of level 'redirect'.
  73       */
  74      const NOTIFY_REDIRECT = 'redirect';
  75  
  76      /**
  77       * @var string Message payload.
  78       */
  79      protected $message = '';
  80  
  81      /**
  82       * @var string Message type.
  83       */
  84      protected $messagetype = self::NOTIFY_WARNING;
  85  
  86      /**
  87       * @var bool $announce Whether this notification should be announced assertively to screen readers.
  88       */
  89      protected $announce = true;
  90  
  91      /**
  92       * @var bool $closebutton Whether this notification should inlcude a button to dismiss itself.
  93       */
  94      protected $closebutton = true;
  95  
  96      /**
  97       * @var array $extraclasses A list of any extra classes that may be required.
  98       */
  99      protected $extraclasses = array();
 100  
 101      /**
 102       * Notification constructor.
 103       *
 104       * @param string $message the message to print out
 105       * @param string $messagetype normally NOTIFY_PROBLEM or NOTIFY_SUCCESS.
 106       */
 107      public function __construct($message, $messagetype = null) {
 108          $this->message = $message;
 109  
 110          if (empty($messagetype)) {
 111              $messagetype = self::NOTIFY_ERROR;
 112          }
 113  
 114          $this->messagetype = $messagetype;
 115  
 116          switch ($messagetype) {
 117              case self::NOTIFY_PROBLEM:
 118              case self::NOTIFY_REDIRECT:
 119              case self::NOTIFY_MESSAGE:
 120                  debugging('Use of ' . $messagetype . ' has been deprecated. Please switch to an alternative type.');
 121          }
 122      }
 123  
 124      /**
 125       * Set whether this notification should be announced assertively to screen readers.
 126       *
 127       * @param bool $announce
 128       * @return $this
 129       */
 130      public function set_announce($announce = false) {
 131          $this->announce = (bool) $announce;
 132  
 133          return $this;
 134      }
 135  
 136      /**
 137       * Set whether this notification should include a button to disiss itself.
 138       *
 139       * @param bool $button
 140       * @return $this
 141       */
 142      public function set_show_closebutton($button = false) {
 143          $this->closebutton = (bool) $button;
 144  
 145          return $this;
 146      }
 147  
 148      /**
 149       * Add any extra classes that this notification requires.
 150       *
 151       * @param array $classes
 152       * @return $this
 153       */
 154      public function set_extra_classes($classes = array()) {
 155          $this->extraclasses = $classes;
 156  
 157          return $this;
 158      }
 159  
 160      /**
 161       * Get the message for this notification.
 162       *
 163       * @return string message
 164       */
 165      public function get_message() {
 166          return $this->message;
 167      }
 168  
 169      /**
 170       * Get the message type for this notification.
 171       *
 172       * @return string message type
 173       */
 174      public function get_message_type() {
 175          return $this->messagetype;
 176      }
 177  
 178      /**
 179       * Export this data so it can be used as the context for a mustache template.
 180       *
 181       * @param renderer_base $output typically, the renderer that's calling this function
 182       * @return stdClass data context for a mustache template
 183       */
 184      public function export_for_template(\renderer_base $output) {
 185          return array(
 186              'message'       => clean_text($this->message),
 187              'extraclasses'  => implode(' ', $this->extraclasses),
 188              'announce'      => $this->announce,
 189              'closebutton'   => $this->closebutton,
 190          );
 191      }
 192  
 193      public function get_template_name() {
 194          $templatemappings = [
 195              // Current types mapped to template names.
 196              'success'           => 'core/notification_success',
 197              'info'              => 'core/notification_info',
 198              'warning'           => 'core/notification_warning',
 199              'error'             => 'core/notification_error',
 200          ];
 201  
 202          if (isset($templatemappings[$this->messagetype])) {
 203              return $templatemappings[$this->messagetype];
 204          }
 205          return $templatemappings['error'];
 206      }
 207  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1