ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilChatroomUser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/User/classes/class.ilObjUser.php';
5 
13 {
17  private $user;
18 
22  private $username;
23 
27  private $room;
28 
36  public function __construct(ilObjUser $user, ilChatroom $chatroom)
37  {
38  $this->user = $user;
39  $this->room = $chatroom;
40  }
41 
48  public function getUserId()
49  {
50  $user_id = $this->user->getId();
51 
52  if ($this->user->isAnonymous()) {
53  if (isset($_SESSION['chat'][$this->room->getRoomId()]['user_id'])) {
54  return $_SESSION['chat'][$this->room->getRoomId()]['user_id'];
55  } else {
56  $user_id = mt_rand(-99999, -20);
57  $_SESSION['chat'][$this->room->getRoomId()]['user_id'] = $user_id;
58  return $user_id;
59  }
60  } else {
61  return $user_id;
62  }
63  }
64 
70  public function getUsername()
71  {
72  if ($this->username) {
73  return $this->username;
74  } elseif ($_SESSION['chat'][$this->room->getRoomId()]['username']) {
75  return $_SESSION['chat'][$this->room->getRoomId()]['username'];
76  } else {
77  return $this->user->getLogin();
78  }
79  }
80 
85  public function setUsername($username)
86  {
87  $this->username = htmlspecialchars($username);
88  $_SESSION['chat'][$this->room->getRoomId()]['username'] = $this->username;
89  }
90 
95  public function getChatNameSuggestions()
96  {
97  $options = array();
98 
99  if ($this->user->isAnonymous()) {
100  $options['anonymousName'] = $this->buildAnonymousName();
101  } else {
102  $options['fullname'] = $this->buildFullname();
103  $options['shortname'] = $this->buildShortname();
104  $options['login'] = $this->buildLogin();
105  }
106 
107  return $options;
108  }
109 
114  public function buildAnonymousName()
115  {
116  $anonymous_name = str_replace(
117  '#',
118  mt_rand(0, 10000),
119  $this->room->getSetting('autogen_usernames')
120  );
121 
122  return $anonymous_name;
123  }
124 
129  public function buildFullname()
130  {
131  $tmp = $this->user->getPref('public_profile');
132  $this->user->setPref('public_profile', 'y');
133  $pn = $this->user->getPublicName();
134  $this->user->setPref('public_profile', $tmp);
135  return $pn;
136  }
137 
142  public function buildShortname()
143  {
144  $firstname = $this->user->getFirstname();
145 
146  return $firstname{0} . '. ' . $this->user->getLastname();
147  }
148 
153  public function buildLogin()
154  {
155  return $this->user->getLogin();
156  }
157 
163  {
164  global $DIC;
165 
166  $username = htmlspecialchars(trim($username));
167  $usernames = array();
168  $uniqueName = $username;
169 
170  $rset = $DIC->database()->query(
171  'SELECT * FROM chatroom_users WHERE '
172  . $DIC->database()->like('userdata', 'text', '%"login":"' . $username . '%')
173  . ' AND room_id = ' . $DIC->database()->quote($this->room->getRoomId(), 'integer')
174  );
175 
176  while (($row = $DIC->database()->fetchAssoc($rset))) {
177  $json = json_decode($row['userdata'], true);
178  $usernames[] = $json['login'];
179  }
180 
181  for ($index = 1; $index <= \count($usernames); $index++) {
182  if (in_array($uniqueName, $usernames)) {
183  $uniqueName = sprintf('%s_%d', $username, $index);
184  }
185  }
186 
187  return $uniqueName;
188  }
189 }
buildAnonymousName()
Returns an anonymous username containing a random number.
getUserId()
Returns Ilias User ID.
setUsername($username)
Sets and stores given username in SESSION.
$_SESSION["AccountId"]
global $DIC
Definition: saml.php:7
$index
Definition: metadata.php:60
user()
Definition: user.php:4
buildFullname()
Returns users first & lastname.
buildUniqueUsername($username)
buildShortname()
Returns first letter of users firstname, followed by dot lastname.
Create styles array
The data for the language used.
Class ilChatroom.
getChatNameSuggestions()
Returns an array of chat-name suggestions.
Class ilChatroomUser.
getUsername()
Returns username from Object or SESSION.
__construct(ilObjUser $user, ilChatroom $chatroom)
Constructor Requires ilObjUser and sets $this->user and $this->room using given $user and $chatroom...
buildLogin()
Returns user login.
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20