PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/classes/message/message.php

https://github.com/lazydaisy/moodle
PHP | 304 lines | 114 code | 38 blank | 152 comment | 15 complexity | 3586c017f59398deb5a75607dcd92754 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. * 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. *
  52. * @package core_message
  53. * @since Moodle 2.9
  54. * @copyright 2015 onwards Ankit Agarwal
  55. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  56. */
  57. class message {
  58. /** @var int Course id. */
  59. private $courseid;
  60. /** @var string Module name. */
  61. private $modulename;
  62. /** @var string Component name. */
  63. private $component;
  64. /** @var string Name. */
  65. private $name;
  66. /** @var object|int The user who is sending this message. */
  67. private $userfrom;
  68. /** @var int The conversation id where userfrom is sending this message. */
  69. private $convid;
  70. /** @var object|int The user who is receiving from which is sending this message. */
  71. private $userto;
  72. /** @var string Subject of the message. */
  73. private $subject;
  74. /** @var string Complete message. */
  75. private $fullmessage;
  76. /** @var int Message format. */
  77. private $fullmessageformat;
  78. /** @var string Complete message in html format. */
  79. private $fullmessagehtml;
  80. /** @var string Smaller version of the message. */
  81. private $smallmessage;
  82. /** @var int Is it a notification? */
  83. private $notification;
  84. /** @var string context url. */
  85. private $contexturl;
  86. /** @var string context name. */
  87. private $contexturlname;
  88. /** @var string An email address which can be used to send an reply. */
  89. private $replyto;
  90. /** @var string A name which can be used with replyto. */
  91. private $replytoname;
  92. /** @var int Used internally to store the id of the row representing this message in DB. */
  93. private $savedmessageid;
  94. /** @var \stored_file File to be attached to the message. Note:- not all processors support this.*/
  95. private $attachment;
  96. /** @var string Name of the attachment. Note:- not all processors support this.*/
  97. private $attachname;
  98. /** @var int The time the message was created.*/
  99. private $timecreated;
  100. /** @var array a list of properties that is allowed for each message. */
  101. private $properties = array(
  102. 'courseid',
  103. 'modulename',
  104. 'component',
  105. 'name',
  106. 'userfrom',
  107. 'convid',
  108. 'userto',
  109. 'subject',
  110. 'fullmessage',
  111. 'fullmessageformat',
  112. 'fullmessagehtml',
  113. 'smallmessage',
  114. 'notification',
  115. 'contexturl',
  116. 'contexturlname',
  117. 'replyto',
  118. 'replytoname',
  119. 'savedmessageid',
  120. 'attachment',
  121. 'attachname',
  122. 'timecreated'
  123. );
  124. /** @var array property to store any additional message processor specific content */
  125. private $additionalcontent = array();
  126. /**
  127. * Fullmessagehtml content including any processor specific content.
  128. *
  129. * @param string $processorname Name of the processor.
  130. *
  131. * @return mixed|string
  132. */
  133. protected function get_fullmessagehtml($processorname = '') {
  134. if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
  135. return $this->get_message_with_additional_content($processorname, 'fullmessagehtml');
  136. } else {
  137. return $this->fullmessagehtml;
  138. }
  139. }
  140. /**
  141. * Fullmessage content including any processor specific content.
  142. *
  143. * @param string $processorname Name of the processor.
  144. *
  145. * @return mixed|string
  146. */
  147. protected function get_fullmessage($processorname = '') {
  148. if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
  149. return $this->get_message_with_additional_content($processorname, 'fullmessage');
  150. } else {
  151. return $this->fullmessage;
  152. }
  153. }
  154. /**
  155. * Smallmessage content including any processor specific content.
  156. *
  157. * @param string $processorname Name of the processor.
  158. *
  159. * @return mixed|string
  160. */
  161. protected function get_smallmessage($processorname = '') {
  162. if (!empty($processorname) && isset($this->additionalcontent[$processorname])) {
  163. return $this->get_message_with_additional_content($processorname, 'smallmessage');
  164. } else {
  165. return $this->smallmessage;
  166. }
  167. }
  168. /**
  169. * Helper method used to get message content added with processor specific content.
  170. *
  171. * @param string $processorname Name of the processor.
  172. * @param string $messagetype one of 'fullmessagehtml', 'fullmessage', 'smallmessage'.
  173. *
  174. * @return mixed|string
  175. */
  176. protected function get_message_with_additional_content($processorname, $messagetype) {
  177. $message = $this->$messagetype;
  178. if (isset($this->additionalcontent[$processorname]['*'])) {
  179. // Content that needs to be added to all format.
  180. $pattern = $this->additionalcontent[$processorname]['*'];
  181. $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
  182. $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
  183. }
  184. if (isset($this->additionalcontent[$processorname][$messagetype])) {
  185. // Content that needs to be added to the specific given format.
  186. $pattern = $this->additionalcontent[$processorname][$messagetype];
  187. $message = empty($pattern['header']) ? $message : $pattern['header'] . $message;
  188. $message = empty($pattern['footer']) ? $message : $message . $pattern['footer'];
  189. }
  190. return $message;
  191. }
  192. /**
  193. * Magic getter method.
  194. *
  195. * @param string $prop name of property to get.
  196. *
  197. * @return mixed
  198. * @throws \coding_exception
  199. */
  200. public function __get($prop) {
  201. if (in_array($prop, $this->properties)) {
  202. return $this->$prop;
  203. }
  204. throw new \coding_exception("Invalid property $prop specified");
  205. }
  206. /**
  207. * Magic setter method.
  208. *
  209. * @param string $prop name of property to set.
  210. * @param mixed $value value to assign to the property.
  211. *
  212. * @return mixed
  213. * @throws \coding_exception
  214. */
  215. public function __set($prop, $value) {
  216. if (in_array($prop, $this->properties)) {
  217. return $this->$prop = $value;
  218. }
  219. throw new \coding_exception("Invalid property $prop specified");
  220. }
  221. /**
  222. * Magic method to check if property is set.
  223. *
  224. * @param string $prop name of property to check.
  225. * @return bool
  226. * @throws \coding_exception
  227. */
  228. public function __isset($prop) {
  229. if (in_array($prop, $this->properties)) {
  230. return isset($this->$prop);
  231. }
  232. throw new \coding_exception("Invalid property $prop specified");
  233. }
  234. /**
  235. * This method lets you define content that would be added to the message only for specific message processors.
  236. *
  237. * Example of $content:-
  238. * array('fullmessagehtml' => array('header' => 'header content', 'footer' => 'footer content'),
  239. * 'smallmessage' => array('header' => 'header content for small message', 'footer' => 'footer content'),
  240. * '*' => array('header' => 'header content for all types', 'footer' => 'footer content')
  241. * )
  242. *
  243. * @param string $processorname name of the processor.
  244. * @param array $content content to add in the above defined format.
  245. */
  246. public function set_additional_content($processorname, $content) {
  247. $this->additionalcontent[$processorname] = $content;
  248. }
  249. /**
  250. * Get a event object for a specific processor in stdClass format.
  251. *
  252. * @param string $processorname Name of the processor.
  253. *
  254. * @return \stdClass event object in stdClass format.
  255. */
  256. public function get_eventobject_for_processor($processorname) {
  257. // This is done for Backwards compatibility. We should consider throwing notices here in future versions and requesting
  258. // them to use proper api.
  259. $eventdata = new \stdClass();
  260. foreach ($this->properties as $prop) {
  261. $func = "get_$prop";
  262. $eventdata->$prop = method_exists($this, $func) ? $this->$func($processorname) : $this->$prop;
  263. }
  264. return $eventdata;
  265. }
  266. }