ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
LSChoicePageBuilder.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29use ilLanguage;
30
44{
45 public const HEADLINE = 'lso_choice_page_headline';
46 public const START_LABEL = 'lso_choice_page_start_label';
47
48 public function __construct(
49 protected Factory $ui_factory,
50 protected ilLanguage $lang,
51 protected \ilObjectDataCache $obj_data_cache,
52 protected \LSUrlBuilder $url_builder,
53 protected string $goto_command
54 ) {
55 }
56
60 public function getHeadline(): string
61 {
62 return $this->lang->txt(self::HEADLINE);
63 }
64
72 public function build(array $successors): array
73 {
74 $this->preloadObjects($successors);
75
76 return [
77 $this->ui_factory->deck(array_map(
78 fn(\LSLearnerItem $successor): Card => $this->buildCard($successor),
79 $successors
80 ))->withSmallCardsSize()
81 ];
82 }
83
87 public function buildCard(\LSLearnerItem $item): Card
88 {
89 $start_url = $this->getStartUrl($item);
90
91 return $this->ui_factory->card()->repositoryObject(
92 htmlspecialchars($item->getTitle()),
93 $this->getTileImage($item)
94 )->withObjectIcon(
95 $this->getObjectIcon($item)
96 )->withTitleAction(
97 $start_url
98 )->withSections(
99 $this->getSections($item, $start_url)
100 );
101 }
102
103 protected function getStartUrl(\LSLearnerItem $item): string
104 {
105 return $this->url_builder->getHref(
106 $this->goto_command,
107 $this->obj_data_cache->lookupObjId($item->getRefId())
108 );
109 }
110
114 protected function getSections(\LSLearnerItem $item, string $start_url): array
115 {
116 $sections = [];
117 $description = trim($item->getDescription());
118 if ($description !== '') {
119 $sections[] = $this->ui_factory->listing()->property()
120 ->withProperty($this->lang->txt('description'), $description, false);
121 }
122 $sections[] = $this->ui_factory->button()->standard($this->lang->txt(self::START_LABEL), $start_url);
123
124 return $sections;
125 }
126
127 protected function getObjectIcon(\LSLearnerItem $item): Icon
128 {
129 $label = $this->lang->txt('obj_' . $item->getType());
130 if ($item->getIconPath() !== '') {
131 return $this->ui_factory->symbol()->icon()->custom($item->getIconPath(), $label, Icon::MEDIUM);
132 }
133
134 return $this->ui_factory->symbol()->icon()->standard($item->getType(), $label, Icon::MEDIUM);
135 }
136
140 protected function preloadObjects(array $successors): void
141 {
142 $this->obj_data_cache->preloadReferenceCache(
143 array_map(static fn(\LSLearnerItem $successor): int => $successor->getRefId(), $successors)
144 );
145 }
146
151 protected function getTileImage(\LSLearnerItem $item): Image
152 {
153 $tile_image = $this->getObjectTileImage($item);
154 if ($tile_image !== null) {
155 return $tile_image->getImage($this->ui_factory->image())->withAlt($item->getTitle());
156 }
157
158 $path = \ilUtil::getImagePath('cont_tile/cont_tile_default_' . $item->getType() . '.svg');
159 if (!is_file($path)) {
160 $path = \ilUtil::getImagePath('cont_tile/cont_tile_default.svg');
161 }
162
163 return $this->ui_factory->image()->responsive($path, $item->getTitle());
164 }
165
166 protected function getObjectTileImage(\LSLearnerItem $item): ?TileImage
167 {
168 $obj_id = $this->obj_data_cache->lookupObjId($item->getRefId());
169 $object = $obj_id > 0 ? \ilObjectFactory::getInstanceByObjId($obj_id, false) : null;
170
171 return $object?->getObjectProperties()->getPropertyTileImage()->getTileImage();
172 }
173}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Reusable "template" for the adaptive branch-selection page.
buildCard(\LSLearnerItem $item)
Builds the card for one selectable object.
build(array $successors)
Builds the choice page content: a deck with one card per selectable successor.
getSections(\LSLearnerItem $item, string $start_url)
__construct(protected Factory $ui_factory, protected ilLanguage $lang, protected \ilObjectDataCache $obj_data_cache, protected \LSUrlBuilder $url_builder, protected string $goto_command)
getHeadline()
The friendly statement shown above the cards; usable as page title.
getTileImage(\LSLearnerItem $item)
The object's tile image, or the default tile of the object type, as in the tiles of the repository.
getRefId()
Definition: LSItem.php:93
getIconPath()
Definition: LSItem.php:73
getType()
Definition: LSItem.php:58
getDescription()
Definition: LSItem.php:68
getTitle()
Definition: LSItem.php:63
Add learning progress and availability information to the LSItem.
language handling
class ilObjectDataCache
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
This is how the factory for UI elements looks.
Definition: Factory.php:38