MDL-30811 output: Tidy up notifications
[moodle.git] / lib / classes / output / notification.php
CommitLineData
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
25namespace 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 */
36class 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
86 /**
87 * @var array $extraclasses A list of any extra classes that may be required.
88 */
89 protected $extraclasses = array();
263fb9d1
JC
90
91 /**
92 * Notification constructor.
93 *
94 * @param string $message the message to print out
95 * @param string $messagetype normally NOTIFY_PROBLEM or NOTIFY_SUCCESS.
96 */
24346803
AN
97 public function __construct($message, $messagetype = null) {
98 $this->message = $message;
99
100 if (empty($messagetype)) {
101 $messagetype = self::NOTIFY_ERROR;
102 }
263fb9d1 103
263fb9d1
JC
104 $this->messagetype = $messagetype;
105
24346803
AN
106 switch ($messagetype) {
107 case self::NOTIFY_PROBLEM:
108 case self::NOTIFY_REDIRECT:
109 case self::NOTIFY_MESSAGE:
110 debugging('Use of ' . $messagetype . ' has been deprecated. Please switch to an alternative type.');
111 }
112 }
113
114 /**
115 * Add any extra classes that this notification requires.
116 *
117 * @param array $classes
118 * @return $this
119 */
120 public function set_extra_classes($classes = array()) {
121 $this->extraclasses = $classes;
122
123 return $this;
263fb9d1
JC
124 }
125
126 /**
127 * Export this data so it can be used as the context for a mustache template.
128 *
129 * @param renderer_base $output typically, the renderer that's calling this function
130 * @return stdClass data context for a mustache template
131 */
132 public function export_for_template(\renderer_base $output) {
24346803
AN
133 return array(
134 'message' => clean_text($this->message),
135 'extraclasses' => implode(' ', $this->extraclasses),
136 );
137 }
263fb9d1 138
24346803
AN
139 public function get_template_name() {
140 $templatemappings = [
141 // Current types mapped to template names.
142 'success' => 'core/notification_success',
143 'info' => 'core/notification_info',
144 'warning' => 'core/notification_warning',
145 'error' => 'core/notification_error',
146 ];
263fb9d1 147
24346803
AN
148 if (isset($templatemappings[$this->messagetype])) {
149 return $templatemappings[$this->messagetype];
150 }
151 return $templatemappings['error'];
263fb9d1
JC
152 }
153}