chat.ajax.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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::disableChat()){
  37. exit;
  38. }
  39. if ($chat->is_chat_blocked_by_exercises()) {
  40. // Disconnecting the user
  41. $chat->setUserStatus(0);
  42. exit;
  43. }
  44. switch ($action) {
  45. case 'chatheartbeat':
  46. $chat->heartbeat();
  47. break;
  48. case 'closechat':
  49. $chat->close();
  50. break;
  51. case 'sendchat':
  52. $chat->send(api_get_user_id(), $to_user_id, $message);
  53. break;
  54. case 'startchatsession':
  55. $chat->startSession();
  56. break;
  57. case 'set_status':
  58. $status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
  59. $chat->setUserStatus($status);
  60. break;
  61. case 'create_room':
  62. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
  63. if ($room === false) {
  64. $createdRoom = VideoChat::createRoom(api_get_user_id(), $to_user_id);
  65. if ($createdRoom === false) {
  66. echo Display::return_message(get_lang('ChatRoomNotCreated'), 'error');
  67. break;
  68. }
  69. $room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
  70. }
  71. $videoChatUrl = api_get_path(WEB_LIBRARY_JS_PATH) . "chat/video.php?room={$room['id']}";
  72. $videoChatLink = Display::url(
  73. Display::returnFontAswesomeIcon('video-camera') . get_lang('StartVideoChat'),
  74. $videoChatUrl
  75. );
  76. $chat->send(
  77. api_get_user_id(),
  78. $to_user_id,
  79. $videoChatLink,
  80. false,
  81. false
  82. );
  83. echo Display::tag('p', $videoChatLink, ['class' => 'lead']);
  84. break;
  85. case 'notify_not_support':
  86. $chat->send(api_get_user_id(), $to_user_id, get_lang('TheXUserBrowserDoesNotSupportWebRTC'));
  87. break;
  88. default:
  89. echo '';
  90. }
  91. exit;