notification.lib.php 10 KB

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