| Total Complexity | 79 |
| Total Lines | 703 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CourseChatUtils often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CourseChatUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class CourseChatUtils |
||
| 24 | { |
||
| 25 | private $groupId; |
||
| 26 | private $courseId; |
||
| 27 | private $sessionId; |
||
| 28 | private $userId; |
||
| 29 | |||
| 30 | /** @var ResourceNode */ |
||
| 31 | private $resourceNode; |
||
| 32 | |||
| 33 | /** @var ResourceRepository */ |
||
| 34 | private $repository; |
||
| 35 | |||
| 36 | /** Debug flag */ |
||
| 37 | private $debug = false; |
||
| 38 | |||
| 39 | public function __construct($courseId, $userId, $sessionId, $groupId, ResourceNode $resourceNode, ResourceRepository $repository) |
||
| 40 | { |
||
| 41 | $this->courseId = (int) $courseId; |
||
| 42 | $this->userId = (int) $userId; |
||
| 43 | $this->sessionId = (int) $sessionId; |
||
| 44 | $this->groupId = (int) $groupId; |
||
| 45 | $this->resourceNode = $resourceNode; |
||
| 46 | $this->repository = $repository; |
||
| 47 | |||
| 48 | $this->dbg('construct', [ |
||
| 49 | 'courseId' => $courseId, |
||
| 50 | 'userId' => $userId, |
||
| 51 | 'sessionId' => $sessionId, |
||
| 52 | 'groupId' => $groupId, |
||
| 53 | 'parentNodeId' => $resourceNode->getId() ?? null, |
||
| 54 | 'repo' => get_class($repository), |
||
| 55 | ]); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** Simple debug helper */ |
||
| 59 | private function dbg(string $msg, array $ctx = []): void |
||
| 60 | { |
||
| 61 | if (!$this->debug) { return; } |
||
| 62 | $line = '[CourseChat] '.$msg; |
||
| 63 | if ($ctx) { $line .= ' | '.json_encode($ctx, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); } |
||
|
|
|||
| 64 | error_log($line); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** Build a slug out of the file title (matches our nodes like: messages-...-log-html) */ |
||
| 68 | private function makeSlug(string $fileTitle): string |
||
| 69 | { |
||
| 70 | $slug = strtolower($fileTitle); |
||
| 71 | $slug = strtr($slug, ['.' => '-']); |
||
| 72 | $slug = preg_replace('~[^a-z0-9\-\_]+~', '-', $slug); |
||
| 73 | $slug = preg_replace('~-+~', '-', $slug); |
||
| 74 | return trim($slug, '-'); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** Build a base name by day and scope (all / session / group / 1:1) */ |
||
| 78 | private function buildBasename(int $friendId = 0): string |
||
| 79 | { |
||
| 80 | $dateNow = date('Y-m-d'); |
||
| 81 | $basename = 'messages-'.$dateNow; |
||
| 82 | |||
| 83 | if ($this->groupId && !$friendId) { |
||
| 84 | $basename .= '_gid-'.$this->groupId; |
||
| 85 | } elseif ($this->sessionId && !$friendId) { |
||
| 86 | $basename .= '_sid-'.$this->sessionId; |
||
| 87 | } elseif ($friendId) { |
||
| 88 | // stable order for 1:1 (smallest id first) |
||
| 89 | $basename .= ($this->userId < $friendId) |
||
| 90 | ? '_uid-'.$this->userId.'-'.$friendId |
||
| 91 | : '_uid-'.$friendId.'-'.$this->userId; |
||
| 92 | } |
||
| 93 | return $basename; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** Returns [fileTitle, slug] */ |
||
| 97 | private function buildNames(int $friendId = 0): array |
||
| 98 | { |
||
| 99 | $fileTitle = $this->buildBasename($friendId).'-log.html'; |
||
| 100 | $slug = $this->makeSlug($fileTitle); |
||
| 101 | return [$fileTitle, $slug]; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** Create node + conversation + empty file (used only from saveMessage under a lock) */ |
||
| 105 | private function createNodeWithResource(string $fileTitle, string $slug, ResourceNode $parentNode): ResourceNode |
||
| 106 | { |
||
| 107 | $em = Database::getManager(); |
||
| 108 | |||
| 109 | $this->dbg('node.create.start', ['slug' => $slug, 'title' => $fileTitle, 'parent' => $parentNode->getId()]); |
||
| 110 | |||
| 111 | // temporary empty file |
||
| 112 | $h = tmpfile(); |
||
| 113 | fwrite($h, ''); |
||
| 114 | $meta = stream_get_meta_data($h); |
||
| 115 | $uploaded = new UploadedFile($meta['uri'], $fileTitle, 'text/html', null, true); |
||
| 116 | |||
| 117 | // conversation |
||
| 118 | $conversation = new CChatConversation(); |
||
| 119 | if (method_exists($conversation, 'setTitle')) { |
||
| 120 | $conversation->setTitle($fileTitle); |
||
| 121 | } else { |
||
| 122 | $conversation->setResourceName($fileTitle); |
||
| 123 | } |
||
| 124 | $conversation->setParentResourceNode($parentNode->getId()); |
||
| 125 | |||
| 126 | // node |
||
| 127 | $node = new ResourceNode(); |
||
| 128 | $node->setTitle($fileTitle); |
||
| 129 | $node->setSlug($slug); |
||
| 130 | $node->setResourceType($parentNode->getResourceType()); |
||
| 131 | $node->setCreator(api_get_user_entity(api_get_user_id())); |
||
| 132 | $node->setParent($parentNode); |
||
| 133 | |||
| 134 | if (method_exists($conversation, 'setResourceNode')) { |
||
| 135 | $conversation->setResourceNode($node); |
||
| 136 | } |
||
| 137 | |||
| 138 | $em->persist($node); |
||
| 139 | $em->persist($conversation); |
||
| 140 | |||
| 141 | // attach file |
||
| 142 | $this->repository->addFile($conversation, $uploaded); |
||
| 143 | |||
| 144 | // publish |
||
| 145 | $course = api_get_course_entity(); |
||
| 146 | $session = api_get_session_entity(); |
||
| 147 | $group = api_get_group_entity(); |
||
| 148 | $conversation->setParent($course); |
||
| 149 | $conversation->addCourseLink($course, $session, $group); |
||
| 150 | |||
| 151 | $em->flush(); |
||
| 152 | |||
| 153 | $this->dbg('node.create.ok', ['nodeId' => $node->getId()]); |
||
| 154 | |||
| 155 | return $node; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** Sanitize and convert message to safe HTML */ |
||
| 159 | public function prepareMessage($message) |
||
| 160 | { |
||
| 161 | $this->dbg('prepareMessage.in', ['len' => strlen((string) $message)]); |
||
| 162 | if (empty($message)) { |
||
| 163 | return ''; |
||
| 164 | } |
||
| 165 | |||
| 166 | $message = trim($message); |
||
| 167 | $message = nl2br($message); |
||
| 168 | $message = Security::remove_XSS($message); |
||
| 169 | |||
| 170 | // url -> anchor |
||
| 171 | $message = preg_replace( |
||
| 172 | '@((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@', |
||
| 173 | '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>', |
||
| 174 | $message |
||
| 175 | ); |
||
| 176 | // add http:// when missing |
||
| 177 | $message = preg_replace( |
||
| 178 | '/<a\s[^>]*href\s*=\s*"((?!https?:\/\/)[^"]*)"[^>]*>/i', |
||
| 179 | '<a href="http://$1" target="_blank" rel="noopener noreferrer">', |
||
| 180 | $message |
||
| 181 | ); |
||
| 182 | |||
| 183 | $message = MarkdownExtra::defaultTransform($message); |
||
| 184 | $this->dbg('prepareMessage.out', ['len' => strlen($message)]); |
||
| 185 | |||
| 186 | return $message; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Return the latest node (by id DESC) matching title or slug under the same parent. |
||
| 191 | * Read-only; does not create. |
||
| 192 | */ |
||
| 193 | private function findExistingNode(string $fileTitle, string $slug, ResourceNode $parentNode): ?ResourceNode |
||
| 209 | ); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Append a message to the last daily file (chronological order). |
||
| 214 | */ |
||
| 215 | public function saveMessage($message, $friendId = 0) |
||
| 216 | { |
||
| 217 | $this->dbg('saveMessage.in', ['friendId' => (int)$friendId, 'rawLen' => strlen((string)$message)]); |
||
| 218 | if (!is_string($message) || trim($message) === '') { return false; } |
||
| 219 | |||
| 220 | // names (one file per day/scope) |
||
| 221 | [$fileTitle, $slug] = $this->buildNames((int)$friendId); |
||
| 222 | |||
| 223 | $em = Database::getManager(); |
||
| 224 | /** @var ResourceNodeRepository $nodeRepo */ |
||
| 225 | $nodeRepo = $em->getRepository(ResourceNode::class); |
||
| 226 | $convRepo = $em->getRepository(CChatConversation::class); |
||
| 227 | $rfRepo = $em->getRepository(ResourceFile::class); |
||
| 228 | |||
| 229 | // parent (chat root) |
||
| 230 | $parent = $nodeRepo->find($this->resourceNode->getId()); |
||
| 231 | if (!$parent) { $this->dbg('saveMessage.error.noParent'); return false; } |
||
| 232 | |||
| 233 | // serialize writers for the same daily file (parent+slug) |
||
| 234 | $lockPath = sys_get_temp_dir().'/ch_chat_lock_'.$parent->getId().'_'.$slug.'.lock'; |
||
| 235 | $lockH = @fopen($lockPath, 'c'); |
||
| 236 | if ($lockH) { @flock($lockH, LOCK_EX); } |
||
| 237 | |||
| 238 | try { |
||
| 239 | // latest node for this day/scope (title OR slug) |
||
| 240 | $qb = $em->createQueryBuilder(); |
||
| 241 | $qb->select('n') |
||
| 242 | ->from(ResourceNode::class, 'n') |
||
| 243 | ->where('n.parent = :parent AND (n.title = :title OR n.slug = :slug)') |
||
| 244 | ->setParameter('parent', $parent) |
||
| 245 | ->setParameter('title', $fileTitle) |
||
| 246 | ->setParameter('slug', $slug) |
||
| 247 | ->orderBy('n.createdAt', 'DESC') |
||
| 248 | ->addOrderBy('n.id', 'DESC') |
||
| 249 | ->setMaxResults(1); |
||
| 250 | /** @var ResourceNode|null $node */ |
||
| 251 | $node = $qb->getQuery()->getOneOrNullResult(); |
||
| 252 | |||
| 253 | // create node + conversation once |
||
| 254 | if (!$node) { |
||
| 255 | $conversation = new CChatConversation(); |
||
| 256 | (method_exists($conversation, 'setTitle') |
||
| 257 | ? $conversation->setTitle($fileTitle) |
||
| 258 | : $conversation->setResourceName($fileTitle)); |
||
| 259 | $conversation->setParentResourceNode($parent->getId()); |
||
| 260 | |||
| 261 | $node = new ResourceNode(); |
||
| 262 | $node->setTitle($fileTitle); |
||
| 263 | $node->setSlug($slug); |
||
| 264 | $node->setResourceType($parent->getResourceType()); |
||
| 265 | $node->setCreator(api_get_user_entity(api_get_user_id())); |
||
| 266 | $node->setParent($parent); |
||
| 267 | |||
| 268 | if (method_exists($conversation, 'setResourceNode')) { |
||
| 269 | $conversation->setResourceNode($node); |
||
| 270 | } |
||
| 271 | |||
| 272 | $em->persist($node); |
||
| 273 | $em->persist($conversation); |
||
| 274 | |||
| 275 | $course = api_get_course_entity(); |
||
| 276 | $session = api_get_session_entity(); |
||
| 277 | $group = api_get_group_entity(); |
||
| 278 | $conversation->setParent($course); |
||
| 279 | $conversation->addCourseLink( |
||
| 280 | $course, $session, $group |
||
| 281 | ); |
||
| 282 | |||
| 283 | $em->flush(); |
||
| 284 | } |
||
| 285 | |||
| 286 | // ensure conversation exists for node |
||
| 287 | $conversation = $convRepo->findOneBy(['resourceNode' => $node]); |
||
| 288 | if (!$conversation) { |
||
| 289 | $conversation = new CChatConversation(); |
||
| 290 | (method_exists($conversation, 'setTitle') |
||
| 291 | ? $conversation->setTitle($fileTitle) |
||
| 292 | : $conversation->setResourceName($fileTitle)); |
||
| 293 | $conversation->setParentResourceNode($parent->getId()); |
||
| 294 | if (method_exists($conversation, 'setResourceNode')) { |
||
| 295 | $conversation->setResourceNode($node); |
||
| 296 | } |
||
| 297 | $em->persist($conversation); |
||
| 298 | |||
| 299 | $course = api_get_course_entity(); |
||
| 300 | $session = api_get_session_entity(); |
||
| 301 | $group = api_get_group_entity(); |
||
| 302 | $conversation->setParent($course); |
||
| 303 | $conversation->addCourseLink( |
||
| 304 | $course, $session, $group |
||
| 305 | ); |
||
| 306 | |||
| 307 | $em->flush(); |
||
| 308 | } |
||
| 309 | |||
| 310 | // build message bubble |
||
| 311 | $user = api_get_user_entity($this->userId); |
||
| 312 | $isMaster = api_is_course_admin(); |
||
| 313 | $timeNow = date('d/m/y H:i:s'); |
||
| 314 | $userPhoto = \UserManager::getUserPicture($this->userId); |
||
| 315 | $htmlMsg = $this->prepareMessage($message); |
||
| 316 | |||
| 317 | $bubble = $isMaster |
||
| 318 | ? '<div class="message-teacher"><div class="content-message"><div class="chat-message-block-name">' |
||
| 319 | .\UserManager::formatUserFullName($user).'</div><div class="chat-message-block-content">' |
||
| 320 | .$htmlMsg.'</div><div class="message-date">'.$timeNow |
||
| 321 | .'</div></div><div class="icon-message"></div><img class="chat-image" src="'.$userPhoto.'"></div>' |
||
| 322 | : '<div class="message-student"><img class="chat-image" src="'.$userPhoto.'"><div class="icon-message"></div>' |
||
| 323 | .'<div class="content-message"><div class="chat-message-block-name">'.\UserManager::formatUserFullName($user) |
||
| 324 | .'</div><div class="chat-message-block-content">'.$htmlMsg.'</div><div class="message-date">' |
||
| 325 | .$timeNow.'</div></div></div>'; |
||
| 326 | |||
| 327 | // always target latest ResourceFile for today (by id desc) |
||
| 328 | $rfQb = $em->createQueryBuilder(); |
||
| 329 | $rf = $rfQb->select('rf') |
||
| 330 | ->from(ResourceFile::class, 'rf') |
||
| 331 | ->where('rf.resourceNode = :node AND rf.originalName = :name') |
||
| 332 | ->setParameter('node', $node) |
||
| 333 | ->setParameter('name', $fileTitle) |
||
| 334 | ->orderBy('rf.id', 'DESC') |
||
| 335 | ->setMaxResults(1) |
||
| 336 | ->getQuery() |
||
| 337 | ->getOneOrNullResult(); |
||
| 338 | |||
| 339 | // read current content and append |
||
| 340 | $existing = ''; |
||
| 341 | if ($rf) { |
||
| 342 | try { $existing = $nodeRepo->getResourceNodeFileContent($node, $rf) ?? ''; } |
||
| 343 | catch (\Throwable $e) { $existing = ''; } |
||
| 344 | } |
||
| 345 | $newContent = $existing.$bubble; |
||
| 346 | |||
| 347 | // write back (reuse same physical path) |
||
| 348 | if ($rf) { |
||
| 349 | $fs = $nodeRepo->getFileSystem(); |
||
| 350 | $fileName = $nodeRepo->getFilename($rf); |
||
| 351 | if ($fs->fileExists($fileName)) { $fs->delete($fileName); } |
||
| 352 | $fs->write($fileName, $newContent); |
||
| 353 | if (method_exists($rf, 'setSize')) { $rf->setSize(strlen($newContent)); $em->persist($rf); } |
||
| 354 | $em->flush(); |
||
| 355 | } else { |
||
| 356 | // first write of the day → create the ResourceFile with the whole content |
||
| 357 | if (method_exists($this->repository, 'addFileFromString')) { |
||
| 358 | $this->repository->addFileFromString($conversation, $fileTitle, 'text/html', $newContent, true); |
||
| 359 | $em->flush(); |
||
| 360 | } else { |
||
| 361 | $h = tmpfile(); fwrite($h, $newContent); |
||
| 362 | $meta = stream_get_meta_data($h); |
||
| 363 | $uploaded = new UploadedFile( |
||
| 364 | $meta['uri'], $fileTitle, 'text/html', null, true |
||
| 365 | ); |
||
| 366 | $this->repository->addFile($conversation, $uploaded); |
||
| 367 | $em->flush(); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | $this->dbg('saveMessage.append.ok', ['nodeId' => $node->getId(), 'bytes' => strlen($newContent)]); |
||
| 372 | return true; |
||
| 373 | |||
| 374 | } catch (\Throwable $e) { |
||
| 375 | $this->dbg('saveMessage.error', ['err' => $e->getMessage()]); |
||
| 376 | return false; |
||
| 377 | } finally { |
||
| 378 | if ($lockH) { @flock($lockH, LOCK_UN); @fclose($lockH); } |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Read the last daily file HTML (optionally reset it). |
||
| 384 | */ |
||
| 385 | public function readMessages($reset = false, $friendId = 0) |
||
| 386 | { |
||
| 387 | [$fileTitle, $slug] = $this->buildNames((int)$friendId); |
||
| 388 | |||
| 389 | $this->dbg('readMessages.in', [ |
||
| 390 | 'friendId' => (int)$friendId, |
||
| 391 | 'reset' => (bool)$reset, |
||
| 392 | 'file' => $fileTitle, |
||
| 393 | 'slug' => $slug, |
||
| 394 | ]); |
||
| 395 | |||
| 396 | $em = \Database::getManager(); |
||
| 397 | /** @var ResourceNodeRepository $nodeRepo */ |
||
| 398 | $nodeRepo = $em->getRepository(ResourceNode::class); |
||
| 399 | |||
| 400 | $parent = $nodeRepo->find($this->resourceNode->getId()); |
||
| 401 | if (!$parent) { $this->dbg('readMessages.error.noParent'); return ''; } |
||
| 402 | |||
| 403 | // read-only: do not create |
||
| 404 | $node = $this->findExistingNode($fileTitle, $slug, $parent); |
||
| 405 | if (!$node) { $this->dbg('readMessages.notfound'); return ''; } |
||
| 406 | |||
| 407 | // locate the same ResourceFile by originalName (latest id desc) |
||
| 408 | $rfRepo = $em->getRepository(ResourceFile::class); |
||
| 409 | /** @var ResourceFile|null $rf */ |
||
| 410 | $rf = $rfRepo->findOneBy( |
||
| 411 | ['resourceNode' => $node, 'originalName' => $fileTitle], |
||
| 412 | ['id' => 'DESC'] |
||
| 413 | ); |
||
| 414 | |||
| 415 | // optional reset |
||
| 416 | if ($reset) { |
||
| 417 | $target = $rf ?: ($node->getResourceFiles()->first() ?: null); |
||
| 418 | if ($target) { |
||
| 419 | $fs = $nodeRepo->getFileSystem(); |
||
| 420 | $fileName = $nodeRepo->getFilename($target); |
||
| 421 | if ($fs->fileExists($fileName)) { |
||
| 422 | $fs->delete($fileName); |
||
| 423 | $fs->write($fileName, ''); |
||
| 424 | } |
||
| 425 | if (method_exists($target, 'setSize')) { $target->setSize(0); $em->persist($target); } |
||
| 426 | $em->flush(); |
||
| 427 | $this->dbg('readMessages.reset.ok', ['nodeId' => $node->getId(), 'rfId' => $target->getId()]); |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | try { |
||
| 432 | // primary: exact RF by originalName |
||
| 433 | if ($rf) { |
||
| 434 | $html = $nodeRepo->getResourceNodeFileContent($node, $rf); |
||
| 435 | $this->dbg('readMessages.out.byOriginalName', [ |
||
| 436 | 'nodeId' => $node->getId(), |
||
| 437 | 'rfId' => $rf->getId(), |
||
| 438 | 'bytes' => strlen($html ?? ''), |
||
| 439 | ]); |
||
| 440 | return $html ?? ''; |
||
| 441 | } |
||
| 442 | |||
| 443 | // fallback: first attached file (covers legacy hashed names) |
||
| 444 | $html = $nodeRepo->getResourceNodeFileContent($node); |
||
| 445 | $this->dbg('readMessages.out.fallbackFirst', [ |
||
| 446 | 'nodeId' => $node->getId(), |
||
| 447 | 'bytes' => strlen($html ?? ''), |
||
| 448 | ]); |
||
| 449 | return $html ?? ''; |
||
| 450 | |||
| 451 | } catch (\Throwable $e) { |
||
| 452 | $this->dbg('readMessages.read.error', ['err' => $e->getMessage()]); |
||
| 453 | return ''; |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | /** Force a user to exit all course chat connections */ |
||
| 458 | public static function exitChat($userId) |
||
| 459 | { |
||
| 460 | $listCourse = CourseManager::get_courses_list_by_user_id($userId); |
||
| 461 | foreach ($listCourse as $course) { |
||
| 462 | Database::getManager() |
||
| 463 | ->createQuery(' |
||
| 464 | DELETE FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 465 | WHERE ccc.cId = :course AND ccc.userId = :user |
||
| 466 | ') |
||
| 467 | ->execute([ |
||
| 468 | 'course' => intval($course['real_id']), |
||
| 469 | 'user' => intval($userId), |
||
| 470 | ]); |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | /** Remove inactive connections (simple heartbeat) */ |
||
| 475 | public function disconnectInactiveUsers(): void |
||
| 476 | { |
||
| 477 | $em = Database::getManager(); |
||
| 478 | $extraCondition = $this->groupId |
||
| 479 | ? "AND ccc.toGroupId = {$this->groupId}" |
||
| 480 | : "AND ccc.sessionId = {$this->sessionId}"; |
||
| 481 | |||
| 482 | $connectedUsers = $em |
||
| 483 | ->createQuery(" |
||
| 484 | SELECT ccc FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 485 | WHERE ccc.cId = :course $extraCondition |
||
| 486 | ") |
||
| 487 | ->setParameter('course', $this->courseId) |
||
| 488 | ->getResult(); |
||
| 489 | |||
| 490 | $now = new \DateTime(api_get_utc_datetime(), new \DateTimeZone('UTC')); |
||
| 491 | $nowTs = $now->getTimestamp(); |
||
| 492 | |||
| 493 | /** @var CChatConnected $connection */ |
||
| 494 | foreach ($connectedUsers as $connection) { |
||
| 495 | $lastTs = $connection->getLastConnection()->getTimestamp(); |
||
| 496 | if (0 !== strcmp($now->format('Y-m-d'), $connection->getLastConnection()->format('Y-m-d'))) { |
||
| 497 | continue; |
||
| 498 | } |
||
| 499 | if (($nowTs - $lastTs) <= 5) { |
||
| 500 | continue; |
||
| 501 | } |
||
| 502 | |||
| 503 | $em |
||
| 504 | ->createQuery(' |
||
| 505 | DELETE FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 506 | WHERE ccc.cId = :course AND ccc.userId = :user AND ccc.toGroupId = :group |
||
| 507 | ') |
||
| 508 | ->execute([ |
||
| 509 | 'course' => $this->courseId, |
||
| 510 | 'user' => $connection->getUserId(), |
||
| 511 | 'group' => $this->groupId, |
||
| 512 | ]); |
||
| 513 | } |
||
| 514 | } |
||
| 515 | |||
| 516 | /** Keep (or create) the "connected" record for current user */ |
||
| 517 | public function keepUserAsConnected(): void |
||
| 518 | { |
||
| 519 | $em = Database::getManager(); |
||
| 520 | $extraCondition = $this->groupId |
||
| 521 | ? 'AND ccc.toGroupId = '.$this->groupId |
||
| 522 | : 'AND ccc.sessionId = '.$this->sessionId; |
||
| 523 | |||
| 524 | $currentTime = new \DateTime(api_get_utc_datetime(), new \DateTimeZone('UTC')); |
||
| 525 | |||
| 526 | /** @var CChatConnected|null $connection */ |
||
| 527 | $connection = $em |
||
| 528 | ->createQuery(" |
||
| 529 | SELECT ccc FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 530 | WHERE ccc.userId = :user AND ccc.cId = :course $extraCondition |
||
| 531 | ") |
||
| 532 | ->setParameters([ |
||
| 533 | 'user' => $this->userId, |
||
| 534 | 'course' => $this->courseId, |
||
| 535 | ]) |
||
| 536 | ->getOneOrNullResult(); |
||
| 537 | |||
| 538 | if ($connection) { |
||
| 539 | $connection->setLastConnection($currentTime); |
||
| 540 | $em->persist($connection); |
||
| 541 | $em->flush(); |
||
| 542 | return; |
||
| 543 | } |
||
| 544 | |||
| 545 | $connection = new CChatConnected(); |
||
| 546 | $connection |
||
| 547 | ->setCId($this->courseId) |
||
| 548 | ->setUserId($this->userId) |
||
| 549 | ->setLastConnection($currentTime) |
||
| 550 | ->setSessionId($this->sessionId) |
||
| 551 | ->setToGroupId($this->groupId); |
||
| 552 | |||
| 553 | $em->persist($connection); |
||
| 554 | $em->flush(); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** Legacy helper (kept for BC) */ |
||
| 558 | public function getFileName($absolute = false, $friendId = 0): string |
||
| 559 | { |
||
| 560 | $base = $this->buildBasename((int)$friendId).'.log.html'; |
||
| 561 | if (!$absolute) { return $base; } |
||
| 562 | |||
| 563 | $document_path = '/document'; |
||
| 564 | $chatPath = $document_path.'/chat_files/'; |
||
| 565 | |||
| 566 | if ($this->groupId) { |
||
| 567 | $group_info = GroupManager::get_group_properties($this->groupId); |
||
| 568 | $chatPath = $document_path.$group_info['directory'].'/chat_files/'; |
||
| 569 | } |
||
| 570 | |||
| 571 | return $chatPath.$base; |
||
| 572 | } |
||
| 573 | |||
| 574 | /** Count users online (simple 5s heartbeat window) */ |
||
| 575 | public function countUsersOnline(): int |
||
| 576 | { |
||
| 577 | $date = new \DateTime(api_get_utc_datetime(), new \DateTimeZone('UTC')); |
||
| 578 | $date->modify('-5 seconds'); |
||
| 579 | |||
| 580 | $extraCondition = $this->groupId |
||
| 581 | ? 'AND ccc.toGroupId = '.$this->groupId |
||
| 582 | : 'AND ccc.sessionId = '.$this->sessionId; |
||
| 583 | |||
| 584 | $number = Database::getManager() |
||
| 585 | ->createQuery(" |
||
| 586 | SELECT COUNT(ccc.userId) FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 587 | WHERE ccc.lastConnection > :date AND ccc.cId = :course $extraCondition |
||
| 588 | ") |
||
| 589 | ->setParameters([ |
||
| 590 | 'date' => $date, |
||
| 591 | 'course' => $this->courseId, |
||
| 592 | ]) |
||
| 593 | ->getSingleScalarResult(); |
||
| 594 | |||
| 595 | return (int) $number; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** Return basic info for connected/eligible users */ |
||
| 599 | public function listUsersOnline(): array |
||
| 600 | { |
||
| 601 | $subscriptions = $this->getUsersSubscriptions(); |
||
| 602 | $usersInfo = []; |
||
| 603 | |||
| 604 | if ($this->groupId) { |
||
| 605 | /** @var User $groupUser */ |
||
| 606 | foreach ($subscriptions as $groupUser) { |
||
| 607 | $usersInfo[] = $this->formatUser($groupUser, $groupUser->getStatus()); |
||
| 608 | } |
||
| 609 | } else { |
||
| 610 | /** @var CourseRelUser|SessionRelCourseRelUser $subscription */ |
||
| 611 | foreach ($subscriptions as $subscription) { |
||
| 612 | $user = $subscription->getUser(); |
||
| 613 | $usersInfo[] = $this->formatUser( |
||
| 614 | $user, |
||
| 615 | $this->sessionId ? $user->getStatus() : $subscription->getStatus() |
||
| 616 | ); |
||
| 617 | } |
||
| 618 | } |
||
| 619 | |||
| 620 | return $usersInfo; |
||
| 621 | } |
||
| 622 | |||
| 623 | /** Normalize user card info */ |
||
| 624 | private function formatUser(User $user, $status): array |
||
| 625 | { |
||
| 626 | return [ |
||
| 627 | 'id' => $user->getId(), |
||
| 628 | 'firstname' => $user->getFirstname(), |
||
| 629 | 'lastname' => $user->getLastname(), |
||
| 630 | 'status' => $status, |
||
| 631 | 'image_url' => UserManager::getUserPicture($user->getId()), |
||
| 632 | 'profile_url' => api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$user->getId(), |
||
| 633 | 'complete_name' => UserManager::formatUserFullName($user), |
||
| 634 | 'username' => $user->getUsername(), |
||
| 635 | 'email' => $user->getEmail(), |
||
| 636 | 'isConnected' => $this->userIsConnected($user->getId()), |
||
| 637 | ]; |
||
| 638 | } |
||
| 639 | |||
| 640 | /** Fetch subscriptions (course / session / group) */ |
||
| 641 | private function getUsersSubscriptions() |
||
| 700 | }); |
||
| 701 | } |
||
| 702 | |||
| 703 | /** Quick online check for one user */ |
||
| 704 | private function userIsConnected($userId): int |
||
| 705 | { |
||
| 706 | $date = new \DateTime(api_get_utc_datetime(), new \DateTimeZone('UTC')); |
||
| 726 | } |
||
| 727 | } |
||
| 728 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.