Skip to content

Commit

Permalink
If closing the contact list, user will logout from global chat.
Browse files Browse the repository at this point in the history
Fix closing chat windows
  • Loading branch information
jmontoyaa committed Mar 7, 2019
1 parent e4b2514 commit 401cf0d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 52 deletions.
16 changes: 13 additions & 3 deletions main/inc/ajax/chat.ajax.php
Expand Up @@ -15,10 +15,10 @@
if (api_is_anonymous()) {
exit;
}
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';

// Course Chat
if ($action == 'preview') {
if ($action === 'preview') {
echo CourseChatUtils::prepareMessage($_REQUEST['message']);
exit;
}
Expand All @@ -31,11 +31,13 @@
if (Chat::disableChat()) {
exit;
}

if ($chat->isChatBlockedByExercises()) {
// Disconnecting the user
$chat->setUserStatus(0);
exit;
}

switch ($action) {
case 'get_message_status':
$messageId = isset($_REQUEST['message_id']) ? $_REQUEST['message_id'] : 0;
Expand All @@ -47,7 +49,15 @@
case 'chatheartbeat':
$chat->heartbeat();
break;
case 'closechat':
case 'close_window':
// Closes friend window
$chatId = isset($_POST['chatbox']) ? $_POST['chatbox'] : '';
$chat->closeWindow($chatId);
echo '1';
exit;
break;
case 'close':
// Disconnects user from all chat
$chat->close();
break;
case 'create_room':
Expand Down
40 changes: 34 additions & 6 deletions main/inc/lib/chat.lib.php
Expand Up @@ -91,7 +91,6 @@ public function getContacts()
$html = SocialManager::listMyFriendsBlock(
api_get_user_id(),
'',
true,
true
);

Expand Down Expand Up @@ -422,7 +421,6 @@ public function heartbeat()
/**
* Saves into session the fact that a chat window exists with the given user.
*
* @param int The ID of the user with whom the current user is chatting
* @param int $userId
*/
public function saveWindow($userId)
Expand Down Expand Up @@ -480,8 +478,8 @@ public function send(
unset($_SESSION['tsChatBoxes'][$to_user_id]);

$params = [];
$params['from_user'] = intval($fromUserId);
$params['to_user'] = intval($to_user_id);
$params['from_user'] = (int) $fromUserId;
$params['to_user'] = (int) $to_user_id;
$params['message'] = $message;
$params['sent'] = api_get_utc_datetime();

Expand All @@ -502,11 +500,41 @@ public function send(

/**
* Close a specific chat box (user ID taken from $_POST['chatbox']).
*
* @param $userId
*/
public function closeWindow($userId)
{
if (empty($userId)) {
return false;
}

$list = Session::read('openChatBoxes');
if (isset($list[$userId])) {
unset($list[$userId]);
Session::write('openChatBoxes', $list);
}

$list = Session::read('chatHistory');
if (isset($list[$userId])) {
unset($list[$userId]);
Session::write('chatHistory', $list);
}

return true;
}


/**
* Close chat - disconnects the user
*/
public function close()
{
unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
unset($_SESSION['chatHistory'][$_POST['chatbox']]);
Session::erase('tsChatBoxes');
Session::erase('openChatBoxes');
Session::erase('chatHistory');
Session::erase('window_list');

echo '1';
exit;
}
Expand Down
41 changes: 33 additions & 8 deletions main/inc/lib/javascript/chat/js/chat.js
Expand Up @@ -102,7 +102,12 @@ $(document).ready(function() {
// Close button
$('body').on('click', '.chatboxhead .closelink', function(){
var chat_id = $(this).attr('rel');
closeChatBox(chat_id);
closeWindow(chat_id);
});

// Close main chat
$('body').on('click', '.chatboxhead .close_chat', function(){
closeChat();
});
});

Expand Down Expand Up @@ -278,7 +283,6 @@ function chatHeartbeat()
);

if (item.s == 2) {
//$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
} else {
newMessages[my_user_id] = {'status':true, 'username':item.username};
newMessagesWin[my_user_id]= {'status':true, 'username':item.username};
Expand Down Expand Up @@ -355,17 +359,38 @@ function createChatBubble(my_user_id, item)
return message;
}

function closeChatBox(user_id)
/**
* Disconnect user from chat
*/
function closeChat()
{
$.post(
ajax_url + "?action=close",
{
},
function (data) {
// Disconnects from chat
set_user_status(0);
// Clean cookies
Cookies.set('chatbox_minimized', new Array());
// Delete all windows
$('.chatbox ').remove();
}
);
}

function closeWindow(user_id)
{
$('#chatbox_'+user_id).css('display','none');
restructureChatBoxes();
$.post(
ajax_url+"?action=closechat",
ajax_url + "?action=close_window",
{
chatbox: user_id
} ,
function(data) {
});
},
function (data) {
}
);
}

function restructureChatBoxes()
Expand Down Expand Up @@ -457,7 +482,7 @@ function createMyContactsWindow()
.appendTo(chatboxoptions);

$('<a>')
.addClass('btn btn-xs closelink')
.addClass('btn btn-xs close_chat')
.attr({
href: 'javascript:void(0)',
rel: user_id
Expand Down
80 changes: 45 additions & 35 deletions main/inc/lib/social.lib.php
Expand Up @@ -2328,17 +2328,19 @@ public static function listMyFriendsBlock($user_id, $link_shared = '', $showLink
{
//SOCIALGOODFRIEND , USER_RELATION_TYPE_FRIEND, USER_RELATION_TYPE_PARENT
$friends = self::get_friends($user_id, USER_RELATION_TYPE_FRIEND);
$number_of_images = 30;
$numberFriends = count($friends);
$friendHtml = '';

if (!empty($numberFriends)) {
$friendHtml .= '<div class="list-group contact-list">';
$j = 1;

usort($friends, function ($a, $b) {
return strcmp($b['user_info']['user_is_online_in_chat'], $a['user_info']['user_is_online_in_chat']);
});
usort(
$friends,
function ($a, $b) {
return strcmp($b['user_info']['user_is_online_in_chat'], $a['user_info']['user_is_online_in_chat']);
}
);

foreach ($friends as $friend) {
if ($j > $numberFriends) {
Expand All @@ -2356,19 +2358,19 @@ public static function listMyFriendsBlock($user_id, $link_shared = '', $showLink
}

$friendAvatarMedium = UserManager::getUserPicture(
$friend['friend_user_id'],
USER_IMAGE_SIZE_MEDIUM
);
$friend['friend_user_id'],
USER_IMAGE_SIZE_MEDIUM
);
$friendAvatarSmall = UserManager::getUserPicture(
$friend['friend_user_id'],
USER_IMAGE_SIZE_SMALL
);
$friend['friend_user_id'],
USER_IMAGE_SIZE_SMALL
);
$friend_avatar = '<img src="'.$friendAvatarMedium.'" id="imgfriend_'.$friend['friend_user_id'].'" title="'.$name_user.'" class="user-image"/>';

$relation = self::get_relation_between_contacts(
$friend['friend_user_id'],
api_get_user_id()
);
$friend['friend_user_id'],
api_get_user_id()
);

if ($showLinkToChat) {
$friendHtml .= '<a onclick="javascript:chatWith(\''.$friend['friend_user_id'].'\', \''.$name_user.'\', \''.$status.'\',\''.$friendAvatarSmall.'\')" href="javascript:void(0);" class="list-group-item">';
Expand Down Expand Up @@ -2400,39 +2402,41 @@ public static function getWallForm($urlForm)
{
$userId = isset($_GET['u']) ? '?u='.intval($_GET['u']) : '';
$form = new FormValidator(
'social_wall_main',
'post',
'social_wall_main',
'post',
$urlForm.$userId,
null,
['enctype' => 'multipart/form-data'],
FormValidator::LAYOUT_HORIZONTAL
);
null,
['enctype' => 'multipart/form-data'],
FormValidator::LAYOUT_HORIZONTAL
);

$socialWallPlaceholder = isset($_GET['u']) ? get_lang('SocialWallWriteNewPostToFriend') : get_lang('SocialWallWhatAreYouThinkingAbout');
$socialWallPlaceholder = isset($_GET['u']) ? get_lang('SocialWallWriteNewPostToFriend') : get_lang(
'SocialWallWhatAreYouThinkingAbout'
);

$form->addTextarea(
'social_wall_new_msg_main',
null,
[
'placeholder' => $socialWallPlaceholder,
'cols-size' => [1, 10, 1],
'aria-label' => $socialWallPlaceholder,
]
);
'social_wall_new_msg_main',
null,
[
'placeholder' => $socialWallPlaceholder,
'cols-size' => [1, 10, 1],
'aria-label' => $socialWallPlaceholder,
]
);
$form->addHtml('<div class="form-group">');
$form->addHtml('<div class="col-sm-4 col-md-offset-1">');
$form->addFile('picture', get_lang('UploadFile'), ['custom' => true]);
$form->addHtml('</div>');
$form->addHtml('<div class="col-sm-6">');
$form->addButtonSend(
get_lang('Post'),
'wall_post_button',
false,
get_lang('Post'),
'wall_post_button',
false,
[
'cols-size' => [1, 10, 1],
'custom' => true,
]
);
);
$form->addHtml('</div>');
$form->addHtml('</div>');

Expand Down Expand Up @@ -2496,6 +2500,12 @@ public static function getMyWallMessages($userId, $start = 0, $length = 10, $thr
];
}

/**
* @param string $message
* @param string $content
*
* @return string
*/
public static function wrapPost($message, $content)
{
return Display::panel($content, '',
Expand Down Expand Up @@ -3091,18 +3101,18 @@ private static function headerMessagePost($authorInfo, $receiverInfo, $message)
$iconStatus = null;
$authorId = (int) $authorInfo['user_id'];
$receiverId = (int) $receiverInfo['user_id'];
$userStatus = $authorInfo['status'];
$userStatus = (int) $authorInfo['status'];
$urlImg = api_get_path(WEB_IMG_PATH);
$isAdmin = self::is_admin($authorId);

if ($userStatus == 5) {
if ($userStatus === 5) {
if ($authorInfo['has_certificates']) {
$iconStatus = '<img class="pull-left" src="'.$urlImg.'icons/svg/identifier_graduated.svg" width="22px" height="22px">';
} else {
$iconStatus = '<img class="pull-left" src="'.$urlImg.'icons/svg/identifier_student.svg" width="22px" height="22px">';
}
} else {
if ($userStatus == 1) {
if ($userStatus === 1) {
if ($isAdmin) {
$iconStatus = '<img class="pull-left" src="'.$urlImg.'icons/svg/identifier_admin.svg" width="22px" height="22px">';
} else {
Expand Down

0 comments on commit 401cf0d

Please sign in to comment.