PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/classes/message/message.php

http://github.com/moodle/moodle
PHP | 337 lines | 130 code | 44 blank | 163 comment | 20 complexity | 10cbdf329230592603fa52c948efb0e9 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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 array a list of properties that is allowed for each message. */
  108. private $properties = array(
  109. 'courseid',
  110. 'modulename',
  111. 'component',
  112. 'name',
  113. 'userfrom',
  114. 'convid',
  115. 'conversationtype',
  116. 'userto',
  117. 'subject',
  118. 'fullmessage',
  119. 'fullmessageformat',
  120. 'fullmessagehtml',
  121. 'smallmessage',
  122. 'notification',
  123. 'contexturl',
  124. 'contexturlname',
  125. 'replyto',
  126. 'replytoname',
  127. 'savedmessageid',
  128. 'attachment',
  129. 'attachname',
  130. 'timecreated',
  131. 'fullmessagetrust',
  132. 'customdata',
  133. );
  134. /** @var array property to store any additional message processor specific content */
  135. private $additionalcontent = array();
  136. /**
  137. * Fullmessagehtml content including any processor specific content.
  138. *
  139. * @param string $processorname Name of the processor.
  140. *
  141. * @return mixed|string
  142. */
  143. protected function get_fullmessagehtml($processorname = '') {
  144. if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
  145. return $this->get_message_with_additional_content($processorname, 'fullmessagehtml');
  146. } else {
  147. return $this->fullmessagehtml;
  148. }
  149. }
  150. /**
  151. * Fullmessage content including any processor specific content.
  152. *
  153. * @param string $processorname Name of the processor.
  154. *
  155. * @return mixed|string
  156. */
  157. protected function get_fullmessage($processorname = '') {
  158. if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
  159. return $this->get_message_with_additional_content($processorname, 'fullmessage');
  160. } else {
  161. return $this->fullmessage;
  162. }
  163. }
  164. /**
  165. * Smallmessage content including any processor specific content.
  166. *
  167. * @param string $processorname Name of the processor.
  168. *
  169. * @return mixed|string
  170. */
  171. protected function get_smallmessage($processorname = '') {
  172. if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
  173. return $this->get_message_with_additional_content($processorname, 'smallmessage');
  174. } else {
  175. return $this->smallmessage;
  176. }
  177. }
  178. /**
  179. * Always JSON encode customdata.
  180. *
  181. * @param mixed $customdata a data structure that must be serialisable using json_encode().
  182. */
  183. protected function set_customdata($customdata) {
  184. // Always include the courseid (because is not stored in the notifications or messages table).
  185. if (!empty($this->courseid) && (is_object($customdata) || is_array($customdata))) {
  186. $customdata = (array) $customdata;
  187. $customdata['courseid'] = $this->courseid;
  188. }
  189. $this->customdata = json_encode($customdata);
  190. }
  191. /**
  192. * Helper method used to get message content added with processor specific content.
  193. *
  194. * @param string $processorname Name of the processor.
  195. * @param string $messagetype one of 'fullmessagehtml', 'fullmessage', 'smallmessage'.
  196. *
  197. * @return mixed|string
  198. */
  199. protected function get_message_with_additional_content($processorname, $messagetype) {
  200. $message = $this->$messagetype;
  201. if (isset($this->additionalcontent[$processorname]['*'])) {
  202. // Content that needs to be added to all format.
  203. $pattern = $this->additionalcontent[$processorname]['*'];
  204. $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
  205. $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
  206. }
  207. if (isset($this->additionalcontent[$processorname][$messagetype])) {
  208. // Content that needs to be added to the specific given format.
  209. $pattern = $this->additionalcontent[$processorname][$messagetype];
  210. $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
  211. $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
  212. }
  213. return $message;
  214. }
  215. /**
  216. * Magic getter method.
  217. *
  218. * @param string $prop name of property to get.
  219. *
  220. * @return mixed
  221. * @throws \coding_exception
  222. */
  223. public function __get($prop) {
  224. if (in_array($prop, $this->properties)) {
  225. return $this->$prop;
  226. }
  227. throw new \coding_exception("Invalid property $prop specified");
  228. }
  229. /**
  230. * Magic setter method.
  231. *
  232. * @param string $prop name of property to set.
  233. * @param mixed $value value to assign to the property.
  234. *
  235. * @return mixed
  236. * @throws \coding_exception
  237. */
  238. public function __set($prop, $value) {
  239. // Custom data must be JSON encoded always.
  240. if ($prop == 'customdata') {
  241. return $this->set_customdata($value);
  242. }
  243. if (in_array($prop, $this->properties)) {
  244. return $this->$prop = $value;
  245. }
  246. throw new \coding_exception("Invalid property $prop specified");
  247. }
  248. /**
  249. * Magic method to check if property is set.
  250. *
  251. * @param string $prop name of property to check.
  252. * @return bool
  253. * @throws \coding_exception
  254. */
  255. public function __isset($prop) {
  256. if (in_array($prop, $this->properties)) {
  257. return isset($this->$prop);
  258. }
  259. throw new \coding_exception("Invalid property $prop specified");
  260. }
  261. /**
  262. * This method lets you define content that would be added to the message only for specific message processors.
  263. *
  264. * Example of $content:-
  265. * array('fullmessagehtml' => array('header' => 'header content', 'footer' => 'footer content'),
  266. * 'smallmessage' => array('header' => 'header content for small message', 'footer' => 'footer content'),
  267. * '*' => array('header' => 'header content for all types', 'footer' => 'footer content')
  268. * )
  269. *
  270. * @param string $processorname name of the processor.
  271. * @param array $content content to add in the above defined format.
  272. */
  273. public function set_additional_content($processorname, $content) {
  274. $this->additionalcontent[$processorname] = $content;
  275. }
  276. /**
  277. * Get a event object for a specific processor in stdClass format.
  278. *
  279. * @param string $processorname Name of the processor.
  280. *
  281. * @return \stdClass event object in stdClass format.
  282. */
  283. public function get_eventobject_for_processor($processorname) {
  284. // This is done for Backwards compatibility. We should consider throwing notices here in future versions and requesting
  285. // them to use proper api.
  286. $eventdata = new \stdClass();
  287. foreach ($this->properties as $prop) {
  288. $func = "get_$prop";
  289. $eventdata->$prop = method_exists($this, $func) ? $this->$func($processorname) : $this->$prop;
  290. }
  291. return $eventdata;
  292. }
  293. }