';
if (api_get_setting('allow_social_tool') == 'true') {
$message_content .= '';
if ($source == 'outbox') {
$message_content .= '
';
$message_content .= '
';
$message_content .= '- '.$userImage.'
';
$message_content .= '- '.$name.' ';
if (!empty($receiverUserInfo)) {
$message_content .= api_strtolower(
get_lang('To')
).' '.$receiverUserInfo['complete_name_with_username'].'
';
} else {
$message_content .= api_strtolower(get_lang('To')).' -';
}
$message_content .= '- '.Display::dateToStringAgoAndLongDate($row['send_date']).'
';
$message_content .= '
';
$message_content .= '
';
} else {
$message_content .= '
';
$message_content .= '
';
if (!empty($user_sender_id)) {
$message_content .= '- '.$userImage.'
';
$message_content .= '- '.$name.'';
} else {
$message_content .= '
- '.$name;
}
$message_content .= ' '.api_strtolower(get_lang('To')).' '.get_lang('Me');
$message_content .= '
- '.Display::dateToStringAgoAndLongDate($row['send_date']).'
';
$message_content .= '
';
$message_content .= '
';
}
$message_content .= '
';
} else {
if ($source == 'outbox') {
$message_content .= get_lang('From').': '.$name.' '.api_strtolower(get_lang('To')).' '.
$receiverUserInfo['complete_name_with_username'].'';
} else {
$message_content .= get_lang('From').': '.$name.' '.api_strtolower(get_lang('To')).' '.
get_lang('Me').'';
}
}
$message_content .= '
'.str_replace("\\", "", $content).' |
'.(!empty($files_attachments) ? implode('
', $files_attachments) : '').'
|
';
return $message_content;
}
/**
* 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
*
* @return string
*/
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 = ['id' => $group_id, 'topics_page_nr' => 0];
if (is_array($rows) && count($rows) > 0) {
// prepare array for topics with its items
$topics = [];
$x = 0;
foreach ($rows as $index => $value) {
if (empty($value['parent_id'])) {
$topics[$value['id']] = $value;
}
}
$new_topics = [];
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;
}
$array_html = [];
foreach ($new_topics as $index => $topic) {
$html = '';
// topics
$user_sender_info = api_get_user_info($topic['user_sender_id']);
$name = $user_sender_info['complete_name'];
$html .= '';
$html .= '
';
$items = $topic['count'];
$reply_label = ($items == 1) ? get_lang('GroupReply') : get_lang('GroupReplies');
$label = '
'.$items.' '.$reply_label;
$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),
api_get_path(WEB_CODE_PATH).'social/group_topics.php?id='.$group_id.'&topic_id='.$topic['id']
),
['class' => 'title']
);
$actions = '';
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'],
['class' => 'btn btn-default']
);
}
$date = '';
if ($topic['send_date'] != $topic['update_date']) {
if (!empty($topic['update_date'])) {
$date .= '
'.get_lang(
'LastUpdate'
).' '.Display::dateToStringAgoAndLongDate($topic['update_date']);
}
} else {
$date .= '
'.get_lang(
'Created'
).' '.Display::dateToStringAgoAndLongDate($topic['send_date']);
}
$html .= '
'.$label.' - '.$date.$actions.'
';
$html .= '
';
$image = $user_sender_info['avatar'];
$user_info = '
';
$user_info .= '
';
$html .= '
';
$html .= $user_info;
$html .= '
';
$html .= '
';
$html .= '
';
$array_html[] = [$html];
}
// grids for items and topics with paginations
$html_messages .= Display::return_sortable_grid(
'topics',
[],
$array_html,
[
'hide_navigation' => false,
'per_page' => $topics_per_page,
],
$query_vars,
false,
[true, true, true, false],
false
);
}
return $html_messages;
}
/**
* Displays messages of a group with nested view.
*
* @param $group_id
* @param $topic_id
* @param $is_member
* @param $messageId
*
* @return string
*/
public static function display_message_for_group($group_id, $topic_id, $is_member, $messageId)
{
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 = ['id' => $group_id, 'topic_id' => $topic_id, 'topics_page_nr' => 0];
// Main message
$links = '';
$main_content = '';
$html = '';
$items_page_nr = null;
$user_sender_info = api_get_user_info($main_message['user_sender_id']);
$files_attachments = self::get_links_message_attachment_files($main_message['id']);
$name = $user_sender_info['complete_name'];
$topic_page_nr = isset($_GET['topics_page_nr']) ? intval($_GET['topics_page_nr']) : null;
$links .= '';
$links .= '
';
if (($my_group_role == GROUP_USER_PERMISSION_ADMIN ||
$my_group_role == GROUP_USER_PERMISSION_MODERATOR) ||
$main_message['user_sender_id'] == $current_user_id
) {
$urlEdit = api_get_path(WEB_CODE_PATH);
$urlEdit .= 'social/message_for_group_form.inc.php?';
$urlEdit .= http_build_query(
[
'user_friend' => $current_user_id,
'group_id' => $group_id,
'message_id' => $main_message['id'],
'action' => 'edit_message_group',
'anchor_topic' => 'topic_'.$main_message['id'],
'topics_page_nr' => $topic_page_nr,
'items_page_nr' => $items_page_nr,
'topic_id' => $main_message['id'],
]
);
$links .= Display::url(
Display::returnFontAwesomeIcon('pencil'),
$urlEdit,
[
'class' => 'btn btn-default ajax',
'title' => get_lang('Edit'),
'data-title' => get_lang('Edit'),
'data-size' => 'lg',
]
);
}
$urlReply = api_get_path(WEB_CODE_PATH);
$urlReply .= 'social/message_for_group_form.inc.php?';
$urlReply .= http_build_query(
[
'user_friend' => api_get_user_id(),
'group_id' => $group_id,
'message_id' => $main_message['id'],
'action' => 'reply_message_group',
'anchor_topic' => 'topic_'.$main_message['id'],
'topics_page_nr' => $topic_page_nr,
'topic_id' => $main_message['id'],
]
);
$links .= Display::url(
Display::returnFontAwesomeIcon('commenting'),
$urlReply,
[
'class' => 'btn btn-default ajax',
'title' => get_lang('Reply'),
'data-title' => get_lang('Reply'),
'data-size' => 'lg',
]
);
if (api_is_platform_admin()) {
$links .= Display::url(
Display::returnFontAwesomeIcon('trash'),
'group_topics.php?action=delete&id='.$group_id.'&topic_id='.$topic_id,
[
'class' => 'btn btn-default',
]
);
}
$links .= '
';
$links .= '
';
$title = '';
$main_content .= '
';
$main_content .= '
';
$main_content .= '

';
$main_content .= '
';
$main_content .= '
';
$date = '';
if ($main_message['send_date'] != $main_message['update_date']) {
if (!empty($main_message['update_date'])) {
$date = '
'.
Display::returnFontAwesomeIcon('calendar').' '.get_lang('LastUpdate').' '.
Display::dateToStringAgoAndLongDate($main_message['update_date']).
'
';
}
} else {
$date = '
'.
Display::returnFontAwesomeIcon('calendar').' '.get_lang('Created').' '.
Display::dateToStringAgoAndLongDate($main_message['send_date']).
'
';
}
$attachment = '
'.(!empty($files_attachments) ? implode(
'
',
$files_attachments
) : '').'
';
$main_content .= '
';
$user_link = '
'.$name.'';
$main_content .= '
';
$main_content .= '
'.$user_link.'
';
$main_content .= $date;
$main_content .= '
'.$main_message['content'].$attachment.'
';
$main_content .= '
';
$main_content .= '
';
$html .= Display::div(
Display::div(
$title.$main_content,
['class' => 'message-topic']
),
['class' => 'sm-groups-message']
);
$topic_id = $main_message['id'];
if (is_array($rows) && count($rows) > 0) {
$topics = $rows;
$array_html_items = [];
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;
$links = '';
$links .= '';
$html_items .= '
';
$html_items .= '
';
$html_items .= '

';
$html_items .= '
';
$date = '';
if ($topic['send_date'] != $topic['update_date']) {
if (!empty($topic['update_date'])) {
$date = '
'.
Display::returnFontAwesomeIcon('calendar').' '.
get_lang('LastUpdate').' '.Display::dateToStringAgoAndLongDate($topic['update_date']).
'
';
}
} else {
$date = '
'.
Display::returnFontAwesomeIcon('calendar').
get_lang('Created').' '.Display::dateToStringAgoAndLongDate($topic['send_date']).
'
';
}
$attachment = '
'.(!empty($files_attachments) ? implode(
'
',
$files_attachments
) : '').'
';
$html_items .= '
';
$html_items .= '
';
$html_items .= $links;
$html_items .= '
'.$user_link.'
';
$html_items .= $date;
$html_items .= '
'.
Security::remove_XSS($topic['content'], STUDENT, true).
'
'.$attachment.'
';
$html_items .= '
';
$html_items .= '
';
$base_padding = 20;
if ($topic['indent_cnt'] == 0) {
$indent = $base_padding;
} else {
$indent = intval($topic['indent_cnt']) * $base_padding + $base_padding;
}
$html_items = Display::div($html_items, ['class' => 'message-post', 'id' => 'msg_'.$topic['id']]);
$html_items = Display::div($html_items, ['class' => '', 'style' => 'margin-left:'.$indent.'px']);
$array_html_items[] = [$html_items];
}
// grids for items with paginations
$options = ['hide_navigation' => false, 'per_page' => $items_per_page];
$visibility = [true, true, true, false];
$style_class = [
'item' => ['class' => 'user-post'],
'main' => ['class' => 'user-list'],
];
if (!empty($array_html_items)) {
$html .= Display::return_sortable_grid(
'items_'.$topic['id'],
[],
$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 rows of messages
*
* @return array $first_seed new list adding the item children
*/
public static function calculate_children($rows, $first_seed)
{
$rows_with_children = [];
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 = [0 => []];
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
*/
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 array $array1 first array to compare
* @param array $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 $messageId
* @param string $type message list (inbox/outbox)
*
* @return array
*/
public static function get_links_message_attachment_files($messageId, $type = '')
{
$table = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
$messageId = intval($messageId);
// get file attachments by message id
$links_attach_file = [];
if (!empty($messageId)) {
$sql = "SELECT * FROM $table
WHERE message_id = '$messageId'";
$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 = Security::remove_XSS($row_file['comment']);
$filename = Security::remove_XSS($filename);
$links_attach_file[] = $attach_icon.'