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