ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilChatroomBanGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  private readonly ilCtrlInterface $controller;
30  private readonly ilLanguage $language;
31  private readonly ilObjUser $user;
32 
33  public function __construct(
34  ?ilChatroomObjectGUI $gui,
35  ?ilCtrlInterface $controller = null,
36  ?ilLanguage $language = null,
37  ?ilObjUser $user = null
38  ) {
39  if ($controller === null) {
40  global $DIC;
41  $controller = $DIC->ctrl();
42  }
43  $this->controller = $controller;
44 
45  if ($language === null) {
46  global $DIC;
47  $language = $DIC->language();
48  }
49  $this->language = $language;
50 
51  if ($user === null) {
52  global $DIC;
53  $user = $DIC->user();
54  }
55  $this->user = $user;
56 
57  parent::__construct($gui);
58  }
59 
60  public function handleTableActions(): void
61  {
62  $action = $this->http->wrapper()->query()->retrieve(
63  'chat_ban_table_action',
64  $this->refinery->byTrying([
65  $this->refinery->kindlyTo()->string(),
66  $this->refinery->always('')
67  ])
68  );
69  match ($action) {
70  'delete' => $this->delete(),
71  default => $this->ilCtrl->redirect($this, 'show'),
72  };
73  }
74 
75  public function delete(): void
76  {
77  $userTrafo = $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string());
78 
79  $users = $this->getRequestValue('chat_ban_table_user_ids', $userTrafo, []);
80  if ($users === []) {
81  $this->mainTpl->setOnScreenMessage('info', $this->ilLng->txt('no_checkbox'), true);
82  $this->ilCtrl->redirect($this->gui, 'ban-show');
83  }
84 
85  $room = ilChatroom::byObjectId($this->gui->getObject()->getId());
86  $this->exitIfNoRoomExists($room);
87 
88  if ((string) current($users) === 'ALL_OBJECTS') {
89  $users = array_map(static fn(array $row): int => (int) $row['user_id'], $room->getBannedUsers());
90  } else {
91  $users = array_map(intval(...), $users);
92  }
93 
94  $room->unbanUser($users);
95 
96  $this->mainTpl->setOnScreenMessage(
97  $this->mainTpl::MESSAGE_TYPE_SUCCESS,
98  $this->ilLng->txt('saved_successfully'),
99  true
100  );
101  $this->ilCtrl->redirect($this->gui, 'ban-show');
102  }
103 
104  public function executeDefault(string $requestedMethod): void
105  {
106  $this->show();
107  }
108 
112  public function show(): void
113  {
114  $this->redirectIfNoPermission('read');
115 
116  $this->gui->switchToVisibleMode();
117 
118  $room = ilChatroom::byObjectId($this->gui->getObject()->getId());
119  $this->exitIfNoRoomExists($room);
120 
121  $data = $room->getBannedUsers();
122  $actorId = array_filter(array_map(static fn(array $row): int => (int) $row['actor_id'], $data));
123 
124  $sortable_names = ilUserUtil::getNamePresentation($actorId);
125  $names = ilUserUtil::getNamePresentation($actorId, false, false, '', false, false, false);
126 
127  array_walk($data, function (&$row) use ($names, $sortable_names): void {
128  if ($row['actor_id'] > 0 && isset($names[$row['actor_id']])) {
129  $row['actor_display'] = $names[$row['actor_id']];
130  $row['actor'] = $sortable_names[$row['actor_id']];
131  } else {
132  $row['actor_display'] = $this->language->txt('unknown');
133  $row['actor'] = $this->language->txt('unknown');
134  }
135  });
136 
137  $tbl = new \ILIAS\Chatroom\Bans\BannedUsersTable(
138  $this->user,
139  $room->getRoomId(),
140  $data,
142  $this->ilLng,
143  $this->http,
145  );
146 
147  $this->mainTpl->setContent($this->uiRenderer->render($tbl->getComponent()));
148  }
149 
150  public function active(): void
151  {
152  $this->redirectIfNoPermission(['read', 'moderate']);
153 
154  $room = ilChatroom::byObjectId($this->gui->getObject()->getId());
155  $this->exitIfNoRoomExists($room);
156 
157  $userToBan = $this->getRequestValue('user', $this->refinery->kindlyTo()->int());
158 
159  $connector = $this->gui->getConnector();
160  $response = $connector->sendBan($room->getRoomId(), $userToBan);
161 
162  if ($this->isSuccessful($response)) {
163  $room->banUser($userToBan, $this->user->getId());
164  $room->disconnectUser($userToBan);
165  }
166 
167  $this->sendResponse($response, 'application/json');
168  }
169 }
redirectIfNoPermission($permission)
Checks for requested permissions and redirects if the permission check failed.
getRequestValue(string $key, Transformation $trafo, $default=null)
$response
Definition: xapitoken.php:93
exitIfNoRoomExists(?ilChatroom $room)
Checks if a ilChatroom exists.
show()
Displays banned users task.
Class ilChatroomBanGUI.
readonly ilObjUser $user
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
sendResponse(string $content, string $type)
Sends a response and exits the php process.
static http()
Fetches the global http state from ILIAS.
readonly ilCtrlInterface $controller
__construct(?ilChatroomObjectGUI $gui, ?ilCtrlInterface $controller=null, ?ilLanguage $language=null, ?ilObjUser $user=null)
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path='ilpublicuserprofilegui')
Default behaviour is:
Class ilChatroomGUIHandler.
global $DIC
Definition: shib_login.php:26
readonly ilLanguage $language
isSuccessful($response)
Checks for success param in an json decoded response.
__construct(Container $dic, ilPlugin $plugin)
static byObjectId(int $object_id)
executeDefault(string $requestedMethod)
language()
description: > Example for rendring a language glyph.
Definition: language.php:41