chat_functions.lib.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * @return boolean
  10. * @todo this function need more parameters seems not to be use anymore
  11. * @deprecated fix this function or create another
  12. */
  13. function user_connected_in_chat ($user_id)
  14. {
  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. {
  37. $user_id = intval($user_id);
  38. $list_course = CourseManager::get_courses_list_by_user_id($user_id);
  39. /*$session_id = api_get_session_id();
  40. $group_id = api_get_group_id();
  41. $extra_condition = '';
  42. if (!empty($group_id)) {
  43. $extra_condition = " AND to_group_id = '$group_id'";
  44. } else {
  45. $extra_condition = api_get_session_condition($session_id);
  46. }
  47. $extra_condition.= " AND course_id = $course_id";*/
  48. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  49. foreach ($list_course as $course) {
  50. $response = user_connected_in_chat($user_id);
  51. //if ($response === true) {
  52. $sql = 'DELETE FROM '.$tbl_chat_connected.' WHERE c_id = '.$course['real_id'].' AND user_id = '.$user_id;
  53. Database::query($sql);
  54. //}
  55. }
  56. }
  57. /**
  58. * @return void
  59. */
  60. function disconnect_user_of_chat()
  61. {
  62. $list_info_user_in_chat = array();
  63. $course_id = api_get_course_int_id();
  64. $list_info_user_in_chat = users_list_in_chat();
  65. $course_id = api_get_course_int_id();
  66. $cd_date = date('Y-m-d',time());
  67. $cdate_h = date('H',time());
  68. $cdate_m = date('i',time());
  69. $cdate_s = date('s',time());
  70. $cd_count_time_seconds = $cdate_h*3600 + $cdate_m*60 + $cdate_s;
  71. if (is_array($list_info_user_in_chat) && count($list_info_user_in_chat) > 0 ) {
  72. foreach ($list_info_user_in_chat as $list_info_user) {
  73. $date_db_date = date('Y-m-d', strtotime($list_info_user['last_connection']));
  74. $date_db_h = date('H', strtotime($list_info_user['last_connection']));
  75. $date_db_m = date('i', strtotime($list_info_user['last_connection']));
  76. $date_db_s = date('s', strtotime($list_info_user['last_connection']));
  77. $date_count_time_seconds=$date_db_h*3600 + $date_db_m*60 + $date_db_s;
  78. if ($cd_date == $date_db_date) {
  79. if (($cd_count_time_seconds - $date_count_time_seconds) > 5) {
  80. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  81. $sql = 'DELETE FROM '.$tbl_chat_connected.'
  82. WHERE c_id = '.$course_id.' AND user_id ='.$list_info_user['user_id'];
  83. Database::query($sql);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. /**
  90. * @return array user list in chat
  91. */
  92. function users_list_in_chat()
  93. {
  94. $list_users_in_chat = array();
  95. $tbl_chat_connected = Database::get_course_table(TABLE_CHAT_CONNECTED);
  96. $course_id = api_get_course_int_id();
  97. $session_id = api_get_session_id();
  98. $group_id = api_get_group_id();
  99. $extra_condition = '';
  100. if (!empty($group_id)) {
  101. $extra_condition = " WHERE to_group_id = '$group_id'";
  102. } else{
  103. $extra_condition = api_get_session_condition($session_id, false);
  104. }
  105. $extra_condition.= " AND c_id = $course_id ";
  106. $sql = 'SELECT user_id, last_connection FROM '.$tbl_chat_connected.$extra_condition;
  107. $result = Database::query($sql);
  108. while ($row = Database::fetch_array($result, 'ASSOC')) {
  109. $list_users_in_chat[] = $row;
  110. }
  111. return $list_users_in_chat;
  112. }