ILIAS  release_8 Revision v8.23
Conversation.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ilDBInterface;
26 use ilObjUser;
27 
33 {
34  private ilDBInterface $db;
35  protected ilObjUser $user;
36 
37  public function __construct(ilDBInterface $db, ilObjUser $user)
38  {
39  $this->db = $db;
40  $this->user = $user;
41  }
42 
47  public function findByIds(array $conversationIds): array
48  {
49  $conversations = [];
50 
51  $res = $this->db->query(
52  'SELECT * FROM osc_conversation WHERE ' . $this->db->in(
53  'id',
54  $conversationIds,
55  false,
56  'text'
57  )
58  );
59 
60  while ($row = $this->db->fetchAssoc($res)) {
61  $participants = json_decode($row['participants'], true, 512, JSON_THROW_ON_ERROR);
62  $participantIds = array_filter(array_map(static function ($user): int {
63  if (is_array($user) && isset($user['id'])) {
64  return (int) $user['id'];
65  }
66 
67  return 0;
68  }, $participants));
69 
70  if (!in_array($this->user->getId(), $participantIds, true)) {
71  continue;
72  }
73 
74  $conversation = new ConversationDto($row['id']);
75  $conversation->setIsGroup((bool) $row['is_group']);
76  $conversation->setSubscriberUsrIds($participantIds);
77 
78  $inParticipants = $this->db->in(
79  'osc_messages.user_id',
80  $participantIds,
81  false,
82  'text'
83  );
84 
85  $this->db->setLimit(1, 0);
86  $query = "
87  SELECT osc_messages.*
88  FROM osc_messages
89  WHERE osc_messages.conversation_id = %s
90  AND $inParticipants
91  ORDER BY osc_messages.timestamp DESC
92  ";
93  $msgRes = $this->db->queryF($query, ['text'], [$conversation->getId()]);
94  while ($msgRow = $this->db->fetchAssoc($msgRes)) {
95  $message = new MessageDto($msgRow['id'], $conversation);
96  $message->setMessage($msgRow['message']);
97  $message->setAuthorUsrId((int) $msgRow['user_id']);
98  $message->setCreatedTimestamp((int) $msgRow['timestamp']);
99  $conversation->setLastMessage($message);
100  break;
101  }
102 
103  $conversations[] = $conversation;
104  }
105 
106  return $conversations;
107  }
108 }
$res
Definition: ltiservices.php:69
$query
$message
Definition: xapiexit.php:32
__construct(ilDBInterface $db, ilObjUser $user)