VideoChat.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. *
  15. * @param int $user1 User id
  16. * @param int $user2 Other user id
  17. *
  18. * @return array The video chat info. Otherwise return false
  19. */
  20. public static function getChatRoomByUsers($user1, $user2)
  21. {
  22. $user1 = (int) $user1;
  23. $user2 = (int) $user2;
  24. if (empty($user1) || empty($user2)) {
  25. return false;
  26. }
  27. return Database::select(
  28. '*',
  29. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  30. [
  31. 'where' => [
  32. '(from_user = ? AND to_user = ?)' => [$user1, $user2],
  33. 'OR (from_user = ? AND to_user = ?)' => [$user2, $user1],
  34. ],
  35. ],
  36. 'first'
  37. );
  38. }
  39. /**
  40. * Create a video chat.
  41. *
  42. * @param int $fromUser The sender user
  43. * @param int $toUser The receiver user
  44. *
  45. * @return int The created video chat id. Otherwise return false
  46. */
  47. public static function createRoom($fromUser, $toUser)
  48. {
  49. $fromUserInfo = api_get_user_info($fromUser);
  50. $toUserInfo = api_get_user_info($toUser);
  51. $chatName = vsprintf(
  52. get_lang('VideoChatBetweenUserXAndUserY'),
  53. [$fromUserInfo['firstname'], $toUserInfo['firstname']]
  54. );
  55. return Database::insert(
  56. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  57. [
  58. 'from_user' => $fromUser,
  59. 'to_user' => $toUser,
  60. 'room_name' => $chatName,
  61. 'datetime' => api_get_utc_datetime(),
  62. ]
  63. );
  64. }
  65. /**
  66. * Check if the video chat exists by its room name.
  67. *
  68. * @param string $name The video chat name
  69. *
  70. * @return bool
  71. */
  72. public static function nameExists($name)
  73. {
  74. $resultData = Database::select(
  75. 'COUNT(1) AS count',
  76. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  77. [
  78. 'where' => ['room_name = ?' => $name],
  79. ],
  80. 'first'
  81. );
  82. if ($resultData !== false) {
  83. return $resultData['count'] > 0;
  84. }
  85. return false;
  86. }
  87. }