ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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  private ?bool $profile_picture_visible = null;
31 
32  public function __construct(private readonly ilObjUser $user, private readonly ilChatroom $room)
33  {
34  }
35 
36  public function enabledBroadcastTyping(): bool
37  {
38  return ilUtil::yn2tf((string) $this->user->getPref('chat_broadcast_typing'));
39  }
40 
45  public function getUserId(): int
46  {
47  $user_id = $this->user->getId();
48 
49  if ($this->user->isAnonymous()) {
50  $session = ilSession::get('chat');
51  if (isset($session[$this->room->getRoomId()]['user_id'])) {
52  return $session[$this->room->getRoomId()]['user_id'];
53  }
54 
55  $user_id = random_int(-99999, -20);
56 
57  $session[$this->room->getRoomId()]['user_id'] = $user_id;
58  ilSession::set('chat', $session);
59  }
60 
61  return $user_id;
62  }
63 
68  public function getUsername(): string
69  {
70  if ($this->username) {
71  return $this->username;
72  }
73 
74  $session = ilSession::get('chat');
75  if (
76  is_array($session) &&
77  isset($session[$this->room->getRoomId()]['username']) &&
78  $session[$this->room->getRoomId()]['username']
79  ) {
80  return $session[$this->room->getRoomId()]['username'];
81  }
82 
83  return $this->user->getPublicName();
84  }
85 
89  public function setUsername(string $username): void
90  {
91  $this->username = htmlspecialchars($username);
92 
93  $session = ilSession::get('chat');
94  $session[$this->room->getRoomId()]['username'] = $this->username;
95  ilSession::set('chat', $session);
96  }
97 
98  public function isProfilePictureVisible(): bool
99  {
100  if ($this->profile_picture_visible === null) {
101  $this->profile_picture_visible = ilSession::get('chat')[$this->room->getRoomId()]['profile-picture-visible'] ?? false;
102  }
104  }
105 
106  public function setProfilePictureVisible(bool $show_it): void
107  {
108  $session = ilSession::get('chat');
109  $session[$this->room->getRoomId()]['profile-picture-visible'] = $show_it;
110  ilSession::set('chat', $session);
111  }
112 
117  public function getChatNameSuggestions(): array
118  {
119  $options = [];
120 
121  if ($this->user->isAnonymous()) {
122  $options['anonymousName'] = $this->buildAnonymousName();
123  } else {
124  $options['fullname'] = $this->buildFullname();
125  $options['shortname'] = $this->buildShortname();
126  $options['login'] = $this->buildLogin();
127  }
128 
129  return $options;
130  }
131 
132  public function buildAnonymousName(): string
133  {
134  return str_replace(
135  '#',
136  (string) random_int(0, 10000),
137  $this->room->getSetting('autogen_usernames')
138  );
139  }
140 
141  public function buildFullname(): string
142  {
143  $tmp = $this->user->getPref('public_profile');
144  $this->user->setPref('public_profile', 'y');
145  $public_name = $this->user->getPublicName();
146  $this->user->setPref('public_profile', $tmp);
147 
148  return $public_name;
149  }
150 
154  public function buildShortname(): string
155  {
156  $firstname = $this->user->getFirstname();
157 
158  return $firstname[0] . '. ' . $this->user->getLastname();
159  }
160 
161  public function buildLogin(): string
162  {
163  return $this->user->getLogin();
164  }
165 
166  public function buildUniqueUsername(string $username): string
167  {
168  global $DIC;
169 
170  $username = htmlspecialchars(trim($username));
171  $usernames = [];
172  $uniqueName = $username;
173 
174  $rset = $DIC->database()->query(
175  'SELECT * FROM chatroom_users WHERE ' .
176  $DIC->database()->like('userdata', 'text', '%"login":"' . $username . '%') .
177  ' AND room_id = ' . $DIC->database()->quote($this->room->getRoomId(), 'integer')
178  );
179 
180  while (($row = $DIC->database()->fetchAssoc($rset))) {
181  $json = json_decode($row['userdata'], true, 512, JSON_THROW_ON_ERROR);
182  $usernames[] = $json['login'];
183  }
184 
185  for ($index = 1, $indexMax = count($usernames); $index <= $indexMax; $index++) {
186  if (in_array($uniqueName, $usernames, true)) {
187  $uniqueName = sprintf('%s_%d', $username, $index);
188  }
189  }
190 
191  return $uniqueName;
192  }
193 
197  public static function getUserInformation(array $usrIds, ?int $roomId = null): array
198  {
199  global $DIC;
200 
201  $users = [];
202 
203  $query = '
204  SELECT userdata
205  FROM chatroom_users WHERE ' . $DIC->database()->in('user_id', $usrIds, false, 'integer');
206 
207  if (null !== $roomId) {
208  $query .= ' AND room_id = ' . $DIC->database()->quote($roomId, 'integer');
209  }
210 
211  $res = $DIC->database()->query($query);
212  while ($row = $DIC->database()->fetchAssoc($res)) {
213  $users[] = json_decode($row['userdata'], false, 512, JSON_THROW_ON_ERROR);
214  }
215 
216  return $users;
217  }
218 }
static get(string $a_var)
__construct(private readonly ilObjUser $user, private readonly ilChatroom $room)
$res
Definition: ltiservices.php:66
getUserId()
Returns Ilias User ID.
setProfilePictureVisible(bool $show_it)
static getUserInformation(array $usrIds, ?int $roomId=null)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setUsername(string $username)
Sets and stores given username in SESSION.
buildShortname()
Returns first letter of users firstname, followed by dot lastname.
buildUniqueUsername(string $username)
global $DIC
Definition: shib_login.php:22
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.