ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBadgePersonalTableGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Message\RequestInterface;
38
40{
41 private readonly Factory $factory;
42 private readonly Renderer $renderer;
43 private readonly ServerRequestInterface|RequestInterface $request;
44 private readonly ilLanguage $lng;
46 private readonly ILIAS\DI\Container $dic;
47 private readonly ilObjUser $user;
48 private readonly ilAccessHandler $access;
49 private readonly Tile $tile;
50 private readonly IRSS $irss;
51
64 private ?array $cached_records = null;
66
71
72 public function __construct()
73 {
74 global $DIC;
75 $this->dic = $DIC;
76 $this->lng = $DIC->language();
77 $this->tpl = $DIC->ui()->mainTemplate();
78 $this->factory = $DIC->ui()->factory();
79 $this->renderer = $DIC->ui()->renderer();
80 $this->request = $DIC->http()->request();
81 $this->user = $DIC->user();
82 $this->access = $DIC->access();
83 $this->tile = new Tile($DIC);
84 $this->irss = $DIC['resource_storage'];
85 $this->object_data_cache = $DIC['ilObjDataCache'];
86 }
87
88 public function getRows(
89 DataRowBuilder $row_builder,
90 array $visible_column_ids,
92 Order $order,
93 ?array $filter_data,
94 ?array $additional_parameters
95 ): Generator {
96 $records = $this->getRecords();
97
98 if ($order) {
99 [$order_field, $order_direction] = $order->join(
100 [],
101 fn($ret, $key, $value) => [$key, $value]
102 );
103
104 usort($records, static function (array $left, array $right) use ($order_field): int {
105 if (in_array($order_field, ['title', 'awarded_by'], true)) {
106 if (in_array($order_field, ['title', 'awarded_by'], true)) {
107 $order_field .= '_sortable';
108 }
109
110 return ilStr::strCmp(
111 $left[$order_field],
112 $right[$order_field]
113 );
114 }
115
116 if ($order_field === 'active') {
117 return $right[$order_field] <=> $left[$order_field];
118 }
119
120 return $left[$order_field] <=> $right[$order_field];
121 });
122
123 if ($order_direction === Order::DESC) {
124 $records = array_reverse($records);
125 }
126 }
127
128 if ($range) {
129 $records = \array_slice($records, $range->getStart(), $range->getLength());
130 }
131
132 $access_cache = [];
133 $ref_id_cache = [];
134 $parent_obj_ids = [];
135 $identifications = [];
136
137 foreach ($records as $record) {
138 $badge = $record['badge'];
139 $parent_obj_ids[] = $badge->getParentId();
140 $identifications[] = $badge->getImageRid();
141 }
142 $this->irss->preload(array_filter($identifications));
143 $this->object_data_cache->preloadObjectCache(array_unique($parent_obj_ids));
144
145 foreach ($records as $record) {
146 yield $row_builder->buildDataRow((string) $record['id'], $this->enrichRecord(
147 $record,
148 $access_cache,
149 $ref_id_cache
150 ));
151 }
152 }
153
168 private function enrichRecord(
169 array $record,
170 array &$access_cache,
171 array &$ref_id_cache
172 ): array {
173 $badge = $record['badge'];
174 $ass = $record['assignment'];
175
176 $parent = null;
177 if ($badge->getParentId()) {
178 if (isset($this->parent_metadata_cache[$badge->getParentId()])) {
179 $parent = $this->parent_metadata_cache[$badge->getParentId()];
180 } else {
181 $parent = $badge->getParentMeta();
182 $this->parent_metadata_cache[$badge->getParentId()] = $parent;
183 }
184 if ($parent['type'] === 'bdga') {
185 $parent = null;
186 }
187 }
188
189 $awarded_by = '';
190 if ($parent !== null) {
191 if (isset($ref_id_cache[$parent['id']])) {
192 $ref_id = $ref_id_cache[$parent['id']];
193 } else {
194 $ref_id = current(ilObject::_getAllReferences($parent['id']));
195 $ref_id_cache[$parent['id']] = $ref_id;
196 }
197
198 $awarded_by = $parent['title'];
199 if ($ref_id) {
200 $access = $access_cache[$ref_id] ?? $this->access->checkAccess('read', '', $ref_id);
201 if (!isset($access_cache[$ref_id])) {
202 $access_cache[$ref_id] = $access;
203 }
204 }
205 if ($ref_id && $access) {
206 $awarded_by = $this->renderer->render(
207 new Standard(
208 $awarded_by,
209 (string) new URI(ilLink::_getLink($ref_id, $parent['type']))
210 )
211 );
212 }
213
214 $awarded_by = implode(' ', [
215 $this->renderer->render(
216 $this->factory->symbol()->icon()->standard(
217 $parent['type'],
218 $parent['title']
219 )
220 ),
221 $awarded_by
222 ]);
223 }
224
225 $record += [
226 'image' => $this->renderer->render(
227 $this->tile->asImage(
228 $this->tile->modalContentWithAssignment($badge, $ass),
229 ilBadgeImage::IMAGE_SIZE_XS
230 )
231 ),
232 'title' => $this->renderer->render(
233 $this->tile->asTitle(
234 $this->tile->modalContentWithAssignment($badge, $ass)
235 )
236 ),
237 'awarded_by' => $awarded_by,
238 ];
239
240 return $record;
241 }
242
243 public function getTotalRowCount(
244 ?array $filter_data,
245 ?array $additional_parameters
246 ): ?int {
247 return count($this->getRecords());
248 }
249
261 private function getRecords(): array
262 {
263 if ($this->cached_records !== null) {
264 return $this->cached_records;
265 }
266
267 $rows = [];
268 $a_user_id = $this->user->getId();
269
270 foreach (ilBadgeAssignment::getInstancesByUserId($a_user_id) as $ass) {
271 $badge = new ilBadge($ass->getBadgeId());
272
273 $parent = null;
274 if ($badge->getParentId()) {
275 if (isset($this->parent_metadata_cache[$badge->getParentId()])) {
276 $parent = $this->parent_metadata_cache[$badge->getParentId()];
277 } else {
278 $parent = $badge->getParentMeta();
279 $this->parent_metadata_cache[$badge->getParentId()] = $parent;
280 }
281 if ($parent['type'] === 'bdga') {
282 $parent = null;
283 }
284 }
285
286 $awarded_by_sortable = '';
287 if ($parent !== null) {
288 $awarded_by_sortable = $parent['title'];
289 }
290
291 $rows[] = [
292 'id' => $badge->getId(),
293 'title_sortable' => $badge->getTitle(),
294 'awarded_by_sortable' => $awarded_by_sortable,
295 'badge_issued_on' => (new DateTimeImmutable())
296 ->setTimestamp($ass->getTimestamp())
297 ->setTimezone(new DateTimeZone($this->user->getTimeZone())),
298 'active' => (bool) $ass->getPosition(),
299 'assignment' => $ass,
300 'badge' => $badge
301 ];
302 }
303
304 $this->cached_records = $rows;
305
306 return $rows;
307 }
308
312 private function getColumns(\ILIAS\Data\DateFormat\DateFormat $date_format): array
313 {
314 return [
315 'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
316 'title' => $this->factory->table()->column()->text($this->lng->txt('title')),
317 'awarded_by' => $this->factory->table()->column()->text($this->lng->txt('awarded_by')),
318 'badge_issued_on' => $this->factory->table()->column()->date(
319 $this->lng->txt('badge_issued_on'),
320 $date_format
321 ),
322 'active' => $this->factory->table()->column()->boolean(
323 $this->lng->txt('badge_in_profile'),
324 $this->lng->txt('yes'),
325 $this->lng->txt('no')
326 )->withOrderingLabels(
327 $this->lng->txt('badge_sort_added_to_profile_first'),
328 $this->lng->txt('badge_sort_excluded_from_profile_first')
329 )
330 ];
331 }
332
336 protected function getActions(
337 URLBuilder $url_builder,
338 URLBuilderToken $action_parameter_token,
339 URLBuilderToken $row_id_token
340 ): array {
341 return [
342 'obj_badge_activate' => $this->factory->table()->action()->multi(
343 $this->lng->txt('badge_add_to_profile'),
344 $url_builder->withParameter($action_parameter_token, 'obj_badge_activate'),
345 $row_id_token
346 ),
347 'obj_badge_deactivate' =>
348 $this->factory->table()->action()->multi(
349 $this->lng->txt('badge_remove_from_profile'),
350 $url_builder->withParameter($action_parameter_token, 'obj_badge_deactivate'),
351 $row_id_token
352 )
353 ];
354 }
355
356 public function renderTable(string $url): void
357 {
358 $df = new \ILIAS\Data\Factory();
359
360 $table_uri = $df->uri($url);
361 $url_builder = new URLBuilder($table_uri);
362 $query_params_namespace = ['badge'];
363
364 [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
365 $query_params_namespace,
366 'table_action',
367 'id',
368 );
369
370 $table = $this->factory
371 ->table()
372 ->data(
373 $this,
374 $this->lng->txt('badge_personal_badges'),
375 $this->getColumns($this->user->getDateTimeFormat()),
376 )
377 ->withId(self::class)
378 ->withOrder(new Order('title', Order::ASC))
379 ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
380 ->withRequest($this->request);
381
382 $pres = new PresentationHeader($this->dic, ilBadgeProfileGUI::class);
383 $pres->show($this->lng->txt('table_view'));
384
385 $this->tpl->setContent($this->renderer->render($table));
386 }
387}
renderer()
factory()
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
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
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
static getInstancesByUserId(int $a_user_id)
getColumns(\ILIAS\Data\DateFormat\DateFormat $date_format)
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
enrichRecord(array $record, array &$access_cache, array &$ref_id_cache)
readonly ServerRequestInterface RequestInterface $request
readonly ILIAS DI Container $dic
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
readonly ilGlobalTemplateInterface $tpl
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...
language handling
User class.
class ilObjectDataCache
static _getAllReferences(int $id)
get all reference ids for object ID
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:28
buildDataRow(string $id, array $record)
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
$ref_id
Definition: ltiauth.php:66
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68