ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilChatroomUser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  private ilObjUser $user;
30  private ilChatroom $room;
31  private string $username = '';
32 
33  public function __construct(ilObjUser $user, ilChatroom $chatroom)
34  {
35  $this->user = $user;
36  $this->room = $chatroom;
37  }
38 
39  public function enabledBroadcastTyping(): bool
40  {
41  return ilUtil::yn2tf((string) $this->user->getPref('chat_broadcast_typing'));
42  }
43 
49  public function getUserId(): int
50  {
51  $user_id = $this->user->getId();
52 
53  if ($this->user->isAnonymous()) {
54  $session = ilSession::get('chat');
55  if (isset($session[$this->room->getRoomId()]['user_id'])) {
56  return $session[$this->room->getRoomId()]['user_id'];
57  }
58 
59  $user_id = random_int(-99999, -20);
60 
61  $session[$this->room->getRoomId()]['user_id'] = $user_id;
62  ilSession::set('chat', $session);
63  }
64 
65  return $user_id;
66  }
67 
73  public function getUsername(): string
74  {
75  if ($this->username) {
76  return $this->username;
77  }
78 
79  $session = ilSession::get('chat');
80  if (
81  is_array($session) &&
82  isset($session[$this->room->getRoomId()]['username']) &&
83  $session[$this->room->getRoomId()]['username']
84  ) {
85  return $session[$this->room->getRoomId()]['username'];
86  }
87 
88  return $this->user->getLogin();
89  }
90 
95  public function setUsername(string $username): void
96  {
97  $this->username = htmlspecialchars($username);
98 
99  $session = ilSession::get('chat');
100  $session[$this->room->getRoomId()]['username'] = $this->username;
101  ilSession::set('chat', $session);
102  }
103 
108  public function getChatNameSuggestions(): array
109  {
110  $options = [];
111 
112  if ($this->user->isAnonymous()) {
113  $options['anonymousName'] = $this->buildAnonymousName();
114  } else {
115  $options['fullname'] = $this->buildFullname();
116  $options['shortname'] = $this->buildShortname();
117  $options['login'] = $this->buildLogin();
118  }
119 
120  return $options;
121  }
122 
123  public function buildAnonymousName(): string
124  {
125  return str_replace(
126  '#',
127  (string) random_int(0, 10000),
128  $this->room->getSetting('autogen_usernames')
129  );
130  }
131 
132  public function buildFullname(): string
133  {
134  $tmp = $this->user->getPref('public_profile');
135  $this->user->setPref('public_profile', 'y');
136  $public_name = $this->user->getPublicName();
137  $this->user->setPref('public_profile', $tmp);
138 
139  return $public_name;
140  }
141 
146  public function buildShortname(): string
147  {
148  $firstname = $this->user->getFirstname();
149 
150  return $firstname[0] . '. ' . $this->user->getLastname();
151  }
152 
153  public function buildLogin(): string
154  {
155  return $this->user->getLogin();
156  }
157 
158  public function buildUniqueUsername(string $username): string
159  {
160  global $DIC;
161 
162  $username = htmlspecialchars(trim($username));
163  $usernames = [];
164  $uniqueName = $username;
165 
166  $rset = $DIC->database()->query(
167  'SELECT * FROM chatroom_users WHERE ' .
168  $DIC->database()->like('userdata', 'text', '%"login":"' . $username . '%') .
169  ' AND room_id = ' . $DIC->database()->quote($this->room->getRoomId(), 'integer')
170  );
171 
172  while (($row = $DIC->database()->fetchAssoc($rset))) {
173  $json = json_decode($row['userdata'], true, 512, JSON_THROW_ON_ERROR);
174  $usernames[] = $json['login'];
175  }
176 
177  for ($index = 1, $indexMax = count($usernames); $index <= $indexMax; $index++) {
178  if (in_array($uniqueName, $usernames, true)) {
179  $uniqueName = sprintf('%s_%d', $username, $index);
180  }
181  }
182 
183  return $uniqueName;
184  }
185 
191  public static function getUserInformation(array $usrIds, ?int $roomId = null): array
192  {
193  global $DIC;
194 
195  $users = [];
196 
197  $query = '
198  SELECT userdata
199  FROM chatroom_users WHERE ' . $DIC->database()->in('user_id', $usrIds, false, 'integer');
200 
201  if (null !== $roomId) {
202  $query .= ' AND room_id = ' . $DIC->database()->quote($roomId, 'integer');
203  }
204 
205  $res = $DIC->database()->query($query);
206  while ($row = $DIC->database()->fetchAssoc($res)) {
207  $users[] = json_decode($row['userdata'], false, 512, JSON_THROW_ON_ERROR);
208  }
209 
210  return $users;
211  }
212 }
static get(string $a_var)
$res
Definition: ltiservices.php:69
getUserId()
Returns Ilias User ID.
$session
static getUserInformation(array $usrIds, ?int $roomId=null)
$index
Definition: metadata.php:145
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)
$query
Class ilChatroom.
getChatNameSuggestions()
Returns an array of chat-name suggestions.
Class ilChatroomUser.
static yn2tf(string $a_yn)
getUsername()
Returns username from Object or SESSION.
__construct(ilObjUser $user, ilChatroom $chatroom)
static set(string $a_var, $a_val)
Set a value.