notification.lib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * Notification class
  6. * This class provides methods for the Notification management.
  7. * Include/require it in your code to use its features.
  8. * @package chamilo.library
  9. */
  10. class Notification extends Model
  11. {
  12. public $table;
  13. public $columns = array(
  14. 'id',
  15. 'dest_user_id',
  16. 'dest_mail',
  17. 'title',
  18. 'content',
  19. 'send_freq',
  20. 'created_at',
  21. 'sent_at'
  22. );
  23. //Max length of the notification.content field
  24. public $max_content_length = 254;
  25. public $debug = false;
  26. /* message, invitation, group messages */
  27. public $type;
  28. public $adminName;
  29. public $adminEmail;
  30. public $titlePrefix;
  31. // mail_notify_message ("At once", "Daily", "No")
  32. const NOTIFY_MESSAGE_AT_ONCE = 1;
  33. const NOTIFY_MESSAGE_DAILY = 8;
  34. const NOTIFY_MESSAGE_WEEKLY = 12;
  35. const NOTIFY_MESSAGE_NO = 0;
  36. // mail_notify_invitation ("At once", "Daily", "No")
  37. const NOTIFY_INVITATION_AT_ONCE = 1;
  38. const NOTIFY_INVITATION_DAILY = 8;
  39. const NOTIFY_INVITATION_WEEKLY = 12;
  40. const NOTIFY_INVITATION_NO = 0;
  41. // mail_notify_group_message ("At once", "Daily", "No")
  42. const NOTIFY_GROUP_AT_ONCE = 1;
  43. const NOTIFY_GROUP_DAILY = 8;
  44. const NOTIFY_GROUP_WEEKLY = 12;
  45. const NOTIFY_GROUP_NO = 0;
  46. // Notification types
  47. const NOTIFICATION_TYPE_MESSAGE = 1;
  48. const NOTIFICATION_TYPE_INVITATION = 2;
  49. const NOTIFICATION_TYPE_GROUP = 3;
  50. const NOTIFICATION_TYPE_WALL_MESSAGE = 4;
  51. const NOTIFICATION_TYPE_DIRECT_MESSAGE = 5;
  52. /**
  53. * Constructor
  54. */
  55. public function __construct()
  56. {
  57. $this->table = Database::get_main_table(TABLE_NOTIFICATION);
  58. // Default no-reply email
  59. $this->adminEmail = api_get_setting('mail.noreply_email_address');
  60. $this->adminName = api_get_setting('platform.site_name');
  61. $this->titlePrefix = '['.api_get_setting('platform.site_name').'] ';
  62. // If no-reply email doesn't exist use the admin name/email
  63. if (empty($this->adminEmail)) {
  64. $this->adminEmail = api_get_setting('admin.administrator_email');
  65. $this->adminName = api_get_person_name(
  66. api_get_setting('admin.administrator_name'),
  67. api_get_setting('admin.administrator_surname'),
  68. null,
  69. PERSON_NAME_EMAIL_ADDRESS
  70. );
  71. }
  72. }
  73. /**
  74. * @return string
  75. */
  76. public function getTitlePrefix()
  77. {
  78. return $this->titlePrefix;
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function getDefaultPlatformSenderEmail()
  84. {
  85. return $this->adminEmail;
  86. }
  87. /**
  88. * @return string
  89. */
  90. public function getDefaultPlatformSenderName()
  91. {
  92. return $this->adminName;
  93. }
  94. /**
  95. * Send the notifications
  96. * @param int $frequency notification frequency
  97. */
  98. public function send($frequency = 8)
  99. {
  100. $notifications = $this->find(
  101. 'all',
  102. array('where' => array('sent_at IS NULL AND send_freq = ?' => $frequency))
  103. );
  104. if (!empty($notifications)) {
  105. foreach ($notifications as $item_to_send) {
  106. // Sending email
  107. api_mail_html(
  108. $item_to_send['dest_mail'],
  109. $item_to_send['dest_mail'],
  110. Security::filter_terms($item_to_send['title']),
  111. Security::filter_terms($item_to_send['content']),
  112. $this->adminName,
  113. $this->adminEmail
  114. );
  115. if ($this->debug) {
  116. error_log('Sending message to: '.$item_to_send['dest_mail']);
  117. }
  118. // Updating
  119. $item_to_send['sent_at'] = api_get_utc_datetime();
  120. $this->update($item_to_send);
  121. if ($this->debug) {
  122. error_log('Updating record : '.print_r($item_to_send, 1));
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * @param string $title
  129. * @param array $senderInfo
  130. *
  131. * @return string
  132. */
  133. public function formatTitle($title, $senderInfo)
  134. {
  135. $hook = HookNotificationTitle::create();
  136. if (!empty($hook)) {
  137. $hook->setEventData(array('title' => $title));
  138. $data = $hook->notifyNotificationTitle(HOOK_EVENT_TYPE_PRE);
  139. if (isset($data['title'])) {
  140. $title = $data['title'];
  141. }
  142. }
  143. $newTitle = $this->getTitlePrefix();
  144. switch ($this->type) {
  145. case self::NOTIFICATION_TYPE_MESSAGE:
  146. if (!empty($senderInfo)) {
  147. $senderName = api_get_person_name(
  148. $senderInfo['firstname'],
  149. $senderInfo['lastname'],
  150. null,
  151. PERSON_NAME_EMAIL_ADDRESS
  152. );
  153. $newTitle .= sprintf(get_lang('YouHaveANewMessageFromX'), $senderName);
  154. }
  155. break;
  156. case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
  157. $newTitle = $title;
  158. break;
  159. case self::NOTIFICATION_TYPE_INVITATION:
  160. if (!empty($senderInfo)) {
  161. $senderName = api_get_person_name(
  162. $senderInfo['firstname'],
  163. $senderInfo['lastname'],
  164. null,
  165. PERSON_NAME_EMAIL_ADDRESS
  166. );
  167. $newTitle .= sprintf(get_lang('YouHaveANewInvitationFromX'), $senderName);
  168. }
  169. break;
  170. case self::NOTIFICATION_TYPE_GROUP:
  171. if (!empty($senderInfo)) {
  172. $senderName = $senderInfo['group_info']['name'];
  173. $newTitle .= sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $senderName);
  174. $senderName = api_get_person_name(
  175. $senderInfo['user_info']['firstname'],
  176. $senderInfo['user_info']['lastname'],
  177. null,
  178. PERSON_NAME_EMAIL_ADDRESS
  179. );
  180. $newTitle .= $senderName;
  181. }
  182. break;
  183. }
  184. if (!empty($hook)) {
  185. $hook->setEventData(array('title' => $newTitle));
  186. $data = $hook->notifyNotificationTitle(HOOK_EVENT_TYPE_POST);
  187. if (isset($data['title'])) {
  188. $newTitle = $data['title'];
  189. }
  190. }
  191. return $newTitle;
  192. }
  193. /**
  194. * Save message notification
  195. * @param int $type message type
  196. * NOTIFICATION_TYPE_MESSAGE,
  197. * NOTIFICATION_TYPE_INVITATION,
  198. * NOTIFICATION_TYPE_GROUP
  199. * @param array $user_list recipients: user list of ids
  200. * @param string $title
  201. * @param string $content
  202. * @param array $sender_info
  203. * result of api_get_user_info() or GroupPortalManager:get_group_data()
  204. */
  205. public function save_notification(
  206. $type,
  207. $user_list,
  208. $title,
  209. $content,
  210. $senderInfo = array(),
  211. $attachments = array()
  212. ) {
  213. $this->type = intval($type);
  214. $content = $this->formatContent($content, $senderInfo);
  215. $titleToNotification = $this->formatTitle($title, $senderInfo);
  216. $settingToCheck = '';
  217. $avoid_my_self = false;
  218. switch ($this->type) {
  219. case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
  220. case self::NOTIFICATION_TYPE_MESSAGE:
  221. $settingToCheck = 'mail_notify_message';
  222. $defaultStatus = self::NOTIFY_MESSAGE_AT_ONCE;
  223. break;
  224. case self::NOTIFICATION_TYPE_INVITATION:
  225. $settingToCheck = 'mail_notify_invitation';
  226. $defaultStatus = self::NOTIFY_INVITATION_AT_ONCE;
  227. break;
  228. case self::NOTIFICATION_TYPE_GROUP:
  229. $settingToCheck = 'mail_notify_group_message';
  230. $defaultStatus = self::NOTIFY_GROUP_AT_ONCE;
  231. $avoid_my_self = true;
  232. break;
  233. default:
  234. $defaultStatus = self::NOTIFY_MESSAGE_AT_ONCE;
  235. break;
  236. }
  237. $settingInfo = UserManager::get_extra_field_information_by_name($settingToCheck);
  238. if (!empty($user_list)) {
  239. foreach ($user_list as $user_id) {
  240. if ($avoid_my_self) {
  241. if ($user_id == api_get_user_id()) {
  242. continue;
  243. }
  244. }
  245. $userInfo = api_get_user_info($user_id);
  246. // Extra field was deleted or removed? Use the default status.
  247. $userSetting = $defaultStatus;
  248. if (!empty($settingInfo)) {
  249. $extra_data = UserManager::get_extra_user_data($user_id);
  250. if (isset($extra_data[$settingToCheck]) && !empty($extra_data[$settingToCheck])) {
  251. $userSetting = $extra_data[$settingToCheck];
  252. }
  253. }
  254. $sendDate = null;
  255. switch ($userSetting) {
  256. // No notifications
  257. case self::NOTIFY_MESSAGE_NO:
  258. case self::NOTIFY_INVITATION_NO:
  259. case self::NOTIFY_GROUP_NO:
  260. break;
  261. // Send notification right now!
  262. case self::NOTIFY_MESSAGE_AT_ONCE:
  263. case self::NOTIFY_INVITATION_AT_ONCE:
  264. case self::NOTIFY_GROUP_AT_ONCE:
  265. $extraHeaders = [];
  266. if (isset($senderInfo['email'])) {
  267. $extraHeaders = array(
  268. 'reply_to' => array(
  269. 'name' => $senderInfo['complete_name'],
  270. 'mail' => $senderInfo['email']
  271. )
  272. );
  273. }
  274. if (!empty($userInfo['email'])) {
  275. api_mail_html(
  276. $userInfo['complete_name'],
  277. $userInfo['mail'],
  278. Security::filter_terms($titleToNotification),
  279. Security::filter_terms($content),
  280. $this->adminName,
  281. $this->adminEmail,
  282. $extraHeaders,
  283. $attachments
  284. );
  285. }
  286. $sendDate = api_get_utc_datetime();
  287. }
  288. // Saving the notification to be sent some day.
  289. $content = cut($content, $this->max_content_length);
  290. $params = array(
  291. 'sent_at' => $sendDate,
  292. 'dest_user_id' => $user_id,
  293. 'dest_mail' => $userInfo['email'],
  294. 'title' => $title,
  295. 'content' => $content,
  296. 'send_freq' => $userSetting
  297. );
  298. $this->save($params);
  299. }
  300. MessagesWebService::sendPushNotification($user_list, $title, $content);
  301. }
  302. }
  303. /**
  304. * Formats the content in order to add the welcome message,
  305. * the notification preference, etc
  306. * @param string $content
  307. * @param array $senderInfo result of api_get_user_info() or
  308. * GroupPortalManager:get_group_data()
  309. *
  310. * @return string
  311. * */
  312. public function formatContent($content, $senderInfo)
  313. {
  314. $hook = HookNotificationContent::create();
  315. if (!empty($hook)) {
  316. $hook->setEventData(array('content' => $content));
  317. $data = $hook->notifyNotificationContent(HOOK_EVENT_TYPE_PRE);
  318. if (isset($data['content'])) {
  319. $content = $data['content'];
  320. }
  321. }
  322. $newMessageText = $linkToNewMessage = '';
  323. switch ($this->type) {
  324. case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
  325. $newMessageText = '';
  326. $linkToNewMessage = Display::url(
  327. get_lang('SeeMessage'),
  328. api_get_path(WEB_CODE_PATH) . 'messages/inbox.php'
  329. );
  330. break;
  331. case self::NOTIFICATION_TYPE_MESSAGE:
  332. if (!empty($senderInfo)) {
  333. $senderName = api_get_person_name(
  334. $senderInfo['firstname'],
  335. $senderInfo['lastname'],
  336. null,
  337. PERSON_NAME_EMAIL_ADDRESS
  338. );
  339. $newMessageText = sprintf(get_lang('YouHaveANewMessageFromX'), $senderName);
  340. }
  341. $linkToNewMessage = Display::url(
  342. get_lang('SeeMessage'),
  343. api_get_path(WEB_CODE_PATH) . 'messages/inbox.php'
  344. );
  345. break;
  346. case self::NOTIFICATION_TYPE_INVITATION:
  347. if (!empty($senderInfo)) {
  348. $senderName = api_get_person_name(
  349. $senderInfo['firstname'],
  350. $senderInfo['lastname'],
  351. null,
  352. PERSON_NAME_EMAIL_ADDRESS
  353. );
  354. $newMessageText = sprintf(get_lang('YouHaveANewInvitationFromX'), $senderName);
  355. }
  356. $linkToNewMessage = Display::url(
  357. get_lang('SeeInvitation'),
  358. api_get_path(WEB_CODE_PATH) . 'social/invitations.php'
  359. );
  360. break;
  361. case self::NOTIFICATION_TYPE_GROUP:
  362. $topic_page = intval($_REQUEST['topics_page_nr']);
  363. if (!empty($senderInfo)) {
  364. $senderName = $senderInfo['group_info']['name'];
  365. $newMessageText = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $senderName);
  366. $senderName = api_get_person_name(
  367. $senderInfo['user_info']['firstname'],
  368. $senderInfo['user_info']['lastname'],
  369. null,
  370. PERSON_NAME_EMAIL_ADDRESS
  371. );
  372. $senderName = Display::url(
  373. $senderName,
  374. api_get_path(WEB_CODE_PATH).'social/profile.php?'.$senderInfo['user_info']['user_id']
  375. );
  376. $newMessageText .= '<br />'.get_lang('User').': '.$senderName;
  377. }
  378. $group_url = api_get_path(WEB_CODE_PATH).'social/group_topics.php?id='.$senderInfo['group_info']['id'].'&topic_id='.$senderInfo['group_info']['topic_id'].'&msg_id='.$senderInfo['group_info']['msg_id'].'&topics_page_nr='.$topic_page;
  379. $linkToNewMessage = Display::url(get_lang('SeeMessage'), $group_url);
  380. break;
  381. }
  382. $preference_url = Container::getRouter()->generate('fos_user_profile_edit');
  383. // You have received a new message text
  384. if (!empty($newMessageText)) {
  385. $content = $newMessageText.'<br /><hr><br />'.$content;
  386. }
  387. // See message with link text
  388. if (!empty($linkToNewMessage) && api_get_setting(
  389. 'message.allow_message_tool'
  390. ) == 'true'
  391. ) {
  392. $content = $content.'<br /><br />'.$linkToNewMessage;
  393. }
  394. // You have received this message because you are subscribed text
  395. $content = $content.'<br /><hr><i>'.
  396. sprintf(
  397. get_lang('YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'),
  398. Display::url($preference_url, $preference_url)
  399. ).'</i>';
  400. if (!empty($hook)) {
  401. $hook->setEventData(array('content' => $content));
  402. $data = $hook->notifyNotificationContent(HOOK_EVENT_TYPE_POST);
  403. if (isset($data['content'])) {
  404. $content = $data['content'];
  405. }
  406. }
  407. return $content;
  408. }
  409. }