VideoChat.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 string $name The video chat name
  40. * @param int $fromUser The sender user
  41. * @param int $toUser The receiver user
  42. *
  43. * @return int The created video chat id. Otherwise return false
  44. */
  45. public static function createRoom($name, $fromUser, $toUser)
  46. {
  47. return Database::insert(
  48. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  49. [
  50. 'from_user' => intval($fromUser),
  51. 'to_user' => intval($toUser),
  52. 'room_name' => $name,
  53. 'datetime' => api_get_utc_datetime()
  54. ]
  55. );
  56. }
  57. /**
  58. * Check if the video chat exists by its room name
  59. * @param string $name The video chat name
  60. *
  61. * @return boolean
  62. */
  63. public static function nameExists($name)
  64. {
  65. $resultData = Database::select(
  66. 'COUNT(1) AS count',
  67. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  68. [
  69. 'where' => ['room_name = ?' => $name]
  70. ],
  71. 'first'
  72. );
  73. if ($resultData !== false) {
  74. return $resultData['count'] > 0;
  75. }
  76. return false;
  77. }
  78. /**
  79. * Get the video chat info by its room name
  80. * @param string $name The video chat name
  81. *
  82. * @return array The video chat info. Otherwise return false
  83. */
  84. public static function getChatRoomByName($name)
  85. {
  86. return Database::select(
  87. '*',
  88. Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
  89. [
  90. 'where' => ['room_name = ?' => $name]
  91. ],
  92. 'first'
  93. );
  94. }
  95. }