Commit | Line | Data |
---|---|---|
263fb9d1 JC |
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; | |
263fb9d1 JC |
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 | /** | |
24346803 | 39 | * A notification of level 'success'. |
263fb9d1 | 40 | */ |
24346803 AN |
41 | const NOTIFY_SUCCESS = 'success'; |
42 | ||
263fb9d1 | 43 | /** |
24346803 | 44 | * A notification of level 'warning'. |
263fb9d1 | 45 | */ |
24346803 AN |
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 | ||
263fb9d1 | 64 | /** |
24346803 | 65 | * @deprecated |
263fb9d1 JC |
66 | * A message notifying the user that a problem occurred. |
67 | */ | |
68 | const NOTIFY_PROBLEM = 'problem'; | |
24346803 | 69 | |
263fb9d1 | 70 | /** |
24346803 AN |
71 | * @deprecated |
72 | * A notification of level 'redirect'. | |
263fb9d1 JC |
73 | */ |
74 | const NOTIFY_REDIRECT = 'redirect'; | |
75 | ||
76 | /** | |
77 | * @var string Message payload. | |
78 | */ | |
24346803 | 79 | protected $message = ''; |
263fb9d1 JC |
80 | |
81 | /** | |
82 | * @var string Message type. | |
83 | */ | |
24346803 AN |
84 | protected $messagetype = self::NOTIFY_WARNING; |
85 | ||
0346323c AN |
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 | ||
24346803 AN |
96 | /** |
97 | * @var array $extraclasses A list of any extra classes that may be required. | |
98 | */ | |
99 | protected $extraclasses = array(); | |
263fb9d1 JC |
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 | */ | |
24346803 AN |
107 | public function __construct($message, $messagetype = null) { |
108 | $this->message = $message; | |
109 | ||
110 | if (empty($messagetype)) { | |
111 | $messagetype = self::NOTIFY_ERROR; | |
112 | } | |
263fb9d1 | 113 | |
263fb9d1 JC |
114 | $this->messagetype = $messagetype; |
115 | ||
24346803 AN |
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 | ||
0346323c AN |
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 | ||
24346803 AN |
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; | |
263fb9d1 JC |
158 | } |
159 | ||
160 | /** | |
161 | * Export this data so it can be used as the context for a mustache template. | |
162 | * | |
163 | * @param renderer_base $output typically, the renderer that's calling this function | |
164 | * @return stdClass data context for a mustache template | |
165 | */ | |
166 | public function export_for_template(\renderer_base $output) { | |
24346803 AN |
167 | return array( |
168 | 'message' => clean_text($this->message), | |
169 | 'extraclasses' => implode(' ', $this->extraclasses), | |
0346323c AN |
170 | 'announce' => $this->announce, |
171 | 'closebutton' => $this->closebutton, | |
24346803 AN |
172 | ); |
173 | } | |
263fb9d1 | 174 | |
24346803 AN |
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 | ]; | |
263fb9d1 | 183 | |
24346803 AN |
184 | if (isset($templatemappings[$this->messagetype])) { |
185 | return $templatemappings[$this->messagetype]; | |
186 | } | |
187 | return $templatemappings['error']; | |
263fb9d1 JC |
188 | } |
189 | } |