2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Notification renderable component.
21 * @copyright 2015 Jetha Chan
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core\output;
28 * Data structure representing a notification.
30 * @copyright 2015 Jetha Chan
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class notification implements \renderable, \templatable {
39 * A notification of level 'success'.
41 const NOTIFY_SUCCESS = 'success';
44 * A notification of level 'warning'.
46 const NOTIFY_WARNING = 'warning';
49 * A notification of level 'info'.
51 const NOTIFY_INFO = 'info';
54 * A notification of level 'error'.
56 const NOTIFY_ERROR = 'error';
62 const NOTIFY_MESSAGE = 'message';
66 * A message notifying the user that a problem occurred.
68 const NOTIFY_PROBLEM = 'problem';
72 * A notification of level 'redirect'.
74 const NOTIFY_REDIRECT = 'redirect';
77 * @var string Message payload.
79 protected $message = '';
82 * @var string Message type.
84 protected $messagetype = self::NOTIFY_WARNING;
87 * @var bool $announce Whether this notification should be announced assertively to screen readers.
89 protected $announce = true;
92 * @var bool $closebutton Whether this notification should inlcude a button to dismiss itself.
94 protected $closebutton = true;
97 * @var array $extraclasses A list of any extra classes that may be required.
99 protected $extraclasses = array();
102 * Notification constructor.
104 * @param string $message the message to print out
105 * @param string $messagetype normally NOTIFY_PROBLEM or NOTIFY_SUCCESS.
107 public function __construct($message, $messagetype = null) {
108 $this->message = $message;
110 if (empty($messagetype)) {
111 $messagetype = self::NOTIFY_ERROR;
114 $this->messagetype = $messagetype;
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.');
125 * Set whether this notification should be announced assertively to screen readers.
127 * @param bool $announce
130 public function set_announce($announce = false) {
131 $this->announce = (bool) $announce;
137 * Set whether this notification should include a button to disiss itself.
139 * @param bool $button
142 public function set_show_closebutton($button = false) {
143 $this->closebutton = (bool) $button;
149 * Add any extra classes that this notification requires.
151 * @param array $classes
154 public function set_extra_classes($classes = array()) {
155 $this->extraclasses = $classes;
161 * Export this data so it can be used as the context for a mustache template.
163 * @param renderer_base $output typically, the renderer that's calling this function
164 * @return stdClass data context for a mustache template
166 public function export_for_template(\renderer_base $output) {
168 'message' => clean_text($this->message),
169 'extraclasses' => implode(' ', $this->extraclasses),
170 'announce' => $this->announce,
171 'closebutton' => $this->closebutton,
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',
184 if (isset($templatemappings[$this->messagetype])) {
185 return $templatemappings[$this->messagetype];
187 return $templatemappings['error'];