ILIAS  trunk Revision v11.0_alpha-1846-g895b5f47236
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
BadgeParent.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Badge;
22 
23 use Closure;
26 use ilBadge;
27 use ilLink;
28 use ilObject;
29 
31 {
33  private readonly Closure $icon;
35  private readonly Closure $references_of;
37  private readonly Closure $link_to;
38 
44  public function __construct(
45  private readonly Container $container,
46  $icon = [ilObject::class, '_getIcon'],
47  $references_of = [ilObject::class, '_getAllReferences'],
48  $link_to = [ilLink::class, '_getLink']
49  ) {
50  $this->icon = Closure::fromCallable($icon);
51  $this->references_of = Closure::fromCallable($references_of);
52  $this->link_to = Closure::fromCallable($link_to);
53  }
54 
55  public function asComponent(ilBadge $badge): ?Component
56  {
57  $meta_data = $this->metaData($badge);
58  if (null === $meta_data) {
59  return null;
60  }
61 
62  $parent_icon = $this->container->ui()->factory()->symbol()->icon()->custom(
63  ($this->icon)($meta_data['id'], 'big', $meta_data['type']),
64  $this->container->language()->txt('obj_' . $meta_data['type']),
65  'medium'
66  );
67 
68  $parent_ref_id = current(($this->references_of)($meta_data['id']));
69  if ($parent_ref_id && $this->container->access()->checkAccess('read', '', $parent_ref_id)) {
70  $parent_link = $this->container->ui()
71  ->factory()
72  ->link()
73  ->standard($meta_data['title'], ($this->link_to)($parent_ref_id));
74  } else {
75  $parent_link = $this->container->ui()->factory()->legacy()->content($meta_data['title']);
76  }
77 
78  return $this->container->ui()->factory()->listing()->descriptive([
79  $this->container->language()->txt('object') => $this->container->ui()->factory()->legacy()->content(
80  $this->container->ui()->renderer()->render([$parent_icon, $parent_link])
81  )
82  ]);
83  }
84 
85  public function asProperty(ilBadge $badge): ?string
86  {
87  $meta_data = $this->metaData($badge);
88  if (null === $meta_data) {
89  return null;
90  }
91 
92  $icon = [$this->container->ui()->factory()->symbol()->icon(), 'custom'];
93 
94  return implode(' ', [
95  $this->container->ui()->renderer()->render($icon(($this->icon)($meta_data['id'], 'small', $meta_data['type']), $meta_data['title'])),
96  $meta_data['title'],
97  ]);
98  }
99 
100  private function metaData(ilBadge $badge): ?array
101  {
102  if (!$badge->getParentId()) {
103  return null;
104  }
105  $parent = $badge->getParentMeta();
106  if ($parent['type'] === 'bdga') {
107  return null;
108  }
109 
110  return $parent;
111  }
112 }
__construct(private readonly Container $container, $icon=[ilObject::class, '_getIcon'], $references_of=[ilObject::class, '_getAllReferences'], $link_to=[ilLink::class, '_getLink'])
Definition: BadgeParent.php:44
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$container
Definition: wac.php:36
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
readonly Closure $link_to
Definition: BadgeParent.php:37
readonly Closure $icon
Definition: BadgeParent.php:33
metaData(ilBadge $badge)
asProperty(ilBadge $badge)
Definition: BadgeParent.php:85
readonly Closure $references_of
Definition: BadgeParent.php:35
asComponent(ilBadge $badge)
Definition: BadgeParent.php:55