notification.lib.php 11 KB

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