Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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