weekly release 3.1dev
[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;
26use stdClass;
27
28/**
29 * Data structure representing a notification.
30 *
31 * @copyright 2015 Jetha Chan
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 * @since Moodle 2.9
34 * @package core
35 * @category output
36 */
37class notification implements \renderable, \templatable {
38
39 /**
40 * A generic message.
41 */
42 const NOTIFY_MESSAGE = 'message';
43 /**
44 * A message notifying the user of a successful operation.
45 */
46 const NOTIFY_SUCCESS = 'success';
47 /**
48 * A message notifying the user that a problem occurred.
49 */
50 const NOTIFY_PROBLEM = 'problem';
51 /**
52 * A message to display during a redirect..
53 */
54 const NOTIFY_REDIRECT = 'redirect';
55
56 /**
57 * @var string Message payload.
58 */
59 private $message = '';
60
61 /**
62 * @var string Message type.
63 */
64 private $messagetype = self::NOTIFY_PROBLEM;
65
66 /**
67 * Notification constructor.
68 *
69 * @param string $message the message to print out
70 * @param string $messagetype normally NOTIFY_PROBLEM or NOTIFY_SUCCESS.
71 */
72 public function __construct($message, $messagetype = self::NOTIFY_PROBLEM) {
73
74 $this->message = clean_text($message);
75 $this->messagetype = $messagetype;
76
77 }
78
79 /**
80 * Export this data so it can be used as the context for a mustache template.
81 *
82 * @param renderer_base $output typically, the renderer that's calling this function
83 * @return stdClass data context for a mustache template
84 */
85 public function export_for_template(\renderer_base $output) {
86
87 $data = new stdClass();
88
89 $data->type = $this->messagetype;
90 $data->message = $this->message;
91
92 return $data;
93 }
94}