course_chat.ajax.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Responses to AJAX calls for course chat
  5. */
  6. require_once '../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 'track':
  18. $courseChatUtils->keepUserAsConnected();
  19. $courseChatUtils->disconnectInactiveUsers();
  20. $friend = isset($_REQUEST['friend']) ? intval($_REQUEST['friend']) : 0;
  21. $filePath = $courseChatUtils->getFileName(true, $friend);
  22. $newFileSize = file_exists($filePath) ? filesize($filePath) : 0;
  23. $oldFileSize = isset($_GET['size']) ? intval($_GET['size']) : -1;
  24. $newUsersOnline = $courseChatUtils->countUsersOnline();
  25. $oldUsersOnline = isset($_GET['users_online']) ? intval($_GET['users_online']) : 0;
  26. $json = [
  27. 'status' => true,
  28. 'data' => [
  29. 'chatIsDenied' => $courseChatUtils->isChatDenied(),
  30. 'oldFileSize' => file_exists($filePath) ? filesize($filePath) : 0,
  31. 'history' => $newFileSize !== $oldFileSize ? $courseChatUtils->readMessages(false, $friend) : null,
  32. 'usersOnline' => $newUsersOnline,
  33. 'userList' => $newUsersOnline != $oldUsersOnline ? $courseChatUtils->listUsersOnline() : null,
  34. 'currentFriend' => $friend
  35. ]
  36. ];
  37. break;
  38. case 'preview':
  39. $json = [
  40. 'status' => true,
  41. 'data' => [
  42. 'message' => CourseChatUtils::prepareMessage($_REQUEST['message'])
  43. ]
  44. ];
  45. break;
  46. case 'reset':
  47. $friend = isset($_REQUEST['friend']) ? intval($_REQUEST['friend']) : 0;
  48. $json = [
  49. 'status' => true,
  50. 'data' => $courseChatUtils->readMessages(true, $friend)
  51. ];
  52. break;
  53. case 'write':
  54. $friend = isset($_REQUEST['friend']) ? intval($_REQUEST['friend']) : 0;
  55. $writed = $courseChatUtils->saveMessage($_POST['message'], $friend);
  56. $json = [
  57. 'status' => $writed,
  58. 'data' => [
  59. 'writed' => $writed
  60. ]
  61. ];
  62. break;
  63. }
  64. header('Content-Type: application/json');
  65. echo json_encode($json);