notification.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. const NOTIFICATION_TYPE_MESSAGE = 1;
  46. const NOTIFICATION_TYPE_INVITATION = 2;
  47. const NOTIFICATION_TYPE_GROUP = 3;
  48. const NOTIFICATION_TYPE_WALL_MESSAGE = 4;
  49. const NOTIFICATION_TYPE_DIRECT_MESSAGE = 5;
  50. /**
  51. *
  52. */
  53. public function __construct()
  54. {
  55. $this->table = Database::get_main_table(TABLE_NOTIFICATION);
  56. // Default no-reply email
  57. $this->adminEmail = api_get_setting('noreply_email_address');
  58. $this->adminName = api_get_setting('siteName');
  59. $this->titlePrefix = '['.api_get_setting('siteName').'] ';
  60. // If no-reply email doesn't exist use the admin name/email
  61. if (empty($this->adminEmail)) {
  62. $this->adminEmail = api_get_setting('emailAdministrator');
  63. $this->adminName = api_get_person_name(
  64. api_get_setting('administratorName'),
  65. api_get_setting('administratorSurname'),
  66. null,
  67. PERSON_NAME_EMAIL_ADDRESS
  68. );
  69. }
  70. }
  71. /**
  72. * @return string
  73. */
  74. public function getTitlePrefix()
  75. {
  76. return $this->titlePrefix;
  77. }
  78. /**
  79. * @return string
  80. */
  81. public function getDefaultPlatformSenderEmail()
  82. {
  83. return $this->adminEmail;
  84. }
  85. /**
  86. * @return string
  87. */
  88. public function getDefaultPlatformSenderName()
  89. {
  90. return $this->adminName;
  91. }
  92. /**
  93. * Send the notifications
  94. * @param int notification frequency
  95. */
  96. public function send($frequency = 8)
  97. {
  98. $notifications = $this->find(
  99. 'all',
  100. array('where' => array('sent_at IS NULL AND send_freq = ?' => $frequency))
  101. );
  102. if (!empty($notifications)) {
  103. foreach ($notifications as $item_to_send) {
  104. // Sending email
  105. api_mail_html(
  106. $item_to_send['dest_mail'],
  107. $item_to_send['dest_mail'],
  108. Security::filter_terms($item_to_send['title']),
  109. Security::filter_terms($item_to_send['content']),
  110. $this->adminName,
  111. $this->adminEmail
  112. );
  113. if ($this->debug) {
  114. error_log('Sending message to: '.$item_to_send['dest_mail']);
  115. }
  116. // Updating
  117. $item_to_send['sent_at'] = api_get_utc_datetime();
  118. $this->update($item_to_send);
  119. if ($this->debug) {
  120. error_log('Updating record : '.print_r($item_to_send, 1));
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * @param string $title
  127. * @param array $senderInfo
  128. *
  129. * @return string
  130. */
  131. public function formatTitle($title, $senderInfo)
  132. {
  133. $newTitle = $this->getTitlePrefix();
  134. switch ($this->type) {
  135. case self::NOTIFICATION_TYPE_MESSAGE:
  136. if (!empty($senderInfo)) {
  137. $senderName = api_get_person_name(
  138. $senderInfo['firstname'],
  139. $senderInfo['lastname'],
  140. null,
  141. PERSON_NAME_EMAIL_ADDRESS
  142. );
  143. $newTitle .= sprintf(get_lang('YouHaveANewMessageFromX'), $senderName);
  144. }
  145. break;
  146. case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
  147. $newTitle = $title;
  148. break;
  149. case self::NOTIFICATION_TYPE_INVITATION:
  150. if (!empty($senderInfo)) {
  151. $senderName = api_get_person_name(
  152. $senderInfo['firstname'],
  153. $senderInfo['lastname'],
  154. null,
  155. PERSON_NAME_EMAIL_ADDRESS
  156. );
  157. $newTitle .= sprintf(get_lang('YouHaveANewInvitationFromX'), $senderName);
  158. }
  159. break;
  160. case self::NOTIFICATION_TYPE_GROUP:
  161. if (!empty($senderInfo)) {
  162. $senderName = $senderInfo['group_info']['name'];
  163. $newTitle .= sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $senderName);
  164. $senderName = api_get_person_name(
  165. $senderInfo['user_info']['firstname'],
  166. $senderInfo['user_info']['lastname'],
  167. null,
  168. PERSON_NAME_EMAIL_ADDRESS
  169. );
  170. $newTitle .= $senderName;
  171. }
  172. break;
  173. }
  174. return $newTitle;
  175. }
  176. /**
  177. * Save message notification
  178. * @param int $type message type
  179. * NOTIFICATION_TYPE_MESSAGE,
  180. * NOTIFICATION_TYPE_INVITATION,
  181. * NOTIFICATION_TYPE_GROUP
  182. * @param array $user_list recipients: user list of ids
  183. * @param string $title
  184. * @param string $content
  185. * @param array $sender_info
  186. * result of api_get_user_info() or GroupPortalManager:get_group_data()
  187. */
  188. public function save_notification(
  189. $type,
  190. $user_list,
  191. $title,
  192. $content,
  193. $senderInfo = array()
  194. ) {
  195. $this->type = intval($type);
  196. $content = $this->formatContent($content, $senderInfo);
  197. $titleToNotification = $this->formatTitle($title, $senderInfo);
  198. $setting_to_check = '';
  199. $avoid_my_self = false;
  200. switch ($this->type) {
  201. case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
  202. case self::NOTIFICATION_TYPE_MESSAGE:
  203. $setting_to_check = 'mail_notify_message';
  204. $defaultStatus = self::NOTIFY_MESSAGE_AT_ONCE;
  205. break;
  206. case self::NOTIFICATION_TYPE_INVITATION:
  207. $setting_to_check = 'mail_notify_invitation';
  208. $defaultStatus = self::NOTIFY_INVITATION_AT_ONCE;
  209. break;
  210. case self::NOTIFICATION_TYPE_GROUP:
  211. $setting_to_check = 'mail_notify_group_message';
  212. $defaultStatus = self::NOTIFY_GROUP_AT_ONCE;
  213. $avoid_my_self = true;
  214. break;
  215. default:
  216. $defaultStatus = self::NOTIFY_MESSAGE_AT_ONCE;
  217. break;
  218. }
  219. $settingInfo = UserManager::get_extra_field_information_by_name($setting_to_check);
  220. if (!empty($user_list)) {
  221. foreach ($user_list as $user_id) {
  222. if ($avoid_my_self) {
  223. if ($user_id == api_get_user_id()) {
  224. continue;
  225. }
  226. }
  227. $userInfo = api_get_user_info($user_id);
  228. // Extra field was deleted or removed? Use the default status.
  229. if (empty($settingInfo)) {
  230. $userSetting = $defaultStatus;
  231. } else {
  232. $extra_data = UserManager::get_extra_user_data($user_id);
  233. $userSetting = $extra_data[$setting_to_check];
  234. }
  235. $sendDate = null;
  236. switch ($userSetting) {
  237. // No notifications
  238. case self::NOTIFY_MESSAGE_NO:
  239. case self::NOTIFY_INVITATION_NO:
  240. case self::NOTIFY_GROUP_NO:
  241. break;
  242. // Send notification right now!
  243. case self::NOTIFY_MESSAGE_AT_ONCE:
  244. case self::NOTIFY_INVITATION_AT_ONCE:
  245. case self::NOTIFY_GROUP_AT_ONCE:
  246. $extraHeaders = array(
  247. 'reply_to' => array(
  248. 'name' => $senderInfo['complete_name'],
  249. 'mail' => $senderInfo['email']
  250. )
  251. );
  252. if (!empty($userInfo['email'])) {
  253. api_mail_html(
  254. $userInfo['complete_name'],
  255. $userInfo['mail'],
  256. Security::filter_terms($titleToNotification),
  257. Security::filter_terms($content),
  258. $this->adminName,
  259. $this->adminEmail,
  260. $extraHeaders
  261. );
  262. }
  263. $sendDate = api_get_utc_datetime();
  264. }
  265. // Saving the notification to be sent some day.
  266. $params = array(
  267. 'sent_at' => $sendDate,
  268. 'dest_user_id' => $user_id,
  269. 'dest_mail' => $userInfo['email'],
  270. 'title' => $title,
  271. 'content' => cut($content, $this->max_content_length),
  272. 'send_freq' => $userSetting
  273. );
  274. $this->save($params);
  275. }
  276. }
  277. }
  278. /**
  279. * Formats the content in order to add the welcome message,
  280. * the notification preference, etc
  281. * @param string $content
  282. * @param array $sender_info result of api_get_user_info() or
  283. * GroupPortalManager:get_group_data()
  284. * @return string
  285. * */
  286. public function formatContent($content, $sender_info)
  287. {
  288. $new_message_text = $link_to_new_message = '';
  289. switch ($this->type) {
  290. case self::NOTIFICATION_TYPE_DIRECT_MESSAGE:
  291. $new_message_text = '';
  292. $link_to_new_message = Display::url(
  293. get_lang('SeeMessage'),
  294. api_get_path(WEB_CODE_PATH) . 'messages/inbox.php'
  295. );
  296. break;
  297. case self::NOTIFICATION_TYPE_MESSAGE:
  298. if (!empty($sender_info)) {
  299. $senderName = api_get_person_name(
  300. $sender_info['firstname'],
  301. $sender_info['lastname'],
  302. null,
  303. PERSON_NAME_EMAIL_ADDRESS
  304. );
  305. $new_message_text = sprintf(get_lang('YouHaveANewMessageFromX'), $senderName);
  306. }
  307. $link_to_new_message = Display::url(
  308. get_lang('SeeMessage'),
  309. api_get_path(WEB_CODE_PATH) . 'messages/inbox.php'
  310. );
  311. break;
  312. case self::NOTIFICATION_TYPE_INVITATION:
  313. if (!empty($sender_info)) {
  314. $senderName = api_get_person_name(
  315. $sender_info['firstname'],
  316. $sender_info['lastname'],
  317. null,
  318. PERSON_NAME_EMAIL_ADDRESS
  319. );
  320. $new_message_text = sprintf(get_lang('YouHaveANewInvitationFromX'), $senderName);
  321. }
  322. $link_to_new_message = Display::url(
  323. get_lang('SeeInvitation'),
  324. api_get_path(WEB_CODE_PATH) . 'social/invitations.php'
  325. );
  326. break;
  327. case self::NOTIFICATION_TYPE_GROUP:
  328. $topic_page = intval($_REQUEST['topics_page_nr']);
  329. if (!empty($sender_info)) {
  330. $senderName = $sender_info['group_info']['name'];
  331. $new_message_text = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $senderName);
  332. $senderName = api_get_person_name(
  333. $sender_info['user_info']['firstname'],
  334. $sender_info['user_info']['lastname'],
  335. null,
  336. PERSON_NAME_EMAIL_ADDRESS
  337. );
  338. $senderName = Display::url(
  339. $senderName,
  340. api_get_path(WEB_CODE_PATH).'social/profile.php?'.$sender_info['user_info']['user_id']
  341. );
  342. $new_message_text .= '<br />'.get_lang('User').': '.$senderName;
  343. }
  344. $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;
  345. $link_to_new_message = Display::url(get_lang('SeeMessage'), $group_url);
  346. break;
  347. }
  348. $preference_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  349. // You have received a new message text
  350. if (!empty($new_message_text)) {
  351. $content = $new_message_text.'<br /><hr><br />'.$content;
  352. }
  353. // See message with link text
  354. if (!empty($link_to_new_message) && api_get_setting('allow_message_tool') == 'true') {
  355. $content = $content.'<br /><br />'.$link_to_new_message;
  356. }
  357. // You have received this message because you are subscribed text
  358. $content = $content.'<br /><hr><i>'.
  359. sprintf(
  360. get_lang('YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'),
  361. Display::url($preference_url, $preference_url)
  362. ).'</i>';
  363. return $content;
  364. }
  365. }