ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
54  if(isset($_SESSION['chat'][$this->room->getRoomId()]['user_id']))
55  {
56  return $_SESSION['chat'][$this->room->getRoomId()]['user_id'];
57  }
58  else
59  {
60  $user_id = mt_rand(-99999, -20);
61  $_SESSION['chat'][$this->room->getRoomId()]['user_id'] = $user_id;
62  return $user_id;
63  }
64  }
65  else
66  {
67  return $user_id;
68  }
69  }
70 
76  public function getUsername()
77  {
78  if($this->username)
79  {
80  return $this->username;
81  }
82  else if($_SESSION['chat'][$this->room->getRoomId()]['username'])
83  {
84  return $_SESSION['chat'][$this->room->getRoomId()]['username'];
85  }
86  else
87  {
88  return $this->user->getLogin();
89  }
90  }
91 
96  public function setUsername($username)
97  {
98  $this->username = htmlspecialchars($username);
99  $_SESSION['chat'][$this->room->getRoomId()]['username'] = $this->username;
100  }
101 
106  public function getChatNameSuggestions()
107  {
108  $options = array();
109 
110  if($this->user->isAnonymous())
111  {
112  $options['anonymousName'] = $this->buildAnonymousName();
113  }
114  else
115  {
116  $options['fullname'] = $this->buildFullname();
117  $options['shortname'] = $this->buildShortname();
118  $options['login'] = $this->buildLogin();
119  }
120 
121  return $options;
122  }
123 
128  public function buildAnonymousName()
129  {
130  $anonymous_name = str_replace(
131  '#', mt_rand(0, 10000), $this->room->getSetting('autogen_usernames')
132  );
133 
134  return $anonymous_name;
135  }
136 
141  public function buildFullname()
142  {
143  $tmp = $this->user->getPref('public_profile');
144  $this->user->setPref('public_profile', 'y');
145  $pn = $this->user->getPublicName();
146  $this->user->setPref('public_profile', $tmp);
147  return $pn;
148  }
149 
154  public function buildShortname()
155  {
156  $firstname = $this->user->getFirstname();
157 
158  return $firstname{0} . '. ' . $this->user->getLastname();
159  }
160 
165  public function buildLogin()
166  {
167  return $this->user->getLogin();
168  }
169 
174  public function buildUniqueUsername($username)
175  {
179  global $ilDB;
180  $username = htmlspecialchars(trim($username));
181  $usernames = array();
182  $uniqueName = $username;
183 
184  $rset = $ilDB->query('SELECT * FROM chatroom_users WHERE '
185  . $ilDB->like('userdata', 'text', '%"login":"' . $username . '%')
186  . ' AND room_id = ' . $ilDB->quote($this->room->getRoomId(), 'integer')
187  );
188 
189  while(($row = $ilDB->fetchAssoc($rset)))
190  {
191  $json = json_decode($row['userdata'], true);
192  $usernames[] = $json['login'];
193  }
194 
195  for($index = 1; $index <= \count($usernames); $index++)
196  {
197  if(in_array($uniqueName, $usernames))
198  {
199  $uniqueName = sprintf('%s_%d', $username, $index);
200  }
201  }
202 
203  return $uniqueName;
204  }
205 }
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"]
user()
Definition: user.php:4
buildFullname()
Returns users first & lastname.
buildShortname()
Returns first letter of users firstname, followed by dot lastname.
if(!is_array($argv)) $options
Create styles array
The data for the language used.
Class ilChatroom.
getChatNameSuggestions()
Returns an array of chat-name suggestions.
Class ilChatroomUser.
global $ilDB
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.