PageRenderTime 274ms CodeModel.GetById 69ms app.highlight 9ms RepoModel.GetById 71ms app.codeStats 0ms

/lib/classes/message/message.php

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