chat.ajax.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls
  5. */
  6. $_dont_save_user_course_access = true;
  7. require_once '../global.inc.php';
  8. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  9. if (api_is_anonymous()) {
  10. exit;
  11. }
  12. // Course Chat
  13. if ($action == 'preview') {
  14. require_once api_get_path(SYS_CODE_PATH).'chat/chat_functions.lib.php';
  15. echo saveMessage(
  16. $_REQUEST['message'],
  17. api_get_user_id(),
  18. api_get_course_info(),
  19. api_get_session_id(),
  20. api_get_group_id(),
  21. true
  22. );
  23. }
  24. if (api_get_setting('allow_global_chat') == 'false') {
  25. exit;
  26. }
  27. $to_user_id = isset($_REQUEST['to']) ? $_REQUEST['to'] : null;
  28. $message = isset($_REQUEST['message']) ? $_REQUEST['message'] : null;
  29. if (!isset($_SESSION['chatHistory'])) {
  30. $_SESSION['chatHistory'] = array();
  31. }
  32. if (!isset($_SESSION['openChatBoxes'])) {
  33. $_SESSION['openChatBoxes'] = array();
  34. }
  35. $chat = new Chat();
  36. if ($chat->is_chat_blocked_by_exercises()) {
  37. // Disconnecting the user
  38. $chat->setUserStatus(0);
  39. exit;
  40. }
  41. switch ($action) {
  42. case 'chatheartbeat':
  43. $chat->heartbeat();
  44. break;
  45. case 'closechat':
  46. $chat->close();
  47. break;
  48. case 'sendchat':
  49. $chat->send(api_get_user_id(), $to_user_id, $message);
  50. break;
  51. case 'startchatsession':
  52. $chat->startSession();
  53. break;
  54. case 'set_status':
  55. $status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
  56. $chat->setUserStatus($status);
  57. break;
  58. case 'start_video':
  59. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
  60. if ($room !== false) {
  61. $videoChatLink = Display::url(
  62. Display::tag(
  63. 'i',
  64. null,
  65. ['class' => 'fa fa-video-camera']
  66. ) . "&nbsp;" . get_lang('StartVideoChat'),
  67. api_get_path(WEB_LIBRARY_JS_PATH) . "chat/video.php?room={$room['room_name']}"
  68. );
  69. $chat->send(api_get_user_id(), $to_user_id, $videoChatLink, false);
  70. echo Display::tag('p', $videoChatLink, ['class' => 'lead']);
  71. break;
  72. }
  73. $form = new FormValidator('start_video_chat');
  74. $form->addText('chat_room_name', get_lang('ChatRoomName'), false);
  75. $form->addHidden('to', $to_user_id);
  76. $form->addButtonSend(get_lang('Create'));
  77. $template = new Template();
  78. $template->assign('form', $form->returnForm());
  79. echo $template->fetch('default/javascript/chat/start_video.tpl');
  80. break;
  81. case 'create_room':
  82. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
  83. $createdRoom = false;
  84. if ($room === false) {
  85. $roomName = isset($_REQUEST['room_name']) ? Security::remove_XSS($_REQUEST['room_name']) : null;
  86. if (VideoChat::nameExists($roomName)) {
  87. echo Display::return_message(get_lang('TheVideoChatRoomXNameAlreadyExists'), 'error');
  88. break;
  89. }
  90. $createdRoom = VideoChat::createRoom($roomName, api_get_user_id(), $to_user_id);
  91. } else {
  92. $roomName = $room['room_name'];
  93. $createdRoom = true;
  94. }
  95. if ($createdRoom === false) {
  96. echo Display::return_message(get_lang('ChatRoomNotCreated'), 'error');
  97. break;
  98. }
  99. $videoChatLink = Display::url(
  100. Display::tag(
  101. 'i',
  102. null,
  103. ['class' => 'fa fa-video-camera']
  104. ) . "&nbsp;" . get_lang('StartVideoChat'),
  105. api_get_path(WEB_LIBRARY_JS_PATH) . "chat/video.php?room=$roomName"
  106. );
  107. $chat->send(api_get_user_id(), $to_user_id, $videoChatLink, false);
  108. echo Display::tag('p', $videoChatLink, ['class' => 'lead']);
  109. break;
  110. case 'notify_not_support':
  111. $chat->send(api_get_user_id(), $to_user_id, get_lang('TheXUserBrowserDoesNotSupportWebRTC'));
  112. break;
  113. default:
  114. echo '';
  115. }
  116. exit;