diff --git a/main/inc/lib/message.lib.php b/main/inc/lib/message.lib.php index 03b79bebfe..61bdd39fc8 100755 --- a/main/inc/lib/message.lib.php +++ b/main/inc/lib/message.lib.php @@ -289,7 +289,7 @@ class MessageManager $notification = new Notification(); if (empty($group_id)) { $sender_info = api_get_user_info($user_sender_id); - $notification->save_message_notifications(array($receiver_user_id), $subject, $content, $sender_info); + $notification->save_notification(NOTIFICATION_TYPE_MESSAGE, array($receiver_user_id), $subject, $content, $sender_info); } else { $group_info = GroupPortalManager::get_group_data($group_id); $user_list = GroupPortalManager::get_users_by_group($group_id, false, array(),0, 1000); @@ -297,7 +297,7 @@ class MessageManager foreach($user_list as $user_data) { $new_user_list[]= $user_data['user_id']; } - $notification->save_group_notifications($new_user_list, $subject, $content, $group_info); + $notification->save_notification(NOTIFICATION_TYPE_GROUP, $new_user_list, $subject, $content, $group_info); } return $inbox_last_id; } else { diff --git a/main/inc/lib/notification.lib.php b/main/inc/lib/notification.lib.php index ac0ed0d32e..4eb1fc458b 100644 --- a/main/inc/lib/notification.lib.php +++ b/main/inc/lib/notification.lib.php @@ -10,6 +10,8 @@ require_once 'model.lib.php'; require_once 'usermanager.lib.php'; +//@todo put constants in an array + //default values //mail_notify_message ("At once", "Daily", "No") define('NOTIFY_MESSAGE_AT_ONCE', '1'); @@ -30,168 +32,162 @@ define('NOTIFY_GROUP_DAILY', '8'); define('NOTIFY_GROUP_WEEKLY', '12'); define('NOTIFY_GROUP_NO', '0'); +define('NOTIFICATION_TYPE_MESSAGE', 1); +define('NOTIFICATION_TYPE_INVITATION', 2); +define('NOTIFICATION_TYPE_GROUP', 3); + class Notification extends Model { var $table; var $columns = array('id','dest_user_id','dest_mail','title','content','send_freq','created_at','sent_at'); var $max_content_length = 254; //Max lenght of the notification.content field - var $debug = true; + var $debug = false; + /* message, invitation, group messages */ + var $type; var $admin_name; var $admin_email; public function __construct() { $this->table = Database::get_main_table(TABLE_NOTIFICATION); $this->admin_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS); //api_get_setting('siteName') - $this->admin_email = api_get_setting('emailAdministrator'); - } - + $this->admin_email = api_get_setting('emailAdministrator'); + } + + /** + * Send the notifications + * @param int notification frecuency + */ public function send($frec = NOTIFY_MESSAGE_DAILY) { $notifications = $this->find('all',array('where'=>array('sent_at IS NULL AND send_freq = ?'=>$frec))); if (!empty($notifications)) { foreach($notifications as $item_to_send) { + //Sending email 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); if ($this->debug) { error_log('Sending message to: '.$item_to_send['dest_mail']); } + //Updating $item_to_send['sent_at'] = api_get_utc_datetime(); $this->update($item_to_send); - //if ($this->debug) { error_log('Updating record : '.print_r($item_to_send,1)); } + if ($this->debug) { error_log('Updating record : '.print_r($item_to_send,1)); } } } } /** * Save message notification + * @param array message type NOTIFICATION_TYPE_MESSAGE, NOTIFICATION_TYPE_INVITATION, NOTIFICATION_TYPE_GROUP * @param array user list of ids * @param string title * @param string content of the message * @param array sender info (return of the api_get_user_info() function ) - * */ - public function save_message_notifications($user_list, $title, $content, $sender_info = array()) { - if (!empty($sender_info)) { - $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS); - $sender_mail = $sender_info['email'] ; - $content = sprintf(get_lang('YouHaveANewMessageFromX'), $sender_name).'
'.$content; + public function save_notification($type, $user_list, $title, $content, $sender_info = array()) { + $this->type = intval($type); + $content = $this->format_content($content, $sender_info); + + $setting_to_check = ''; + $avoid_my_self = false; + + switch($this->type) { + case NOTIFICATION_TYPE_MESSAGE; + $setting_to_check = 'mail_notify_message'; + break; + case NOTIFICATION_TYPE_INVITATION; + $setting_to_check = 'mail_notify_invitation'; + break; + case NOTIFICATION_TYPE_GROUP; + $setting_to_check = 'mail_notify_group_message'; + $avoid_my_self = true; + break; } - $content = $content.'
'.Display::url(get_lang('SeeMessage'), api_get_path(WEB_CODE_PATH).'messages/inbox.php'); - if (!empty($user_list)) { + if (!empty($user_list)) { foreach($user_list as $user_id) { $extra_data = UserManager::get_extra_user_data($user_id); - $params = array(); - switch ($extra_data['mail_notify_message']) { + $params = array(); + if ($avoid_my_self) { + if ($user_id == api_get_user_id()) { + continue; + } + } + switch ($setting_to_check) { + //No notifications case NOTIFY_MESSAGE_NO: + case NOTIFY_INVITATION_NO: + case NOTIFY_GROUP_NO: break; - case NOTIFY_MESSAGE_AT_ONCE: - $user_info = api_get_user_info($user_id); - if (!empty($user_info['mail'])) { - $name = api_get_person_name($user_info['firstname'], $user_info['lastname']); + //Send notification right now! + case NOTIFY_MESSAGE_AT_ONCE: + case NOTIFY_INVITATION_AT_ONCE: + case NOTIFY_GROUP_AT_ONCE: + if (!empty($user_info['mail'])) { + $name = api_get_person_name($user_info['firstname'], $user_info['lastname']); api_mail_html($name, $user_info['mail'], $title, $content, $this->admin_name, $this->admin_email); } $params['sent_at'] = api_get_utc_datetime(); - default: + //Saving the notification to be sent some day + default: $user_info = api_get_user_info($user_id); $params['dest_user_id'] = $user_id; $params['dest_mail'] = $user_info['mail']; $params['title'] = $title; $params['content'] = cut($content, $this->max_content_length); - $params['send_freq'] = $extra_data['mail_notify_message']; - $this->save($params); + $params['send_freq'] = $extra_data[$setting_to_check]; + $this->save($params); break; } } } - } - - /** - * Save invitation notification - * @param array user list of ids - * @param string title - * @param string content of the message - * - */ - public function save_invitation_notifications($user_list, $title, $content, $sender_info = array()) { - if (!empty($sender_info)) { - $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS); - $sender_mail = $sender_info['email'] ; - $content = sprintf(get_lang('YouHaveANewInvitationFromX'), $sender_name).'
'.$content; + } + + /* + * Formats the content in order to add the welcome message, the notification preference, etc + * */ + public function format_content($content, $sender_info) { + $new_message_text = $link_to_new_message = ''; + + switch($this->type) { + case NOTIFICATION_TYPE_MESSAGE: + if (!empty($sender_info)) { + $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS); + //$sender_mail = $sender_info['email'] ; + $new_message_text = sprintf(get_lang('YouHaveANewMessageFromX'), $sender_name); + } + $link_to_new_message = Display::url(get_lang('SeeMessage'), api_get_path(WEB_CODE_PATH).'messages/inbox.php'); + break; + case NOTIFICATION_TYPE_INVITATION: + if (!empty($sender_info)) { + $sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS); + //$sender_mail = $sender_info['email'] ; + $new_message_text = sprintf(get_lang('YouHaveANewInvitationFromX'), $sender_name); + } + $link_to_new_message = Display::url(get_lang('SeeInvitation'), api_get_path(WEB_CODE_PATH).'social/invitations.php'); + break; + case NOTIFICATION_TYPE_GROUP: + if (!empty($sender_info)) { + $sender_name = $sender_info['name']; + $new_message_text = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $sender_name); + } + $link_to_new_message = Display::url(get_lang('SeeMessage'), api_get_path(WEB_CODE_PATH).'social/groups.php?id='.$sender_info['id']); + break; } + $preference_url = api_get_path(WEB_CODE_PATH).'auth/profile.php'; - $content = $content.'
'.Display::url(get_lang('SeeInvitation'), api_get_path(WEB_CODE_PATH).'social/invitations.php'); + // You have received a new message text + if (!empty($new_message_text)) { + $content = $new_message_text.'

'.$content; + } - if (!empty($user_list)) { - foreach($user_list as $user_id) { - $extra_data = UserManager::get_extra_user_data($user_id); - $params = array(); - switch ($extra_data['mail_notify_invitation']) { - case NOTIFY_INVITATION_NO: - break; - case NOTIFY_INVITATION_AT_ONCE: - $user_info = api_get_user_info($user_id); - if (!empty($user_info['mail'])) { - $name = api_get_person_name($user_info['firstname'], $user_info['lastname']); - api_mail_html($name, $user_info['mail'], $title, $content, $this->admin_name, $this->admin_email); - } - $params['sent_at'] = api_get_utc_datetime(); - default: - $user_info = api_get_user_info($user_id); - $params['dest_user_id'] = $user_id; - $params['dest_mail'] = $user_info['mail']; - $params['title'] = $title; - $params['content'] = cut($content, $this->max_content_length); - $params['send_freq'] = $extra_data['mail_notify_invitation']; - $this->save($params); - break; - } - } + // See message with link text + if (!empty($link_to_new_message)) { + $content = $content.'
'.$link_to_new_message; } + + // You have received this message because you are subscribed text + $content = $content.'

'. + sprintf(get_lang('YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'), Display::url($preference_url, $preference_url)).''; + return $content; } - - - /** - * Save group notifications - * @param array user list of ids - * @param string title - * @param string content of the message - * - */ - public function save_group_notifications($user_list, $title, $content, $sender_info = array()) { - if (!empty($sender_info)) { - $sender_name = $sender_info['name']; - $content = sprintf(get_lang('YouHaveReceivedANewMessageInTheGroupX'), $sender_name).'
'.$content; - $content = $content.'
'.Display::url(get_lang('SeeMessage'), api_get_path(WEB_CODE_PATH).'social/groups.php?id='.$sender_info['id']); - } - if (!empty($user_list)) { - foreach($user_list as $user_id) { - //Avoiding sending a message to myself - if ($user_id == api_get_user_id()) { - continue; - } - $extra_data = UserManager::get_extra_user_data($user_id); - $params = array(); - switch ($extra_data['mail_notify_group_message']) { - case NOTIFY_GROUP_NO: - break; - case NOTIFY_GROUP_AT_ONCE: - $user_info = api_get_user_info($user_id); - if (!empty($user_info['mail'])) { - $name = api_get_person_name($user_info['firstname'], $user_info['lastname']); - api_mail_html($name, $user_info['mail'], $title, $content, $this->admin_name, $this->admin_email); - } - $params['sent_at'] = api_get_utc_datetime(); - default: - $user_info = api_get_user_info($user_id); - $params['dest_user_id'] = $user_id; - $params['dest_mail'] = $user_info['mail']; - $params['title'] = $subject; - $params['content'] = cut($content,$this->max_content_length); - $params['send_freq'] = $extra_data['mail_notify_group_message']; - $this->save($params); - break; - } - } - } - } } \ No newline at end of file diff --git a/main/inc/lib/social.lib.php b/main/inc/lib/social.lib.php index 6c1673904c..3638a7a2f8 100755 --- a/main/inc/lib/social.lib.php +++ b/main/inc/lib/social.lib.php @@ -191,7 +191,7 @@ class SocialManager extends UserManager { require_once api_get_path(LIBRARY_PATH).'notification.lib.php'; $sender_info = api_get_user_info($user_id); $notification = new Notification(); - $notification->save_invitation_notifications(array($friend_id), $message_title, $message_content, $sender_info); + $notification->save_notification(NOTIFICATION_TYPE_INVITATION, array($friend_id), $message_title, $message_content, $sender_info); return true; } else { diff --git a/main/lang/english/trad4all.inc.php b/main/lang/english/trad4all.inc.php index 9e71c6c9af..fbfa87ae5c 100644 --- a/main/lang/english/trad4all.inc.php +++ b/main/lang/english/trad4all.inc.php @@ -1109,4 +1109,6 @@ $SelectFilter = "Select filter"; $ThereIsNoClassScheduledTodayTryPickingAnotherDay = "There is no class scheduled today, try picking another day or add your attendance entry yourself using the action icons."; $AddToCalendar = "Add to calendar"; $RandomPick = "Random pick"; +$YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX = "You have received this notification because you have either subscribed to it, or are involved in it. +To change your notification preferences, please click here: %s"; ?> \ No newline at end of file diff --git a/main/messages/send_message_to_userfriend.inc.php b/main/messages/send_message_to_userfriend.inc.php index 8c4c278f00..432e823ee6 100755 --- a/main/messages/send_message_to_userfriend.inc.php +++ b/main/messages/send_message_to_userfriend.inc.php @@ -4,7 +4,7 @@ * @package chamilo.messages */ $language_file = array('registration','messages','userInfo','admin'); -$cidReset=true; +$cidReset = true; require_once '../inc/global.inc.php'; require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php'; require_once api_get_path(LIBRARY_PATH).'message.lib.php'; @@ -18,10 +18,10 @@ if (api_get_setting('allow_message_tool') != 'true' && api_get_setting('allow_so } if (isset($_REQUEST['user_friend']) ) { $info_user_friend=array(); - $userfriend_id=Security::remove_XSS($_REQUEST['user_friend']); + $userfriend_id=intval($_REQUEST['user_friend']); // panel=1 send message // panel=2 send invitation - $panel=Security::remove_XSS($_REQUEST['view_panel']); + $panel=intval($_REQUEST['view_panel']); $info_user_friend=api_get_user_info($userfriend_id); }