ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilChatroom.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
34 {
35  public const ROOM_INVITATION = 'invitation_to_room';
36  private static string $settingsTable = 'chatroom_settings';
37  private static string $historyTable = 'chatroom_history';
38  private static string $userTable = 'chatroom_users';
39  private static string $sessionTable = 'chatroom_sessions';
40  private static string $banTable = 'chatroom_bans';
41  private static string $uploadTable = 'chatroom_uploads';
42  private array $settings = [];
49  private array $availableSettings = [
50  'object_id' => 'integer',
51  'allow_anonymous' => 'boolean',
52  'allow_custom_usernames' => 'boolean',
53  'enable_history' => 'boolean',
54  'autogen_usernames' => 'string',
55  'room_type' => 'string',
56  'display_past_msgs' => 'integer',
57  ];
58  private int $roomId = 0;
59  private ?ilObjChatroom $object = null;
60 
65  public static function checkUserPermissions($permissions, int $ref_id, bool $send_info = true): bool
66  {
67  global $DIC;
68  $main_tpl = $DIC->ui()->mainTemplate();
69 
70  if (!is_array($permissions)) {
71  $permissions = [$permissions];
72  }
73 
74  $hasPermissions = self::checkPermissions($DIC->user()->getId(), $ref_id, $permissions);
75  if (!$hasPermissions && $send_info) {
76  $main_tpl->setOnScreenMessage('failure', $DIC->language()->txt('permission_denied'), true);
77 
78  return false;
79  }
80 
81  return $hasPermissions;
82  }
83 
89  public static function checkPermissionsOfUser(int $usr_id, $permissions, int $ref_id): bool
90  {
91  if (!is_array($permissions)) {
92  $permissions = [$permissions];
93  }
94 
95  return self::checkPermissions($usr_id, $ref_id, $permissions);
96  }
97 
101  protected static function checkPermissions(int $usr_id, int $ref_id, array $permissions): bool
102  {
103  global $DIC;
104 
105  $pub_ref_id = ilObjChatroom::_getPublicRefId();
106 
107  if ($pub_ref_id === $ref_id) {
108  $obj_id = ilObject::_lookupObjId($ref_id);
109  foreach ($permissions as $permission) {
110  $no_access = !$DIC->rbac()->system()->checkAccessOfUser($usr_id, $permission, $ref_id) || (
111  !$DIC->rbac()->system()->checkAccessOfUser($usr_id, 'write', $ref_id) &&
113  in_array($permission, ['read', 'visible'], true)
114  );
115 
116  if ($no_access) {
117  return false;
118  }
119  }
120  } else {
121  foreach ($permissions as $permission) {
122  if (!$DIC->access()->checkAccessOfUser($usr_id, $permission, '', $ref_id)) {
123  return false;
124  }
125  }
126  }
127 
128  return true;
129  }
130 
131  public static function byObjectId(int $object_id): ?ilChatroom
132  {
133  global $DIC;
134 
135  $query = 'SELECT * FROM ' . self::$settingsTable . ' WHERE object_id = %s';
136  $types = [ilDBConstants::T_INTEGER];
137  $values = [$object_id];
138  $rset = $DIC->database()->queryF($query, $types, $values);
139 
140  if ($row = $DIC->database()->fetchAssoc($rset)) {
141  $room = new self();
142  $room->initialize($row);
143  return $room;
144  }
145 
146  return null;
147  }
148 
153  public function initialize(array $rowdata): void
154  {
155  $this->roomId = (int) $rowdata['room_id'];
156 
157  foreach ($this->availableSettings as $setting => $type) {
158  if (isset($rowdata[$setting])) {
159  settype($rowdata[$setting], $this->availableSettings[$setting]);
160  $this->setSetting($setting, $rowdata[$setting]);
161  }
162  }
163  }
164 
169  public function setSetting(string $name, $value): void
170  {
171  $this->settings[$name] = $value;
172  }
173 
174  public static function byRoomId(int $room_id, bool $initObject = false): ?ilChatroom
175  {
176  global $DIC;
177 
178  $query = 'SELECT * FROM ' . self::$settingsTable . ' WHERE room_id = %s';
179 
180  $types = [ilDBConstants::T_INTEGER];
181  $values = [$room_id];
182 
183  $rset = $DIC->database()->queryF($query, $types, $values);
184 
185  if ($row = $DIC->database()->fetchAssoc($rset)) {
186  $room = new self();
187  $room->initialize($row);
188 
189  if ($initObject) {
190  $room->object = ilObjectFactory::getInstanceByObjId((int) $row['object_id']);
191  }
192 
193  return $room;
194  }
195 
196  return null;
197  }
198 
199  public function getDescription(): string
200  {
201  if (!$this->object) {
202  $this->object = ilObjectFactory::getInstanceByObjId((int) $this->getSetting('object_id'));
203  }
204 
205  return $this->object->getDescription();
206  }
207 
208  public function getSetting(string $name)
209  {
210  return $this->settings[$name];
211  }
212 
213  public function save(): void
214  {
215  $this->saveSettings($this->settings);
216  }
217 
218  public function saveSettings(array $settings): void
219  {
220  global $DIC;
221 
222  $localSettings = [];
223 
224  foreach ($this->availableSettings as $setting => $type) {
225  if (array_key_exists($setting, $settings)) {
226  if ($type === 'boolean') {
227  $settings[$setting] = (bool) $settings[$setting];
228  }
229  $localSettings[$setting] = [$this->phpTypeToMDBType($type), $settings[$setting]];
230  }
231  }
232 
233  if (!isset($localSettings['room_type']) || !$localSettings['room_type'][1]) {
234  $localSettings['room_type'][0] = ilDBConstants::T_TEXT;
235  $localSettings['room_type'][1] = 'repository';
236  }
237 
238  if ($this->roomId > 0) {
239  $DIC->database()->update(
240  self::$settingsTable,
241  $localSettings,
242  ['room_id' => [ilDBConstants::T_INTEGER, $this->roomId]]
243  );
244  } else {
245  $this->roomId = $DIC->database()->nextId(self::$settingsTable);
246 
247  $localSettings['room_id'] = [
250  ];
251 
252  $DIC->database()->insert(self::$settingsTable, $localSettings);
253  }
254  }
255 
256  private function phpTypeToMDBType(string $type): string
257  {
258  return match ($type) {
259  'string' => ilDBConstants::T_TEXT,
260  'boolean' => ilDBConstants::T_INTEGER,
261  default => $type,
262  };
263  }
264 
268  public function addHistoryEntry($message): void
269  {
270  global $DIC;
271 
272  $timestamp = 0;
273  if (is_array($message)) {
274  $timestamp = (int) $message['timestamp'];
275  } elseif (is_object($message)) {
276  $timestamp = (int) $message->timestamp;
277  }
278 
279  $id = $DIC->database()->nextId(self::$historyTable);
280  $DIC->database()->insert(
281  self::$historyTable,
282  [
283  'hist_id' => [ilDBConstants::T_INTEGER, $id],
284  'room_id' => [ilDBConstants::T_INTEGER, $this->roomId],
285  'message' => [ilDBConstants::T_TEXT, json_encode($message, JSON_THROW_ON_ERROR)],
286  'timestamp' => [ilDBConstants::T_INTEGER, ($timestamp > 0 ? $timestamp : time())],
287  ]
288  );
289  }
290 
291  public function connectUser(ilChatroomUser $user): bool
292  {
293  global $DIC;
294 
295  $userdata = [
296  'login' => $user->getUsername(),
297  'id' => $user->getUserId()
298  ];
299 
300  $query = 'SELECT user_id FROM ' . self::$userTable . ' WHERE room_id = %s AND user_id = %s';
302  $values = [$this->roomId, $user->getUserId()];
303 
304  if (!$DIC->database()->fetchAssoc($DIC->database()->queryF($query, $types, $values))) {
305  // Notice: Using replace instead of insert looks strange, because we actually know whether the selected data exists or not
306  // But we occasionally found some duplicate key errors although the data set should not exist when the following code is reached
307  $DIC->database()->replace(
308  self::$userTable,
309  [
310  'room_id' => [ilDBConstants::T_INTEGER, $this->roomId],
311  'user_id' => [ilDBConstants::T_INTEGER, $user->getUserId()]
312  ],
313  [
314  'userdata' => [ilDBConstants::T_TEXT, json_encode($userdata, JSON_THROW_ON_ERROR)],
315  'connected' => [ilDBConstants::T_INTEGER, time()],
316  ]
317  );
318 
319  return true;
320  }
321 
322  return false;
323  }
324 
325  public function getConnectedUsers(bool $only_data = true): array
326  {
327  global $DIC;
328 
329  $query = 'SELECT ' . ($only_data ? 'userdata' : '*') . ' FROM ' . self::$userTable . ' WHERE room_id = %s';
330  $types = [ilDBConstants::T_INTEGER];
331  $values = [$this->roomId];
332  $rset = $DIC->database()->queryF($query, $types, $values);
333  $users = [];
334 
335  while ($row = $DIC->database()->fetchAssoc($rset)) {
336  $users[] = $only_data ? json_decode($row['userdata'], false, 512, JSON_THROW_ON_ERROR) : $row;
337  }
338 
339  return $users;
340  }
341 
342  public function disconnectUser(int $user_id): void
343  {
344  $this->disconnectUsers([$user_id]);
345  }
346 
350  public function disconnectUsers(array $userIds): void
351  {
352  global $DIC;
353 
354  $query = 'SELECT * FROM ' . self::$userTable . ' WHERE room_id = %s AND ' .
355  $DIC->database()->in('user_id', $userIds, false, ilDBConstants::T_INTEGER);
356 
357  $types = [ilDBConstants::T_INTEGER];
358  $values = [$this->roomId];
359  $res = $DIC->database()->queryF($query, $types, $values);
360 
361  if ($row = $DIC->database()->fetchAssoc($res)) {
362  $query = 'DELETE FROM ' . self::$userTable . ' WHERE room_id = %s AND ' .
363  $DIC->database()->in('user_id', $userIds, false, ilDBConstants::T_INTEGER);
364 
365  $types = [ilDBConstants::T_INTEGER];
366  $values = [$this->roomId];
367  $DIC->database()->manipulateF($query, $types, $values);
368 
369  do {
370  if ($this->getSetting('enable_history')) {
371  $id = $DIC->database()->nextId(self::$sessionTable);
372  $DIC->database()->insert(
373  self::$sessionTable,
374  [
375  'sess_id' => [ilDBConstants::T_INTEGER, $id],
376  'room_id' => [ilDBConstants::T_INTEGER, $this->roomId],
377  'user_id' => [ilDBConstants::T_INTEGER, $row['user_id']],
378  'userdata' => [ilDBConstants::T_TEXT, $row['userdata']],
379  'connected' => [ilDBConstants::T_INTEGER, $row['connected']],
380  'disconnected' => [ilDBConstants::T_INTEGER, time()]
381  ]
382  );
383  }
384  } while ($row = $DIC->database()->fetchAssoc($res));
385  }
386  }
387 
388  public function getSettings(): array
389  {
390  return $this->settings;
391  }
392 
393  public function isSubscribed(int $chat_userid): bool
394  {
395  global $DIC;
396 
397  $query = 'SELECT COUNT(user_id) as cnt FROM ' . self::$userTable .
398  ' WHERE room_id = %s AND user_id = %s';
399 
401  $values = [$this->roomId, $chat_userid];
402  $res = $DIC->database()->queryF($query, $types, $values);
403 
404  return ($row = $DIC->database()->fetchAssoc($res)) && (int) $row['cnt'] === 1;
405  }
406 
407  public function getHistory(
408  ilDateTime $from = null,
409  ilDateTime $to = null,
410  int $restricted_session_userid = null,
411  bool $respect_target = true
412  ): array {
413  global $DIC;
414 
415  $join = '';
416 
417  $query =
418  'SELECT historyTable.* ' .
419  'FROM ' . self::$historyTable . ' historyTable ' . $join . ' ' .
420  'WHERE historyTable.room_id = ' . $this->getRoomId();
421 
422  $filter = [];
423 
424  if ($from !== null) {
425  $filter[] = 'timestamp >= ' . $DIC->database()->quote($from->getUnixTime(), ilDBConstants::T_INTEGER);
426  }
427 
428  if ($to !== null) {
429  $filter[] = 'timestamp <= ' . $DIC->database()->quote($to->getUnixTime(), ilDBConstants::T_INTEGER);
430  }
431 
432  if ($filter) {
433  $query .= ' AND ' . implode(' AND ', $filter);
434  }
435  $query .= ' ORDER BY timestamp ASC';
436 
437  $rset = $DIC->database()->query($query);
438  $result = [];
439 
440  while ($row = $DIC->database()->fetchAssoc($rset)) {
441  try {
442  $message = json_decode($row['message'], false, 512, JSON_THROW_ON_ERROR);
443  } catch (JsonException) {
444  $message = null;
445  } finally {
446  if ($message === null) {
447  $message = json_decode('{}', false, 512, JSON_THROW_ON_ERROR);
448  }
449  }
450 
451  $row['message'] = $message;
452  $row['message']->timestamp = $row['timestamp'];
453  if (
454  $respect_target &&
455  property_exists($row['message'], 'target') &&
456  $row['message']->target !== null &&
457  !$row['message']->target->public && (
458  !isset($row['recipients']) ||
459  !in_array($DIC->user()->getId(), explode(',', (string) $row['recipients']), false)
460  )
461  ) {
462  continue;
463  }
464 
465  $result[] = $row;
466  }
467  return $result;
468  }
469 
470  public function getRoomId(): int
471  {
472  return $this->roomId;
473  }
474 
475  public function saveFileUploadToDb(int $user_id, string $filename, string $type): void
476  {
477  global $DIC;
478 
479  $upload_id = $DIC->database()->nextId(self::$uploadTable);
480  $DIC->database()->insert(
481  self::$uploadTable,
482  [
483  'upload_id' => [ilDBConstants::T_INTEGER, $upload_id],
484  'room_id' => [ilDBConstants::T_INTEGER, $this->roomId],
485  'user_id' => [ilDBConstants::T_INTEGER, $user_id],
486  'filename' => [ilDBConstants::T_TEXT, $filename],
487  'filetype' => [ilDBConstants::T_TEXT, $type],
488  'timestamp' => [ilDBConstants::T_INTEGER, time()]
489  ]
490  );
491  }
492 
493  public function banUser(int $user_id, int $actor_id, string $comment = ''): void
494  {
495  global $DIC;
496 
497  $DIC->database()->replace(
498  self::$banTable,
499  [
500  'room_id' => [ilDBConstants::T_INTEGER, $this->roomId],
501  'user_id' => [ilDBConstants::T_INTEGER, $user_id]
502  ],
503  [
504  'actor_id' => [ilDBConstants::T_INTEGER, $actor_id],
505  'timestamp' => [ilDBConstants::T_INTEGER, time()],
506  'remark' => [ilDBConstants::T_TEXT, $comment]
507  ]
508  );
509  }
510 
516  public function unbanUser($user_id): int
517  {
518  global $DIC;
519 
520  if (!is_array($user_id)) {
521  $user_id = [$user_id];
522  }
523 
524  $query = 'DELETE FROM ' . self::$banTable . ' WHERE room_id = %s AND ' . $DIC->database()->in('user_id', $user_id, false, ilDBConstants::T_INTEGER);
525  $types = [ilDBConstants::T_INTEGER];
526  $values = [$this->getRoomId()];
527 
528  return $DIC->database()->manipulateF($query, $types, $values);
529  }
530 
531  public function isUserBanned(int $user_id): bool
532  {
533  global $DIC;
534 
535  $query = 'SELECT COUNT(user_id) cnt FROM ' . self::$banTable . ' WHERE user_id = %s AND room_id = %s';
537  $values = [$user_id, $this->getRoomId()];
538 
539  $res = $DIC->database()->queryF($query, $types, $values);
540 
541  return ($row = $DIC->database()->fetchAssoc($res)) && $row['cnt'];
542  }
543 
544  public function getBannedUsers(): array
545  {
546  global $DIC;
547 
548  $query = 'SELECT chb.* FROM ' . self::$banTable . ' chb INNER JOIN usr_data ud ON chb.user_id = ud.usr_id WHERE chb.room_id = %s ';
549  $types = [ilDBConstants::T_INTEGER];
550  $values = [$this->getRoomId()];
551  $res = $DIC->database()->queryF($query, $types, $values);
552  $result = [];
553 
554  while ($row = $DIC->database()->fetchAssoc($res)) {
555  if ($row['user_id'] > 0) {
556  $user = new ilObjUser((int) $row['user_id']);
557  $userdata = [
558  'user_id' => $user->getId(),
559  'firstname' => $user->getFirstname(),
560  'lastname' => $user->getLastname(),
561  'login' => $user->getLogin(),
562  'timestamp' => (int) $row['timestamp'],
563  'actor_id' => (int) $row['actor_id'],
564  'remark' => $row['remark']
565  ];
566 
567  $result[] = $userdata;
568  }
569  }
570 
571  return $result;
572  }
573 
574  public function getLastSession(ilChatroomUser $user): ?array
575  {
576  global $DIC;
577 
578  $query = 'SELECT * FROM ' . self::$sessionTable . ' WHERE user_id = ' .
579  $DIC->database()->quote($user->getUserId(), ilDBConstants::T_INTEGER) .
580  ' ORDER BY connected DESC';
581 
582  $DIC->database()->setLimit(1);
583  $res = $DIC->database()->query($query);
584 
585  if ($row = $DIC->database()->fetchAssoc($res)) {
586  return $row;
587  }
588 
589  return null;
590  }
591 
592  public function getSessions(ilChatroomUser $user): array
593  {
594  global $DIC;
595 
596  $query = 'SELECT * FROM ' . self::$sessionTable
597  . ' WHERE room_id = ' .
598  $DIC->database()->quote($this->getRoomId(), ilDBConstants::T_INTEGER) .
599  ' ORDER BY connected DESC';
600 
601  $res = $DIC->database()->query($query);
602 
603  $result = [];
604  while ($row = $DIC->database()->fetchAssoc($res)) {
605  $result[] = $row;
606  }
607 
608  return $result;
609  }
610 
615  public function sendInvitationNotification(
616  ?ilChatroomObjectGUI $gui,
617  $sender,
618  int $recipient_id,
619  string $invitationLink = ''
620  ): void {
621  $links = [];
622 
623  if ($gui && $invitationLink === '') {
624  $invitationLink = $this->getChatURL($gui);
625  }
626 
627  $links[] = new ilNotificationLink(
628  new ilNotificationParameter('chat_join', [], 'chatroom'),
629  $invitationLink
630  );
631 
632  if ($recipient_id > 0 && ANONYMOUS_USER_ID !== $recipient_id) {
633  if (is_numeric($sender) && $sender > 0) {
634  $sender_id = $sender;
636  $usr = ilObjectFactory::getInstanceByObjId($sender);
637  $public_name = $usr->getPublicName();
638  } elseif ($sender instanceof ilChatroomUser) {
639  if ($sender->getUserId() > 0) {
640  $sender_id = $sender->getUserId();
641  } else {
642  $sender_id = ANONYMOUS_USER_ID;
643  }
644  $public_name = $sender->getUsername();
645  } else {
646  throw new InvalidArgumentException(
647  '$sender must be an instance of ilChatroomUser or an id of an ilObjUser instance'
648  );
649  }
650 
651  $userLang = ilLanguageFactory::_getLanguageOfUser($recipient_id);
652  $userLang->loadLanguageModule('mail');
653  $bodyParams = [
654  'link' => $invitationLink,
655  'inviter_name' => $public_name,
656  'room_name' => $this->getTitle(),
657  'salutation' => ilMail::getSalutation($recipient_id, $userLang),
658  'BR' => "\n",
659  ];
660 
661  $notification = new ilNotificationConfig(ChatInvitationNotificationProvider::NOTIFICATION_TYPE);
662  $notification->setTitleVar('chat_invitation', $bodyParams, 'chatroom');
663  $notification->setShortDescriptionVar('chat_invitation_short', $bodyParams, 'chatroom');
664  $notification->setLongDescriptionVar('chat_invitation_long', $bodyParams, 'chatroom');
665  $notification->setLinks($links);
666  $notification->setIconPath('templates/default/images/standard/icon_chtr.svg');
667  $notification->setValidForSeconds(ilNotificationConfig::TTL_LONG);
668  $notification->setVisibleForSeconds(ilNotificationConfig::DEFAULT_TTS);
669  $notification->setIdentification(new NotificationIdentification(
670  ChatInvitationNotificationProvider::NOTIFICATION_TYPE,
671  self::ROOM_INVITATION . '_' . $this->getRefIdByRoomId($this->getRoomId()),
672  ));
673  $notification->setHandlerParam('mail.sender', (string) $sender_id);
674 
675  $notification->notifyByUsers([$recipient_id]);
676  }
677  }
678 
679  public function getChatURL(ilChatroomObjectGUI $gui): string
680  {
681  return ilLink::_getStaticLink($gui->getObject()->getRefId(), $gui->getObject()->getType());
682  }
683 
684  public function getTitle(): string
685  {
686  if (!$this->object) {
687  $this->object = ilObjectFactory::getInstanceByObjId((int) $this->getSetting('object_id'));
688  }
689 
690  return $this->object->getTitle();
691  }
692 
693  public function countActiveUsers(): int
694  {
695  global $DIC;
696 
697  $query = 'SELECT COUNT(user_id) cnt FROM ' . self::$userTable . ' WHERE room_id = %s';
698  $types = [ilDBConstants::T_INTEGER];
699  $values = [$this->roomId];
700  $res = $DIC->database()->queryF($query, $types, $values);
701 
702  if ($row = $DIC->database()->fetchAssoc($res)) {
703  return (int) $row['cnt'];
704  }
705 
706  return 0;
707  }
708 
713  public function getAccessibleRoomIdByTitleMap(int $user_id): array
714  {
715  global $DIC;
716 
717  $query = "
718  SELECT room_id, od.title, objr.ref_id
719  FROM object_data od
720  INNER JOIN " . self::$settingsTable . "
721  ON object_id = od.obj_id
722  INNER JOIN object_reference objr
723  ON objr.obj_id = od.obj_id
724  AND objr.deleted IS NULL
725  INNER JOIN tree
726  ON tree.child = objr.ref_id
727  AND tree.tree = %s
728  WHERE od.type = %s
729  ";
730 
732  $values = [1, 'chtr'];
733  $res = $DIC->database()->queryF($query, $types, $values);
734 
735  $rooms = [];
736  while ($row = $DIC->database()->fetchAssoc($res)) {
737  if (self::checkPermissionsOfUser($user_id, 'read', (int) $row['ref_id'])) {
738  $rooms[(int) $row['room_id']] = $row['title'];
739  }
740  }
741 
742  return $rooms;
743  }
744 
745  public function getRefIdByRoomId(int $room_id): int
746  {
747  global $DIC;
748 
749  $query = "
750  SELECT objr.ref_id
751  FROM object_reference objr
752 
753  INNER JOIN chatroom_settings cs
754  ON cs.object_id = objr.obj_id
755 
756  INNER JOIN object_data od
757  ON od.obj_id = cs.object_id
758 
759  WHERE cs.room_id = %s
760  ";
761 
762  $types = [ilDBConstants::T_INTEGER];
763  $values = [$room_id];
764 
765  $res = $DIC->database()->queryF($query, $types, $values);
766 
767  $row = $DIC->database()->fetchAssoc($res);
768 
769  return (int) ($row['ref_id'] ?? 0);
770  }
771 
772  public function getLastMessages(int $number, ilChatroomUser $chatuser): array
773  {
774  global $DIC;
775 
776  // There is currently no way to check if a message is private or not
777  // by sql. So we fetch twice as much as we need and hope that there
778  // are not more than $number private messages.
779  $DIC->database()->setLimit($number);
780  $rset = $DIC->database()->query(
781  'SELECT *
782  FROM ' . self::$historyTable . '
783  WHERE room_id = ' . $DIC->database()->quote($this->roomId, ilDBConstants::T_INTEGER) . '
784  AND (
785  (' . $DIC->database()->like('message', ilDBConstants::T_TEXT, '%"type":"message"%') . ' AND NOT ' . $DIC->database()->like('message', ilDBConstants::T_TEXT, '%"public":0%') . ')
786  OR ' . $DIC->database()->like('message', ilDBConstants::T_TEXT, '%"target":{%"id":"' . $chatuser->getUserId() . '"%') . '
787  OR ' . $DIC->database()->like('message', ilDBConstants::T_TEXT, '%"from":{"id":' . $chatuser->getUserId() . '%') . '
788  )
789  ORDER BY timestamp DESC'
790  );
791 
792  $result_count = 0;
793  $results = [];
794  while (($row = $DIC->database()->fetchAssoc($rset)) && $result_count < $number) {
795  $tmp = json_decode($row['message'], false, 512, JSON_THROW_ON_ERROR);
796  if (property_exists($tmp, 'target') && $tmp->target instanceof stdClass && (int) $tmp->target->public === 0) {
797  if (in_array($chatuser->getUserId(), [(int) $tmp->target->id, (int) $tmp->from->id], true)) {
798  $results[] = $tmp;
799  ++$result_count;
800  }
801  } else {
802  $results[] = $tmp;
803  ++$result_count;
804  }
805  }
806 
807  if ($results !== []) {
808  $rset = $DIC->database()->queryF(
809  'SELECT *
810  FROM ' . self::$historyTable . '
811  WHERE room_id = %s
812  AND ' . $DIC->database()->like('message', ilDBConstants::T_TEXT, '%%"type":"notice"%%') . '
813  AND timestamp <= %s AND timestamp >= %s
814  ORDER BY timestamp DESC',
816  [$this->roomId, $results[0]->timestamp, $results[$result_count - 1]->timestamp]
817  );
818 
819  while (($row = $DIC->database()->fetchAssoc($rset))) {
820  $tmp = json_decode($row['message'], false, 512, JSON_THROW_ON_ERROR);
821  $results[] = $tmp;
822  }
823  }
824 
825  usort($results, static function (stdClass $a, stdClass $b): int {
826  $a_timestamp = strlen((string) $a->timestamp) === 13 ? ((int) substr((string) $a->timestamp, 0, -3)) : $a->timestamp;
827  $b_timestamp = strlen((string) $b->timestamp) === 13 ? ((int) substr((string) $b->timestamp, 0, -3)) : $b->timestamp;
828 
829  return $b_timestamp - $a_timestamp;
830  });
831 
832  return $results;
833  }
834 
835  public function clearMessages(): void
836  {
837  global $DIC;
838 
839  $DIC->database()->queryF(
840  'DELETE FROM ' . self::$historyTable . ' WHERE room_id = %s',
842  [$this->roomId]
843  );
844 
845  $DIC->database()->queryF(
846  'DELETE FROM ' . self::$sessionTable . ' WHERE room_id = %s AND disconnected < %s',
848  [$this->roomId, time()]
849  );
850  }
851 }
static checkUserPermissions($permissions, int $ref_id, bool $send_info=true)
Checks user permissions by given array and ref_id.
static checkPermissions(int $usr_id, int $ref_id, array $permissions)
getSetting(string $name)
$res
Definition: ltiservices.php:69
getUserId()
Returns Ilias User ID.
static string $banTable
const ROOM_INVITATION
const ANONYMOUS_USER_ID
Definition: constants.php:27
description of a localized parameter this information is used locate translations while processing no...
getRefIdByRoomId(int $room_id)
banUser(int $user_id, int $actor_id, string $comment='')
getLastSession(ilChatroomUser $user)
getLastMessages(int $number, ilChatroomUser $chatuser)
array $availableSettings
disconnectUsers(array $userIds)
static string $historyTable
getConnectedUsers(bool $only_data=true)
phpTypeToMDBType(string $type)
setSetting(string $name, $value)
Sets given name and value as setting into $this->settings array.
ilObjChatroom $object
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
saveFileUploadToDb(int $user_id, string $filename, string $type)
getHistory(ilDateTime $from=null, ilDateTime $to=null, int $restricted_session_userid=null, bool $respect_target=true)
$ref_id
Definition: ltiauth.php:67
static string $settingsTable
addHistoryEntry($message)
static checkPermissionsOfUser(int $usr_id, $permissions, int $ref_id)
Checks user permissions in question for a given user id in relation to a given ref_id.
static _getLanguageOfUser(int $a_usr_id)
Get language object of user.
getChatURL(ilChatroomObjectGUI $gui)
isSubscribed(int $chat_userid)
static getSalutation(int $a_usr_id, ?ilLanguage $a_language=null)
getSessions(ilChatroomUser $user)
isUserBanned(int $user_id)
static byRoomId(int $room_id, bool $initObject=false)
$results
unbanUser($user_id)
Deletes entry from banTable matching roomId and given $user_id and returns the number of affected row...
connectUser(ilChatroomUser $user)
$comment
Definition: buildRTE.php:72
$filename
Definition: buildRTE.php:78
Class ilChatroom.
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static _isOffline(int $obj_id)
Type-specific implementation of general status, has to be overwritten if object type does not support...
Class ilChatroomUser.
static byObjectId(int $object_id)
disconnectUser(int $user_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
static string $sessionTable
$message
Definition: xapiexit.php:32
getUsername()
Returns username from Object or SESSION.
static string $userTable
getAccessibleRoomIdByTitleMap(int $user_id)
Fetches and returns a Array<Integer, String> of all accessible repository object chats in the main tr...
initialize(array $rowdata)
Sets $this->roomId by given array $rowdata and calls setSetting method foreach available setting in $...
array int $roomId
saveSettings(array $settings)
static string $uploadTable