false]; $courseChatUtils = new CourseChatUtils($courseId, $userId, $sessionId, $groupId); switch ($_REQUEST['action']) { case 'track': $courseChatUtils->keepUserAsConnected(); $courseChatUtils->disconnectInactiveUsers(); $friend = isset($_REQUEST['friend']) ? intval($_REQUEST['friend']) : 0; $filePath = $courseChatUtils->getFileName(true, $friend); $newFileSize = file_exists($filePath) ? filesize($filePath) : 0; $oldFileSize = isset($_GET['size']) ? intval($_GET['size']) : -1; $newUsersOnline = $courseChatUtils->countUsersOnline(); $oldUsersOnline = isset($_GET['users_online']) ? intval($_GET['users_online']) : 0; $json = [ 'status' => true, 'data' => [ 'oldFileSize' => file_exists($filePath) ? filesize($filePath) : 0, 'history' => $newFileSize !== $oldFileSize ? $courseChatUtils->readMessages(false, $friend) : null, 'usersOnline' => $newUsersOnline, 'userList' => $newUsersOnline != $oldUsersOnline ? $courseChatUtils->listUsersOnline() : null, 'currentFriend' => $friend ] ]; break; case 'preview': $json = [ 'status' => true, 'data' => [ 'message' => CourseChatUtils::prepareMessage($_REQUEST['message']) ] ]; break; case 'reset': $friend = isset($_REQUEST['friend']) ? intval($_REQUEST['friend']) : 0; $json = [ 'status' => true, 'data' => $courseChatUtils->readMessages(true, $friend) ]; break; case 'write': $friend = isset($_REQUEST['friend']) ? intval($_REQUEST['friend']) : 0; $writed = $courseChatUtils->saveMessage($_POST['message'], $friend); $json = [ 'status' => $writed, 'data' => [ 'writed' => $writed ] ]; break; } header('Content-Type: application/json'); echo json_encode($json);