ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilObjectBadgeTableGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Badge;
22 
27 use ilLanguage;
34 use Generator;
37 use ilBadge;
38 use ilBadgeHandler;
39 use ILIAS\Data\URI;
41 use ilObject;
42 use ilLink;
46 use ilAccessHandler;
48 
50 {
51  private readonly Factory $factory;
52  private readonly Renderer $renderer;
53  private readonly \ILIAS\Refinery\Factory $refinery;
55  private readonly Services $http;
56  private readonly ilLanguage $lng;
57  private readonly ilGlobalTemplateInterface $tpl;
59  private readonly ilAccessHandler $access;
73  private ?array $cached_records = null;
74 
75  public function __construct(
76  ilObjBadgeAdministrationGUI $parentObj,
77  protected bool $has_write = false
78  ) {
79  global $DIC;
80 
81  $this->lng = $DIC->language();
82  $this->tpl = $DIC->ui()->mainTemplate();
83  $this->factory = $DIC->ui()->factory();
84  $this->renderer = $DIC->ui()->renderer();
85  $this->refinery = $DIC->refinery();
86  $this->request = $DIC->http()->request();
87  $this->http = $DIC->http();
88  $this->access = $DIC->access();
89  $this->parent_obj = $parentObj;
90  $this->badge_image_service = new ilBadgeImage(
91  $DIC->resourceStorage(),
92  $DIC->upload(),
93  $DIC->ui()->mainTemplate()
94  );
95  }
96 
97  public function getRows(
98  DataRowBuilder $row_builder,
99  array $visible_column_ids,
100  Range $range,
101  Order $order,
102  ?array $filter_data,
103  ?array $additional_parameters
104  ): Generator {
105  $records = $this->getRecords();
106 
107  if ($order) {
108  [$order_field, $order_direction] = $order->join(
109  [],
110  fn($ret, $key, $value) => [$key, $value]
111  );
112 
113  usort($records, static function (array $left, array $right) use ($order_field): int {
114  if (\in_array($order_field, ['container', 'title', 'type'], true)) {
115  if (\in_array($order_field, ['container', 'title'], true)) {
116  $order_field .= '_sortable';
117  }
118 
119  return \ilStr::strCmp(
120  $left[$order_field],
121  $right[$order_field]
122  );
123  }
124 
125  if ($order_field === 'active') {
126  return $right[$order_field] <=> $left[$order_field];
127  }
128 
129  return $left[$order_field] <=> $right[$order_field];
130  });
131 
132  if ($order_direction === Order::DESC) {
133  $records = array_reverse($records);
134  }
135  }
136 
137  if ($range) {
138  $records = \array_slice($records, $range->getStart(), $range->getLength());
139  }
140 
141  foreach ($records as $record) {
142  yield $row_builder->buildDataRow((string) $record['id'], $record);
143  }
144  }
145 
146  public function getTotalRowCount(
147  ?array $filter_data,
148  ?array $additional_parameters
149  ): ?int {
150  return \count($this->getRecords());
151  }
152 
165  private function getRecords(): array
166  {
167  if ($this->cached_records !== null) {
168  return $this->cached_records;
169  }
170 
171  $container_deleted_title_part = '<span class="il_ItemAlertProperty">' . $this->lng->txt('deleted') . '</span>';
172  $modal_container = new ModalBuilder();
173 
174  // A filter is not implemented, yet
175  $filter = [
176  'type' => '',
177  'title' => '',
178  'object' => ''
179  ];
180 
181  $types = ilBadgeHandler::getInstance()->getAvailableTypes(false);
182  $rows = [];
183  foreach (ilBadge::getObjectInstances($filter) as $badge_item) {
184  $type_caption = ilBadge::getExtendedTypeCaption($types[$badge_item['type_id']]);
185 
186  $badge = new ilBadge(0);
187  $badge->setId($badge_item['id']);
188  $badge->setImageRid($badge_item['image_rid']);
189  $badge->setImage($badge_item['image']);
190 
191  $images = [
192  'rendered' => null,
193  'large' => null,
194  ];
195  $image_src = $this->badge_image_service->getImageFromResourceId($badge);
196  if ($image_src !== '') {
197  $images['rendered'] = $this->renderer->render(
198  $this->factory->image()->responsive(
199  $image_src,
200  $badge_item['title']
201  )
202  );
203 
204  $image_src_large = $this->badge_image_service->getImageFromResourceId(
205  $badge,
207  );
208  if ($image_src_large !== '') {
209  $images['large'] = $this->factory->image()->responsive(
210  $image_src_large,
211  $badge_item['title']
212  );
213  }
214  }
215 
216  $sortable_container_title_parts = [
217  'title' => $badge_item['parent_title'] ?? ''
218  ];
219  $container_title_parts = [
220  'icon' => $this->renderer->render(
221  $this->factory->symbol()->icon()->custom(
222  ilObject::_getIcon($badge_item['parent_id'], 'big', $badge_item['parent_type'] ?? ''),
223  $this->lng->txt('obj_' . ($badge_item['parent_type'] ?? ''))
224  )
225  ),
226  'title' => $sortable_container_title_parts['title'],
227  ];
228 
229  if ($badge_item['deleted']) {
230  $container_title_parts['suffix'] = $container_deleted_title_part;
231  $sortable_container_title_parts['suffix'] = $container_deleted_title_part;
232  } else {
233  $ref_ids = ilObject::_getAllReferences($badge_item['parent_id']);
234  $ref_id = array_shift($ref_ids);
235  if ($ref_id && $this->access->checkAccess('read', '', $ref_id)) {
236  $container_title_parts['title'] = $this->renderer->render(
237  new Standard(
238  $container_title_parts['title'],
239  (string) new URI(ilLink::_getLink($ref_id))
240  )
241  );
242  } else {
243  $container_title_parts['suffix'] = $container_deleted_title_part;
244  $sortable_container_title_parts['suffix'] = $container_deleted_title_part;
245  }
246  }
247 
248  $modal = $modal_container->constructModal(
249  $images['large'],
250  $badge_item['title'],
251  [
252  'active' => $badge_item['active'] ? $this->lng->txt('yes') : $this->lng->txt('no'),
253  'type' => $type_caption,
254  'container' => implode(' ', \array_slice($container_title_parts, 1, null, true)),
255  ]
256  );
257 
258  $rows[] = [
259  'id' => $badge_item['id'],
260  'active' => (bool) $badge_item['active'],
261  'type' => $type_caption,
262  'image' => $images['rendered'] ? ($modal_container->renderShyButton(
263  $images['rendered'],
264  $modal
265  ) . ' ') : '',
266  'title' => implode('', [
267  $modal_container->renderShyButton($badge_item['title'], $modal),
268  $modal_container->renderModal($modal)
269  ]),
270  'title_sortable' => $badge_item['title'],
271  'container' => implode(' ', $container_title_parts),
272  'container_sortable' => implode(' ', $sortable_container_title_parts),
273  ];
274  }
275 
276  $this->cached_records = $rows;
277 
278  return $rows;
279  }
280 
284  public function getColumns(): array
285  {
286  return [
287  'image' => $this->factory->table()->column()->text($this->lng->txt('image'))->withIsSortable(false),
288  'title' => $this->factory->table()->column()->text($this->lng->txt('title')),
289  'type' => $this->factory->table()->column()->text($this->lng->txt('type')),
290  'container' => $this->factory->table()->column()->text($this->lng->txt('container')),
291  'active' => $this->factory->table()->column()->boolean(
292  $this->lng->txt('active'),
293  $this->lng->txt('yes'),
294  $this->lng->txt('no')
295  )->withOrderingLabels(
296  $this->lng->txt('badge_sort_active_badges_first'),
297  $this->lng->txt('badge_sort_active_badges_last')
298  )
299  ];
300  }
301 
305  private function getActions(
306  URLBuilder $url_builder,
307  URLBuilderToken $action_parameter_token,
308  URLBuilderToken $row_id_token
309  ): array {
310  return $this->has_write ? [
311  'obj_badge_activate' => $this->factory->table()->action()->multi(
312  $this->lng->txt('activate'),
313  $url_builder->withParameter($action_parameter_token, 'obj_badge_activate'),
314  $row_id_token
315  ),
316  'obj_badge_deactivate' =>
317  $this->factory->table()->action()->multi(
318  $this->lng->txt('deactivate'),
319  $url_builder->withParameter($action_parameter_token, 'obj_badge_deactivate'),
320  $row_id_token
321  ),
322  'obj_badge_delete' =>
323  $this->factory->table()->action()->multi(
324  $this->lng->txt('delete'),
325  $url_builder->withParameter($action_parameter_token, 'obj_badge_delete'),
326  $row_id_token
327  ),
328  'obj_badge_show_users' =>
329  $this->factory->table()->action()->single(
330  $this->lng->txt('user'),
331  $url_builder->withParameter($action_parameter_token, 'obj_badge_show_users'),
332  $row_id_token
333  )
334  ] : [];
335  }
336 
337  public function renderTable(): void
338  {
339  $df = new \ILIAS\Data\Factory();
340 
341  $table_uri = $df->uri($this->request->getUri()->__toString());
342  $url_builder = new URLBuilder($table_uri);
343  $query_params_namespace = ['tid'];
344 
345  [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
346  $query_params_namespace,
347  'table_action',
348  'id',
349  );
350 
351  $table = $this->factory
352  ->table()
353  ->data($this, $this->lng->txt('badge_object_badges'), $this->getColumns())
354  ->withId(self::class)
355  ->withOrder(new Order('title', Order::ASC))
356  ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
357  ->withRequest($this->request);
358 
359  $out = [$table];
360 
361  $query = $this->http->wrapper()->query();
362  if ($query->has($action_parameter_token->getName())) {
363  $action = $query->retrieve($action_parameter_token->getName(), $this->refinery->kindlyTo()->string());
364  $ids = $query->retrieve($row_id_token->getName(), $this->refinery->custom()->transformation(fn($v) => $v));
365 
366  if ($action === 'obj_badge_delete') {
367  $items = [];
368  if (\is_array($ids) && \count($ids) > 0) {
369  if ($ids === ['ALL_OBJECTS']) {
370  $filter = [
371  'type' => '',
372  'title' => '',
373  'object' => ''
374  ];
375  $ids = [];
376  foreach (ilBadge::getObjectInstances($filter) as $badge_item) {
377  $ids[] = $badge_item['id'];
378  }
379  }
380 
381  foreach ($ids as $id) {
382  $badge = new ilBadge((int) $id);
383  $items[] = $this->factory->modal()->interruptiveItem()->keyValue(
384  (string) $id,
385  (string) $badge->getId(),
386  $badge->getTitle()
387  );
388  }
389 
390  $this->http->saveResponse(
391  $this->http
392  ->response()
393  ->withBody(
394  Streams::ofString($this->renderer->renderAsync([
395  $this->factory->modal()->interruptive(
396  $this->lng->txt('badge_deletion'),
397  $this->lng->txt('badge_deletion_confirmation'),
398  '#'
399  )->withAffectedItems($items)
400  ]))
401  )
402  );
403  $this->http->sendResponse();
404  $this->http->close();
405  }
406  }
407  }
408 
409  $this->tpl->setContent($this->renderer->render($out));
410  }
411 }
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
join($init, callable $fn)
Definition: Order.php:75
static _getIcon(int $obj_id=0, string $size="big", string $type="", bool $offline=false)
Get icon for repository item.
__construct(ilObjBadgeAdministrationGUI $parentObj, protected bool $has_write=false)
factory()
static _getAllReferences(int $id)
get all reference ids for object ID
renderer()
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
readonly ServerRequestInterface RequestInterface $request
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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...
$ref_id
Definition: ltiauth.php:65
static http()
Fetches the global http state from ILIAS.
readonly ilObjBadgeAdministrationGUI $parent_obj
static getExtendedTypeCaption(ilBadgeType $a_type)
$out
Definition: buildRTE.php:24
global $DIC
Definition: shib_login.php:22
Builds data types.
Definition: Factory.php:35
readonly ilGlobalTemplateInterface $tpl
readonly ILIAS Refinery Factory $refinery
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter&#39;s value if the supplied token is valid.
Definition: URLBuilder.php:166
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static getObjectInstances(?array $filter=null)
URLBuilder.
Definition: URLBuilder.php:40
const DESC
Definition: Order.php:31
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28