diff --git a/main/chat/chat_functions.lib.php b/main/chat/chat_functions.lib.php index d52f077dc5..0bfa63af50 100755 --- a/main/chat/chat_functions.lib.php +++ b/main/chat/chat_functions.lib.php @@ -30,7 +30,7 @@ function exit_of_chat ($user_id) { $list_course=array(); $list_course=CourseManager::get_courses_list_by_user_id($user_id); - + $group_id = intval($_SESSION['id_group']); $session_id = intval($_SESSION['id_session']); $extra_condition = ''; @@ -40,7 +40,7 @@ function exit_of_chat ($user_id) { foreach($list_course as $courses) { $response=user_connected_in_chat($user_id,$courses['db_name']); if ($response===true) { - $tbl_chat_connected = Database::get_course_chat_connected_table($courses['db_name']);; + $tbl_chat_connected = Database::get_course_chat_connected_table($courses['db_name']); $sql='DELETE FROM '.$tbl_chat_connected.' WHERE user_id='.$user_id.$extra_condition; Database::query($sql,__FILE__,__LINE__); } @@ -75,9 +75,15 @@ function user_connected_in_chat ($user_id,$database_name) { * @param void * @return void */ -function disconnect_user_of_chat () { +function disconnect_user_of_chat($database_name='') { + $list_info_user_in_chat = array(); - $list_info_user_in_chat = users_list_in_chat (); + + if (!empty($database_name)) { + $list_info_user_in_chat = users_list_in_chat($database_name); + } else { + $list_info_user_in_chat = users_list_in_chat(); + } $cd_date = date('Y-m-d',time()); $cdate_h = date('H',time()); @@ -85,37 +91,44 @@ function disconnect_user_of_chat () { $cdate_s = date('s',time()); $cd_count_time_seconds=$cdate_h*3600 + $cdate_m*60 + $cdate_s; - foreach ($list_info_user_in_chat as $list_info_user) { - $date_db_date = date('Y-m-d',strtotime($list_info_user['last_connection'])); - $date_db_h = date('H',strtotime($list_info_user['last_connection'])); - $date_db_m = date('i',strtotime($list_info_user['last_connection'])); - $date_db_s = date('s',strtotime($list_info_user['last_connection'])); - $date_count_time_seconds=$date_db_h*3600 + $date_db_m*60 + $date_db_s; - if ($cd_date==$date_db_date) { - if (($cd_count_time_seconds - $date_count_time_seconds)>5) { - $tbl_chat_connected = Database::get_course_chat_connected_table(); - $sql='DELETE FROM '.$tbl_chat_connected.' WHERE user_id='.$list_info_user['user_id']; - Database::query($sql,__FILE__,__LINE__); + + if (is_array($list_info_user_in_chat) && count($list_info_user_in_chat) > 0 ) { + foreach ($list_info_user_in_chat as $list_info_user) { + $date_db_date = date('Y-m-d',strtotime($list_info_user['last_connection'])); + $date_db_h = date('H',strtotime($list_info_user['last_connection'])); + $date_db_m = date('i',strtotime($list_info_user['last_connection'])); + $date_db_s = date('s',strtotime($list_info_user['last_connection'])); + $date_count_time_seconds=$date_db_h*3600 + $date_db_m*60 + $date_db_s; + if ($cd_date==$date_db_date) { + if (($cd_count_time_seconds - $date_count_time_seconds)>5) { + + $tbl_chat_connected = Database::get_course_chat_connected_table(); + if (!empty($database_name)) { + $tbl_chat_connected = Database::get_course_chat_connected_table($database_name); + } + + $sql='DELETE FROM '.$tbl_chat_connected.' WHERE user_id='.$list_info_user['user_id']; + //return $sql; + Database::query($sql,__FILE__,__LINE__); + } } - } + } } -} +} /** * @param void * @return array user list in chat */ -function users_list_in_chat () { +function users_list_in_chat ($database_name = '') { $list_users_in_chat=array(); - $tbl_chat_connected = Database::get_course_chat_connected_table(); - + $tbl_chat_connected = Database::get_course_chat_connected_table($database_name); $group_id = intval($_SESSION['id_group']); $session_id = intval($_SESSION['id_session']); $extra_condition = ''; if (!empty($group_id)) $extra_condition = " WHERE to_group_id = '$group_id'"; - else $extra_condition = api_get_session_condition($session_id,false); - - $sql='SELECT user_id,last_connection FROM '.$tbl_chat_connected.$extra_condition; + else $extra_condition = api_get_session_condition($session_id,false); + $sql='SELECT user_id,last_connection FROM '.$tbl_chat_connected.$extra_condition; $result=Database::query($sql,__FILE__,__LINE__); while ($row = Database::fetch_array($result,'ASSOC')) { $list_users_in_chat[]=$row; diff --git a/tests/main/chat/chat_functions.lib.test.php b/tests/main/chat/chat_functions.lib.test.php index 0a28a4cff2..7503682592 100755 --- a/tests/main/chat/chat_functions.lib.test.php +++ b/tests/main/chat/chat_functions.lib.test.php @@ -5,34 +5,105 @@ require_once(api_get_path(LIBRARY_PATH).'course.lib.php'); Mock::generate('CourseManager'); class TestChatFunctions extends UnitTestCase { - function testdisconnect_user_of_chat() { - $res = disconnect_user_of_chat(); - $this->assertTrue(is_null($res)); - //var_dump($res); + public function setUp() { + $this->tcourse = new CourseManager(); } - - function testexit_of_chat () { - $docu = new MockCourseManager(); - $user_id=1; - $res = exit_of_chat(); - $docu->expectOnce(CourseManager::get_courses_list_by_user_id($user_id),'admin'); - $this->assertTrue(is_object($docu)); - $this->assertTrue(is_null($res)); - //var_dump($res); + + public function tearDown() { + $this->tcourse = null; } + + function testuser_connected_in_chat () { - $user_id=1; - $database_name=''; + global $_configuration; + + require_once api_get_path(SYS_PATH).'tests/main/inc/lib/add_course.lib.inc.test.php'; + + // create a course + + $course_datos = array( + 'wanted_code'=> 'COD21', + 'title'=>'metodologia de calculo diferencial', + 'tutor_name'=>'R. J. Wolfagan', + 'category_code'=>'2121', + 'course_language'=>'english', + 'course_admin_id'=>'1211', + 'db_prefix'=> $_configuration['db_prefix'].'COD21', + 'db_prefix'=> $_configuration['db_prefix'].'COD21', + 'firstExpirationDelay'=>'112' + ); + $res = create_course($course_datos['wanted_code'], $course_datos['title'], + $course_datos['tutor_name'], $course_datos['category_code'], + $course_datos['course_language'],$course_datos['course_admin_id'], + $course_datos['db_prefix'], $course_datos['firstExpirationDelay']); + if ($res) { + $course_code = 'COD21'; + $course_info = api_get_course_info($course_code); + + } + $user_id=1; + $course_info = api_get_course_info($course_code); + $database_name = $course_info['dbName']; $res = user_connected_in_chat($user_id,$database_name); - $this->assertTrue(is_bool($res)); - //var_dump($res); + $this->assertTrue(is_bool($res)); } + - function testusers_list_in_chat () { - $res = users_list_in_chat(); + function testUsersListInChat () { + $course_code = 'COD21'; + $course_info = api_get_course_info($course_code); + $database_name = $course_info['dbName']; + $res = users_list_in_chat($database_name); $this->assertTrue(is_array($res)); //var_dump($res); } + + function CreateChatConnection($database_name) { + $session_id = 1; + $tbl_chat_connected = Database::get_course_chat_connected_table($database_name); + $sql = "SELECT user_id FROM $tbl_chat_connected WHERE user_id = 1"; + $result = Database::query($sql); + //The user_id exists so we must do an UPDATE and not a INSERT + $current_time = date('Y-m-d H:i:s'); + if (Database::num_rows($result)==0) { + $query="INSERT INTO $tbl_chat_connected(user_id,last_connection,session_id) VALUES(1,'$current_time','$session_id')"; + } else { + $query="UPDATE $tbl_chat_connected set last_connection='".$current_time."' WHERE user_id=1 AND session_id='$session_id'"; + } + Database::query($query,__FILE__,__LINE__); + } + + + function testExitOfChat () { + $course_code = 'COD21'; + $course_info = api_get_course_info($course_code); + $database_name = $course_info['dbName']; + $this->CreateChatConnection($database_name); + $user_id = 1; + $res = exit_of_chat($user_id); + //$resu = $this->tcourse->delete_course($course_code); + $this->assertTrue(is_null($res)); + //var_dump($res); + } + + + function testDisconnectUserOfChat() { + + $_SESSION['is_courseAdmin'] = 1; + $course_code = 'COD21'; + + $course_info = api_get_course_info($course_code); + $database_name = $course_info['dbName']; + + $this->CreateChatConnection($database_name); + $res = disconnect_user_of_chat($database_name); + $this->assertTrue(is_null($res)); + + $resu = $this->tcourse->delete_course($course_code); + + } + + } ?>