notification.lib.php 16 KB

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