ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatroomPostMessageTask.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 
16  private $gui;
17 
25  public function __construct(ilDBayObjectGUI $gui)
26  {
27  $this->gui = $gui;
28  }
29 
38  public function executeDefault($method)
39  {
40  global $ilUser;
41 
42  require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
43  require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
44 
45  if ( !ilChatroom::checkUserPermissions( 'read', $this->gui->ref_id ) )
46  {
47  ilUtil::redirect("repository.php");
48  }
49 
50  $room = ilChatroom::byObjectId( $this->gui->object->getId() );
51 
52  $chat_user = new ilChatroomUser($ilUser, $room);
53  $user_id = $chat_user->getUserId();
54 
55  if( !$room )
56  {
57  throw new Exception('unkown room');
58  }
59  else if( !$room->isSubscribed( $chat_user->getUserId() ) )
60  {
61  throw new Exception('not subscribed');
62  }
63  else if ( isset($_REQUEST['sub']) && !$room->userIsInPrivateRoom( $_REQUEST['sub'], $chat_user->getUserId() ))
64  {
65  $response = json_encode( array(
66  'success' => false,
67  'reason' => 'not subscribed to private room'
68  ) );
69  echo json_encode( $response );
70  exit;
71  }
72 
73  $scope = $room->getRoomId();
74  $params = array();
75 
76  if( ($recipient = $_REQUEST['recipient'] ) )
77  {
78  $params['recipients'] = join( ',', array_unique( array($user_id, $recipient) ) );
79  $params['recipient_names'] = implode( ',', array($chat_user->getUsername(), $_REQUEST['recipient_name']) );
80  $params['public'] = isset( $_REQUEST['public'] ) ? (int)$_REQUEST['public'] : 0;
81  }
82  else
83  {
84  $params['public'] = 1;
85  }
86 
87  if ($_REQUEST['sub'])
88  $params['sub'] = (int)$_REQUEST['sub'];
89 
90  $messageObject = $this->buildMessage(
91  ilUtil::stripSlashes( $_REQUEST['message'] ),
92  $params,
93  $chat_user
94  );
95 
96  $message = json_encode( $messageObject );
97 
98  $params = array_merge( $params, array('message' => $message) );
99  //print_r($params);exit;
100  $query = http_build_query( $params );
101  $connector = $this->gui->getConnector();
102  $response = $connector->post( $scope, $query );
103 
104  $responseObject = json_decode( $response );
105 
106  if( $responseObject->success == true /*&& $room->getSetting( 'enable_history' )*/ )
107  {
108  $room->addHistoryEntry( $messageObject, $recipient, $publicMessage );
109  }
110 
111  echo $response;
112  exit;
113  }
114 
125  private function buildMessage($messageString, $params, ilChatroomUser $chat_user)
126  {
127  $data = new stdClass();
128 
129  $data->user = $this->gui->object->getPersonalInformation( $chat_user );
130  $data->message = $messageString;
131  $data->timestamp = time() * 1000;//date( 'c' );
132  $data->type = 'message';
133  isset($params['sub']) ? ($data->sub = $params['sub']) : false;
134  $data->public = (int)$params['public'];
135  $data->recipients = $params['recipients']; // ? explode(",", $params['recipients']) : array();
136  $data->recipient_names = $params['recipient_names'];
137 
138  return $data;
139  }
140 
141 }
142 
143 ?>