ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 mixed $additional_viewcontrol_data,
94 mixed $filter_data,
95 mixed $additional_parameters
96 ): Generator {
97 $records = $this->getRecords();
98
99 if ($order) {
100 [$order_field, $order_direction] = $order->join(
101 [],
102 fn($ret, $key, $value) => [$key, $value]
103 );
104
105 usort($records, static function (array $left, array $right) use ($order_field): int {
106 if (in_array($order_field, ['title', 'awarded_by'], true)) {
107 if (in_array($order_field, ['title', 'awarded_by'], true)) {
108 $order_field .= '_sortable';
109 }
110
111 return ilStr::strCmp(
112 $left[$order_field],
113 $right[$order_field]
114 );
115 }
116
117 if ($order_field === 'active') {
118 return $right[$order_field] <=> $left[$order_field];
119 }
120
121 return $left[$order_field] <=> $right[$order_field];
122 });
123
124 if ($order_direction === Order::DESC) {
125 $records = array_reverse($records);
126 }
127 }
128
129 if ($range) {
130 $records = \array_slice($records, $range->getStart(), $range->getLength());
131 }
132
133 $access_cache = [];
134 $ref_id_cache = [];
135 $parent_obj_ids = [];
136 $identifications = [];
137
138 foreach ($records as $record) {
139 $badge = $record['badge'];
140 $parent_obj_ids[] = $badge->getParentId();
141 $identifications[] = $badge->getImageRid();
142 }
143 $this->irss->preload(array_filter($identifications));
144 $this->object_data_cache->preloadObjectCache(array_unique($parent_obj_ids));
145
146 foreach ($records as $record) {
147 yield $row_builder->buildDataRow((string) $record['id'], $this->enrichRecord(
148 $record,
149 $access_cache,
150 $ref_id_cache
151 ));
152 }
153 }
154
169 private function enrichRecord(
170 array $record,
171 array &$access_cache,
172 array &$ref_id_cache
173 ): array {
174 $badge = $record['badge'];
175 $ass = $record['assignment'];
176
177 $parent = null;
178 if ($badge->getParentId()) {
179 if (isset($this->parent_metadata_cache[$badge->getParentId()])) {
180 $parent = $this->parent_metadata_cache[$badge->getParentId()];
181 } else {
182 $parent = $badge->getParentMeta();
183 $this->parent_metadata_cache[$badge->getParentId()] = $parent;
184 }
185 if ($parent['type'] === 'bdga') {
186 $parent = null;
187 }
188 }
189
190 $awarded_by = '';
191 if ($parent !== null) {
192 if (isset($ref_id_cache[$parent['id']])) {
193 $ref_id = $ref_id_cache[$parent['id']];
194 } else {
195 $ref_id = current(ilObject::_getAllReferences($parent['id']));
196 $ref_id_cache[$parent['id']] = $ref_id;
197 }
198
199 $awarded_by = $parent['title'];
200 if ($ref_id) {
201 $access = $access_cache[$ref_id] ?? $this->access->checkAccess('read', '', $ref_id);
202 if (!isset($access_cache[$ref_id])) {
203 $access_cache[$ref_id] = $access;
204 }
205 }
206 if ($ref_id && $access) {
207 $awarded_by = $this->renderer->render(
208 new Standard(
209 $awarded_by,
210 (string) new URI(ilLink::_getLink($ref_id, $parent['type']))
211 )
212 );
213 }
214
215 $awarded_by = implode(' ', [
216 $this->renderer->render(
217 $this->factory->symbol()->icon()->standard(
218 $parent['type'],
219 $parent['title']
220 )
221 ),
222 $awarded_by
223 ]);
224 }
225
226 $record += [
227 'image' => $this->renderer->render(
228 $this->tile->asImage(
229 $this->tile->modalContentWithAssignment($badge, $ass),
230 ilBadgeImage::IMAGE_SIZE_XS
231 )
232 ),
233 'title' => $this->renderer->render(
234 $this->tile->asTitle(
235 $this->tile->modalContentWithAssignment($badge, $ass)
236 )
237 ),
238 'awarded_by' => $awarded_by,
239 ];
240
241 return $record;
242 }
243
244 public function getTotalRowCount(
245 mixed $additional_viewcontrol_data,
246 mixed $filter_data,
247 mixed $additional_parameters
248 ): ?int {
249 return count($this->getRecords());
250 }
251
263 private function getRecords(): array
264 {
265 if ($this->cached_records !== null) {
266 return $this->cached_records;
267 }
268
269 $rows = [];
270 $a_user_id = $this->user->getId();
271
272 foreach (ilBadgeAssignment::getInstancesByUserId($a_user_id) as $ass) {
273 $badge = new ilBadge($ass->getBadgeId());
274
275 $parent = null;
276 if ($badge->getParentId()) {
277 if (isset($this->parent_metadata_cache[$badge->getParentId()])) {
278 $parent = $this->parent_metadata_cache[$badge->getParentId()];
279 } else {
280 $parent = $badge->getParentMeta();
281 $this->parent_metadata_cache[$badge->getParentId()] = $parent;
282 }
283 if ($parent['type'] === 'bdga') {
284 $parent = null;
285 }
286 }
287
288 $awarded_by_sortable = '';
289 if ($parent !== null) {
290 $awarded_by_sortable = $parent['title'];
291 }
292
293 $rows[] = [
294 'id' => $badge->getId(),
295 'title_sortable' => $badge->getTitle(),
296 'awarded_by_sortable' => $awarded_by_sortable,
297 'badge_issued_on' => (new DateTimeImmutable())
298 ->setTimestamp($ass->getTimestamp())
299 ->setTimezone(new DateTimeZone($this->user->getTimeZone())),
300 'active' => (bool) $ass->getPosition(),
301 'assignment' => $ass,
302 'badge' => $badge
303 ];
304 }
305
306 $this->cached_records = $rows;
307
308 return $rows;
309 }
310
314 private function getColumns(\ILIAS\Data\DateFormat\DateFormat $date_format): array
315 {
316 return [
317 'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
318 'title' => $this->factory->table()->column()->text($this->lng->txt('title')),
319 'awarded_by' => $this->factory->table()->column()->text($this->lng->txt('awarded_by')),
320 'badge_issued_on' => $this->factory->table()->column()->date(
321 $this->lng->txt('badge_issued_on'),
322 $date_format
323 ),
324 'active' => $this->factory->table()->column()->boolean(
325 $this->lng->txt('badge_in_profile'),
326 $this->lng->txt('yes'),
327 $this->lng->txt('no')
328 )->withOrderingLabels(
329 $this->lng->txt('badge_sort_added_to_profile_first'),
330 $this->lng->txt('badge_sort_excluded_from_profile_first')
331 )
332 ];
333 }
334
338 protected function getActions(
339 URLBuilder $url_builder,
340 URLBuilderToken $action_parameter_token,
341 URLBuilderToken $row_id_token
342 ): array {
343 return [
344 'obj_badge_activate' => $this->factory->table()->action()->multi(
345 $this->lng->txt('badge_add_to_profile'),
346 $url_builder->withParameter($action_parameter_token, 'obj_badge_activate'),
347 $row_id_token
348 ),
349 'obj_badge_deactivate' =>
350 $this->factory->table()->action()->multi(
351 $this->lng->txt('badge_remove_from_profile'),
352 $url_builder->withParameter($action_parameter_token, 'obj_badge_deactivate'),
353 $row_id_token
354 )
355 ];
356 }
357
358 public function renderTable(string $url): void
359 {
360 $df = new \ILIAS\Data\Factory();
361
362 $table_uri = $df->uri($url);
363 $url_builder = new URLBuilder($table_uri);
364 $query_params_namespace = ['badge'];
365
366 [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
367 $query_params_namespace,
368 'table_action',
369 'id',
370 );
371
372 $table = $this->factory
373 ->table()
374 ->data(
375 $this,
376 $this->lng->txt('badge_personal_badges'),
377 $this->getColumns($this->user->getDateTimeFormat()),
378 )
379 ->withId(self::class)
380 ->withOrder(new Order('title', Order::ASC))
381 ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
382 ->withRequest($this->request);
383
384 $pres = new PresentationHeader($this->dic, ilBadgeProfileGUI::class);
385 $pres->show($this->lng->txt('table_view'));
386
387 $this->tpl->setContent($this->renderer->render($table));
388 }
389}
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)
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....
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
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...
readonly ILIAS DI Container $dic
readonly ilGlobalTemplateInterface $tpl
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