notification.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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('id', 'dest_user_id', 'dest_mail', 'title', 'content', 'send_freq', 'created_at', 'sent_at');
  19. public $max_content_length = 254; //Max lenght of the notification.content field
  20. public $debug = false;
  21. public $type;
  22. public $admin_name;
  23. public $admin_email;
  24. public $send_email_as_user;
  25. //default values
  26. const NOTIFY_MESSAGE_AT_ONCE = 1;
  27. const NOTIFY_MESSAGE_DAILY = 8;
  28. const NOTIFY_MESSAGE_WEEKLY = 12;
  29. const NOTIFY_MESSAGE_NO = 0;
  30. //mail_notify_message ("At once", "Daily", "No")
  31. const NOTIFY_INVITATION_AT_ONCE = 1;
  32. const NOTIFY_INVITATION_DAILY = 8;
  33. const NOTIFY_INVITATION_WEEKLY = 12;
  34. const NOTIFY_INVITATION_NO = 0;
  35. //mail_notify_invitation ("At once", "Daily", "No")
  36. const NOTIFY_GROUP_AT_ONCE = 1;
  37. const NOTIFY_GROUP_DAILY = 8;
  38. const NOTIFY_GROUP_WEEKLY = 12;
  39. const NOTIFY_GROUP_NO = 0;
  40. const NOTIFICATION_TYPE_MESSAGE = 1;
  41. const NOTIFICATION_TYPE_INVITATION = 2;
  42. const NOTIFICATION_TYPE_GROUP = 3;
  43. // mail_notify_group_message ("At once", "Daily", "No")
  44. /**
  45. *
  46. */
  47. public function __construct()
  48. {
  49. $this->table = Database::get_main_table(TABLE_NOTIFICATION);
  50. $this->sender_email = api_get_setting('noreply_email_address');
  51. $this->sender_name = api_get_setting('siteName');
  52. // If no-reply email doesn't exist use the admin email
  53. if (empty($this->sender_email)) {
  54. $this->sender_email = api_get_setting('emailAdministrator');
  55. $this->sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  56. }
  57. }
  58. /**
  59. * Send the notifications
  60. * @param int notification frecuency
  61. */
  62. public function send($frec = NOTIFY_MESSAGE_DAILY) {
  63. $notifications = $this->find('all', array('where'=> array('sent_at IS NULL AND send_freq = ?' => $frec)));
  64. if (!empty($notifications)) {
  65. foreach ($notifications as $item_to_send) {
  66. $this->set_sender_info($item_to_send['sender_id']);
  67. //Sending email
  68. api_mail_html($item_to_send['dest_mail'],
  69. $item_to_send['dest_mail'],
  70. Security::filter_terms($item_to_send['title']),
  71. Security::filter_terms($item_to_send['content']),
  72. $this->sender_name,
  73. $this->sender_email,
  74. $this->extra_headers
  75. );
  76. if ($this->debug) { error_log('Sending message to: '.$item_to_send['dest_mail']); }
  77. //Updating
  78. $item_to_send['sent_at'] = api_get_utc_datetime();
  79. $this->update($item_to_send);
  80. if ($this->debug) { error_log('Updating record : '.print_r($item_to_send,1)); }
  81. }
  82. }
  83. }
  84. /**
  85. * Sets the sender info in order to add the reply-to
  86. */
  87. function set_sender_info($user_id) {
  88. if (!empty($user_id)) {
  89. $sender_user_info = api_get_user_info($user_id);
  90. if ($this->send_email_as_user) {
  91. $this->sender_email = $sender_user_info['email'];
  92. $this->sender_name = $sender_user_info['complete_name'];
  93. } else {
  94. $this->extra_headers = array('reply_to' => array('name' => $sender_user_info['complete_name'], 'mail' => $sender_user_info['email']));
  95. }
  96. }
  97. }
  98. /**
  99. * Save message notification
  100. * @param array message type NOTIFICATION_TYPE_MESSAGE, NOTIFICATION_TYPE_INVITATION, NOTIFICATION_TYPE_GROUP
  101. * @param array recipients: user list of ids
  102. * @param string title
  103. * @param string content of the message
  104. * @param array result of api_get_user_info() or UserGroup->get()
  105. */
  106. public function save_notification($type, $user_list, $title, $content, $sender_info = array()) {
  107. $this->type = intval($type);
  108. $content = $this->format_content($content, $sender_info);
  109. $sender_id = 0;
  110. if (!empty($sender_info) && isset($sender_info['user_id'])) {
  111. $sender_id = $sender_info['user_id'];
  112. $this->set_sender_info($sender_id);
  113. }
  114. $setting_to_check = '';
  115. $avoid_my_self = false;
  116. $default_status = self::NOTIFY_MESSAGE_AT_ONCE;
  117. switch ($this->type) {
  118. case self::NOTIFICATION_TYPE_MESSAGE;
  119. $setting_to_check = 'mail_notify_message';
  120. $default_status = self::NOTIFY_MESSAGE_AT_ONCE;
  121. break;
  122. case self::NOTIFICATION_TYPE_INVITATION;
  123. $setting_to_check = 'mail_notify_invitation';
  124. $default_status = self::NOTIFY_INVITATION_AT_ONCE;
  125. break;
  126. case self::NOTIFICATION_TYPE_GROUP;
  127. $setting_to_check = 'mail_notify_group_message';
  128. $default_status = self::NOTIFY_GROUP_AT_ONCE;
  129. $avoid_my_self = true;
  130. break;
  131. }
  132. $setting_info = UserManager::get_extra_field_information_by_name($setting_to_check);
  133. if (!empty($user_list)) {
  134. foreach ($user_list as $user_id) {
  135. if ($avoid_my_self) {
  136. if ($user_id == api_get_user_id()) {
  137. continue;
  138. }
  139. }
  140. $user_info = api_get_user_info($user_id);
  141. //Extra field was deleted or removed? Use the default status
  142. if (empty($setting_info)) {
  143. $user_setting = $default_status;
  144. } else {
  145. $extra_data = UserManager::get_extra_user_data($user_id);
  146. $user_setting = $extra_data[$setting_to_check];
  147. }
  148. $params = array();
  149. switch ($user_setting) {
  150. //No notifications
  151. case self::NOTIFY_MESSAGE_NO:
  152. case self::NOTIFY_INVITATION_NO:
  153. case self::NOTIFY_GROUP_NO:
  154. break;
  155. //Send notification right now!
  156. case self::NOTIFY_MESSAGE_AT_ONCE:
  157. case self::NOTIFY_INVITATION_AT_ONCE:
  158. case self::NOTIFY_GROUP_AT_ONCE:
  159. if (!empty($user_info['mail'])) {
  160. $name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  161. if (!empty($sender_info['complete_name']) && !empty($sender_info['email'])) {
  162. $extra_headers = array();
  163. $extra_headers['reply_to']['mail'] = $sender_info['email'];
  164. $extra_headers['reply_to']['name'] = $sender_info['complete_name'];
  165. api_mail_html($name, $user_info['mail'], Security::filter_terms($title), Security::filter_terms($content), $sender_info['complete_name'], $sender_info['email'], $extra_headers);
  166. } else {
  167. api_mail_html($name, $user_info['mail'], Security::filter_terms($title), Security::filter_terms($content), $sender_info['complete_name'], $sender_info['email']);
  168. }
  169. }
  170. $params['sent_at'] = api_get_utc_datetime();
  171. //Saving the notification to be sent some day
  172. default:
  173. $params['dest_user_id'] = $user_id;
  174. $params['dest_mail'] = $user_info['mail'];
  175. $params['title'] = $title;
  176. $params['content'] = Text::cut($content, $this->max_content_length);
  177. $params['send_freq'] = $user_setting;
  178. $params['sender_id'] = $sender_id;
  179. $this->save($params);
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * Formats the content in order to add the welcome message, the notification preference, etc
  187. * @param string the content
  188. * @param array result of api_get_user_info() or UserGroup->get()
  189. * */
  190. public function format_content($content, $sender_info) {
  191. $new_message_text = $link_to_new_message = '';
  192. switch ($this->type) {
  193. case self::NOTIFICATION_TYPE_MESSAGE:
  194. if (!empty($sender_info)) {
  195. $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  196. $new_message_text = sprintf(get_lang('YouHaveANewMessageFromX'), $sender_name);
  197. }
  198. $link_to_new_message = Display::url(get_lang('SeeMessage'), api_get_path(WEB_CODE_PATH).'messages/inbox.php');
  199. break;
  200. case self::NOTIFICATION_TYPE_INVITATION:
  201. if (!empty($sender_info)) {
  202. $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  203. //$sender_mail = $sender_info['email'] ;
  204. $new_message_text = sprintf(get_lang('YouHaveANewInvitationFromX'), $sender_name);
  205. }
  206. $link_to_new_message = Display::url(get_lang('SeeInvitation'), api_get_path(WEB_CODE_PATH).'social/invitations.php');
  207. break;
  208. case self::NOTIFICATION_TYPE_GROUP:
  209. $topic_page = intval($_REQUEST['topics_page_nr']);
  210. if (!empty($sender_info)) {
  211. $sender_name = $sender_info['group_info']['name'];
  212. $new_message_text = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $sender_name);
  213. if (isset($sender_info['user_info']) && !empty($sender_info['user_info'])) {
  214. $sender_name = api_get_person_name($sender_info['user_info']['firstname'], $sender_info['user_info']['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  215. $sender_name = Display::url($sender_name , api_get_path(WEB_CODE_PATH).'social/profile.php?'.$sender_info['user_info']['user_id']);
  216. $new_message_text .= '<br />'.get_lang('User').': '.$sender_name;
  217. }
  218. }
  219. $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;
  220. $link_to_new_message = Display::url(get_lang('SeeMessage'), $group_url);
  221. break;
  222. }
  223. $preference_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  224. // You have received a new message text
  225. if (!empty($new_message_text)) {
  226. $content = $new_message_text.'<br /><hr><br />'.$content;
  227. }
  228. // See message with link text
  229. if (!empty($link_to_new_message)) {
  230. $content = $content.'<br /><br />'.$link_to_new_message;
  231. }
  232. // You have received this message because you are subscribed text
  233. $content = $content.'<br /><hr><i>'.
  234. sprintf(get_lang('YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'), Display::url($preference_url, $preference_url)).'</i>';
  235. return $content;
  236. }
  237. }