course_chat.ajax.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls for course chat.
  5. */
  6. require_once __DIR__.'/../global.inc.php';
  7. if (!api_protect_course_script(false)) {
  8. exit;
  9. }
  10. $courseId = api_get_course_int_id();
  11. $userId = api_get_user_id();
  12. $sessionId = api_get_session_id();
  13. $groupId = api_get_group_id();
  14. $json = ['status' => false];
  15. $courseChatUtils = new CourseChatUtils($courseId, $userId, $sessionId, $groupId);
  16. switch ($_REQUEST['action']) {
  17. case 'chat_logout':
  18. $logInfo = [
  19. 'tool' => TOOL_CHAT,
  20. 'tool_id' => 0,
  21. 'tool_id_detail' => 0,
  22. 'action' => 'exit',
  23. 'action_details' => 'exit-chat',
  24. 'info' => '',
  25. ];
  26. Event::registerLog($logInfo);
  27. break;
  28. case 'track':
  29. $courseChatUtils->keepUserAsConnected();
  30. $courseChatUtils->disconnectInactiveUsers();
  31. $friend = isset($_REQUEST['friend']) ? (int) $_REQUEST['friend'] : 0;
  32. $filePath = $courseChatUtils->getFileName(true, $friend);
  33. $newFileSize = file_exists($filePath) ? filesize($filePath) : 0;
  34. $oldFileSize = isset($_GET['size']) ? (int) $_GET['size'] : -1;
  35. $newUsersOnline = $courseChatUtils->countUsersOnline();
  36. $oldUsersOnline = isset($_GET['users_online']) ? (int) $_GET['users_online'] : 0;
  37. $json = [
  38. 'status' => true,
  39. 'data' => [
  40. 'oldFileSize' => file_exists($filePath) ? filesize($filePath) : 0,
  41. 'history' => $newFileSize !== $oldFileSize ? $courseChatUtils->readMessages(false, $friend) : null,
  42. 'usersOnline' => $newUsersOnline,
  43. 'userList' => $newUsersOnline != $oldUsersOnline ? $courseChatUtils->listUsersOnline() : null,
  44. 'currentFriend' => $friend,
  45. ],
  46. ];
  47. break;
  48. case 'preview':
  49. $json = [
  50. 'status' => true,
  51. 'data' => [
  52. 'message' => CourseChatUtils::prepareMessage($_REQUEST['message']),
  53. ],
  54. ];
  55. break;
  56. case 'reset':
  57. $friend = isset($_REQUEST['friend']) ? (int) $_REQUEST['friend'] : 0;
  58. $json = [
  59. 'status' => true,
  60. 'data' => $courseChatUtils->readMessages(true, $friend),
  61. ];
  62. break;
  63. case 'write':
  64. $friend = isset($_REQUEST['friend']) ? (int) $_REQUEST['friend'] : 0;
  65. $writed = $courseChatUtils->saveMessage($_POST['message'], $friend);
  66. $json = [
  67. 'status' => $writed,
  68. 'data' => [
  69. 'writed' => $writed,
  70. ],
  71. ];
  72. break;
  73. }
  74. header('Content-Type: application/json');
  75. echo json_encode($json);