1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
5require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
6
7/**
8 * Class ilChatroomBanGUI
9 * @author  Jan Posselt <jposselt@databay.de>
10 * @version $Id$
11 * @ingroup ModulesChatroom
12 */
13class ilChatroomBanGUI extends ilChatroomGUIHandler
14{
15    /** @var ilCtrl|null */
16    private $controller;
17
18    /** @var ilLanguage|null */
19    private $language;
20
21    /** @var ilObjUser|ilUser|null */
22    private $user;
23
24    /**
25     * @param ilChatroomObjectGUI $gui
26     * @param ilCtrl|null $controller
27     * @param ilLanguage|null $language
28     * @param ilUser|null $user
29     */
30    public function __construct(
31        ilChatroomObjectGUI $gui,
32        \ilCtrl $controller = null,
33        \ilLanguage $language = null,
34        \ilUser $user = null
35    ) {
36        if ($controller === null) {
37            global $DIC;
38            $controller = $DIC->ctrl();
39        }
40        $this->controller = $controller;
41
42        if ($language === null) {
43            global $DIC;
44            $language = $DIC->language();
45        }
46        $this->language = $language;
47
48        if ($user === null) {
49            global $DIC;
50            $user = $DIC->user();
51        }
52        $this->user = $user;
53
54        parent::__construct($gui);
55    }
56
57    /**
58     * Unbans users fetched from $_REQUEST['banned_user_id'].
59     */
60    public function delete()
61    {
62        $users = $_REQUEST['banned_user_id'];
63
64        if (!is_array($users)) {
65            ilUtil::sendInfo($this->ilLng->txt('no_checkbox'), true);
66            $this->ilCtrl->redirect($this->gui, 'ban-show');
67        }
68
69        $room = ilChatroom::byObjectId($this->gui->object->getId());
70        $room->unbanUser($users);
71
72        $this->ilCtrl->redirect($this->gui, 'ban-show');
73    }
74
75    /**
76     * {@inheritdoc}
77     */
78    public function executeDefault($method)
79    {
80        $this->show();
81    }
82
83    /**
84     * Displays banned users task.
85     */
86    public function show()
87    {
88        include_once 'Modules/Chatroom/classes/class.ilChatroom.php';
89
90        $this->redirectIfNoPermission('read');
91
92        $this->gui->switchToVisibleMode();
93
94        require_once 'Modules/Chatroom/classes/class.ilBannedUsersTableGUI.php';
95
96        $table = new ilBannedUsersTableGUI($this->gui, 'ban-show');
97        $table->setFormAction($this->controller->getFormAction($this->gui, 'ban-show'));
98
99        $room = ilChatroom::byObjectId($this->gui->object->getId());
100        if ($room) {
101            $data = $room->getBannedUsers();
102
103            $actorIDs = array_filter(array_map(function ($row) {
104                return $row['actor_id'];
105            }, $data));
106
107            require_once 'Services/User/classes/class.ilUserUtil.php';
108            $sortable_names = ilUserUtil::getNamePresentation($actorIDs);
109            $names = ilUserUtil::getNamePresentation($actorIDs, false, false, '', false, false, false);
110
111            array_walk($data, function (&$row) use ($names, $sortable_names) {
112                if ($row['actor_id'] > 0 && isset($names[$row['actor_id']])) {
113                    $row['actor_display'] = $names[$row['actor_id']];
114                    $row['actor'] = $sortable_names[$row['actor_id']];
115                } else {
116                    $row['actor_display'] = $this->language->txt('unknown');
117                    $row['actor'] = $this->language->txt('unknown');
118                }
119            });
120
121            $table->setData($data);
122        }
123
124        $this->gui->tpl->setVariable('ADM_CONTENT', $table->getHTML());
125    }
126
127    /**
128     * Kicks and bans user, fetched from $_REQUEST['user'] and adds history entry.
129     */
130    public function active()
131    {
132        $this->redirectIfNoPermission(array('read', 'moderate'));
133
134        $room = ilChatroom::byObjectId($this->gui->object->getId());
135        $subRoomId = $_REQUEST['sub'];
136        $userToBan = $_REQUEST['user'];
137
138        $this->exitIfNoRoomExists($room);
139
140        $connector = $this->gui->getConnector();
141        $response = $connector->sendBan($room->getRoomId(), $subRoomId, $userToBan); // @TODO Respect Scope
142
143        if ($this->isSuccessful($response)) {
144            $room->banUser($_REQUEST['user'], $this->user->getId());
145            $room->disconnectUser($_REQUEST['user']);
146        }
147
148        $this->sendResponse($response);
149    }
150}
151