chat.ajax.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Responses to AJAX calls
  6. */
  7. $_dont_save_user_course_access = true;
  8. //require_once '../global.inc.php';
  9. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  10. if (api_is_anonymous()) {
  11. exit;
  12. }
  13. // Course Chat
  14. if ($action == 'preview') {
  15. echo CourseChatUtils::prepareMessage($_REQUEST['message']);
  16. exit;
  17. }
  18. if (api_get_setting('chat.allow_global_chat') == 'false') {
  19. exit;
  20. }
  21. $to_user_id = isset($_REQUEST['to']) ? $_REQUEST['to'] : null;
  22. $message = isset($_REQUEST['message']) ? $_REQUEST['message'] : null;
  23. $chatHistory = Session::read('chatHistory');
  24. if (!isset($chatHistory)) {
  25. Session::write('chatHistory', []);
  26. }
  27. $chatBox = Session::read('openChatBoxes');
  28. if (!isset($chatBox)) {
  29. Session::write('openChatBoxes', []);
  30. }
  31. $chat = new Chat();
  32. if (chat::disableChat()){
  33. exit;
  34. }
  35. if ($chat->is_chat_blocked_by_exercises()) {
  36. // Disconnecting the user
  37. $chat->setUserStatus(0);
  38. exit;
  39. }
  40. switch ($action) {
  41. case 'chatheartbeat':
  42. $chat->heartbeat();
  43. break;
  44. case 'closechat':
  45. $chat->close();
  46. break;
  47. case 'sendchat':
  48. $chat->send(api_get_user_id(), $to_user_id, $message);
  49. break;
  50. case 'startchatsession':
  51. $chat->startSession();
  52. break;
  53. case 'set_status':
  54. $status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
  55. $chat->setUserStatus($status);
  56. break;
  57. case 'create_room':
  58. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
  59. if ($room === false) {
  60. $createdRoom = VideoChat::createRoom(api_get_user_id(), $to_user_id);
  61. if ($createdRoom === false) {
  62. echo Display::return_message(get_lang('ChatRoomNotCreated'), 'error');
  63. break;
  64. }
  65. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
  66. }
  67. $videoChatUrl = api_get_path(WEB_LIBRARY_JS_PATH) . "chat/video.php?room={$room['id']}";
  68. $videoChatLink = Display::url(
  69. Display::returnFontAwesomeIcon('video-camera') . get_lang('StartVideoChat'),
  70. $videoChatUrl
  71. );
  72. $chat->send(
  73. api_get_user_id(),
  74. $to_user_id,
  75. $videoChatLink,
  76. false,
  77. false
  78. );
  79. echo Display::tag('p', $videoChatLink, ['class' => 'lead']);
  80. break;
  81. case 'notify_not_support':
  82. $chat->send(api_get_user_id(), $to_user_id, get_lang('TheXUserBrowserDoesNotSupportWebRTC'));
  83. break;
  84. default:
  85. echo '';
  86. }
  87. exit;