ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
TileView.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;
28 
29 class TileView
30 {
33 
37  public function __construct(
38  private readonly Container $container,
39  private readonly string $gui,
40  private readonly Tile $tile,
41  private readonly PresentationHeader $head,
42  $assignments_of_user = [ilBadgeAssignment::class, 'getInstancesByUserId']
43  ) {
44  $this->assignments_of_user = Closure::fromCallable($assignments_of_user);
45  }
46 
47  public function show(): string
48  {
49  $sort = new Sorting($this->container->http()->request()->getQueryParams()['sort'] ?? '');
50  $components = $this->componentsOfBadges($this->sort($sort, $this->badgesAndAssignments()));
51 
52  $this->head->show($this->container->language()->txt('tile_view'), $this->sortComponent($sort));
53 
54  return $this->container->ui()->renderer()->render($components);
55  }
56 
60  private function badgesAndAssignments(): array
61  {
62  $badges = [];
63  foreach (($this->assignments_of_user)($this->container->user()->getId()) as $assignment) {
64  $badge = new ilBadge($assignment->getBadgeId());
65  $badges[] = [
66  'badge' => $badge,
67  'assignment' => $assignment,
68  ];
69  }
70 
71  return $badges;
72  }
73 
78  private function componentsOfBadges(array $badge_and_assignments): array
79  {
80  $cards_and_modals = array_map($this->cardsAndModals(...), $badge_and_assignments);
81  $components = array_column($cards_and_modals, 'modal');
82  $components[] = $this->container->ui()
83  ->factory()
84  ->deck(array_column($cards_and_modals, 'card'))
85  ->withNormalCardsSize();
86 
87  return $components;
88  }
89 
94  private function cardsAndModals(array $badge_and_assignments): array
95  {
96  return $this->tile->inDeck(
97  $badge_and_assignments['badge'],
98  $badge_and_assignments['assignment'],
99  $this->gui
100  );
101  }
102 
103  private function sortComponent(Sorting $sort): Component
104  {
105  $txt = [$this->container->language(), 'txt'];
106  $link = $this->container->ctrl()->getLinkTargetByClass($this->gui, 'listBadges');
107  return $this->container->ui()
108  ->factory()
109  ->viewControl()
110  ->sortation(array_map($txt, $sort->options()), $sort->key())
111  ->withTargetURL($link, 'sort');
112  }
113 
119  private function sort(Sorting $sort, array $badges_and_assignments): array
120  {
121  usort($badges_and_assignments, [$sort, 'compare']);
122 
123  return $badges_and_assignments;
124  }
125 }
sortComponent(Sorting $sort)
Definition: TileView.php:103
sort(Sorting $sort, array $badges_and_assignments)
Definition: TileView.php:119
Closure $assignments_of_user
Definition: TileView.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$components
$container
Definition: wac.php:36
$txt
Definition: error.php:31
cardsAndModals(array $badge_and_assignments)
Definition: TileView.php:94
componentsOfBadges(array $badge_and_assignments)
Definition: TileView.php:78
__construct(private readonly Container $container, private readonly string $gui, private readonly Tile $tile, private readonly PresentationHeader $head, $assignments_of_user=[ilBadgeAssignment::class, 'getInstancesByUserId'])
Definition: TileView.php:37