notification.lib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. ) {
  211. $this->type = intval($type);
  212. $content = $this->formatContent($content, $senderInfo);
  213. $titleToNotification = $this->formatTitle($title, $senderInfo);
  214. $settingToCheck = '';
  215. $avoid_my_self = false;
  216. switch ($this->type) {
  217. case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
  218. case self::NOTIFICATION_TYPE_MESSAGE:
  219. $settingToCheck = 'mail_notify_message';
  220. $defaultStatus = self::NOTIFY_MESSAGE_AT_ONCE;
  221. break;
  222. case self::NOTIFICATION_TYPE_INVITATION:
  223. $settingToCheck = 'mail_notify_invitation';
  224. $defaultStatus = self::NOTIFY_INVITATION_AT_ONCE;
  225. break;
  226. case self::NOTIFICATION_TYPE_GROUP:
  227. $settingToCheck = 'mail_notify_group_message';
  228. $defaultStatus = self::NOTIFY_GROUP_AT_ONCE;
  229. $avoid_my_self = true;
  230. break;
  231. default:
  232. $defaultStatus = self::NOTIFY_MESSAGE_AT_ONCE;
  233. break;
  234. }
  235. $settingInfo = UserManager::get_extra_field_information_by_name($settingToCheck);
  236. if (!empty($user_list)) {
  237. foreach ($user_list as $user_id) {
  238. if ($avoid_my_self) {
  239. if ($user_id == api_get_user_id()) {
  240. continue;
  241. }
  242. }
  243. $userInfo = api_get_user_info($user_id);
  244. // Extra field was deleted or removed? Use the default status.
  245. $userSetting = $defaultStatus;
  246. if (!empty($settingInfo)) {
  247. $extra_data = UserManager::get_extra_user_data($user_id);
  248. if (isset($extra_data[$settingToCheck]) && !empty($extra_data[$settingToCheck])) {
  249. $userSetting = $extra_data[$settingToCheck];
  250. }
  251. }
  252. $sendDate = null;
  253. switch ($userSetting) {
  254. // No notifications
  255. case self::NOTIFY_MESSAGE_NO:
  256. case self::NOTIFY_INVITATION_NO:
  257. case self::NOTIFY_GROUP_NO:
  258. break;
  259. // Send notification right now!
  260. case self::NOTIFY_MESSAGE_AT_ONCE:
  261. case self::NOTIFY_INVITATION_AT_ONCE:
  262. case self::NOTIFY_GROUP_AT_ONCE:
  263. $extraHeaders = [];
  264. if (isset($senderInfo['email'])) {
  265. $extraHeaders = array(
  266. 'reply_to' => array(
  267. 'name' => $senderInfo['complete_name'],
  268. 'mail' => $senderInfo['email']
  269. )
  270. );
  271. }
  272. if (!empty($userInfo['email'])) {
  273. api_mail_html(
  274. $userInfo['complete_name'],
  275. $userInfo['mail'],
  276. Security::filter_terms($titleToNotification),
  277. Security::filter_terms($content),
  278. $this->adminName,
  279. $this->adminEmail,
  280. $extraHeaders
  281. );
  282. }
  283. $sendDate = api_get_utc_datetime();
  284. }
  285. // Saving the notification to be sent some day.
  286. $params = array(
  287. 'sent_at' => $sendDate,
  288. 'dest_user_id' => $user_id,
  289. 'dest_mail' => $userInfo['email'],
  290. 'title' => $title,
  291. 'content' => cut($content, $this->max_content_length),
  292. 'send_freq' => $userSetting
  293. );
  294. $this->save($params);
  295. }
  296. }
  297. }
  298. /**
  299. * Formats the content in order to add the welcome message,
  300. * the notification preference, etc
  301. * @param string $content
  302. * @param array $sender_info result of api_get_user_info() or
  303. * GroupPortalManager:get_group_data()
  304. * @return string
  305. * */
  306. public function formatContent($content, $sender_info)
  307. {
  308. $hook = HookNotificationContent::create();
  309. if (!empty($hook)) {
  310. $hook->setEventData(array('content' => $content));
  311. $data = $hook->notifyNotificationContent(HOOK_EVENT_TYPE_PRE);
  312. if (isset($data['content'])) {
  313. $content = $data['content'];
  314. }
  315. }
  316. $new_message_text = $link_to_new_message = '';
  317. switch ($this->type) {
  318. case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
  319. $new_message_text = $content;
  320. $link_to_new_message = Display::url(
  321. get_lang('SeeMessage'),
  322. api_get_path(WEB_CODE_PATH) . 'messages/inbox.php'
  323. );
  324. break;
  325. case self::NOTIFICATION_TYPE_MESSAGE:
  326. if (!empty($sender_info)) {
  327. $senderName = api_get_person_name(
  328. $sender_info['firstname'],
  329. $sender_info['lastname'],
  330. null,
  331. PERSON_NAME_EMAIL_ADDRESS
  332. );
  333. $new_message_text = sprintf(get_lang('YouHaveANewMessageFromX'), $senderName);
  334. }
  335. $link_to_new_message = Display::url(
  336. get_lang('SeeMessage'),
  337. api_get_path(WEB_CODE_PATH) . 'messages/inbox.php'
  338. );
  339. break;
  340. case self::NOTIFICATION_TYPE_INVITATION:
  341. if (!empty($sender_info)) {
  342. $senderName = api_get_person_name(
  343. $sender_info['firstname'],
  344. $sender_info['lastname'],
  345. null,
  346. PERSON_NAME_EMAIL_ADDRESS
  347. );
  348. $new_message_text = sprintf(get_lang('YouHaveANewInvitationFromX'), $senderName);
  349. }
  350. $link_to_new_message = Display::url(
  351. get_lang('SeeInvitation'),
  352. api_get_path(WEB_CODE_PATH) . 'social/invitations.php'
  353. );
  354. break;
  355. case self::NOTIFICATION_TYPE_GROUP:
  356. $topic_page = intval($_REQUEST['topics_page_nr']);
  357. if (!empty($sender_info)) {
  358. $senderName = $sender_info['group_info']['name'];
  359. $new_message_text = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $senderName);
  360. $senderName = api_get_person_name(
  361. $sender_info['user_info']['firstname'],
  362. $sender_info['user_info']['lastname'],
  363. null,
  364. PERSON_NAME_EMAIL_ADDRESS
  365. );
  366. $senderName = Display::url(
  367. $senderName,
  368. api_get_path(WEB_CODE_PATH).'social/profile.php?'.$sender_info['user_info']['user_id']
  369. );
  370. $new_message_text .= '<br />'.get_lang('User').': '.$senderName;
  371. }
  372. $group_url = api_get_path(WEB_CODE_PATH).'social/group_topics.php?id='.$sender_info['group_info']['id'].'&topic_id='.$sender_info['group_info']['topic_id'].'&msg_id='.$sender_info['group_info']['msg_id'].'&topics_page_nr='.$topic_page;
  373. $link_to_new_message = Display::url(get_lang('SeeMessage'), $group_url);
  374. break;
  375. }
  376. $preference_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  377. // You have received a new message text
  378. if (!empty($new_message_text)) {
  379. $content = $new_message_text.'<br /><hr><br />'.$content;
  380. }
  381. // See message with link text
  382. if (!empty($link_to_new_message)) {
  383. $content = $content.'<br /><br />'.$link_to_new_message;
  384. }
  385. // You have received this message because you are subscribed text
  386. $content = $content.'<br /><hr><i>'.
  387. sprintf(
  388. get_lang('YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'),
  389. Display::url($preference_url, $preference_url)
  390. ).'</i>';
  391. if (!empty($hook)) {
  392. $hook->setEventData(array('content' => $content));
  393. $data = $hook->notifyNotificationContent(HOOK_EVENT_TYPE_POST);
  394. if (isset($data['content'])) {
  395. $content = $data['content'];
  396. }
  397. }
  398. return $content;
  399. }
  400. }