notification.lib.php 8.9 KB

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