ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatroomUser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
14 {
15 
20  private $user;
21  private $username;
22  private $room;
23 
33  public function __construct(ilObjUser $user, ilChatroom $chatroom)
34  {
35  require_once 'Services/User/classes/class.ilObjUser.php';
36 
37  $this->user = $user;
38  $this->room = $chatroom;
39  }
40 
48  public function getUserId()
49  {
50  $user_id = $this->user->getId();
51 
52  if( $user_id == ANONYMOUS_USER_ID )
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 
63  return $user_id;
64  }
65  }
66  else
67  {
68  return $user_id;
69  }
70  }
71 
77  public function setUsername($username)
78  {
79  $this->username = htmlspecialchars( $username );
80  $_SESSION['chat'][$this->room->getRoomId()]['username'] = $this->username;
81  }
82 
89  public function getUsername()
90  {
91  /*
92  if( !isset( $this->username ) &&
93  !$_SESSION['chat'][$this->room->getRoomId()]['username'] )
94  {
95  throw new Exception( 'no username set' );
96  }
97  */
98 
99  if( $this->username )
100  {
101  return $this->username;
102  }
103  else if ($_SESSION['chat'][$this->room->getRoomId()]['username'])
104  {
105  return $_SESSION['chat'][$this->room->getRoomId()]['username'];
106  }
107  else {
108  return $this->user->getLogin();
109  }
110  }
111 
117  public function getChatNameSuggestions()
118  {
119  $firstname = $this->user->getFirstname();
120  $lastname = $this->user->getLastname();
121  $options = array();
122 
123  if( $this->user->getId() == ANONYMOUS_USER_ID )
124  {
125  $options['anonymousName'] = $this->buildAnonymousName();
126  }
127  else
128  {
129  $options['fullname'] = $this->buildFullname();
130  $options['shortname'] = $this->buildShortname();
131  $options['login'] = $this->buildLogin();
132  }
133 
134  return $options;
135  }
136 
142  public function buildAnonymousName()
143  {
144  $anonymous_name = str_replace(
145  '#', mt_rand( 0, 10000 ), $this->room->getSetting('autogen_usernames')
146  );
147 
148  return $anonymous_name;
149  }
150 
156  public function buildLogin()
157  {
158  return $this->user->getLogin();
159  }
160 
166  public function buildFullname()
167  {
168  return $this->user->getFullname();
169  }
170 
176  public function buildShortname()
177  {
178  $firstname = $this->user->getFirstname();
179 
180  return $firstname{0} . '. ' . $this->user->getLastname();
181  }
182 
183 }
184 
185 ?>