See Release Notes
Long Term Support Release
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 26 namespace core\message; 27 28 defined('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 */ 62 class 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 boolean If message is anonymous. */ 136 private $anonymous; 137 138 /** @var array a list of properties that is allowed for each message. */ 139 private $properties = array( 140 'courseid', 141 'modulename', 142 'component', 143 'name', 144 'userfrom', 145 'convid', 146 'conversationtype', 147 'userto', 148 'subject', 149 'fullmessage', 150 'fullmessageformat', 151 'fullmessagehtml', 152 'smallmessage', 153 'notification', 154 'contexturl', 155 'contexturlname', 156 'replyto', 157 'replytoname', 158 'savedmessageid', 159 'attachment', 160 'attachname', 161 'timecreated', 162 'fullmessagetrust', 163 'customdata', 164 'anonymous', 165 ); 166 167 /** @var array property to store any additional message processor specific content */ 168 private $additionalcontent = array(); 169 170 /** 171 * Fullmessagehtml content including any processor specific content. 172 * 173 * @param string $processorname Name of the processor. 174 * 175 * @return mixed|string 176 */ 177 protected function get_fullmessagehtml($processorname = '') { 178 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) { 179 return $this->get_message_with_additional_content($processorname, 'fullmessagehtml'); 180 } else { 181 return $this->fullmessagehtml; 182 } 183 } 184 185 /** 186 * Fullmessage content including any processor specific content. 187 * 188 * @param string $processorname Name of the processor. 189 * 190 * @return mixed|string 191 */ 192 protected function get_fullmessage($processorname = '') { 193 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) { 194 return $this->get_message_with_additional_content($processorname, 'fullmessage'); 195 } else { 196 return $this->fullmessage; 197 } 198 } 199 200 /** 201 * Smallmessage content including any processor specific content. 202 * 203 * @param string $processorname Name of the processor. 204 * 205 * @return mixed|string 206 */ 207 protected function get_smallmessage($processorname = '') { 208 if (!empty($processorname) && isset($this->additionalcontent[$processorname])) { 209 return $this->get_message_with_additional_content($processorname, 'smallmessage'); 210 } else { 211 return $this->smallmessage; 212 } 213 } 214 215 /** 216 * Always JSON encode customdata. 217 * 218 * @param mixed $customdata a data structure that must be serialisable using json_encode(). 219 */ 220 protected function set_customdata($customdata) { 221 // Always include the courseid (because is not stored in the notifications or messages table). 222 if (!empty($this->courseid) && (is_object($customdata) || is_array($customdata))) { 223 $customdata = (array) $customdata; 224 $customdata['courseid'] = $this->courseid; 225 } 226 $this->customdata = json_encode($customdata); 227 } 228 229 /** 230 * Helper method used to get message content added with processor specific content. 231 * 232 * @param string $processorname Name of the processor. 233 * @param string $messagetype one of 'fullmessagehtml', 'fullmessage', 'smallmessage'. 234 * 235 * @return mixed|string 236 */ 237 protected function get_message_with_additional_content($processorname, $messagetype) { 238 $message = $this->$messagetype; 239 if (isset($this->additionalcontent[$processorname]['*'])) { 240 // Content that needs to be added to all format. 241 $pattern = $this->additionalcontent[$processorname]['*']; 242 $message = empty($pattern['header']) ? $message : $pattern['header'] . $message; 243 $message = empty($pattern['footer']) ? $message : $message . $pattern['footer']; 244 } 245 246 if (isset($this->additionalcontent[$processorname][$messagetype])) { 247 // Content that needs to be added to the specific given format. 248 $pattern = $this->additionalcontent[$processorname][$messagetype]; 249 $message = empty($pattern['header']) ? $message : $pattern['header'] . $message; 250 $message = empty($pattern['footer']) ? $message : $message . $pattern['footer']; 251 } 252 253 return $message; 254 } 255 256 /** 257 * Magic getter method. 258 * 259 * @param string $prop name of property to get. 260 * 261 * @return mixed 262 * @throws \coding_exception 263 */ 264 public function __get($prop) { 265 if (in_array($prop, $this->properties)) { 266 return $this->$prop; 267 } 268 throw new \coding_exception("Invalid property $prop specified"); 269 } 270 271 /** 272 * Magic setter method. 273 * 274 * @param string $prop name of property to set. 275 * @param mixed $value value to assign to the property. 276 * 277 * @return mixed 278 * @throws \coding_exception 279 */ 280 public function __set($prop, $value) { 281 282 // Custom data must be JSON encoded always. 283 if ($prop == 'customdata') { 284 return $this->set_customdata($value); 285 } 286 287 if (in_array($prop, $this->properties)) { 288 return $this->$prop = $value; 289 } 290 throw new \coding_exception("Invalid property $prop specified"); 291 } 292 293 /** 294 * Magic method to check if property is set. 295 * 296 * @param string $prop name of property to check. 297 * @return bool 298 * @throws \coding_exception 299 */ 300 public function __isset($prop) { 301 if (in_array($prop, $this->properties)) { 302 return isset($this->$prop); 303 } 304 throw new \coding_exception("Invalid property $prop specified"); 305 } 306 307 /** 308 * This method lets you define content that would be added to the message only for specific message processors. 309 * 310 * Example of $content:- 311 * array('fullmessagehtml' => array('header' => 'header content', 'footer' => 'footer content'), 312 * 'smallmessage' => array('header' => 'header content for small message', 'footer' => 'footer content'), 313 * '*' => array('header' => 'header content for all types', 'footer' => 'footer content') 314 * ) 315 * 316 * @param string $processorname name of the processor. 317 * @param array $content content to add in the above defined format. 318 */ 319 public function set_additional_content($processorname, $content) { 320 $this->additionalcontent[$processorname] = $content; 321 } 322 323 /** 324 * Get a event object for a specific processor in stdClass format. 325 * 326 * @param string $processorname Name of the processor. 327 * 328 * @return \stdClass event object in stdClass format. 329 */ 330 public function get_eventobject_for_processor($processorname) { 331 // This is done for Backwards compatibility. We should consider throwing notices here in future versions and requesting 332 // them to use proper api. 333 334 $eventdata = new \stdClass(); 335 foreach ($this->properties as $prop) { 336 $func = "get_$prop"; 337 $eventdata->$prop = method_exists($this, $func) ? $this->$func($processorname) : $this->$prop; 338 } 339 return $eventdata; 340 } 341 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body