notification.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the Notification management.
  5. * Include/require it in your code to use its features.
  6. * @package chamilo.library
  7. */
  8. /**
  9. * Code
  10. */
  11. /**
  12. * Notification class
  13. * @package chamilo.library
  14. */
  15. class Notification extends Model
  16. {
  17. public $table;
  18. public $columns = array(
  19. 'id',
  20. 'dest_user_id',
  21. 'dest_mail',
  22. 'title',
  23. 'content',
  24. 'send_freq',
  25. 'created_at',
  26. 'sent_at'
  27. );
  28. public $max_content_length = 254; //Max lenght of the notification.content field
  29. public $debug = false;
  30. public $type;
  31. public $admin_name;
  32. public $admin_email;
  33. public $send_email_as_user;
  34. //default values
  35. const NOTIFY_MESSAGE_AT_ONCE = 1;
  36. const NOTIFY_MESSAGE_DAILY = 8;
  37. const NOTIFY_MESSAGE_WEEKLY = 12;
  38. const NOTIFY_MESSAGE_NO = 0;
  39. //mail_notify_message ("At once", "Daily", "No")
  40. const NOTIFY_INVITATION_AT_ONCE = 1;
  41. const NOTIFY_INVITATION_DAILY = 8;
  42. const NOTIFY_INVITATION_WEEKLY = 12;
  43. const NOTIFY_INVITATION_NO = 0;
  44. //mail_notify_invitation ("At once", "Daily", "No")
  45. const NOTIFY_GROUP_AT_ONCE = 1;
  46. const NOTIFY_GROUP_DAILY = 8;
  47. const NOTIFY_GROUP_WEEKLY = 12;
  48. const NOTIFY_GROUP_NO = 0;
  49. const NOTIFICATION_TYPE_MESSAGE = 1;
  50. const NOTIFICATION_TYPE_INVITATION = 2;
  51. const NOTIFICATION_TYPE_GROUP = 3;
  52. // mail_notify_group_message ("At once", "Daily", "No")
  53. /**
  54. *
  55. */
  56. public function __construct()
  57. {
  58. $this->table = Database::get_main_table(TABLE_NOTIFICATION);
  59. $this->sender_email = api_get_setting('noreply_email_address');
  60. $this->sender_name = api_get_setting('siteName');
  61. // If no-reply email doesn't exist use the admin email
  62. if (empty($this->sender_email)) {
  63. $this->sender_email = api_get_setting('emailAdministrator');
  64. $this->sender_name = 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. * Send the notifications
  74. * @param int notification frequency
  75. */
  76. public function send($frec = NOTIFY_MESSAGE_DAILY)
  77. {
  78. $notifications = $this->find('all', array('where' => array('sent_at IS NULL AND send_freq = ?' => $frec)));
  79. if (!empty($notifications)) {
  80. foreach ($notifications as $item_to_send) {
  81. $this->set_sender_info($item_to_send['sender_id']);
  82. //Sending email
  83. api_mail_html(
  84. $item_to_send['dest_mail'],
  85. $item_to_send['dest_mail'],
  86. Security::filter_terms($item_to_send['title']),
  87. Security::filter_terms($item_to_send['content']),
  88. $this->sender_name,
  89. $this->sender_email,
  90. $this->extra_headers
  91. );
  92. if ($this->debug) {
  93. error_log('Sending message to: '.$item_to_send['dest_mail']);
  94. }
  95. //Updating
  96. $item_to_send['sent_at'] = api_get_utc_datetime();
  97. $this->update($item_to_send);
  98. if ($this->debug) {
  99. error_log('Updating record : '.print_r($item_to_send, 1));
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Sets the sender info in order to add the reply-to
  106. */
  107. function set_sender_info($user_id)
  108. {
  109. if (!empty($user_id)) {
  110. $sender_user_info = api_get_user_info($user_id);
  111. if ($this->send_email_as_user) {
  112. $this->sender_email = $sender_user_info['email'];
  113. $this->sender_name = $sender_user_info['complete_name'];
  114. } else {
  115. $this->extra_headers = array(
  116. 'reply_to' => array(
  117. 'name' => $sender_user_info['complete_name'],
  118. 'mail' => $sender_user_info['email']
  119. )
  120. );
  121. }
  122. }
  123. }
  124. /**
  125. * Save message notification
  126. * @param array message type NOTIFICATION_TYPE_MESSAGE, NOTIFICATION_TYPE_INVITATION, NOTIFICATION_TYPE_GROUP
  127. * @param array recipients: user list of ids
  128. * @param string title
  129. * @param string content of the message
  130. * @param array result of api_get_user_info() or UserGroup->get()
  131. */
  132. public function save_notification($type, $user_list, $title, $content, $sender_info = array(), $text_content = null)
  133. {
  134. $this->type = intval($type);
  135. $content = $this->format_content($content, $sender_info);
  136. $sender_id = 0;
  137. if (!empty($sender_info) && isset($sender_info['user_id'])) {
  138. $sender_id = $sender_info['user_id'];
  139. $this->set_sender_info($sender_id);
  140. }
  141. $setting_to_check = '';
  142. $avoid_my_self = false;
  143. $default_status = self::NOTIFY_MESSAGE_AT_ONCE;
  144. switch ($this->type) {
  145. case self::NOTIFICATION_TYPE_MESSAGE:
  146. $setting_to_check = 'mail_notify_message';
  147. $default_status = self::NOTIFY_MESSAGE_AT_ONCE;
  148. break;
  149. case self::NOTIFICATION_TYPE_INVITATION:
  150. $setting_to_check = 'mail_notify_invitation';
  151. $default_status = self::NOTIFY_INVITATION_AT_ONCE;
  152. break;
  153. case self::NOTIFICATION_TYPE_GROUP:
  154. $setting_to_check = 'mail_notify_group_message';
  155. $default_status = self::NOTIFY_GROUP_AT_ONCE;
  156. $avoid_my_self = true;
  157. break;
  158. }
  159. $setting_info = UserManager::get_extra_field_information_by_name($setting_to_check);
  160. if (!empty($user_list)) {
  161. foreach ($user_list as $user_id) {
  162. if ($avoid_my_self) {
  163. if ($user_id == api_get_user_id()) {
  164. continue;
  165. }
  166. }
  167. $user_info = api_get_user_info($user_id);
  168. //Extra field was deleted or removed? Use the default status
  169. if (empty($setting_info)) {
  170. $user_setting = $default_status;
  171. } else {
  172. $extra_data = UserManager::get_extra_user_data($user_id);
  173. $user_setting = $extra_data[$setting_to_check];
  174. }
  175. $params = array();
  176. switch ($user_setting) {
  177. //No notifications
  178. case self::NOTIFY_MESSAGE_NO:
  179. case self::NOTIFY_INVITATION_NO:
  180. case self::NOTIFY_GROUP_NO:
  181. break;
  182. //Send notification right now!
  183. case self::NOTIFY_MESSAGE_AT_ONCE:
  184. case self::NOTIFY_INVITATION_AT_ONCE:
  185. case self::NOTIFY_GROUP_AT_ONCE:
  186. if (!empty($user_info['mail'])) {
  187. $name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  188. if (!empty($sender_info['complete_name']) && !empty($sender_info['email'])) {
  189. $extra_headers = array();
  190. $extra_headers['reply_to']['mail'] = $sender_info['email'];
  191. $extra_headers['reply_to']['name'] = $sender_info['complete_name'];
  192. api_mail_html(
  193. $name,
  194. $user_info['mail'],
  195. Security::filter_terms($title),
  196. Security::filter_terms($content),
  197. $sender_info['complete_name'],
  198. $sender_info['email'],
  199. $extra_headers,
  200. array(),
  201. null,
  202. $text_content
  203. );
  204. } else {
  205. api_mail_html(
  206. $name,
  207. $user_info['mail'],
  208. Security::filter_terms($title),
  209. Security::filter_terms($content),
  210. $sender_info['complete_name'],
  211. $sender_info['email'],
  212. array(),
  213. null,
  214. $text_content
  215. );
  216. }
  217. }
  218. $params['sent_at'] = api_get_utc_datetime();
  219. // Saving the notification to be sent some day.
  220. default:
  221. $params['dest_user_id'] = $user_id;
  222. $params['dest_mail'] = $user_info['mail'];
  223. $params['title'] = $title;
  224. $params['content'] = Text::cut($content, $this->max_content_length);
  225. $params['send_freq'] = $user_setting;
  226. $params['sender_id'] = $sender_id;
  227. $this->save($params);
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. /**
  234. * Formats the content in order to add the welcome message, the notification preference, etc
  235. * @param string the content
  236. * @param array result of api_get_user_info() or UserGroup->get()
  237. * @todo create new templates based in Twig
  238. * */
  239. public function format_content($content, $sender_info)
  240. {
  241. return $content;
  242. $new_message_text = $link_to_new_message = '';
  243. switch ($this->type) {
  244. case self::NOTIFICATION_TYPE_MESSAGE:
  245. if (!empty($sender_info)) {
  246. $sender_name = api_get_person_name(
  247. $sender_info['firstname'],
  248. $sender_info['lastname'],
  249. null,
  250. PERSON_NAME_EMAIL_ADDRESS
  251. );
  252. $new_message_text = sprintf(get_lang('YouHaveANewMessageFromX'), $sender_name);
  253. }
  254. $link_to_new_message = Display::url(
  255. get_lang('SeeMessage'),
  256. api_get_path(WEB_CODE_PATH).'messages/inbox.php'
  257. );
  258. break;
  259. case self::NOTIFICATION_TYPE_INVITATION:
  260. if (!empty($sender_info)) {
  261. $sender_name = api_get_person_name(
  262. $sender_info['firstname'],
  263. $sender_info['lastname'],
  264. null,
  265. PERSON_NAME_EMAIL_ADDRESS
  266. );
  267. //$sender_mail = $sender_info['email'] ;
  268. $new_message_text = sprintf(get_lang('YouHaveANewInvitationFromX'), $sender_name);
  269. }
  270. $link_to_new_message = Display::url(
  271. get_lang('SeeInvitation'),
  272. api_get_path(WEB_CODE_PATH).'social/invitations.php'
  273. );
  274. break;
  275. case self::NOTIFICATION_TYPE_GROUP:
  276. $topic_page = intval($_REQUEST['topics_page_nr']);
  277. if (!empty($sender_info)) {
  278. $sender_name = $sender_info['group_info']['name'];
  279. $new_message_text = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $sender_name);
  280. if (isset($sender_info['user_info']) && !empty($sender_info['user_info'])) {
  281. $sender_name = api_get_person_name(
  282. $sender_info['user_info']['firstname'],
  283. $sender_info['user_info']['lastname'],
  284. null,
  285. PERSON_NAME_EMAIL_ADDRESS
  286. );
  287. $sender_name = Display::url(
  288. $sender_name,
  289. api_get_path(WEB_CODE_PATH).'social/profile.php?'.$sender_info['user_info']['user_id']
  290. );
  291. $new_message_text .= '<br />'.get_lang('User').': '.$sender_name;
  292. }
  293. }
  294. $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;
  295. $link_to_new_message = Display::url(get_lang('SeeMessage'), $group_url);
  296. break;
  297. }
  298. $preference_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  299. // You have received a new message text
  300. if (!empty($new_message_text)) {
  301. $content = $new_message_text.'<br /><hr><br />'.$content;
  302. }
  303. // See message with link text
  304. if (!empty($link_to_new_message)) {
  305. $content = $content.'<br /><br />'.$link_to_new_message;
  306. }
  307. // You have received this message because you are subscribed text
  308. $content = $content.'<br /><hr><i>'.
  309. sprintf(
  310. get_lang(
  311. 'YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'
  312. ),
  313. Display::url($preference_url, $preference_url)
  314. ).'</i>';
  315. return $content;
  316. }
  317. }