PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/classes/message/message.php

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