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