weekly release 3.5dev
[moodle.git] / lib / classes / message / message.php
CommitLineData
53a842e6
AA
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 * New messaging class.
19 *
20 * @package core_message
21 * @since Moodle 2.9
22 * @copyright 2015 onwards Ankit Agarwal
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26namespace core\message;
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * New messaging class.
32 *
33 * Required parameters of the $eventdata object:
34 * component string Component name. must exist in message_providers
35 * name string Message type name. must exist in message_providers
36 * userfrom object|int The user sending the message
37 * userto object|int The message recipient
38 * subject string The message subject
39 * fullmessage string The full message in a given format
40 * fullmessageformat int The format if the full message (FORMAT_MOODLE, FORMAT_HTML, ..)
41 * fullmessagehtml string The full version (the message processor will choose with one to use)
42 * smallmessage string The small version of the message
43 *
44 * Optional parameters of the $eventdata object:
45 * notification bool Should the message be considered as a notification rather than a personal message
46 * contexturl string If this is a notification then you can specify a url to view the event.
47 * For example the forum post the user is being notified of.
48 * contexturlname string The display text for contexturl.
49 * replyto string An email address which can be used to send an reply.
50 * attachment stored_file File instance that needs to be sent as attachment.
51 * attachname string Name of the attachment.
52 *
53 * @package core_message
54 * @since Moodle 2.9
55 * @copyright 2015 onwards Ankit Agarwal
56 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
57 */
58class message {
cc350fd9
AD
59 /** @var int Course id. */
60 private $courseid;
61
62 /** @var string Module name. */
63 private $modulename;
64
53a842e6
AA
65 /** @var string Component name. */
66 private $component;
67
68 /** @var string Name. */
69 private $name;
70
71 /** @var object|int The user who is sending this message. */
72 private $userfrom;
73
74 /** @var object|int The user who is receiving from which is sending this message. */
75 private $userto;
76
77 /** @var string Subject of the message. */
78 private $subject;
79
80 /** @var string Complete message. */
81 private $fullmessage;
82
83 /** @var int Message format. */
84 private $fullmessageformat;
85
86 /** @var string Complete message in html format. */
87 private $fullmessagehtml;
88
89 /** @var string Smaller version of the message. */
90 private $smallmessage;
91
92 /** @var int Is it a notification? */
93 private $notification;
94
95 /** @var string context url. */
96 private $contexturl;
97
98 /** @var string context name. */
99 private $contexturlname;
100
101 /** @var string An email address which can be used to send an reply. */
102 private $replyto;
103
104 /** @var int Used internally to store the id of the row representing this message in DB. */
105 private $savedmessageid;
106
107 /** @var \stored_file File to be attached to the message. Note:- not all processors support this.*/
108 private $attachment;
109
110 /** @var string Name of the attachment. Note:- not all processors support this.*/
111 private $attachname;
112
cc350fd9
AD
113 /** @var int The time the message was created.*/
114 private $timecreated;
115
53a842e6 116 /** @var array a list of properties that is allowed for each message. */
cc350fd9
AD
117 private $properties = array(
118 'courseid',
119 'modulename',
120 'component',
121 'name',
122 'userfrom',
123 'userto',
124 'subject',
125 'fullmessage',
126 'fullmessageformat',
127 'fullmessagehtml',
128 'smallmessage',
129 'notification',
130 'contexturl',
131 'contexturlname',
132 'replyto',
133 'savedmessageid',
134 'attachment',
135 'attachname',
136 'timecreated'
137 );
53a842e6
AA
138
139 /** @var array property to store any additional message processor specific content */
140 private $additionalcontent = array();
141
142 /**
143 * Fullmessagehtml content including any processor specific content.
144 *
145 * @param string $processorname Name of the processor.
146 *
147 * @return mixed|string
148 */
149 protected function get_fullmessagehtml($processorname = '') {
150 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
151 return $this->get_message_with_additional_content($processorname, 'fullmessagehtml');
152 } else {
153 return $this->fullmessagehtml;
154 }
155 }
156
157 /**
158 * Fullmessage content including any processor specific content.
159 *
160 * @param string $processorname Name of the processor.
161 *
162 * @return mixed|string
163 */
164 protected function get_fullmessage($processorname = '') {
165 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
166 return $this->get_message_with_additional_content($processorname, 'fullmessage');
167 } else {
168 return $this->fullmessage;
169 }
170 }
171
172 /**
173 * Smallmessage content including any processor specific content.
174 *
175 * @param string $processorname Name of the processor.
176 *
177 * @return mixed|string
178 */
179 protected function get_smallmessage($processorname = '') {
180 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
181 return $this->get_message_with_additional_content($processorname, 'smallmessage');
182 } else {
183 return $this->smallmessage;
184 }
185 }
186
187 /**
188 * Helper method used to get message content added with processor specific content.
189 *
190 * @param string $processorname Name of the processor.
191 * @param string $messagetype one of 'fullmessagehtml', 'fullmessage', 'smallmessage'.
192 *
193 * @return mixed|string
194 */
195 protected function get_message_with_additional_content($processorname, $messagetype) {
196 $message = $this->$messagetype;
197 if (isset($this->additionalcontent[$processorname]['*'])) {
198 // Content that needs to be added to all format.
199 $pattern = $this->additionalcontent[$processorname]['*'];
200 $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
201 $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
202 }
203
204 if (isset($this->additionalcontent[$processorname][$messagetype])) {
205 // Content that needs to be added to the specific given format.
206 $pattern = $this->additionalcontent[$processorname][$messagetype];
207 $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
208 $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
209 }
210
211 return $message;
212 }
213
214 /**
215 * Magic getter method.
216 *
217 * @param string $prop name of property to get.
218 *
219 * @return mixed
220 * @throws \coding_exception
221 */
222 public function __get($prop) {
223 if (in_array($prop, $this->properties)) {
224 return $this->$prop;
225 }
226 throw new \coding_exception("Invalid property $prop specified");
227 }
228
229 /**
230 * Magic setter method.
231 *
232 * @param string $prop name of property to set.
233 * @param mixed $value value to assign to the property.
234 *
235 * @return mixed
236 * @throws \coding_exception
237 */
238 public function __set($prop, $value) {
239 if (in_array($prop, $this->properties)) {
240 return $this->$prop = $value;
241 }
242 throw new \coding_exception("Invalid property $prop specified");
243 }
244
22918830
JL
245 /**
246 * Magic method to check if property is set.
247 *
248 * @param string $prop name of property to check.
249 * @return bool
250 * @throws \coding_exception
251 */
252 public function __isset($prop) {
253 if (in_array($prop, $this->properties)) {
254 return isset($this->$prop);
255 }
256 throw new \coding_exception("Invalid property $prop specified");
257 }
258
53a842e6
AA
259 /**
260 * This method lets you define content that would be added to the message only for specific message processors.
261 *
262 * Example of $content:-
263 * array('fullmessagehtml' => array('header' => 'header content', 'footer' => 'footer content'),
264 * 'smallmessage' => array('header' => 'header content for small message', 'footer' => 'footer content'),
265 * '*' => array('header' => 'header content for all types', 'footer' => 'footer content')
266 * )
267 *
268 * @param string $processorname name of the processor.
269 * @param array $content content to add in the above defined format.
270 */
271 public function set_additional_content($processorname, $content) {
272 $this->additionalcontent[$processorname] = $content;
273 }
274
275 /**
276 * Get a event object for a specific processor in stdClass format.
277 *
278 * @param string $processorname Name of the processor.
279 *
280 * @return \stdClass event object in stdClass format.
281 */
282 public function get_eventobject_for_processor($processorname) {
283 // This is done for Backwards compatibility. We should consider throwing notices here in future versions and requesting
284 // them to use proper api.
285
286 $eventdata = new \stdClass();
287 foreach ($this->properties as $prop) {
288 $func = "get_$prop";
289 $eventdata->$prop = method_exists($this, $func) ? $this->$func($processorname) : $this->$prop;
290 }
291 return $eventdata;
292 }
293}