VideoChat.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * VideoChat class
  5. *
  6. * This class provides methods for video chat management.
  7. *
  8. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  9. */
  10. class VideoChat
  11. {
  12. /**
  13. * Get the video chat info by its users
  14. * @param int $user1 User id
  15. * @param int $user2 Other user id
  16. * @return array The video chat info. Otherwise return false
  17. */
  18. public static function getChatRoomByUsers($user1, $user2)
  19. {
  20. $user1 = intval($user1);
  21. $user2 = intval($user2);
  22. if (empty($user1) || empty($user2)) {
  23. return false;
  24. }
  25. return Database::select(
  26. '*',
  27. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  28. [
  29. 'where' => [
  30. '(from_user = ? AND to_user = ?)' => [$user1, $user2],
  31. 'OR (from_user = ? AND to_user = ?)' => [$user2, $user1]
  32. ]
  33. ],
  34. 'first'
  35. );
  36. }
  37. /**
  38. * Create a video chat
  39. * @param int $fromUser The sender user
  40. * @param int $toUser The receiver user
  41. * @return int The created video chat id. Otherwise return false
  42. */
  43. public static function createRoom($fromUser, $toUser)
  44. {
  45. $fromUserInfo = api_get_user_info($fromUser);
  46. $toUserInfo = api_get_user_info($toUser);
  47. $chatName = vsprintf(
  48. get_lang('VideoChatBetweenUserXAndUserY'),
  49. [$fromUserInfo['firstname'], $toUserInfo['firstname']]
  50. );
  51. return Database::insert(
  52. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  53. [
  54. 'from_user' => intval($fromUser),
  55. 'to_user' => intval($toUser),
  56. 'room_name' => $chatName,
  57. 'datetime' => api_get_utc_datetime()
  58. ]
  59. );
  60. }
  61. /**
  62. * Check if the video chat exists by its room name
  63. * @param string $name The video chat name
  64. *
  65. * @return boolean
  66. */
  67. public static function nameExists($name)
  68. {
  69. $resultData = Database::select(
  70. 'COUNT(1) AS count',
  71. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  72. [
  73. 'where' => ['room_name = ?' => $name]
  74. ],
  75. 'first'
  76. );
  77. if ($resultData !== false) {
  78. return $resultData['count'] > 0;
  79. }
  80. return false;
  81. }
  82. }