|
|
|
@ -41,26 +41,30 @@ 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 $columns = array('id', 'dest_user_id', 'sender_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 = false; |
|
|
|
|
|
|
|
|
|
/* message, invitation, group messages */ |
|
|
|
|
var $type; |
|
|
|
|
var $admin_name; |
|
|
|
|
var $admin_email; |
|
|
|
|
var $sender_name; |
|
|
|
|
var $sender_email; |
|
|
|
|
|
|
|
|
|
var $extra_headers = array(); |
|
|
|
|
|
|
|
|
|
var $send_email_as_user = false; //False, chamilo will sent an email as the user (not recommended) |
|
|
|
|
|
|
|
|
|
public function __construct() { |
|
|
|
|
$this->table = Database::get_main_table(TABLE_NOTIFICATION); |
|
|
|
|
|
|
|
|
|
$this->admin_email = api_get_setting('noreply_email_address'); |
|
|
|
|
$this->admin_name = api_get_setting('siteName'); |
|
|
|
|
$this->sender_email = api_get_setting('noreply_email_address'); |
|
|
|
|
$this->sender_name = api_get_setting('siteName'); |
|
|
|
|
|
|
|
|
|
// If no-reply email doesn't exist use the admin email |
|
|
|
|
if (empty($this->admin_email)) { |
|
|
|
|
$this->admin_email = api_get_setting('emailAdministrator'); |
|
|
|
|
$this->admin_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS); |
|
|
|
|
} |
|
|
|
|
if (empty($this->sender_email)) { |
|
|
|
|
$this->sender_email = api_get_setting('emailAdministrator'); |
|
|
|
|
$this->sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -68,19 +72,21 @@ class Notification extends Model { |
|
|
|
|
* @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))); |
|
|
|
|
|
|
|
|
|
$notifications = $this->find('all', array('where'=> array('sent_at IS NULL AND send_freq = ?' => $frec))); |
|
|
|
|
|
|
|
|
|
if (!empty($notifications)) { |
|
|
|
|
foreach ($notifications as $item_to_send) { |
|
|
|
|
|
|
|
|
|
$this->set_sender_info($item_to_send['sender_id']); |
|
|
|
|
|
|
|
|
|
//Sending email |
|
|
|
|
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 |
|
|
|
|
$this->sender_name, |
|
|
|
|
$this->sender_email, |
|
|
|
|
$this->extra_headers |
|
|
|
|
); |
|
|
|
|
if ($this->debug) { error_log('Sending message to: '.$item_to_send['dest_mail']); } |
|
|
|
|
|
|
|
|
@ -92,6 +98,21 @@ class Notification extends Model { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Sets the sender info in order to add the reply-to |
|
|
|
|
*/ |
|
|
|
|
function set_sender_info($user_id) { |
|
|
|
|
if (!empty($user_id)) { |
|
|
|
|
$sender_user_info = api_get_user_info($user_id); |
|
|
|
|
if ($this->send_email_as_user) { |
|
|
|
|
$this->sender_email = $sender_user_info['email']; |
|
|
|
|
$this->sender_name = $sender_user_info['complete_name']; |
|
|
|
|
} else { |
|
|
|
|
$this->extra_headers = array('reply-to' => $sender_user_info); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Save message notification |
|
|
|
|
* @param array message type NOTIFICATION_TYPE_MESSAGE, NOTIFICATION_TYPE_INVITATION, NOTIFICATION_TYPE_GROUP |
|
|
|
@ -103,11 +124,17 @@ class Notification extends Model { |
|
|
|
|
public function save_notification($type, $user_list, $title, $content, $sender_info = array()) { |
|
|
|
|
$this->type = intval($type); |
|
|
|
|
$content = $this->format_content($content, $sender_info); |
|
|
|
|
|
|
|
|
|
$sender_id = 0; |
|
|
|
|
if (!empty($sender_info) && isset($sender_info['user_id'])) { |
|
|
|
|
$sender_id = $sender_info['user_id']; |
|
|
|
|
$this->set_sender_info($sender_id); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$setting_to_check = ''; |
|
|
|
|
$avoid_my_self = false; |
|
|
|
|
|
|
|
|
|
switch($this->type) { |
|
|
|
|
switch ($this->type) { |
|
|
|
|
case NOTIFICATION_TYPE_MESSAGE; |
|
|
|
|
$setting_to_check = 'mail_notify_message'; |
|
|
|
|
break; |
|
|
|
@ -145,7 +172,7 @@ class Notification extends Model { |
|
|
|
|
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'], Security::filter_terms($title), Security::filter_terms($content), $this->admin_name, $this->admin_email); |
|
|
|
|
api_mail_html($name, $user_info['mail'], Security::filter_terms($title), Security::filter_terms($content), $this->sender_name, $this->sender_email, $this->extra_headers); |
|
|
|
|
} |
|
|
|
|
$params['sent_at'] = api_get_utc_datetime(); |
|
|
|
|
//Saving the notification to be sent some day |
|
|
|
@ -154,7 +181,8 @@ class Notification extends Model { |
|
|
|
|
$params['dest_mail'] = $user_info['mail']; |
|
|
|
|
$params['title'] = $title; |
|
|
|
|
$params['content'] = cut($content, $this->max_content_length); |
|
|
|
|
$params['send_freq'] = $user_setting; |
|
|
|
|
$params['send_freq'] = $user_setting; |
|
|
|
|
$params['sender_id'] = $sender_id; |
|
|
|
|
$this->save($params); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
@ -170,11 +198,10 @@ class Notification extends Model { |
|
|
|
|
public function format_content($content, $sender_info) { |
|
|
|
|
$new_message_text = $link_to_new_message = ''; |
|
|
|
|
|
|
|
|
|
switch($this->type) { |
|
|
|
|
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'] ; |
|
|
|
|
$sender_name = api_get_person_name($sender_info['firstname'], $sender_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS); |
|
|
|
|
$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'); |
|
|
|
@ -214,7 +241,7 @@ class Notification extends Model { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// You have received this message because you are subscribed text |
|
|
|
|
$content = $content.'<br /><hr><i>'. |
|
|
|
|
$content = $content.'<br /><hr><i>'. |
|
|
|
|
sprintf(get_lang('YouHaveReceivedThisNotificationBecauseYouAreSubscribedOrInvolvedInItToChangeYourNotificationPreferencesPleaseClickHereX'), Display::url($preference_url, $preference_url)).'</i>'; |
|
|
|
|
return $content; |
|
|
|
|
} |
|
|
|
|