1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
5
6/**
7 * Class ilChatroomGUIHandler
8 * @author  Jan Posselt <jposselt@databay.de>
9 * @author  Thomas joußen <tjoussen@databay.de>
10 * @version $Id$
11 */
12abstract class ilChatroomGUIHandler
13{
14    /**
15     * @var ilChatroomObjectGUI
16     */
17    protected $gui;
18
19    /**
20     * @var ilObjUser
21     */
22    protected $ilUser;
23
24    /**
25     * @var ilCtrl
26     */
27    protected $ilCtrl;
28
29    /**
30     * @var ilLanguage
31     */
32    protected $ilLng;
33
34    /**
35     * @var \ILIAS\Filesystem\Filesystem
36     */
37    protected $webDirectory;
38
39    /**
40     * @var ilObjectService
41     */
42    protected $obj_service;
43
44    /**
45     * @var \ILIAS\FileUpload\FileUpload
46     */
47    protected $upload;
48
49    /**
50     * @var \ilRbacSystem
51     */
52    protected $rbacsystem;
53
54    /**
55     * @var \ilTemplate
56     */
57    protected $mainTpl;
58
59    /**
60     * @var \ILIAS
61     */
62    protected $ilias;
63
64    /**
65     * @var ilNavigationHistory
66     */
67    protected $navigationHistory;
68
69    /**
70     * @var ilTree
71     */
72    protected $tree;
73
74    /**
75     * @var ilTabsGUI
76     */
77    protected $tabs;
78
79    /**
80     * @param ilChatroomObjectGUI $gui
81     */
82    public function __construct(ilChatroomObjectGUI $gui)
83    {
84        global $DIC;
85
86        $this->gui = $gui;
87        $this->ilUser = $DIC->user();
88        $this->ilCtrl = $DIC->ctrl();
89        $this->ilLng = $DIC->language();
90        $this->rbacsystem = $DIC->rbac()->system();
91        $this->mainTpl = $DIC->ui()->mainTemplate();
92        $this->upload = $DIC->upload();
93        $this->webDirectory = $DIC->filesystem()->web();
94        $this->obj_service = $DIC->object();
95        $this->ilias = $DIC['ilias'];
96        $this->tabs = $DIC->tabs();
97        $this->navigationHistory = $DIC['ilNavigationHistory'];
98        $this->tree = $DIC['tree'];
99    }
100
101    /**
102     * Executes given $method if existing, otherwise executes
103     * executeDefault() method.
104     * @param string $method
105     * @return mixed
106     */
107    public function execute($method)
108    {
109        $this->ilLng->loadLanguageModule('chatroom');
110
111        if (method_exists($this, $method)) {
112            return $this->$method();
113        } else {
114            return $this->executeDefault($method);
115        }
116    }
117
118    /**
119     * @param string $requestedMethod
120     * @return mixed
121     */
122    abstract public function executeDefault($requestedMethod);
123
124    /**
125     * Checks for requested permissions and redirects if the permission check failed
126     * @param array|string $permission
127     */
128    public function redirectIfNoPermission($permission)
129    {
130        if (!ilChatroom::checkUserPermissions($permission, $this->gui->ref_id)) {
131            $this->ilCtrl->setParameterByClass('ilrepositorygui', 'ref_id', ROOT_FOLDER_ID);
132            $this->ilCtrl->redirectByClass('ilrepositorygui', '');
133        }
134    }
135
136    /**
137     * Checks for success param in an json decoded response
138     * @param string $response
139     * @return boolean
140     */
141    public function isSuccessful($response)
142    {
143        $response = json_decode($response, true);
144
145        return $response !== null && array_key_exists('success', $response) && $response['success'];
146    }
147
148    /**
149     * @param $objectId
150     * @return ilChatroom
151     */
152    protected function getRoomByObjectId($objectId)
153    {
154        return ilChatroom::byObjectId($objectId);
155    }
156
157    /**
158     * Checks if a ilChatroom exists. If not, it will send a json encoded response with success = false
159     * @param ilChatroom $room
160     */
161    protected function exitIfNoRoomExists($room)
162    {
163        if (!$room) {
164            $this->sendResponse(
165                array(
166                    'success' => false,
167                    'reason' => 'unkown room',
168                )
169            );
170        }
171    }
172
173    /**
174     * Sends a json encoded response and exits the php process
175     * @param array $response
176     */
177    public function sendResponse($response)
178    {
179        echo json_encode($response);
180        exit;
181    }
182
183    /**
184     * Check if user can moderate a chatroom. If false it send a json decoded response with success = false
185     * @param ilChatroom $room
186     * @param int $subRoom
187     * @param ilChatroomUser $chat_user
188     */
189    protected function exitIfNoRoomPermission($room, $subRoom, $chat_user)
190    {
191        if (!$this->canModerate($room, $subRoom, $chat_user->getUserId())) {
192            $this->sendResponse(
193                array(
194                    'success' => false,
195                    'reason' => 'not owner of private room',
196                )
197            );
198        }
199    }
200
201    /**
202     * Checks if the user has permission to moderate a ilChatroom
203     * @param ilChatroom $room
204     * @param int $subRoom
205     * @param int $user_id
206     * @return bool
207     */
208    protected function canModerate($room, $subRoom, $user_id)
209    {
210        return $this->isMainRoom($subRoom) || $room->isOwnerOfPrivateRoom(
211            $user_id,
212            $subRoom
213        ) || $this->hasPermission('moderate');
214    }
215
216    /**
217     * @param int $subRoomId
218     * @return bool
219     */
220    protected function isMainRoom($subRoomId)
221    {
222        return $subRoomId == 0;
223    }
224
225    /**
226     * Checks for access with ilRbacSystem
227     * @param string $permission
228     * @return bool
229     */
230    public function hasPermission($permission)
231    {
232        return ilChatroom::checkUserPermissions($permission, $this->gui->ref_id);
233    }
234}
235