ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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;
34
36{
37 private readonly ServerRequestInterface $request;
38 private readonly Data\Factory $data_factory;
40 private ?array $records = null;
41
45 public function __construct(
46 private readonly \ilObjUser $actor,
47 private readonly int $room_id,
48 private readonly array $banned_users,
49 private readonly ilCtrlInterface $ctrl,
50 private readonly ilLanguage $lng,
52 private readonly \ILIAS\UI\Factory $ui_factory
53 ) {
54 $this->request = $http->request();
55 $this->data_factory = new Data\Factory();
56 }
57
58 public function getComponent(): UI\Component\Table\Data
59 {
60 $columns = $this->getColumns();
61 $actions = $this->getActions();
62
63 return $this->ui_factory
64 ->table()
65 ->data($this, $this->lng->txt('ban_table_title'), $columns)
66 ->withId(str_replace('\\', '', self::class) . '_' . $this->room_id)
67 ->withOrder(new \ILIAS\Data\Order('datetime', \ILIAS\Data\Order::DESC))
68 ->withRange(new Range(0, 50))
69 ->withActions($actions)
70 ->withRequest($this->request);
71 }
72
76 private function getColumns(): array
77 {
78 if ((int) $this->actor->getTimeFormat() === \ilCalendarSettings::TIME_FORMAT_12) {
79 $date_format = $this->data_factory->dateFormat()->withTime12($this->actor->getDateFormat());
80 } else {
81 $date_format = $this->data_factory->dateFormat()->withTime24($this->actor->getDateFormat());
82 }
83
84 return [
85 'login' => $this->ui_factory
86 ->table()->column()->text($this->lng->txt('login'))
87 ->withIsSortable(true),
88 'firstname' => $this->ui_factory
89 ->table()->column()->text($this->lng->txt('firstname'))
90 ->withIsSortable(true),
91 'lastname' => $this->ui_factory
92 ->table()->column()->text($this->lng->txt('lastname'))
93 ->withIsSortable(true),
94 'datetime' => $this->ui_factory
95 ->table()->column()->date($this->lng->txt('chtr_ban_ts_tbl_head'), $date_format)
96 ->withIsSortable(true),
97 'actor' => $this->ui_factory
98 ->table()->column()->text($this->lng->txt('chtr_ban_actor_tbl_head'))
99 ->withIsSortable(true),
100 ];
101 }
102
106 private function getActions(): array
107 {
108 $query_params_namespace = ['chat', 'ban', 'table'];
109
110 $uri = $this->data_factory->uri(
111 ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(ilObjChatroomGUI::class, 'ban-handleTableActions')
112 );
113
114 $url_builder = new UI\URLBuilder($uri);
115 [
116 $url_builder,
117 $action_parameter_token_copy,
118 $row_id_token
119 ] = $url_builder->acquireParameters(
120 $query_params_namespace,
121 'action',
122 'user_ids'
123 );
124
125 return [
126 'delete' => $this->ui_factory->table()->action()->multi(
127 $this->lng->txt('unban'),
128 $url_builder->withParameter($action_parameter_token_copy, 'delete'),
129 $row_id_token
130 ),
131 ];
132 }
133
134 private function initRecords(): void
135 {
136 if ($this->records === null) {
137 $this->records = [];
138 $i = 0;
139 $entries = $this->banned_users;
140
141 foreach ($entries as $entry) {
142 $this->records[$i]['user_id'] = $entry['user_id'];
143 $this->records[$i]['login'] = $entry['login'];
144 $this->records[$i]['firstname'] = $entry['firstname'];
145 $this->records[$i]['lastname'] = $entry['lastname'];
146 $this->records[$i]['timestamp'] = $entry['timestamp'];
147 $this->records[$i]['datetime'] = (new \DateTimeImmutable('@' . $entry['timestamp']))->setTimezone(
148 new \DateTimeZone($this->actor->getTimeZone())
149 );
150
151 $this->records[$i]['actor'] = $entry['actor'];
152 ++$i;
153 }
154 }
155 }
156
157 public function getRows(
158 UI\Component\Table\DataRowBuilder $row_builder,
159 array $visible_column_ids,
161 Data\Order $order,
162 mixed $additional_viewcontrol_data,
163 mixed $filter_data,
164 mixed $additional_parameters
165 ): \Generator {
166 $records = $this->getRecords($range, $order);
167
168 foreach ($records as $record) {
169 $row_id = (string) $record['user_id'];
170 yield $row_builder->buildDataRow($row_id, $record);
171 }
172 }
173
174 public function getTotalRowCount(
175 mixed $additional_viewcontrol_data,
176 mixed $filter_data,
177 mixed $additional_parameters
178 ): ?int {
179 $this->initRecords();
180
181 return \count($this->records);
182 }
183
187 private function sortedRecords(Data\Order $order): array
188 {
189 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
190
191 if ($order_field === 'datetime') {
192 $order_field = 'timestamp';
193 }
194
196 $this->records,
197 $order_field,
198 strtolower($order_direction),
199 $order_field === 'timestamp'
200 );
201 }
202
206 private function getRecords(Data\Range $range, Data\Order $order): array
207 {
208 $this->initRecords();
209
210 $records = $this->sortedRecords($order);
211
212 return $this->limitRecords($records, $range);
213 }
214
219 private function limitRecords(array $records, Data\Range $range): array
220 {
221 return \array_slice($records, $range->getStart(), $range->getLength());
222 }
223}
getRows(UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Data\Range $range, Data\Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $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
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
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