ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilBadgeTableGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Badge;
22
27use ilLanguage;
30use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Message\RequestInterface;
34use Generator;
37use ilBadge;
38use ilBadgeAuto;
42
44{
45 private readonly Factory $factory;
46 private readonly Renderer $renderer;
47 private readonly \ILIAS\Refinery\Factory $refinery;
48 private readonly ServerRequestInterface|RequestInterface $request;
49 private readonly Services $http;
50 private readonly int $parent_id;
51 private readonly string $parent_type;
52 private readonly ilLanguage $lng;
54 private readonly \ILIAS\ResourceStorage\Services $irss;
68 private ?array $cached_records = null;
69
70 public function __construct(int $parent_obj_id, string $parent_obj_type, protected bool $has_write = false)
71 {
72 global $DIC;
73
74 $this->lng = $DIC->language();
75 $this->tpl = $DIC->ui()->mainTemplate();
76 $this->factory = $DIC->ui()->factory();
77 $this->renderer = $DIC->ui()->renderer();
78 $this->refinery = $DIC->refinery();
79 $this->request = $DIC->http()->request();
80 $this->http = $DIC->http();
81 $this->irss = $DIC->resourceStorage();
82
83 $this->parent_id = $parent_obj_id;
84 $this->parent_type = $parent_obj_type;
85 $this->badge_image_service = new ilBadgeImage(
86 $this->irss,
87 $DIC->upload(),
88 $DIC->ui()->mainTemplate()
89 );
90 }
91
102 private function getRecords(): array
103 {
104 if ($this->cached_records !== null) {
106 }
107
108 $rows = [];
109 foreach (ilBadge::getInstancesByParentId($this->parent_id) as $badge) {
110 $rows[] = [
111 'id' => $badge->getId(),
112 'badge' => $badge,
113 'active' => $badge->isActive(),
114 'type' => $this->parent_type !== 'bdga'
115 ? ilBadge::getExtendedTypeCaption($badge->getTypeInstance())
116 : $badge->getTypeInstance()->getCaption(),
117 'manual' => !$badge->getTypeInstance() instanceof ilBadgeAuto,
118 'title_sortable' => $badge->getTitle()
119 ];
120 }
121
122 $this->cached_records = $rows;
123
124 return $rows;
125 }
126
147 private function enrichRecord(ModalBuilder $modal_builder, array $record): array
148 {
149 $badge = $record['badge'];
150
151 $images = [
152 'rendered' => null,
153 'large' => null,
154 ];
155
156 $image_src = $this->badge_image_service->getImageFromBadge($badge);
157 if ($image_src !== '') {
158 $images['rendered'] = $this->renderer->render(
159 $this->factory->image()->responsive(
160 $image_src,
161 $badge->getTitle()
162 )
163 );
164
165 $image_src_large = $this->badge_image_service->getImageFromBadge(
166 $badge,
168 );
169 if ($image_src_large !== '') {
170 $images['large'] = $this->factory->image()->responsive(
171 $image_src_large,
172 $badge->getTitle()
173 );
174 }
175 }
176
177 $modal = $modal_builder->constructModal(
178 $images['large'],
179 $badge->getTitle(),
180 [
181 'description' => $badge->getDescription(),
182 'badge_criteria' => $badge->getCriteria(),
183 ],
184 true
185 );
186
187 $record['image'] = $images['rendered']
188 ? $modal_builder->renderShyButton($images['rendered'], $modal) . ' '
189 : '';
190 $record['title'] = implode('', [
191 $modal_builder->renderShyButton($badge->getTitle(), $modal),
192 $modal_builder->renderModal($modal)
193 ]);
194
195 return $record;
196 }
197
198
199 public function getRows(
200 DataRowBuilder $row_builder,
201 array $visible_column_ids,
203 Order $order,
204 mixed $additional_viewcontrol_data,
205 mixed $filter_data,
206 mixed $additional_parameters
207 ): Generator {
208 $records = $this->getRecords();
209
210 if ($order) {
211 [$order_field, $order_direction] = $order->join(
212 [],
213 fn($ret, $key, $value) => [$key, $value]
214 );
215
216 usort($records, static function (array $left, array $right) use ($order_field): int {
217 if (\in_array($order_field, ['title', 'type'], true)) {
218 if ($order_field === 'title') {
219 $order_field .= '_sortable';
220 }
221
222 return \ilStr::strCmp(
223 $left[$order_field],
224 $right[$order_field]
225 );
226 }
227
228 if ($order_field === 'active') {
229 return $right[$order_field] <=> $left[$order_field];
230 }
231
232 return $left[$order_field] <=> $right[$order_field];
233 });
234
235 if ($order_direction === Order::DESC) {
236 $records = array_reverse($records);
237 }
238 }
239
240 if ($range) {
241 $records = \array_slice($records, $range->getStart(), $range->getLength());
242 }
243
244 $identifications = [];
245 foreach ($records as $record) {
246 if ($record['badge']->getImageRid() !== null && $record['badge']->getImageRid() !== '') {
247 $identifications[] = $record['badge']->getImageRid();
248 }
249 }
250
251 $this->irss->preload($identifications);
252
253 $modal_container = new ModalBuilder();
254 foreach ($records as $record) {
255 $record = $this->enrichRecord($modal_container, $record);
256
257 yield $row_builder
258 ->buildDataRow((string) $record['id'], $record)
259 ->withDisabledAction(
260 'award_revoke_badge',
261 !$record['manual'] || !$record['active']
262 );
263 }
264 }
265
266 public function getTotalRowCount(
267 mixed $additional_viewcontrol_data,
268 mixed $filter_data,
269 mixed $additional_parameters
270 ): ?int {
271 return \count($this->getRecords());
272 }
273
277 private function getColumns(): array
278 {
279 return [
280 'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
281 'title' => $this->factory->table()->column()->text($this->lng->txt('title')),
282 'type' => $this->factory->table()->column()->text($this->lng->txt('type')),
283 'active' => $this->factory->table()->column()->boolean(
284 $this->lng->txt('active'),
285 $this->lng->txt('yes'),
286 $this->lng->txt('no')
287 )->withOrderingLabels(
288 $this->lng->txt('badge_sort_active_badges_first'),
289 $this->lng->txt('badge_sort_active_badges_last')
290 )
291 ];
292 }
293
297 private function getActions(
298 URLBuilder $url_builder,
299 URLBuilderToken $action_parameter_token,
300 URLBuilderToken $row_id_token,
301 ): array {
302 return $this->has_write ? [
303 'badge_table_activate' =>
304 $this->factory->table()->action()->multi(
305 $this->lng->txt('activate'),
306 $url_builder->withParameter($action_parameter_token, 'badge_table_activate'),
307 $row_id_token
308 ),
309 'badge_table_deactivate' =>
310 $this->factory->table()->action()->multi(
311 $this->lng->txt('deactivate'),
312 $url_builder->withParameter($action_parameter_token, 'badge_table_deactivate'),
313 $row_id_token
314 ),
315 'badge_table_edit' => $this->factory->table()->action()->single(
316 $this->lng->txt('edit'),
317 $url_builder->withParameter($action_parameter_token, 'badge_table_edit'),
318 $row_id_token
319 ),
320 'badge_table_delete' =>
321 $this->factory->table()->action()->standard(
322 $this->lng->txt('delete'),
323 $url_builder->withParameter($action_parameter_token, 'badge_table_delete'),
324 $row_id_token
325 ),
326 'award_revoke_badge' =>
327 $this->factory->table()->action()->single(
328 $this->lng->txt('badge_award_revoke'),
329 $url_builder->withParameter($action_parameter_token, 'award_revoke_badge'),
330 $row_id_token
331 )
332 ] : [];
333 }
334
335 public function renderTable(string $url): void
336 {
337 $df = new \ILIAS\Data\Factory();
338
339 $table_uri = $df->uri($url);
340 $url_builder = new URLBuilder($table_uri);
341 $query_params_namespace = ['tid'];
342
343 [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
344 $query_params_namespace,
345 'table_action',
346 'id',
347 );
348
349 $table = $this->factory
350 ->table()
351 ->data($this, $this->lng->txt('obj_bdga'), $this->getColumns())
352 ->withId(str_replace('\\', '', self::class) . '_' . $this->parent_id)
353 ->withOrder(new Order('title', Order::ASC))
354 ->withRange(new Range(0, 100))
355 ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
356 ->withRequest($this->request);
357
358 $query = $this->http->wrapper()->query();
359
360 if ($query->has($action_parameter_token->getName())) {
361 $action = $query->retrieve($action_parameter_token->getName(), $this->refinery->to()->string());
362 $ids = $query->retrieve($row_id_token->getName(), $this->refinery->custom()->transformation(fn($v) => $v));
363
364 if ($action === 'delete') {
365 $items = [];
366 foreach ($ids as $id) {
367 $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
368 $id,
369 $row_id_token->getName(),
370 $id
371 );
372 }
373
374 $this->http->saveResponse(
375 $this->http
376 ->response()
377 ->withBody(
378 Streams::ofString($this->renderer->renderAsync([
379 $this->factory->modal()->interruptive(
380 $this->lng->txt('badge_deletion'),
381 $this->lng->txt('badge_deletion_confirmation'),
382 '#'
383 )->withAffectedItems($items)
384 ]))
385 )
386 );
387 $this->http->sendResponse();
388 $this->http->close();
389 }
390 }
391
392 $content_wrapper = new TableContentWrapper($this->renderer, $this->factory);
393 $this->tpl->setContent($this->renderer->render(
394 $content_wrapper->wrap(
395 $table
396 )
397 ));
398 }
399}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
renderer()
factory()
renderShyButton(string $label, Modal $modal)
renderModal(Modal $modal)
constructModal(?Image $badge_image, string $badge_title, array $badge_properties=[], bool $enclose_in_div=false)
This class provides a central helper method to wrap the content of a KS/UI > Table > Data into a spec...
readonly ILIAS Refinery Factory $refinery
readonly ILIAS ResourceStorage Services $irss
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...
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....
__construct(int $parent_obj_id, string $parent_obj_type, protected bool $has_write=false)
readonly ServerRequestInterface RequestInterface $request
readonly ilGlobalTemplateInterface $tpl
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token,)
readonly ilBadgeImage $badge_image_service
enrichRecord(ModalBuilder $modal_builder, array $record)
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
const DESC
Definition: Order.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
Class Services.
Definition: Services.php:38
static getExtendedTypeCaption(ilBadgeType $a_type)
static getInstancesByParentId(int $a_parent_id, ?array $a_filter=null)
language handling
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
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:70