ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilChatroomUser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  private string $username = '';
30 
31  public function __construct(private readonly ilObjUser $user, private readonly ilChatroom $room)
32  {
33  }
34 
35  public function enabledBroadcastTyping(): bool
36  {
37  return ilUtil::yn2tf((string) $this->user->getPref('chat_broadcast_typing'));
38  }
39 
44  public function getUserId(): int
45  {
46  $user_id = $this->user->getId();
47 
48  if ($this->user->isAnonymous()) {
49  $session = ilSession::get('chat');
50  if (isset($session[$this->room->getRoomId()]['user_id'])) {
51  return $session[$this->room->getRoomId()]['user_id'];
52  }
53 
54  $user_id = random_int(-99999, -20);
55 
56  $session[$this->room->getRoomId()]['user_id'] = $user_id;
57  ilSession::set('chat', $session);
58  }
59 
60  return $user_id;
61  }
62 
67  public function getUsername(): string
68  {
69  if ($this->username) {
70  return $this->username;
71  }
72 
73  $session = ilSession::get('chat');
74  if (
75  is_array($session) &&
76  isset($session[$this->room->getRoomId()]['username']) &&
77  $session[$this->room->getRoomId()]['username']
78  ) {
79  return $session[$this->room->getRoomId()]['username'];
80  }
81 
82  return $this->user->getLogin();
83  }
84 
88  public function setUsername(string $username): void
89  {
90  $this->username = htmlspecialchars($username);
91 
92  $session = ilSession::get('chat');
93  $session[$this->room->getRoomId()]['username'] = $this->username;
94  ilSession::set('chat', $session);
95  }
96 
101  public function getChatNameSuggestions(): array
102  {
103  $options = [];
104 
105  if ($this->user->isAnonymous()) {
106  $options['anonymousName'] = $this->buildAnonymousName();
107  } else {
108  $options['fullname'] = $this->buildFullname();
109  $options['shortname'] = $this->buildShortname();
110  $options['login'] = $this->buildLogin();
111  }
112 
113  return $options;
114  }
115 
116  public function buildAnonymousName(): string
117  {
118  return str_replace(
119  '#',
120  (string) random_int(0, 10000),
121  $this->room->getSetting('autogen_usernames')
122  );
123  }
124 
125  public function buildFullname(): string
126  {
127  $tmp = $this->user->getPref('public_profile');
128  $this->user->setPref('public_profile', 'y');
129  $public_name = $this->user->getPublicName();
130  $this->user->setPref('public_profile', $tmp);
131 
132  return $public_name;
133  }
134 
138  public function buildShortname(): string
139  {
140  $firstname = $this->user->getFirstname();
141 
142  return $firstname[0] . '. ' . $this->user->getLastname();
143  }
144 
145  public function buildLogin(): string
146  {
147  return $this->user->getLogin();
148  }
149 
150  public function buildUniqueUsername(string $username): string
151  {
152  global $DIC;
153 
154  $username = htmlspecialchars(trim($username));
155  $usernames = [];
156  $uniqueName = $username;
157 
158  $rset = $DIC->database()->query(
159  'SELECT * FROM chatroom_users WHERE ' .
160  $DIC->database()->like('userdata', 'text', '%"login":"' . $username . '%') .
161  ' AND room_id = ' . $DIC->database()->quote($this->room->getRoomId(), 'integer')
162  );
163 
164  while (($row = $DIC->database()->fetchAssoc($rset))) {
165  $json = json_decode($row['userdata'], true, 512, JSON_THROW_ON_ERROR);
166  $usernames[] = $json['login'];
167  }
168 
169  for ($index = 1, $indexMax = count($usernames); $index <= $indexMax; $index++) {
170  if (in_array($uniqueName, $usernames, true)) {
171  $uniqueName = sprintf('%s_%d', $username, $index);
172  }
173  }
174 
175  return $uniqueName;
176  }
177 
181  public static function getUserInformation(array $usrIds, ?int $roomId = null): array
182  {
183  global $DIC;
184 
185  $users = [];
186 
187  $query = '
188  SELECT userdata
189  FROM chatroom_users WHERE ' . $DIC->database()->in('user_id', $usrIds, false, 'integer');
190 
191  if (null !== $roomId) {
192  $query .= ' AND room_id = ' . $DIC->database()->quote($roomId, 'integer');
193  }
194 
195  $res = $DIC->database()->query($query);
196  while ($row = $DIC->database()->fetchAssoc($res)) {
197  $users[] = json_decode($row['userdata'], false, 512, JSON_THROW_ON_ERROR);
198  }
199 
200  return $users;
201  }
202 }
static get(string $a_var)
__construct(private readonly ilObjUser $user, private readonly ilChatroom $room)
$res
Definition: ltiservices.php:69
getUserId()
Returns Ilias User ID.
static getUserInformation(array $usrIds, ?int $roomId=null)
global $DIC
Definition: feed.php:28
setUsername(string $username)
Sets and stores given username in SESSION.
buildShortname()
Returns first letter of users firstname, followed by dot lastname.
buildUniqueUsername(string $username)
Class ilChatroom.
getChatNameSuggestions()
Returns an array of chat-name suggestions.
Class ilChatroomUser.
static yn2tf(string $a_yn)
getUsername()
Returns username from Object or SESSION.
static set(string $a_var, $a_val)
Set a value.