MDL-63466 core_message: added a test helper class
[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
30146ed1
AT
104 /** @var string A name which can be used with replyto. */
105 private $replytoname;
106
53a842e6
AA
107 /** @var int Used internally to store the id of the row representing this message in DB. */
108 private $savedmessageid;
109
110 /** @var \stored_file File to be attached to the message. Note:- not all processors support this.*/
111 private $attachment;
112
113 /** @var string Name of the attachment. Note:- not all processors support this.*/
114 private $attachname;
115
cc350fd9
AD
116 /** @var int The time the message was created.*/
117 private $timecreated;
118
53a842e6 119 /** @var array a list of properties that is allowed for each message. */
cc350fd9
AD
120 private $properties = array(
121 'courseid',
122 'modulename',
123 'component',
124 'name',
125 'userfrom',
126 'userto',
127 'subject',
128 'fullmessage',
129 'fullmessageformat',
130 'fullmessagehtml',
131 'smallmessage',
132 'notification',
133 'contexturl',
134 'contexturlname',
135 'replyto',
30146ed1 136 'replytoname',
cc350fd9
AD
137 'savedmessageid',
138 'attachment',
139 'attachname',
140 'timecreated'
141 );
53a842e6
AA
142
143 /** @var array property to store any additional message processor specific content */
144 private $additionalcontent = array();
145
146 /**
147 * Fullmessagehtml content including any processor specific content.
148 *
149 * @param string $processorname Name of the processor.
150 *
151 * @return mixed|string
152 */
153 protected function get_fullmessagehtml($processorname = '') {
154 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
155 return $this->get_message_with_additional_content($processorname, 'fullmessagehtml');
156 } else {
157 return $this->fullmessagehtml;
158 }
159 }
160
161 /**
162 * Fullmessage content including any processor specific content.
163 *
164 * @param string $processorname Name of the processor.
165 *
166 * @return mixed|string
167 */
168 protected function get_fullmessage($processorname = '') {
169 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
170 return $this->get_message_with_additional_content($processorname, 'fullmessage');
171 } else {
172 return $this->fullmessage;
173 }
174 }
175
176 /**
177 * Smallmessage content including any processor specific content.
178 *
179 * @param string $processorname Name of the processor.
180 *
181 * @return mixed|string
182 */
183 protected function get_smallmessage($processorname = '') {
184 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
185 return $this->get_message_with_additional_content($processorname, 'smallmessage');
186 } else {
187 return $this->smallmessage;
188 }
189 }
190
191 /**
192 * Helper method used to get message content added with processor specific content.
193 *
194 * @param string $processorname Name of the processor.
195 * @param string $messagetype one of 'fullmessagehtml', 'fullmessage', 'smallmessage'.
196 *
197 * @return mixed|string
198 */
199 protected function get_message_with_additional_content($processorname, $messagetype) {
200 $message = $this->$messagetype;
201 if (isset($this->additionalcontent[$processorname]['*'])) {
202 // Content that needs to be added to all format.
203 $pattern = $this->additionalcontent[$processorname]['*'];
204 $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
205 $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
206 }
207
208 if (isset($this->additionalcontent[$processorname][$messagetype])) {
209 // Content that needs to be added to the specific given format.
210 $pattern = $this->additionalcontent[$processorname][$messagetype];
211 $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
212 $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
213 }
214
215 return $message;
216 }
217
218 /**
219 * Magic getter method.
220 *
221 * @param string $prop name of property to get.
222 *
223 * @return mixed
224 * @throws \coding_exception
225 */
226 public function __get($prop) {
227 if (in_array($prop, $this->properties)) {
228 return $this->$prop;
229 }
230 throw new \coding_exception("Invalid property $prop specified");
231 }
232
233 /**
234 * Magic setter method.
235 *
236 * @param string $prop name of property to set.
237 * @param mixed $value value to assign to the property.
238 *
239 * @return mixed
240 * @throws \coding_exception
241 */
242 public function __set($prop, $value) {
243 if (in_array($prop, $this->properties)) {
244 return $this->$prop = $value;
245 }
246 throw new \coding_exception("Invalid property $prop specified");
247 }
248
22918830
JL
249 /**
250 * Magic method to check if property is set.
251 *
252 * @param string $prop name of property to check.
253 * @return bool
254 * @throws \coding_exception
255 */
256 public function __isset($prop) {
257 if (in_array($prop, $this->properties)) {
258 return isset($this->$prop);
259 }
260 throw new \coding_exception("Invalid property $prop specified");
261 }
262
53a842e6
AA
263 /**
264 * This method lets you define content that would be added to the message only for specific message processors.
265 *
266 * Example of $content:-
267 * array('fullmessagehtml' => array('header' => 'header content', 'footer' => 'footer content'),
268 * 'smallmessage' => array('header' => 'header content for small message', 'footer' => 'footer content'),
269 * '*' => array('header' => 'header content for all types', 'footer' => 'footer content')
270 * )
271 *
272 * @param string $processorname name of the processor.
273 * @param array $content content to add in the above defined format.
274 */
275 public function set_additional_content($processorname, $content) {
276 $this->additionalcontent[$processorname] = $content;
277 }
278
279 /**
280 * Get a event object for a specific processor in stdClass format.
281 *
282 * @param string $processorname Name of the processor.
283 *
284 * @return \stdClass event object in stdClass format.
285 */
286 public function get_eventobject_for_processor($processorname) {
287 // This is done for Backwards compatibility. We should consider throwing notices here in future versions and requesting
288 // them to use proper api.
289
290 $eventdata = new \stdClass();
291 foreach ($this->properties as $prop) {
292 $func = "get_$prop";
293 $eventdata->$prop = method_exists($this, $func) ? $this->$func($processorname) : $this->$prop;
294 }
295 return $eventdata;
296 }
297}