Refactor chat function, fix chat js errors.

1.10.x
Julio Montoya 11 years ago
parent f3c835434e
commit f83214dd11
  1. 8
      main/inc/ajax/chat.ajax.php
  2. 43
      main/inc/lib/chat.lib.php
  3. 4
      main/inc/lib/javascript/chat/js/chat.js
  4. 2
      main/inc/lib/online.inc.php
  5. 2
      main/social/profile.php

@ -47,8 +47,8 @@ if (!isset($_SESSION['openChatBoxes'])) {
$chat = new Chat(); $chat = new Chat();
if ($chat->is_chat_blocked_by_exercises()) { if ($chat->is_chat_blocked_by_exercises()) {
//Disconnecting the user // Disconnecting the user
$chat->set_user_status(0); $chat->setUserStatus(0);
exit; exit;
} }
@ -63,11 +63,11 @@ switch ($action) {
$chat->send(api_get_user_id(), $to_user_id, $message); $chat->send(api_get_user_id(), $to_user_id, $message);
break; break;
case 'startchatsession': case 'startchatsession':
$chat->start_session(); $chat->startSession();
break; break;
case 'set_status': case 'set_status':
$status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0; $status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
$chat->set_user_status($status); $chat->setUserStatus($status);
break; break;
default: default:
echo ''; echo '';

@ -1,14 +1,10 @@
<?php <?php
/* For licensing terms, see /license.txt */ /* For licensing terms, see /license.txt */
/**
* This is the Chat library for Chamilo.
* Include/require it in your code to use its functionality.
*
* @package chamilo.library.chat
*/
/** /**
* Chat class * Class Chat
*
* @package chamilo.library.chat
*/ */
class Chat extends Model class Chat extends Model
{ {
@ -36,22 +32,21 @@ class Chat extends Model
return $status['user_chat_status']; return $status['user_chat_status'];
} }
/* /**
* Set user chat status * Set user chat status
* @param int 0 if disconnected, 1 if connected * @param int 0 if disconnected, 1 if connected
* @return void * @return void
*/ */
public function setUserStatus($status)
function set_user_status($status)
{ {
UserManager::update_extra_field_value(api_get_user_id(), 'user_chat_status', $status); UserManager::update_extra_field_value(api_get_user_id(), 'user_chat_status', $status);
} }
/* /**
* Starts a chat session and returns JSON array of status and chat history * Starts a chat session and returns JSON array of status and chat history
* @return void (prints output in JSON format) * @return void (prints output in JSON format)
*/ */
public function start_session() public function startSession()
{ {
$items = array(); $items = array();
if (isset($_SESSION['chatHistory'])) { if (isset($_SESSION['chatHistory'])) {
@ -150,7 +145,7 @@ class Chat extends Model
* @return array Messages list * @return array Messages list
*/ */
function box_session($user_id) public function box_session($user_id)
{ {
$items = array(); $items = array();
if (isset($_SESSION['chatHistory'][$user_id])) { if (isset($_SESSION['chatHistory'][$user_id])) {
@ -164,7 +159,7 @@ class Chat extends Model
* @param int The ID of the user with whom the current user is chatting * @param int The ID of the user with whom the current user is chatting
* @return void * @return void
*/ */
function save_window($user_id) public function save_window($user_id)
{ {
$this->window_list[$user_id] = true; $this->window_list[$user_id] = true;
$_SESSION['window_list'] = $this->window_list; $_SESSION['window_list'] = $this->window_list;
@ -177,9 +172,8 @@ class Chat extends Model
* @param string Message * @param string Message
* @return void Prints "1" * @return void Prints "1"
*/ */
function send($from_user_id, $to_user_id, $message) public function send($from_user_id, $to_user_id, $message)
{ {
$user_friend_relation = SocialManager::get_relation_between_contacts($from_user_id, $to_user_id); $user_friend_relation = SocialManager::get_relation_between_contacts($from_user_id, $to_user_id);
if ($user_friend_relation == USER_RELATION_TYPE_FRIEND) { if ($user_friend_relation == USER_RELATION_TYPE_FRIEND) {
@ -212,7 +206,7 @@ class Chat extends Model
if (!empty($from_user_id) && !empty($to_user_id)) { if (!empty($from_user_id) && !empty($to_user_id)) {
$this->save($params); $this->save($params);
} }
//print_r($_SESSION['chatHistory']);
echo "1"; echo "1";
exit; exit;
} else { } else {
@ -225,7 +219,7 @@ class Chat extends Model
* Close a specific chat box (user ID taken from $_POST['chatbox']) * Close a specific chat box (user ID taken from $_POST['chatbox'])
* @return void Prints "1" * @return void Prints "1"
*/ */
function close() public function close()
{ {
unset($_SESSION['openChatBoxes'][$_POST['chatbox']]); unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
unset($_SESSION['chatHistory'][$_POST['chatbox']]); unset($_SESSION['chatHistory'][$_POST['chatbox']]);
@ -238,7 +232,7 @@ class Chat extends Model
* @param string Unfiltered message * @param string Unfiltered message
* @return string Filterd mssage * @return string Filterd mssage
*/ */
function sanitize($text) public function sanitize($text)
{ {
$text = htmlspecialchars($text, ENT_QUOTES); $text = htmlspecialchars($text, ENT_QUOTES);
$text = str_replace("\n\r", "\n", $text); $text = str_replace("\n\r", "\n", $text);
@ -247,7 +241,7 @@ class Chat extends Model
return $text; return $text;
} }
function is_chat_blocked_by_exercises() public function is_chat_blocked_by_exercises()
{ {
if (isset($_SESSION['current_exercises'])) { if (isset($_SESSION['current_exercises'])) {
foreach ($_SESSION['current_exercises'] as $attempt_status) { foreach ($_SESSION['current_exercises'] as $attempt_status) {
@ -256,6 +250,7 @@ class Chat extends Model
} }
} }
} }
return false; return false;
} }
} }

@ -61,10 +61,10 @@ $(document).ready(function() {
document.title = originalTitle; document.title = originalTitle;
}); });
/* Live conditions */ /* "On" conditions, divs are created dynamically */
// User name header toogle // User name header toogle
$('#chatboxtitlemain').on('click', function() { $('body').on('click', '#chatboxtitlemain', function() {
if (user_status == 1) { if (user_status == 1) {
stopChatHeartBeat(); stopChatHeartBeat();
$('.user_status_main').html(offline_button); $('.user_status_main').html(offline_button);

@ -106,7 +106,7 @@ function online_logout($user_id = null, $logout_redirect = false) {
//Changing global chat status to offline //Changing global chat status to offline
if (api_is_global_chat_enabled()) { if (api_is_global_chat_enabled()) {
$chat = new Chat(); $chat = new Chat();
$chat->set_user_status(0); $chat->setUserStatus(0);
} }
// selecting the last login of the user // selecting the last login of the user

@ -168,7 +168,7 @@ function send_message_to_user(user_id) {
} }
}); });
} }
}, }
}, },
close: function() { close: function() {
} }

Loading…
Cancel
Save