| 1 |
efrain |
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 |
*/
|
| 1441 |
ariadna |
36 |
class notification implements renderable, templatable {
|
| 1 |
efrain |
37 |
/**
|
|
|
38 |
* A notification of level 'success'.
|
|
|
39 |
*/
|
|
|
40 |
const NOTIFY_SUCCESS = 'success';
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* A notification of level 'warning'.
|
|
|
44 |
*/
|
|
|
45 |
const NOTIFY_WARNING = 'warning';
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* A notification of level 'info'.
|
|
|
49 |
*/
|
|
|
50 |
const NOTIFY_INFO = 'info';
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* A notification of level 'error'.
|
|
|
54 |
*/
|
|
|
55 |
const NOTIFY_ERROR = 'error';
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* @var string Message payload.
|
|
|
59 |
*/
|
|
|
60 |
protected $message = '';
|
|
|
61 |
|
|
|
62 |
/**
|
| 1441 |
ariadna |
63 |
* @var string Title payload.
|
|
|
64 |
*/
|
|
|
65 |
protected ?string $title = null;
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* @var string Title icon name.
|
|
|
69 |
*/
|
|
|
70 |
protected ?string $titleicon = null;
|
|
|
71 |
|
|
|
72 |
/**
|
| 1 |
efrain |
73 |
* @var string Message type.
|
|
|
74 |
*/
|
|
|
75 |
protected $messagetype = self::NOTIFY_WARNING;
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* @var bool $announce Whether this notification should be announced assertively to screen readers.
|
|
|
79 |
*/
|
|
|
80 |
protected $announce = true;
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @var bool $closebutton Whether this notification should inlcude a button to dismiss itself.
|
|
|
84 |
*/
|
|
|
85 |
protected $closebutton = true;
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* @var array $extraclasses A list of any extra classes that may be required.
|
|
|
89 |
*/
|
| 1441 |
ariadna |
90 |
protected $extraclasses = [];
|
| 1 |
efrain |
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Notification constructor.
|
|
|
94 |
*
|
|
|
95 |
* @param string $message the message to print out
|
|
|
96 |
* @param ?string $messagetype one of the NOTIFY_* constants..
|
|
|
97 |
* @param bool $closebutton Whether to show a close icon to remove the notification (default true).
|
| 1441 |
ariadna |
98 |
* @param ?string $title the title of the notification
|
|
|
99 |
* @param ?string $titleicon if the title should have an icon you can give the icon name with the component
|
|
|
100 |
* (e.g. 'i/circleinfo, core' or 'i/circleinfo' if the icon is from core
|
| 1 |
efrain |
101 |
*/
|
| 1441 |
ariadna |
102 |
public function __construct(
|
|
|
103 |
$message,
|
|
|
104 |
$messagetype = null,
|
|
|
105 |
$closebutton = true,
|
|
|
106 |
?string $title = null,
|
|
|
107 |
?string $titleicon = null
|
|
|
108 |
) {
|
| 1 |
efrain |
109 |
$this->message = $message;
|
|
|
110 |
|
|
|
111 |
if (empty($messagetype)) {
|
|
|
112 |
$messagetype = self::NOTIFY_ERROR;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
$this->messagetype = $messagetype;
|
|
|
116 |
|
|
|
117 |
$this->closebutton = $closebutton;
|
| 1441 |
ariadna |
118 |
|
|
|
119 |
$this->title = $title;
|
|
|
120 |
|
|
|
121 |
$this->titleicon = $titleicon;
|
| 1 |
efrain |
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 |
*/
|
| 1441 |
ariadna |
154 |
public function set_extra_classes($classes = []) {
|
| 1 |
efrain |
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 array data context for a mustache template
|
|
|
183 |
*/
|
|
|
184 |
public function export_for_template(\renderer_base $output) {
|
| 1441 |
ariadna |
185 |
$titleicon = null;
|
|
|
186 |
if (!empty($this->titleicon)) {
|
|
|
187 |
$icon = $this->titleicon;
|
|
|
188 |
$component = 'core';
|
|
|
189 |
if (strpos($icon, ',') !== false) {
|
|
|
190 |
list($icon, $component) = explode(',', $icon);
|
|
|
191 |
}
|
|
|
192 |
$titleicon = (object) [
|
|
|
193 |
'icon' => $icon,
|
|
|
194 |
'component' => $component,
|
|
|
195 |
];
|
|
|
196 |
}
|
|
|
197 |
return [
|
| 1 |
efrain |
198 |
'message' => clean_text($this->message),
|
|
|
199 |
'extraclasses' => implode(' ', $this->extraclasses),
|
|
|
200 |
'announce' => $this->announce,
|
|
|
201 |
'closebutton' => $this->closebutton,
|
|
|
202 |
'issuccess' => $this->messagetype === 'success',
|
|
|
203 |
'isinfo' => $this->messagetype === 'info',
|
|
|
204 |
'iswarning' => $this->messagetype === 'warning',
|
|
|
205 |
'iserror' => $this->messagetype === 'error',
|
| 1441 |
ariadna |
206 |
'title' => $this->title,
|
|
|
207 |
'titleicon' => $titleicon,
|
|
|
208 |
];
|
| 1 |
efrain |
209 |
}
|
|
|
210 |
|
| 1441 |
ariadna |
211 |
/**
|
|
|
212 |
* Get the name of the template to use for this templatable.
|
|
|
213 |
*
|
|
|
214 |
* @return string
|
|
|
215 |
*/
|
| 1 |
efrain |
216 |
public function get_template_name() {
|
|
|
217 |
$templatemappings = [
|
|
|
218 |
// Current types mapped to template names.
|
|
|
219 |
'success' => 'core/notification_success',
|
|
|
220 |
'info' => 'core/notification_info',
|
|
|
221 |
'warning' => 'core/notification_warning',
|
|
|
222 |
'error' => 'core/notification_error',
|
|
|
223 |
];
|
|
|
224 |
|
|
|
225 |
if (isset($templatemappings[$this->messagetype])) {
|
|
|
226 |
return $templatemappings[$this->messagetype];
|
|
|
227 |
}
|
|
|
228 |
return $templatemappings['error'];
|
|
|
229 |
}
|
|
|
230 |
}
|