moodle /lib/classes/message/message.php

Language PHP Lines 293
MD5 Hash 5030b71f835f9cd79a701e7e8a724aec Estimated Cost $2,611 (why?)
Repository https://bitbucket.org/moodle/moodle.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * New messaging class.
 *
 * @package   core_message
 * @since     Moodle 2.9
 * @copyright 2015 onwards Ankit Agarwal
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace core\message;

defined('MOODLE_INTERNAL') || die();

/**
 * New messaging class.
 *
 * Required parameters of the $eventdata object:
 *  component string Component name. must exist in message_providers
 *  name string Message type name. must exist in message_providers
 *  userfrom object|int The user sending the message
 *  userto object|int The message recipient
 *  subject string The message subject
 *  fullmessage string The full message in a given format
 *  fullmessageformat int The format if the full message (FORMAT_MOODLE, FORMAT_HTML, ..)
 *  fullmessagehtml string The full version (the message processor will choose with one to use)
 *  smallmessage string The small version of the message
 *
 * Optional parameters of the $eventdata object:
 *  notification bool Should the message be considered as a notification rather than a personal message
 *  contexturl string If this is a notification then you can specify a url to view the event.
 *                    For example the forum post the user is being notified of.
 *  contexturlname string The display text for contexturl.
 *  replyto string An email address which can be used to send an reply.
 *  attachment stored_file File instance that needs to be sent as attachment.
 *  attachname string Name of the attachment.
 *
 * @package   core_message
 * @since     Moodle 2.9
 * @copyright 2015 onwards Ankit Agarwal
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class message {
    /** @var int Course id. */
    private $courseid;

    /** @var string Module name. */
    private $modulename;

    /** @var string Component name. */
    private $component;

    /** @var string Name. */
    private $name;

    /** @var object|int The user who is sending this message. */
    private $userfrom;

    /** @var object|int The user who is receiving from which is sending this message. */
    private $userto;

    /** @var string Subject of the message. */
    private $subject;

    /** @var string Complete message. */
    private $fullmessage;

    /** @var int Message format. */
    private $fullmessageformat;

    /** @var string Complete message in html format. */
    private $fullmessagehtml;

    /** @var  string Smaller version of the message. */
    private $smallmessage;

    /** @var  int Is it a notification? */
    private $notification;

    /** @var  string context url. */
    private $contexturl;

    /** @var  string context name. */
    private $contexturlname;

    /** @var  string An email address which can be used to send an reply. */
    private $replyto;

    /** @var  int Used internally to store the id of the row representing this message in DB. */
    private $savedmessageid;

    /** @var  \stored_file  File to be attached to the message. Note:- not all processors support this.*/
    private $attachment;

    /** @var  string Name of the attachment. Note:- not all processors support this.*/
    private $attachname;

    /** @var  int The time the message was created.*/
    private $timecreated;

    /** @var array a list of properties that is allowed for each message. */
    private $properties = array(
        'courseid',
        'modulename',
        'component',
        'name',
        'userfrom',
        'userto',
        'subject',
        'fullmessage',
        'fullmessageformat',
        'fullmessagehtml',
        'smallmessage',
        'notification',
        'contexturl',
        'contexturlname',
        'replyto',
        'savedmessageid',
        'attachment',
        'attachname',
        'timecreated'
        );

    /** @var array property to store any additional message processor specific content */
    private $additionalcontent = array();

    /**
     * Fullmessagehtml content including any processor specific content.
     *
     * @param string $processorname Name of the processor.
     *
     * @return mixed|string
     */
    protected function get_fullmessagehtml($processorname = '') {
        if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
            return $this->get_message_with_additional_content($processorname, 'fullmessagehtml');
        } else {
            return $this->fullmessagehtml;
        }
    }

    /**
     * Fullmessage content including any processor specific content.
     *
     * @param string $processorname Name of the processor.
     *
     * @return mixed|string
     */
    protected function get_fullmessage($processorname = '') {
        if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
            return $this->get_message_with_additional_content($processorname, 'fullmessage');
        } else {
            return $this->fullmessage;
        }
    }

    /**
     * Smallmessage content including any processor specific content.
     *
     * @param string $processorname Name of the processor.
     *
     * @return mixed|string
     */
    protected function get_smallmessage($processorname = '') {
        if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
            return $this->get_message_with_additional_content($processorname, 'smallmessage');
        } else {
            return $this->smallmessage;
        }
    }

    /**
     * Helper method used to get message content added with processor specific content.
     *
     * @param string $processorname Name of the processor.
     * @param string $messagetype one of 'fullmessagehtml', 'fullmessage', 'smallmessage'.
     *
     * @return mixed|string
     */
    protected function get_message_with_additional_content($processorname, $messagetype) {
        $message = $this->$messagetype;
        if (isset($this->additionalcontent[$processorname]['*'])) {
            // Content that needs to be added to all format.
            $pattern = $this->additionalcontent[$processorname]['*'];
            $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
            $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
        }

        if (isset($this->additionalcontent[$processorname][$messagetype])) {
            // Content that needs to be added to the specific given format.
            $pattern = $this->additionalcontent[$processorname][$messagetype];
            $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
            $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
        }

        return $message;
    }

    /**
     * Magic getter method.
     *
     * @param string $prop name of property to get.
     *
     * @return mixed
     * @throws \coding_exception
     */
    public function __get($prop) {
        if (in_array($prop, $this->properties)) {
            return $this->$prop;
        }
        throw new \coding_exception("Invalid property $prop specified");
    }

    /**
     * Magic setter method.
     *
     * @param string $prop name of property to set.
     * @param mixed $value value to assign to the property.
     *
     * @return mixed
     * @throws \coding_exception
     */
    public function __set($prop, $value) {
        if (in_array($prop, $this->properties)) {
            return $this->$prop = $value;
        }
        throw new \coding_exception("Invalid property $prop specified");
    }

    /**
     * Magic method to check if property is set.
     *
     * @param string $prop name of property to check.
     * @return bool
     * @throws \coding_exception
     */
    public function __isset($prop) {
        if (in_array($prop, $this->properties)) {
            return isset($this->$prop);
        }
        throw new \coding_exception("Invalid property $prop specified");
    }

    /**
     * This method lets you define content that would be added to the message only for specific message processors.
     *
     * Example of $content:-
     * array('fullmessagehtml' => array('header' => 'header content', 'footer' => 'footer content'),
     *       'smallmessage' => array('header' => 'header content for small message', 'footer' => 'footer content'),
     *       '*' => array('header' => 'header content for all types', 'footer' => 'footer content')
     * )
     *
     * @param string $processorname name of the processor.
     * @param array $content content to add in the above defined format.
     */
    public function set_additional_content($processorname, $content) {
        $this->additionalcontent[$processorname] = $content;
    }

    /**
     * Get a event object for a specific processor in stdClass format.
     *
     * @param string $processorname Name of the processor.
     *
     * @return \stdClass event object in stdClass format.
     */
    public function get_eventobject_for_processor($processorname) {
        // This is done for Backwards compatibility. We should consider throwing notices here in future versions and requesting
        // them to use proper api.

        $eventdata = new \stdClass();
        foreach ($this->properties as $prop) {
            $func = "get_$prop";
            $eventdata->$prop = method_exists($this, $func) ? $this->$func($processorname) : $this->$prop;
        }
        return $eventdata;
    }
}
Back to Top