ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Tile.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;
27 use ilBadge;
30 use ilDateTime;
32 use ilWACSignedPath;
33 
34 class Tile
35 {
37  private readonly Closure $sign_file;
38  private readonly BadgeParent $parent;
39  private readonly Modal $modal;
41  private readonly Closure $format_date;
43 
48  public function __construct(
49  private readonly Container $container,
50  ?BadgeParent $parent = null,
51  ?Modal $modal = null,
52  $sign_file = [ilWACSignedPath::class, 'signFile'],
53  ?Closure $format_date = null,
54  ) {
55  $this->parent = $parent ?? new BadgeParent($this->container);
56  $this->modal = $modal ?? new Modal($this->container);
57  $this->sign_file = Closure::fromCallable($sign_file);
58  if (!$format_date) {
59  class_exists(ilDateTime::class); // Ensure ilDateTime is loaded as IL_CAL_UNIX is defined in ilDateTime.php.
60  $format_date = static fn($date, int $format = IL_CAL_UNIX): string => (
61  ilDatePresentation::formatDate(new ilDateTime($date, $format))
62  );
63  }
64  $this->format_date = $format_date;
65  $this->badge_image_service = new ilBadgeImage($container->resourceStorage(), $container->upload(), $container->ui()->mainTemplate());
66  }
67 
71  public function inDeck(ilBadge $badge, ilBadgeAssignment $assignment, string $gui): array
72  {
73  $parent = $this->parent->asComponent($badge);
74  $badge_sections = $parent ? [$parent] : [];
75  $badge_sections[] = $this->profileButton($badge, $assignment, $gui);
76 
77  $content = $this->modalContentWithAssignment($badge, $assignment);
78  $card = $this->card($content);
79  $modal = $this->modal($card);
80  $image = $this->image($modal, $badge);
81  $card = $card->withSections($badge_sections)
82  ->withImage($image)
83  ->withTitleAction($modal->getShowSignal());
84 
85  return [
86  'card' => $card,
87  'modal' => $modal,
88  ];
89  }
90 
94  public function asTitleWithLeadingImage(ModalContent $content): array
95  {
96  $modal = $this->modal($this->card($content));
97  return [
98  $modal,
99  $this->image($modal, $content->badge()),
100  $this->title($modal, $content->badge()),
101  ];
102  }
103 
107  public function asImage(ModalContent $content, int $size = ilBadgeImage::IMAGE_SIZE_M): array
108  {
109  $modal = $this->modal($this->card($content));
110  return [
111  $modal,
112  $this->image($modal, $content->badge(), $size),
113  ];
114  }
115 
119  public function asTitle(ModalContent $content): array
120  {
121  $modal = $this->modal($this->card($content));
122  return [
123  $modal,
124  $this->title($modal, $content->badge()),
125  ];
126  }
127 
128  private function card(ModalContent $content): Component
129  {
130  return $this->container
131  ->ui()
132  ->factory()
133  ->card()
134  ->standard($content->badge()->getTitle())
135  ->withHiddenSections($this->modal->components($content));
136  }
137 
138  private function modal(Card $card): Component
139  {
140  return $this->container->ui()->factory()->modal()->lightbox(
141  $this->container->ui()->factory()->modal()->lightboxCardPage($card)
142  );
143  }
144 
145  private function image(Component $modal, ilBadge $badge, int $size = ilBadgeImage::IMAGE_SIZE_M): Component
146  {
147  $image_src = $this->badge_image_service->getImageFromBadge($badge, $size);
148  return $this->container
149  ->ui()
150  ->factory()
151  ->image()
152  ->responsive($image_src, $badge->getTitle())
153  ->withAction($modal->getShowSignal());
154  }
155 
156  private function title(Component $modal, ilBadge $badge): Component
157  {
158  return $this->container
159  ->ui()
160  ->factory()
161  ->button()
162  ->shy($badge->getTitle(), $modal->getShowSignal());
163  }
164 
165  public function modalContent(ilBadge $badge): ModalContent
166  {
167  $awarded_by = $this->parent->asProperty($badge);
168  return new ModalContent($badge, [
169  $this->txt('criteria') => $badge->getCriteria(),
170  ...($awarded_by !== null ? [$this->txt('awarded_by') => $awarded_by] : []),
171  $this->txt('valid_until') => $this->tryFormating($badge->getValid()),
172  ]);
173  }
174 
175  public function modalContentWithAssignment(ilBadge $badge, ilBadgeAssignment $assignment): ModalContent
176  {
177  return $this->addAssignment($this->modalContent($badge), $assignment);
178  }
179 
180  public function addAssignment(ModalContent $content, ilBadgeAssignment $assignment): ModalContent
181  {
182  return $content->withAdditionalProperties([
183  $this->txt('badge_issued_on') => ($this->format_date)($assignment->getTimestamp()),
184  ]);
185  }
186 
187  private function txt(string $key): string
188  {
189  return $this->container->language()->txt($key);
190  }
191 
192  private function tryFormating(string $valid): string
193  {
194  if (!$valid) {
195  return $this->txt('endless');
196  }
197 
198  try {
199  return ($this->format_date)($valid, IL_CAL_DATE);
200  } catch (ilDateTimeException) {
201  return $valid;
202  }
203  }
204 
205  private function profileButton(ilBadge $badge, ilBadgeAssignment $assignment, string $gui): Component
206  {
207  $active = $assignment->getPosition();
208 
209  $this->container->ctrl()->setParameterByClass($gui, 'badge_id', $badge->getId());
210 
211  $url = $this->container->ctrl()->getLinkTargetByClass(
212  $gui,
213  $active ? 'deactivateInCard' : 'activateInCard'
214  );
215 
216  $this->container->ctrl()->setParameterByClass($gui, 'badge_id', '');
217 
218  return $this->container->ui()->factory()->button()->standard(
219  $this->txt($active ? 'badge_remove_from_profile' : 'badge_add_to_profile'),
220  $url
221  );
222  }
223 }
addAssignment(ModalContent $content, ilBadgeAssignment $assignment)
Definition: Tile.php:180
readonly ilBadgeImage $badge_image_service
Definition: Tile.php:42
$valid
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
asTitle(ModalContent $content)
Definition: Tile.php:119
$url
Definition: shib_logout.php:66
const IL_CAL_UNIX
asImage(ModalContent $content, int $size=ilBadgeImage::IMAGE_SIZE_M)
Definition: Tile.php:107
modal(Card $card)
Definition: Tile.php:138
$container
Definition: wac.php:36
modalContent(ilBadge $badge)
Definition: Tile.php:165
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
tryFormating(string $valid)
Definition: Tile.php:192
__construct(private readonly Container $container, ?BadgeParent $parent=null, ?Modal $modal=null, $sign_file=[ilWACSignedPath::class, 'signFile'], ?Closure $format_date=null,)
Definition: Tile.php:48
readonly Modal $modal
Definition: Tile.php:39
image(Component $modal, ilBadge $badge, int $size=ilBadgeImage::IMAGE_SIZE_M)
Definition: Tile.php:145
readonly Closure $sign_file
Definition: Tile.php:37
profileButton(ilBadge $badge, ilBadgeAssignment $assignment, string $gui)
Definition: Tile.php:205
readonly Closure $format_date
Definition: Tile.php:41
const IL_CAL_DATE
readonly BadgeParent $parent
Definition: Tile.php:38
txt(string $key)
Definition: Tile.php:187
modalContentWithAssignment(ilBadge $badge, ilBadgeAssignment $assignment)
Definition: Tile.php:175
asTitleWithLeadingImage(ModalContent $content)
Definition: Tile.php:94
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
card(ModalContent $content)
Definition: Tile.php:128
withAdditionalProperties(array $properties)
inDeck(ilBadge $badge, ilBadgeAssignment $assignment, string $gui)
Definition: Tile.php:71
title(Component $modal, ilBadge $badge)
Definition: Tile.php:156