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