ILIAS  trunk Revision v12.0_alpha-1613-gae4c99ebb18
ParticipantTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Generator;
37use ILIAS\Refinery\Factory as Refinery;
42use ILIAS\UI\Factory as UIFactory;
43use ILIAS\UI\Renderer as UIRenderer;
46use ilLanguage;
47use ilUIService;
48
49class ParticipantTable implements Table
50{
52
53 public const ID = 'bksp';
54
55 public const ROW_ID_PARAMETER = 'participant_id';
56
57 public const ACTION_PARAMETER = 'action';
58
59 public const ACTION_TYPE_PARAMETER = 'action_type';
60
64 public function __construct(
65 private readonly UIFactory $ui_factory,
66 private readonly UIRenderer $ui_renderer,
67 private readonly ilLanguage $lng,
68 private readonly HttpService $http,
69 private readonly ilUIService $ui_service,
70 private readonly ilCtrlInterface $ctrl,
71 private readonly ilGlobalTemplateInterface $tpl,
72 private readonly Refinery $refinery,
73 private readonly AccessManager $access,
74 private readonly ParticipantRepository $participant_repository,
75 private readonly int $ref_id,
76 private readonly int $pool_id,
77 ) {
78 }
79
80 public function getTableId(): string
81 {
82 return self::ID;
83 }
84
88 public function getComponents(URLBuilder $url_builder): array
89 {
90 $filter = $this->getFilterComponent($url_builder->buildURI()->__toString());
91
92 $table = $this->ui_factory->table()->data($this, $this->lng->txt('participants'), $this->getColumns())
93 ->withActions($this->getTableActions()->getEnabledActions(...$this->acquireParameters($url_builder)))
94 ->withRequest($this->http->getRequest())
95 ->withId($this->getTableId())
96 ->withFilter($this->ui_service->filter()->getData($filter));
97
98 return [$filter, $table];
99 }
100
101 public function getTotalRowCount(
102 mixed $additional_viewcontrol_data,
103 mixed $filter_data,
104 mixed $additional_parameters
105 ): ?int {
106 return count($this->loadRecords($filter_data));
107 }
108
109 public function getRows(
110 DataRowBuilder $row_builder,
111 array $visible_column_ids,
113 Order $order,
114 mixed $additional_viewcontrol_data,
115 mixed $filter_data,
116 mixed $additional_parameters
117 ): Generator {
118 $records = $this->limitRecords($range, $this->sortRecords($order, $this->loadRecords($filter_data)));
119
120 foreach ($records as $record) {
121 yield $this->getTableActions()->onDataRow(
122 $row_builder->buildDataRow(
123 (string) $record['user_id'],
124 [
125 'name' => $record['name'] ?? '',
126 'bookable_item' => $this->ui_renderer->render(
127 $this->ui_factory->listing()->unordered($record['object_title'] ?? [])
128 ),
129 ]
130 ),
131 $record
132 );
133 }
134 }
135
139 private function getColumns(): array
140 {
141 $column_factory = $this->ui_factory->table()->column();
142 return [
143 'name' => $column_factory->text($this->lng->txt('name'))->withIsSortable(true),
144 'bookable_item' => $column_factory->text($this->lng->txt('book_bobj'))->withIsSortable(false),
145 ];
146 }
147
148 private function loadRecords(?array $filter_data): array
149 {
150 $filter = [];
151
152 if (isset($filter_data['bookable_item_id']) && $filter_data['bookable_item_id'] !== '') {
153 $filter['object'] = (int) $filter_data['bookable_item_id'];
154 }
155
156 if (isset($filter_data['bookable_item_title']) && $filter_data['bookable_item_title'] !== '') {
157 $filter['title'] = (string) $filter_data['bookable_item_title'];
158 }
159
160 if (isset($filter_data['participant_id']) && $filter_data['participant_id'] !== '') {
161 $filter['user_id'] = (int) $filter_data['participant_id'];
162 }
163
164 $filter_object = isset($filter['object']) ? (int) $filter['object'] : null;
165 if ($filter_object === -1) {
166 return array_filter(
167 ilBookingParticipant::getList($this->pool_id, $filter),
168 static fn(array $item): bool => ($item['obj_count'] ?? 0) === 0
169 );
170 }
171
172 return ilBookingParticipant::getList($this->pool_id, $filter, $filter_object);
173 }
174
175 protected function sortRecords(Order $order, array $records): array
176 {
177 $order_data = $order->get();
178 if ($order_data === []) {
179 return $records;
180 }
181
182 foreach ($order_data as $key => $value) {
183 $order_direction = $value === Order::DESC ? -1 : 1;
184 $callable = match ($key) {
185 'name' => static fn(array $a, array $b): int => strcmp($a['name'], $b['name']) * $order_direction,
186 'bookable_item' => static fn(array $a, array $b): int => strcmp($a['bookable_item'], $b['bookable_item']) * $order_direction,
187 default => null,
188 };
189
190 if ($callable === null) {
191 continue;
192 }
193
194 usort($records, $callable);
195 }
196
197 return $records;
198 }
199
200 protected function limitRecords(Range $range, array $records): array
201 {
202 return array_slice($records, $range->getStart(), $range->getLength());
203 }
204
205 private function getFilterComponent(string $action): FilterComponent
206 {
207 $bookable_items = [];
208 foreach (ilBookingObject::getList($this->pool_id) as $item) {
209 $bookable_items[$item['booking_object_id']] = $item['title'];
210 }
211
212 $field_factory = $this->ui_factory->input()->field();
213 $filter_inputs = [
214 'bookable_item_id' => $field_factory->select(
215 $this->lng->txt('book_bobj'),
216 array_replace(['-1' => $this->lng->txt('book_no_objects')], $bookable_items)
217 ),
218 'bookable_item_title' => $field_factory->text(
219 "{$this->lng->txt('book_bobj')} {$this->lng->txt('title')}/{$this->lng->txt('description')}"
220 ),
221 'participant_id' => $field_factory->select(
222 $this->lng->txt('book_participant'),
224 ),
225 ];
226
227 return $this->ui_service->filter()->standard(
228 "participant_filter_{$this->pool_id}",
229 $action,
230 $filter_inputs,
231 array_fill(0, count($filter_inputs), true),
232 true,
233 true
234 );
235 }
236
240 protected function acquireParameters(URLBuilder $url_builder): array
241 {
242 return $url_builder->acquireParameters(
243 [self::ID],
244 self::ROW_ID_PARAMETER,
245 self::ACTION_PARAMETER,
246 self::ACTION_TYPE_PARAMETER
247 );
248 }
249
250 protected function getTableActions(): TableActions
251 {
253 $this->ctrl,
254 $this->lng,
255 $this->tpl,
256 $this->ui_factory,
257 $this->ui_renderer,
258 $this->refinery,
259 $this->http,
260 $this->access,
261 $this->participant_repository,
262 $this->ref_id,
263 $this->pool_id,
264 ))->getTableActions();
265 }
266}
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...
__construct(private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer, private readonly ilLanguage $lng, private readonly HttpService $http, private readonly ilUIService $ui_service, private readonly ilCtrlInterface $ctrl, private readonly ilGlobalTemplateInterface $tpl, private readonly Refinery $refinery, private readonly AccessManager $access, private readonly ParticipantRepository $participant_repository, private readonly int $ref_id, private readonly int $pool_id,)
@phpstan-type ParticipantRecord array{user_id: int, name: string, object_title: array<string>,...
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
acquireParameters(array $namespace, string ... $names)
Definition: URLBuilder.php:138
buildURI()
Get a URI representation of the full URL including query string and fragment/hash.
Definition: URLBuilder.php:214
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getList(int $a_pool_id, ?string $a_title=null)
Get list of booking objects.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getList(int $a_booking_pool, ?array $a_filter=null, ?int $a_object_id=null)
static getUserFilter(int $a_pool_id)
Get user data from db for an specific pool id.
language handling
Filter service.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This describes a standard filter.
Definition: Standard.php:27
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:28
buildDataRow(string $id, array $record)
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:66
static http()
Fetches the global http state from ILIAS.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $lng
Definition: privfeed.php:26