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