ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatroomPrivateRoomTask.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 require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
5 require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
6 
16 {
17 
18  private $gui;
19 
27  public function __construct(ilDBayObjectGUI $gui)
28  {
29  $this->gui = $gui;
30  }
31 
32  public function executeDefault($method) {
33 
34  }
35 
45  public function create()
46  {
47  global $tpl, $ilUser, $ilCtrl;
48 
49  require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
50  require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
51 
52  if ( !ilChatroom::checkUserPermissions( 'read', $this->gui->ref_id ) )
53  {
54  $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id", ROOT_FOLDER_ID);
55  $ilCtrl->redirectByClass("ilrepositorygui", "");
56  }
57 
58  $room = ilChatroom::byObjectId( $this->gui->object->getId() );
59 
60  $chat_user = new ilChatroomUser($ilUser, $room);
61  $user_id = $chat_user->getUserId();
62 
63  if( !$room )
64  {
65  $response = json_encode( array(
66  'success' => false,
67  'reason' => 'unkown room'
68  ) );
69  echo json_encode( $response );
70  exit;
71  }
72  else if( !$room->isSubscribed( $chat_user->getUserId() ) )
73  {
74  $response = json_encode( array(
75  'success' => false,
76  'reason' => 'not subscribed'
77  ) );
78  echo json_encode( $response );
79  exit;
80  }
81 
82  $title = $room->getUniquePrivateRoomTitle($_REQUEST['title']);
83  $connector = $this->gui->getConnector();
84  $response = $connector->createPrivateRoom($room, $title, $chat_user);
85 
86  echo json_encode($response);
87  exit;
88  }
89 
90  public function delete()
91  {
92  global $tpl, $ilUser, $rbacsystem;
93 
94  require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
95  require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
96 
97  $room = ilChatroom::byObjectId( $this->gui->object->getId() );
98 
99  $chat_user = new ilChatroomUser($ilUser, $room);
100  $user_id = $chat_user->getUserId();
101 
102  if( !$room )
103  {
104  $response = json_encode( array(
105  'success' => false,
106  'reason' => 'unkown room'
107  ) );
108  echo json_encode( $response );
109  exit;
110  }
111  else if( !$room->isOwnerOfPrivateRoom( $user_id, $_REQUEST['sub'] ) && !$rbacsystem->checkAccess('moderate', $this->gui->getRefId()) )
112  {
113  $response = json_encode( array(
114  'success' => false,
115  'reason' => 'not owner of private room'
116  ) );
117  echo json_encode( $response );
118  exit;
119  }
120 
121  $scope = $room->getRoomId();
122  $params = array();
123 
124  $params['user'] = $chat_user->getUserId();
125  $params['id'] = $room->closePrivateRoom($_REQUEST['sub'], $chat_user, $settings);
126  $query = http_build_query( $params );
127  $connector = $this->gui->getConnector();
128 
129  if( true || $responseObject->success == true )
130  {
131  $message = json_encode( array(
132  'type' => 'private_room_deleted',
133  //'recipients' => $chat_user->getUserId(),//$users,
134  'timestamp' => date( 'c' ),
135  'public' => 1,
136  //'title' => $title,
137  'id' => $responseObject->id,
138  'proom_id' => $_REQUEST['sub'],
139  'message' => array(
140  'message' => 'room deleted',
141  'public' => '1',
142  'user' => 'system'
143  )
144  ) );
145 
146  $connector->sendMessage( $room->getRoomId(), $message, array('public' => 1) );
147  }
148 
149  $response = json_encode(array('success' => true));
150  echo $response;
151  exit;
152  }
153 
163  public function leave()
164  {
165  global $tpl, $ilUser;
166 
167  $room = ilChatroom::byObjectId( $this->gui->object->getId() );
168 
169  $chat_user = new ilChatroomUser( $ilUser, $room );
170  $user_id = $chat_user->getUserId();
171  $connector = $this->gui->getConnector();
172 
173  if( !$room )
174  {
175  $response = json_encode( array(
176  'success' => false,
177  'reason' => 'unkown room'
178  ) );
179  echo json_encode( $response );
180  exit;
181  }
182  else if( !$room->isSubscribed( $chat_user->getUserId() ) )
183  {
184  $response = json_encode( array(
185  'success' => false,
186  'reason' => 'not subscribed'
187  ) );
188  echo json_encode( $response );
189  exit;
190  }
191 
192  $scope = $room->getRoomId();
193  $params = array();
194 
195  $params['user'] = $chat_user->getUserId();
196  $params['sub'] = $_REQUEST['sub'];
197 
198  $message = json_encode( array(
199  'type' => 'private_room_left',
200  'user' => $params['user'],
201  'sub' => $params['sub']
202  ));
203 
204  $connector->sendMessage( $room->getRoomId(), $message, array('public' => 1, 'sub' => $params['sub']) );
205 
206  if( $room->userIsInPrivateRoom( $params['sub'], $params['user'] ) )
207  {
208  //$params = array_merge( $params, array('message' => $message) );
209  $query = http_build_query( $params );
210  $connector = $this->gui->getConnector();
211  $response = $connector->leavePrivateRoom( $scope, $query );
212  $responseObject = json_decode( $response );
213 /*
214  if( $responseObject->success == true && $room->getSetting( 'enable_history' ) )
215  {
216  //$room->addHistoryEntry( $message, $recipient, $publicMessage );
217  }
218 */
219  $room->unsubscribeUserFromPrivateRoom( $params['sub'], $params['user'] );
220  }
221  else
222  {
223  $response = json_encode( array('success' => true, 'message' => 'was not in room') );
224  }
225 
226  echo $response;
227  exit;
228  }
229 
230  public function enter()
231  {
232  global $tpl, $ilUser;
233 
234  ilChatroom::checkUserPermissions( 'read', $this->gui->ref_id );
235 
236  $room = ilChatroom::byObjectId( $this->gui->object->getId() );
237 
238  $chat_user = new ilChatroomUser( $ilUser, $room );
239  $user_id = $chat_user->getUserId();
240 
241  if( !$room )
242  {
243  $response = json_encode( array(
244  'success' => false,
245  'reason' => 'unkown room'
246  ) );
247  echo json_encode( $response );
248  exit;
249  }
250  else if( !$room->isAllowedToEnterPrivateRoom( $chat_user->getUserId(), $_REQUEST['sub'] ) )
251  {
252  $response = json_encode( array(
253  'success' => false,
254  'reason' => 'not allowed enter to private room'
255  ) );
256 
257  echo json_encode( $response );
258  exit;
259  }
260 
261  $scope = $room->getRoomId();
262  $params = array();
263 
264  $params['user'] = $chat_user->getUserId();
265  $params['sub'] = $_REQUEST['sub'];
266  $params['message'] = json_encode( array(
267  'type' => 'private_room_entered',
268  'user' => $user_id
269  ));
270 
271 
272  $query = http_build_query( $params );
273  $connector = $this->gui->getConnector();
274  $response = $connector->enterPrivateRoom( $scope, $query );
275  $responseObject = json_decode( $response );
276 
277  $message = json_encode( array(
278  'type' => 'private_room_entered',
279  'user' => $params['user'],
280  'sub' => $params['sub']
281  ));
282 
283  $connector->sendMessage( $room->getRoomId(), $message, array('public' => 1, 'sub' => $params['sub']) );
284 
285  if( $responseObject->success == true )
286  {
287  $room->subscribeUserToPrivateRoom( $params['sub'], $params['user'] );
288  }
289 
290  echo $response;
291  exit;
292  }
293 
294  public function listUsers()
295  {
296  global $ilUser;
297 
298  $room = ilChatroom::byObjectId( $this->gui->object->getId() );
299 
300  echo json_encode( $room->listUsersInPrivateRoom( $_REQUEST['sub'] ) );
301  exit;
302  }
303 
314  private function buildMessage($messageString, $params, ilChatroomUser $chat_user)
315  {
316  $data = new stdClass();
317 
318  $data->user = $chat_user->getUserId(); //$this->gui->object->getPersonalInformation( $chat_user );
319  $data->title = $params['title'];
320  $data->timestamp = date( 'c' );
321  $data->type = 'private_room_created';
322 
323  return $data;
324  }
325 
326 }
327 
328 ?>