notification.lib.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS); //api_get_setting('siteName')
  47. $this->admin_email = api_get_setting('emailAdministrator');
  48. }
  49. /**
  50. * Send the notifications
  51. * @param int notification frecuency
  52. */
  53. public function send($frec = NOTIFY_MESSAGE_DAILY) {
  54. $notifications = $this->find('all',array('where'=>array('sent_at IS NULL AND send_freq = ?'=>$frec)));
  55. if (!empty($notifications)) {
  56. foreach($notifications as $item_to_send) {
  57. //Sending email
  58. api_mail_html($item_to_send['dest_mail'], $item_to_send['dest_mail'], Security::filter_terms($item_to_send['title']), Security::filter_terms($item_to_send['content']), $this->admin_name, $this->admin_email);
  59. if ($this->debug) { error_log('Sending message to: '.$item_to_send['dest_mail']); }
  60. //Updating
  61. $item_to_send['sent_at'] = api_get_utc_datetime();
  62. $this->update($item_to_send);
  63. if ($this->debug) { error_log('Updating record : '.print_r($item_to_send,1)); }
  64. }
  65. }
  66. }
  67. /**
  68. * Save message notification
  69. * @param array message type NOTIFICATION_TYPE_MESSAGE, NOTIFICATION_TYPE_INVITATION, NOTIFICATION_TYPE_GROUP
  70. * @param array user list of ids
  71. * @param string title
  72. * @param string content of the message
  73. * @param array result of api_get_user_info() or GroupPortalManager:get_group_data()
  74. */
  75. public function save_notification($type, $user_list, $title, $content, $sender_info = array()) {
  76. $this->type = intval($type);
  77. $content = $this->format_content($content, $sender_info);
  78. $setting_to_check = '';
  79. $avoid_my_self = false;
  80. switch($this->type) {
  81. case NOTIFICATION_TYPE_MESSAGE;
  82. $setting_to_check = 'mail_notify_message';
  83. break;
  84. case NOTIFICATION_TYPE_INVITATION;
  85. $setting_to_check = 'mail_notify_invitation';
  86. break;
  87. case NOTIFICATION_TYPE_GROUP;
  88. $setting_to_check = 'mail_notify_group_message';
  89. $avoid_my_self = true;
  90. break;
  91. }
  92. if (!empty($user_list)) {
  93. foreach($user_list as $user_id) {
  94. $extra_data = UserManager::get_extra_user_data($user_id);
  95. $params = array();
  96. if ($avoid_my_self) {
  97. if ($user_id == api_get_user_id()) {
  98. continue;
  99. }
  100. }
  101. $user_setting = $extra_data[$setting_to_check];
  102. $user_info = api_get_user_info($user_id);
  103. switch ($user_setting) {
  104. //No notifications
  105. case NOTIFY_MESSAGE_NO:
  106. case NOTIFY_INVITATION_NO:
  107. case NOTIFY_GROUP_NO:
  108. break;
  109. //Send notification right now!
  110. case NOTIFY_MESSAGE_AT_ONCE:
  111. case NOTIFY_INVITATION_AT_ONCE:
  112. case NOTIFY_GROUP_AT_ONCE:
  113. if (!empty($user_info['mail'])) {
  114. $name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  115. api_mail_html($name, $user_info['mail'], Security::filter_terms($title), Security::filter_terms($content), $this->admin_name, $this->admin_email);
  116. }
  117. $params['sent_at'] = api_get_utc_datetime();
  118. //Saving the notification to be sent some day
  119. default:
  120. $params['dest_user_id'] = $user_id;
  121. $params['dest_mail'] = $user_info['mail'];
  122. $params['title'] = $title;
  123. $params['content'] = cut($content, $this->max_content_length);
  124. $params['send_freq'] = $user_setting;
  125. $this->save($params);
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. /**
  132. * Formats the content in order to add the welcome message, the notification preference, etc
  133. * @param string the content
  134. * @param array result of api_get_user_info() or GroupPortalManager:get_group_data()
  135. * */
  136. public function format_content($content, $sender_info) {
  137. $new_message_text = $link_to_new_message = '';
  138. switch($this->type) {
  139. case NOTIFICATION_TYPE_MESSAGE:
  140. if (!empty($sender_info)) {
  141. $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  142. //$sender_mail = $sender_info['email'] ;
  143. $new_message_text = sprintf(get_lang('YouHaveANewMessageFromX'), $sender_name);
  144. }
  145. $link_to_new_message = Display::url(get_lang('SeeMessage'), api_get_path(WEB_CODE_PATH).'messages/inbox.php');
  146. break;
  147. case NOTIFICATION_TYPE_INVITATION:
  148. if (!empty($sender_info)) {
  149. $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  150. //$sender_mail = $sender_info['email'] ;
  151. $new_message_text = sprintf(get_lang('YouHaveANewInvitationFromX'), $sender_name);
  152. }
  153. $link_to_new_message = Display::url(get_lang('SeeInvitation'), api_get_path(WEB_CODE_PATH).'social/invitations.php');
  154. break;
  155. case NOTIFICATION_TYPE_GROUP:
  156. $topic_page = intval($_REQUEST['topics_page_nr']);
  157. if (!empty($sender_info)) {
  158. $sender_name = $sender_info['group_info']['name'];
  159. $new_message_text = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $sender_name);
  160. $sender_name = api_get_person_name($sender_info['user_info']['firstname'], $sender_info['user_info']['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  161. $sender_name = Display::url($sender_name , api_get_path(WEB_CODE_PATH).'social/profile.php?'.$sender_info['user_info']['user_id']);
  162. $new_message_text .= '<br />'.get_lang('User').': '.$sender_name;
  163. }
  164. $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;
  165. $link_to_new_message = Display::url(get_lang('SeeMessage'), $group_url);
  166. break;
  167. }
  168. $preference_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  169. // You have received a new message text
  170. if (!empty($new_message_text)) {
  171. $content = $new_message_text.'<br /><hr><br />'.$content;
  172. }
  173. // See message with link text
  174. if (!empty($link_to_new_message)) {
  175. $content = $content.'<br /><br />'.$link_to_new_message;
  176. }
  177. // You have received this message because you are subscribed text
  178. $content = $content.'<br /><hr><i>'.
  179. sprintf(get_lang('YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'), Display::url($preference_url, $preference_url)).'</i>';
  180. return $content;
  181. }
  182. }