course_chat.ajax.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. 'oldFileSize' => file_exists($filePath) ? filesize($filePath) : 0,
  30. 'history' => $newFileSize !== $oldFileSize ? $courseChatUtils->readMessages(false, $friend) : null,
  31. 'usersOnline' => $newUsersOnline,
  32. 'userList' => $newUsersOnline != $oldUsersOnline ? $courseChatUtils->listUsersOnline() : null,
  33. 'currentFriend' => $friend
  34. ]
  35. ];
  36. break;
  37. case 'preview':
  38. $json = [
  39. 'status' => true,
  40. 'data' => [
  41. 'message' => CourseChatUtils::prepareMessage($_REQUEST['message'])
  42. ]
  43. ];
  44. break;
  45. case 'reset':
  46. $friend = isset($_REQUEST['friend']) ? intval($_REQUEST['friend']) : 0;
  47. $json = [
  48. 'status' => true,
  49. 'data' => $courseChatUtils->readMessages(true, $friend)
  50. ];
  51. break;
  52. case 'write':
  53. $friend = isset($_REQUEST['friend']) ? intval($_REQUEST['friend']) : 0;
  54. $writed = $courseChatUtils->saveMessage($_POST['message'], $friend);
  55. $json = [
  56. 'status' => $writed,
  57. 'data' => [
  58. 'writed' => $writed
  59. ]
  60. ];
  61. break;
  62. }
  63. header('Content-Type: application/json');
  64. echo json_encode($json);