* @param void
* @return integer
*/
public static function get_number_of_messages_sent()
{
$table_message = Database::get_main_table(TABLE_MESSAGE);
$sql_query = "SELECT COUNT(*) as number_messages FROM $table_message WHERE msg_status=".MESSAGE_STATUS_OUTBOX." AND user_sender_id=".api_get_user_id();
$sql_result = Database::query($sql_query);
$result = Database::fetch_array($sql_result);
return $result['number_messages'];
}
/**
* display message box in the inbox
* @param int the message id
* @param string inbox or outbox strings are available
* @todo replace numbers with letters in the $row array pff...
* @return string html with the message content
*/
public static function show_message_box($message_id, $source = 'inbox')
{
$table_message = Database::get_main_table(TABLE_MESSAGE);
$message_id = intval($message_id);
if ($source == 'outbox') {
if (isset($message_id) && is_numeric($message_id)) {
$query = "SELECT * FROM $table_message WHERE user_sender_id=".api_get_user_id()." AND id=".$message_id." AND msg_status=4;";
$result = Database::query($query);
$path = 'outbox.php';
}
} else {
if (is_numeric($message_id) && !empty($message_id)) {
$query = "UPDATE $table_message SET msg_status = '".MESSAGE_STATUS_NEW."' WHERE user_receiver_id=".api_get_user_id()." AND id='".$message_id."';";
$result = Database::query($query);
$query = "SELECT * FROM $table_message WHERE msg_status<>4 AND user_receiver_id=".api_get_user_id()." AND id='".$message_id."';";
$result = Database::query($query);
}
$path = 'inbox.php';
}
$row = Database::fetch_array($result, 'ASSOC');
$user_sender_id = $row['user_sender_id'];
// get file attachments by message id
$files_attachments = self::get_links_message_attachment_files($message_id, $source);
$user_con = self::users_connected_by_id();
$band = 0;
for ($i = 0; $i < count($user_con); $i++)
if ($user_sender_id == $user_con[$i])
$band = 1;
$title = Security::remove_XSS($row['title'], STUDENT, true);
$content = Security::remove_XSS($row['content'], STUDENT, true);
$from_user = UserManager::get_user_info_by_id($user_sender_id);
$name = api_get_person_name($from_user['firstname'], $from_user['lastname']);
$user_image = UserManager::get_picture_user($row['user_sender_id'], $from_user['picture_uri'], 80);
$user_image = Display::img($user_image['file'], $name, array('title' => $name));
$message_content = Display::page_subheader(str_replace("\\", "", $title));
if (api_get_setting('allow_social_tool') == 'true') {
$message_content .= $user_image.' ';
}
$message_content .='';
if (api_get_setting('allow_social_tool') == 'true') {
if ($source == 'outbox') {
$message_content .= get_lang('From').': '.$name.' '.api_strtolower(get_lang('To')).' '.GetFullUserName($row[2]).'';
} else {
$message_content .= get_lang('From').' '.$name.' '.api_strtolower(get_lang('To')).' '.get_lang('Me').'';
}
} else {
if ($source == 'outbox') {
$message_content .= get_lang('From').': '.$name.' '.api_strtolower(get_lang('To')).' '.GetFullUserName($row['user_receiver_id']).'';
} else {
$message_content .= get_lang('From').': '.$name.' '.api_strtolower(get_lang('To')).' '.get_lang('Me').'';
}
}
$message_content .=' '.get_lang('Date').': '.api_get_local_time($row['send_date']).'
'.str_replace("\\", "", $content).' |
'.(!empty($files_attachments) ? implode('
', $files_attachments) : '').'
|
';
return $message_content;
}
/**
* display message box sent showing it into outbox
* @return void
*/
public static function show_message_box_sent()
{
global $charset;
$table_message = Database::get_main_table(TABLE_MESSAGE);
$tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
$message_id = '';
if (is_numeric($_GET['id_send'])) {
$query = "SELECT * FROM $table_message WHERE user_sender_id=".api_get_user_id()." AND id=".intval(Database::escape_string($_GET['id_send']))." AND msg_status=4;";
$result = Database::query($query);
$message_id = intval($_GET['id_send']);
}
$path = 'outbox.php';
// get file attachments by message id
$files_attachments = self::get_links_message_attachment_files($message_id, 'outbox');
$row = Database::fetch_array($result);
$user_con = self::users_connected_by_id();
$band = 0;
$reply = '';
for ($i = 0; $i < count($user_con); $i++)
if ($row[1] == $user_con[$i])
$band = 1;
echo '
';
echo '
|
| '.str_replace("\\", "", api_xml_http_response_encode($row[5])).'
'.api_xml_http_response_encode(get_lang('From').' '.GetFullUserName($row[1]).' '.api_strtolower(get_lang('To')).' '.GetFullUserName($row[2])).' |
'.api_xml_http_response_encode(get_lang('Date').' '.$row[4]).' |
'.str_replace("\\", "", api_xml_http_response_encode($row[6])).' |
'.(!empty($files_attachments) ? implode(' ', $files_attachments) : '').'
|
|
';
}
/**
* get user id by user email
* @param string $user_email
* @return int user id
*/
public static function get_user_id_by_email($user_email)
{
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
$sql = 'SELECT user_id FROM '.$tbl_user.' WHERE email="'.Database::escape_string($user_email).'";';
$rs = Database::query($sql);
$row = Database::fetch_array($rs, 'ASSOC');
if (isset($row['user_id'])) {
return $row['user_id'];
} else {
return null;
}
}
/**
* Displays messages of a group with nested view
* @param int group id
*/
public static function display_messages_for_group($group_id)
{
global $my_group_role;
$rows = self::get_messages_by_group($group_id);
$topics_per_page = 10;
$html_messages = '';
$query_vars = array('id' => $group_id, 'topics_page_nr' => 0);
if (is_array($rows) && count($rows) > 0) {
// prepare array for topics with its items
$topics = array();
$x = 0;
foreach ($rows as $index => $value) {
if (empty($value['parent_id'])) {
$topics[$value['id']] = $value;
}
}
$new_topics = array();
foreach ($topics as $id => $value) {
$rows = null;
$rows = self::get_messages_by_group_by_message($group_id, $value['id']);
if (!empty($rows)) {
$count = count(self::calculate_children($rows, $value['id']));
} else {
$count = 0;
}
$value['count'] = $count;
$new_topics[$id] = $value;
}
//$new_topics = sort_column($new_topics,'count');
$param_names = array_keys($_GET);
$array_html = array();
foreach ($new_topics as $index => $topic) {
$html = '';
// topics
//$indent = 0;
$user_sender_info = UserManager::get_user_info_by_id($topic['user_sender_id']);
//$files_attachments = self::get_links_message_attachment_files($topic['id']);
$name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
$html .= '';
$items = $topic['count'];
$reply_label = ($items == 1) ? get_lang('GroupReply') : get_lang('GroupReplies');
$html .= '
';
$html .= Display::div(Display::tag('span', $items).$reply_label, array('class' => 'group_discussions_replies'));
$html .= '
';
$topic['title'] = trim($topic['title']);
if (empty($topic['title'])) {
$topic['title'] = get_lang('Untitled');
}
$html .= '
';
$html .= Display::tag('h4', Display::url(Security::remove_XSS($topic['title'], STUDENT, true), 'group_topics.php?id='.$group_id.'&topic_id='.$topic['id']));
if ($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) {
$actions = '
'.Display::url(get_lang('Delete'), api_get_path(WEB_CODE_PATH).'social/group_topics.php?action=delete&id='.$group_id.'&topic_id='.$topic['id'], array('class' => 'btn'));
}
$date = '';
if ($topic['send_date'] != $topic['update_date']) {
if (!empty($topic['update_date']) && $topic['update_date'] != '0000-00-00 00:00:00') {
$date .= '
'.get_lang('LastUpdate').' '.date_to_str_ago($topic['update_date']).'
';
}
} else {
$date .= '
'.get_lang('Created').' '.date_to_str_ago($topic['send_date']).'
';
}
$html .= $date.$actions;
$html .= '
';
$image_path = UserManager::get_user_picture_path_by_id($topic['user_sender_id'], 'web', false, true);
$image_repository = $image_path['dir'];
$existing_image = $image_path['file'];
$user_info = '
'.$name.' ';
$user_info .= '';
$user_info .= ' | ';
$html .= '
';
$html .= $user_info;
$html .= '
';
//group_topics.php?action=delete&id=3&topic_id=6
//$date.
//$html .= Display::div($title.Security::remove_XSS(cut($topic['content'], 150), STUDENT, true).$user_info, array('class'=>'group_discussions_info')).' '.$actions.'';
$html .= '
'; //rounded_div
$array_html[] = array($html);
}
// grids for items and topics with paginations
$html_messages .= Display::return_sortable_grid('topics', array(), $array_html, array('hide_navigation' => false, 'per_page' => $topics_per_page), $query_vars, false, array(true, true, true, false), false);
}
return $html_messages;
}
/**
* Displays messages of a group with nested view
* @param int group id
*/
public static function display_message_for_group($group_id, $topic_id, $is_member, $message_id)
{
global $my_group_role;
$main_message = self::get_message_by_id($topic_id);
if (empty($main_message)) {
return false;
}
$rows = self::get_messages_by_group_by_message($group_id, $topic_id);
$rows = self::calculate_children($rows, $topic_id);
$current_user_id = api_get_user_id();
$items_per_page = 50;
$query_vars = array('id' => $group_id, 'topic_id' => $topic_id, 'topics_page_nr' => 0);
// Main message
$user_link = '';
$links = '';
$main_content = '';
$items_page_nr = null;
$html = '';
$delete_button = '';
if (api_is_platform_admin()) {
$delete_button = Display::url(Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL), 'group_topics.php?action=delete&id='.$group_id.'&topic_id='.$topic_id);
}
$html .= Display::page_subheader(Security::remove_XSS($main_message['title'].$delete_button, STUDENT, true));
$user_sender_info = UserManager::get_user_info_by_id($main_message['user_sender_id']);
$files_attachments = self::get_links_message_attachment_files($main_message['id']);
$name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
$topic_page_nr = isset($_GET['topics_page_nr']) ? intval($_GET['topics_page_nr']) : null;
$links.= '';
if (($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) || $main_message['user_sender_id'] == $current_user_id) {
$links.= '';
}
$links.= ' ';
$links.= '
';
$image_path = UserManager::get_user_picture_path_by_id($main_message['user_sender_id'], 'web', false, true);
$image_repository = $image_path['dir'];
$existing_image = $image_path['file'];
$main_content.= '';
$user_link = ''.$name.' ';
$date = '';
if ($main_message['send_date'] != $main_message['update_date']) {
if (!empty($main_message['update_date']) && $main_message['update_date'] != '0000-00-00 00:00:00') {
$date = ' '.get_lang('LastUpdate').' '.date_to_str_ago($main_message['update_date']).'
';
}
} else {
$date = ' '.get_lang('Created').' '.date_to_str_ago($main_message['send_date']).'
';
}
$attachment = ''.(!empty($files_attachments) ? implode('
', $files_attachments) : '').'
';
$main_content.= ''.$links.$user_link.' '.$date.$main_message['content'].$attachment.'
';
$main_content = Security::remove_XSS($main_content, STUDENT, true);
$html .= Display::div(Display::div(Display::div($main_content, array('class' => 'group_social_sub_item', 'style' => 'background-color:#fff;')), array('class' => 'group_social_item')), array('class' => 'group_social_grid'));
$topic_id = $main_message['id'];
if (is_array($rows) && count($rows) > 0) {
$topics = $rows;
$array_html_items = array();
foreach ($topics as $index => $topic) {
if (empty($topic['id'])) {
continue;
}
$items_page_nr = isset($_GET['items_'.$topic['id'].'_page_nr']) ? intval($_GET['items_'.$topic['id'].'_page_nr']) : null;
$user_link = '';
$links = '';
$html_items = '';
$user_sender_info = UserManager::get_user_info_by_id($topic['user_sender_id']);
$files_attachments = self::get_links_message_attachment_files($topic['id']);
$name = api_get_person_name($user_sender_info['firstname'], $user_sender_info['lastname']);
$links.= '';
if (($my_group_role == GROUP_USER_PERMISSION_ADMIN || $my_group_role == GROUP_USER_PERMISSION_MODERATOR) || $topic['user_sender_id'] == $current_user_id) {
$links.= '';
}
$links.= ' ';
$links.= '
';
$image_path = UserManager::get_user_picture_path_by_id($topic['user_sender_id'], 'web', false, true);
$image_repository = $image_path['dir'];
$existing_image = $image_path['file'];
$html_items.= '';
$user_link = ''.$name.' ';
$date = '';
if ($topic['send_date'] != $topic['update_date']) {
if (!empty($topic['update_date']) && $topic['update_date'] != '0000-00-00 00:00:00') {
$date = ' '.get_lang('LastUpdate').' '.date_to_str_ago($topic['update_date']).'
';
}
} else {
$date = ' '.get_lang('Created').' '.date_to_str_ago($topic['send_date']).'
';
}
$attachment = ''.(!empty($files_attachments) ? implode('
', $files_attachments) : '').'
';
$html_items.= ''.$links.$user_link.' '.$date.Security::remove_XSS($topic['content'], STUDENT, true).$attachment.'
';
$base_padding = 20;
if ($topic['indent_cnt'] == 0) {
$indent = $base_padding;
} else {
$indent = intval($topic['indent_cnt']) * $base_padding + $base_padding;
}
$class = 'group_social_sub_item';
if (isset($message_id) && $message_id == $topic['id']) {
$class .= ' group_social_sub_item_highlight';
}
$html_items = Display::div($html_items, array('class' => $class, 'id' => 'msg_'.$topic['id']));
$html_items = Display::div($html_items, array('class' => '', 'style' => 'margin-left:'.$indent.'px'));
$array_html_items[] = array($html_items);
}
// grids for items with paginations
$options = array('hide_navigation' => false, 'per_page' => $items_per_page);
$visibility = array(true, true, true, false);
$style_class = array('item' => array('class' => 'group_social_item'), 'main' => array('class' => 'group_social_grid'));
if (!empty($array_html_items)) {
$html .= Display::return_sortable_grid('items_'.$topic['id'], array(), $array_html_items, $options, $query_vars, null, $visibility, false, $style_class);
}
}
return $html;
}
/**
* Add children to messages by id is used for nested view messages
* @param array rows of messages
* @return array new list adding the item children
*/
public static function calculate_children($rows, $first_seed)
{
$rows_with_children = array();
foreach ($rows as $row) {
$rows_with_children[$row["id"]] = $row;
$rows_with_children[$row["parent_id"]]["children"][] = $row["id"];
}
$rows = $rows_with_children;
$sorted_rows = array(0 => array());
self::message_recursive_sort($rows, $sorted_rows, $first_seed);
unset($sorted_rows[0]);
return $sorted_rows;
}
/**
* Sort recursively the messages, is used for for nested view messages
* @param array original rows of messages
* @param array list recursive of messages
* @param int seed for calculate the indent
* @param int indent for nested view
* @return void
*/
public static function message_recursive_sort($rows, &$messages, $seed = 0, $indent = 0)
{
if ($seed > 0 && isset($rows[$seed]["id"])) {
$messages[$rows[$seed]["id"]] = $rows[$seed];
$messages[$rows[$seed]["id"]]["indent_cnt"] = $indent;
$indent++;
}
if (isset($rows[$seed]["children"])) {
foreach ($rows[$seed]["children"] as $child) {
self::message_recursive_sort($rows, $messages, $child, $indent);
}
}
}
/**
* Sort date by desc from a multi-dimensional array
* @param array1 first array to compare
* @param array2 second array to compare
* @return bool
*/
public function order_desc_date($array1, $array2)
{
return strcmp($array2['send_date'], $array1['send_date']);
}
/**
* Get array of links (download) for message attachment files
* @param int message id
* @param string type message list (inbox/outbox)
* @return array
*/
public static function get_links_message_attachment_files($message_id, $type = '')
{
$tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
$message_id = intval($message_id);
// get file attachments by message id
$links_attach_file = array();
if (!empty($message_id)) {
$sql = "SELECT * FROM $tbl_message_attach WHERE message_id = '$message_id'";
$rs_file = Database::query($sql);
if (Database::num_rows($rs_file) > 0) {
$attach_icon = Display::return_icon('attachment.gif', '');
$archiveURL = api_get_path(WEB_CODE_PATH).'messages/download.php?type='.$type.'&file=';
while ($row_file = Database::fetch_array($rs_file)) {
$archiveFile = $row_file['path'];
$filename = $row_file['filename'];
$filesize = format_file_size($row_file['size']);
$filecomment = $row_file['comment'];
$links_attach_file[] = $attach_icon.' '.$filename.' ('.$filesize.')'.(!empty($filecomment) ? ' - '.$filecomment.'' : '');
}
}
}
return $links_attach_file;
}
/**
* Get message list by id
* @param int message id
* @return array
*/
public static function get_message_by_id($message_id)
{
$tbl_message = Database::get_main_table(TABLE_MESSAGE);
$message_id = intval($message_id);
$sql = "SELECT * FROM $tbl_message WHERE id = '$message_id' AND msg_status <> '".MESSAGE_STATUS_DELETED."' ";
$res = Database::query($sql);
$item = array();
if (Database::num_rows($res) > 0) {
$item = Database::fetch_array($res, 'ASSOC');
}
return $item;
}
static function generate_message_form($id, $params = array())
{
$form = new FormValidator('send_message', null, 'post', null, array('id' => $id.'_form', 'class' => 'form-vertical'));
$form->addElement('text', 'subject', get_lang('Subject'), array('id' => 'subject_id', 'class' => 'span5'));
$form->addElement('textarea', 'content', get_lang('Message'), array('id' => 'content_id', 'rows' => '5', 'class' => 'span5'));
$div = Display::div($form->return_form(), array('id' => $id.'_div', 'style' => 'display:none'));
return $div;
}
static function generate_invitation_form($id, $params = array())
{
$form = new FormValidator('send_invitation', null, 'post', null, array('id' => $id.'_form', 'class' => 'form-vertical'));
//$form->addElement('text', 'subject', get_lang('Subject'), array('id' => 'subject_id'));
$form->addElement('textarea', 'content', get_lang('AddPersonalMessage'), array('id' => 'content_invitation_id', 'rows' => '5', 'class' => 'span5'));
$div = Display::div($form->return_form(), array('id' => $id.'_div', 'style' => 'display:none'));
return $div;
}
//@todo this functions should be in the message class
static function inbox_display()
{
$success = get_lang('SelectedMessagesDeleted');
$html = '';
if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'delete' :
$number_of_selected_messages = count($_POST['id']);
foreach ($_POST['id'] as $index => $message_id) {
MessageManager::delete_message_by_user_receiver(api_get_user_id(), $message_id);
}
$html .= Display::return_message(api_xml_http_response_encode($success), 'normal', false);
break;
case 'deleteone' :
MessageManager::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
$html .= Display::return_message(api_xml_http_response_encode($success), 'confirmation', false);
break;
}
}
// display sortable table with messages of the current user
$table = new SortableTable('message_inbox', array('MessageManager', 'get_number_of_messages'), array('MessageManager', 'get_message_data'), 3, 20, 'DESC');
$table->set_header(0, '', false, array('style' => 'width:15px;'));
$table->set_header(1, get_lang('Messages'), false);
$table->set_header(2, get_lang('Date'), true, array('style' => 'width:180px;'));
$table->set_header(3, get_lang('Modify'), false, array('style' => 'width:70px;'));
if (isset($_REQUEST['f']) && $_REQUEST['f'] == 'social') {
$parameters['f'] = 'social';
$table->set_additional_parameters($parameters);
}
$table->set_form_actions(array('delete' => get_lang('DeleteSelectedMessages')));
$html .= $table->return_table();
return $html;
}
static function outbox_display()
{
$social_link = false;
if (isset($_REQUEST['f']) && $_REQUEST['f'] == 'social') {
$social_link = 'f=social';
}
$success = get_lang('SelectedMessagesDeleted').' 
'.get_lang('BackToOutbox').'';
if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'delete' :
$number_of_selected_messages = count($_POST['id']);
if ($number_of_selected_messages != 0) {
foreach ($_POST['id'] as $index => $message_id) {
MessageManager::delete_message_by_user_receiver(api_get_user_id(), $message_id);
}
}
$html .= Display::return_message(api_xml_http_response_encode($success), 'normal', false);
break;
case 'deleteone' :
MessageManager::delete_message_by_user_receiver(api_get_user_id(), $_GET['id']);
$html .=Display::return_message(api_xml_http_response_encode($success), 'normal', false);
$html .= '
';
break;
}
}
// display sortable table with messages of the current user
$table = new SortableTable('message_outbox', array('MessageManager', 'get_number_of_messages_sent'), array('MessageManager', 'get_message_data_sent'), 3, 20, 'DESC');
$parameters['f'] = Security::remove_XSS($_GET['f']);
$table->set_additional_parameters($parameters);
$table->set_header(0, '', false, array('style' => 'width:15px;'));
$table->set_header(1, get_lang('Messages'), false);
$table->set_header(2, get_lang('Date'), true, array('style' => 'width:160px;'));
$table->set_header(3, get_lang('Modify'), false, array('style' => 'width:70px;'));
$table->set_form_actions(array('delete' => get_lang('DeleteSelectedMessages')));
$html .= $table->return_table();
return $html;
}
}
function get_number_of_messages_mask()
{
return MessageManager::get_number_of_messages();
}
function get_message_data_mask($from, $number_of_items, $column, $direction)
{
$column = '3';
$direction = 'DESC';
//non set by SortableTable ?
$number_of_items = get_number_of_messages_mask();
return MessageManager::get_message_data($from, $number_of_items, $column, $direction);
}
function get_number_of_messages_send_mask()
{
return MessageManager::get_number_of_messages_sent();
}
function get_message_data_send_mask($from, $number_of_items, $column, $direction)
{
$column = '3';
$direction = 'desc';
//non set by SortableTable ?
$number_of_items = get_number_of_messages_send_mask();
return MessageManager::get_message_data_sent($from, $number_of_items, $column, $direction);
}