/lib/classes/message/message.php

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