ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BannedUsersTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data;
24use ILIAS\UI;
25use Psr\Http\Message\ServerRequestInterface;
26use ilArrayUtil;
27use ilDateTime;
30use ilLanguage;
33
35{
36 private readonly ServerRequestInterface $request;
37 private readonly Data\Factory $data_factory;
39 private ?array $records = null;
40
44 public function __construct(
45 private readonly \ilObjUser $actor,
46 private readonly int $room_id,
47 private readonly array $banned_users,
48 private readonly ilCtrlInterface $ctrl,
49 private readonly ilLanguage $lng,
51 private readonly \ILIAS\UI\Factory $ui_factory
52 ) {
53 $this->request = $http->request();
54 $this->data_factory = new Data\Factory();
55 }
56
57 public function getComponent(): UI\Component\Table\Data
58 {
59 $columns = $this->getColumns();
60 $actions = $this->getActions();
61
62 return $this->ui_factory
63 ->table()
64 ->data($this, $this->lng->txt('ban_table_title'), $columns)
65 ->withId(self::class . '_' . $this->room_id)
66 ->withOrder(new \ILIAS\Data\Order('datetime', \ILIAS\Data\Order::DESC))
67 ->withActions($actions)
68 ->withRequest($this->request);
69 }
70
74 private function getColumns(): array
75 {
76 if ((int) $this->actor->getTimeFormat() === \ilCalendarSettings::TIME_FORMAT_12) {
77 $date_format = $this->data_factory->dateFormat()->withTime12($this->actor->getDateFormat());
78 } else {
79 $date_format = $this->data_factory->dateFormat()->withTime24($this->actor->getDateFormat());
80 }
81
82 return [
83 'login' => $this->ui_factory
84 ->table()->column()->text($this->lng->txt('login'))
85 ->withIsSortable(true),
86 'firstname' => $this->ui_factory
87 ->table()->column()->text($this->lng->txt('firstname'))
88 ->withIsSortable(true),
89 'lastname' => $this->ui_factory
90 ->table()->column()->text($this->lng->txt('lastname'))
91 ->withIsSortable(true),
92 'datetime' => $this->ui_factory
93 ->table()->column()->date($this->lng->txt('chtr_ban_ts_tbl_head'), $date_format)
94 ->withIsSortable(true),
95 'actor' => $this->ui_factory
96 ->table()->column()->text($this->lng->txt('chtr_ban_actor_tbl_head'))
97 ->withIsSortable(true),
98 ];
99 }
100
104 private function getActions(): array
105 {
106 $query_params_namespace = ['chat', 'ban', 'table'];
107
108 $uri = $this->data_factory->uri(
109 ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(ilObjChatroomGUI::class, 'ban-handleTableActions')
110 );
111
112 $url_builder = new UI\URLBuilder($uri);
113 [
114 $url_builder,
115 $action_parameter_token_copy,
116 $row_id_token
117 ] = $url_builder->acquireParameters(
118 $query_params_namespace,
119 'action',
120 'user_ids'
121 );
122
123 return [
124 'delete' => $this->ui_factory->table()->action()->multi(
125 $this->lng->txt('unban'),
126 $url_builder->withParameter($action_parameter_token_copy, 'delete'),
127 $row_id_token
128 ),
129 ];
130 }
131
132 private function initRecords(): void
133 {
134 if ($this->records === null) {
135 $this->records = [];
136 $i = 0;
137 $entries = $this->banned_users;
138
139 foreach ($entries as $entry) {
140 $this->records[$i]['user_id'] = $entry['user_id'];
141 $this->records[$i]['login'] = $entry['login'];
142 $this->records[$i]['firstname'] = $entry['firstname'];
143 $this->records[$i]['lastname'] = $entry['lastname'];
144 $this->records[$i]['timestamp'] = $entry['timestamp'];
145 $this->records[$i]['datetime'] = (new \DateTimeImmutable('@' . $entry['timestamp']))->setTimezone(
146 new \DateTimeZone($this->actor->getTimeZone())
147 );
148
149 $this->records[$i]['actor'] = $entry['actor'];
150 ++$i;
151 }
152 }
153 }
154
155 public function getRows(
156 UI\Component\Table\DataRowBuilder $row_builder,
157 array $visible_column_ids,
159 Data\Order $order,
160 ?array $filter_data,
161 ?array $additional_parameters
162 ): \Generator {
163 $records = $this->getRecords($range, $order);
164
165 foreach ($records as $record) {
166 $row_id = (string) $record['user_id'];
167 yield $row_builder->buildDataRow($row_id, $record);
168 }
169 }
170
171 public function getTotalRowCount(
172 ?array $filter_data,
173 ?array $additional_parameters
174 ): ?int {
175 $this->initRecords();
176
177 return \count($this->records);
178 }
179
183 private function sortedRecords(Data\Order $order): array
184 {
185 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
186
187 if ($order_field === 'datetime') {
188 $order_field = 'timestamp';
189 }
190
192 $this->records,
193 $order_field,
194 strtolower($order_direction),
195 $order_field === 'timestamp'
196 );
197 }
198
202 private function getRecords(Data\Range $range, Data\Order $order): array
203 {
204 $this->initRecords();
205
206 $records = $this->sortedRecords($order);
207
208 return $this->limitRecords($records, $range);
209 }
210
215 private function limitRecords(array $records, Data\Range $range): array
216 {
217 return \array_slice($records, $range->getStart(), $range->getLength());
218 }
219}
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
getRows(UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Data\Range $range, Data\Order $order, ?array $filter_data, ?array $additional_parameters)
limitRecords(array $records, Data\Range $range)
getRecords(Data\Range $range, Data\Order $order)
__construct(private readonly \ilObjUser $actor, private readonly int $room_id, private readonly array $banned_users, private readonly ilCtrlInterface $ctrl, private readonly ilLanguage $lng, GlobalHttpState $http, private readonly \ILIAS\UI\Factory $ui_factory)
readonly ServerRequestInterface $request
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
const DESC
Definition: Order.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Definition: UI.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stableSortArray(array $array, string $a_array_sortby, string $a_array_sortorder="asc", bool $a_numeric=false)
Sort an aray using a stable sort algorithm, which preveserves the sequence of array elements which ha...
Class for date presentation.
@classDescription Date and time handling
language handling
@ilCtrl_Calls ilObjChatroomGUI: ilMDEditorGUI, ilInfoScreenGUI, ilPermissionGUI, ilObjectCopyGUI @ilC...
User class.
$http
Definition: deliver.php:30
Interface GlobalHttpState.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31