chat_functions.lib.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.chat
  5. */
  6. /**
  7. * @author isaac flores paz
  8. * @param integer the user id
  9. * @param string the database name
  10. * @return boolean
  11. * @todo this function need more parameters seems not to be use anymore
  12. * @deprecated fix this function or create another
  13. */
  14. function user_connected_in_chat ($user_id) {
  15. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  16. $user_id = intval($user_id);
  17. $session_id = api_get_session_id();
  18. $group_id = api_get_group_id();
  19. $course_id = api_get_course_int_id();
  20. $extra_condition = '';
  21. if (!empty($group_id)) {
  22. $extra_condition = " AND to_group_id = '$group_id'";
  23. } else {
  24. $extra_condition = api_get_session_condition($session_id);
  25. }
  26. $sql = 'SELECT COUNT(*) AS count FROM '.$tbl_chat_connected .' c WHERE c_id = '.$course_id.' AND user_id='.$user_id.$extra_condition;
  27. $result = Database::query($sql);
  28. $count = Database::fetch_array($result,'ASSOC');
  29. return $count['count'] == 1;
  30. }
  31. /**
  32. * @param integer
  33. * @return void
  34. */
  35. function exit_of_chat($user_id) {
  36. $user_id = intval($user_id);
  37. $list_course = CourseManager::get_courses_list_by_user_id($user_id);
  38. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  39. foreach ($list_course as $course) {
  40. $response = user_connected_in_chat($user_id);
  41. $sql = 'DELETE FROM '.$tbl_chat_connected.' WHERE c_id = '.$course['real_id'].' AND user_id = '.$user_id;
  42. Database::query($sql);
  43. }
  44. }
  45. /**
  46. * @param string $database_name (optional)
  47. * @return void
  48. */
  49. function disconnect_user_of_chat() {
  50. $list_info_user_in_chat = array();
  51. $course_id = api_get_course_int_id();
  52. $list_info_user_in_chat = users_list_in_chat();
  53. $course_id = api_get_course_int_id();
  54. $cd_date = date('Y-m-d',time());
  55. $cdate_h = date('H',time());
  56. $cdate_m = date('i',time());
  57. $cdate_s = date('s',time());
  58. $cd_count_time_seconds = $cdate_h*3600 + $cdate_m*60 + $cdate_s;
  59. if (is_array($list_info_user_in_chat) && count($list_info_user_in_chat) > 0 ) {
  60. foreach ($list_info_user_in_chat as $list_info_user) {
  61. $date_db_date = date('Y-m-d', strtotime($list_info_user['last_connection']));
  62. $date_db_h = date('H', strtotime($list_info_user['last_connection']));
  63. $date_db_m = date('i', strtotime($list_info_user['last_connection']));
  64. $date_db_s = date('s', strtotime($list_info_user['last_connection']));
  65. $date_count_time_seconds=$date_db_h*3600 + $date_db_m*60 + $date_db_s;
  66. if ($cd_date == $date_db_date) {
  67. if (($cd_count_time_seconds - $date_count_time_seconds) > 5) {
  68. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  69. $sql = 'DELETE FROM '.$tbl_chat_connected.' WHERE c_id = '.$course_id.' AND user_id ='.$list_info_user['user_id'];
  70. Database::query($sql);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * @param string $database_name (optional)
  78. * @return array user list in chat
  79. */
  80. function users_list_in_chat() {
  81. $list_users_in_chat = array();
  82. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  83. $course_id = api_get_course_int_id();
  84. $session_id = api_get_session_id();
  85. $group_id = api_get_group_id();
  86. $extra_condition = '';
  87. if (!empty($group_id)) {
  88. $extra_condition = " WHERE to_group_id = '$group_id'";
  89. } else{
  90. $extra_condition = api_get_session_condition($session_id, false);
  91. }
  92. $extra_condition.= " AND c_id = $course_id ";
  93. $sql = 'SELECT user_id, last_connection FROM '.$tbl_chat_connected.$extra_condition;
  94. $result = Database::query($sql);
  95. while ($row = Database::fetch_array($result, 'ASSOC')) {
  96. $list_users_in_chat[] = $row;
  97. }
  98. return $list_users_in_chat;
  99. }