ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Presentation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\MetaData\Presentation\ElementsInterface as ElementsPresentation;
24use ILIAS\MetaData\Presentation\UtilitiesInterface as PresentationUtilities;
36
38{
39 protected const int MAX_PREVIEW_VALUES = 5;
40
41 protected ElementsPresentation $elements_presentation;
42 protected PresentationUtilities $presentation_utils;
43 protected VocabPresentation $vocab_presentation;
44 protected SlotHandler $slot_handler;
46 protected NavigatorFactory $navigator_factory;
47 protected PathFactory $path_factory;
48
52 protected array $vocabs;
53
54 public function __construct(
55 ElementsPresentation $elements_presentation,
56 PresentationUtilities $presentation_utils,
57 VocabPresentation $vocab_presentation,
58 SlotHandler $slot_handler,
60 NavigatorFactory $navigator_factory,
61 PathFactory $path_factory
62 ) {
63 $this->elements_presentation = $elements_presentation;
64 $this->presentation_utils = $presentation_utils;
65 $this->vocab_presentation = $vocab_presentation;
66 $this->slot_handler = $slot_handler;
67 $this->structure = $structure;
68 $this->navigator_factory = $navigator_factory;
69 $this->path_factory = $path_factory;
70 }
71
72 public function txt(string $key): string
73 {
74 return $this->presentation_utils->txt($key);
75 }
76
77 public function txtFill(string $key, string ...$values): string
78 {
79 return $this->presentation_utils->txtFill($key, ...$values);
80 }
81
82 public function makeTypePresentable(VocabType $type): string
83 {
84 return match ($type) {
85 VocabType::STANDARD => $this->presentation_utils->txt('md_vocab_type_standard'),
86 VocabType::CONTROLLED_STRING => $this->presentation_utils->txt('md_vocab_type_controlled_string'),
87 VocabType::CONTROLLED_VOCAB_VALUE => $this->presentation_utils->txt('md_vocab_type_controlled_vocab_value'),
88 VocabType::COPYRIGHT => $this->presentation_utils->txt('md_vocab_type_copyright'),
89 default => '',
90 };
91 }
92
93 public function makeSlotPresentable(SlotIdentifier $slot): string
94 {
95 //skip the name of the element if it does not add any information
96 $skip_data = ['string', 'value'];
97
98 $element = $this->getStructureElementFromPath(
99 $this->slot_handler->pathForSlot($slot),
100 $this->structure->getRoot()
101 );
102 $element_name = $this->elements_presentation->nameWithParents(
103 $element,
104 null,
105 false,
106 in_array($element->getDefinition()->name(), $skip_data),
107 );
108
109 if (!$this->slot_handler->isSlotConditional($slot)) {
110 return $element_name;
111 }
112
113 $condition = $this->slot_handler->conditionForSlot($slot);
114
115 $condition_element = $this->getStructureElementFromPath(
116 $condition->path(),
117 $element
118 );
119 $condition_element_name = $this->elements_presentation->nameWithParents(
120 $condition_element,
121 $this->findFirstCommonParent($element, $condition_element)->getSuperElement(),
122 false,
123 in_array($element->getDefinition()->name(), $skip_data),
124 );
125
126 return $this->presentation_utils->txtFill(
127 'md_vocab_element_with_condition',
128 $element_name,
129 $condition_element_name,
130 $this->translateConditionValue($condition, $element)
131 );
132 }
133
137 public function makeValuesPresentable(
138 VocabularyInterface $vocabulary,
139 ?int $limit = null
140 ): array {
141 $presentable_values = [];
142 $i = 0;
143
144 $labelled_values = $this->vocab_presentation->labelsForVocabulary(
145 $this->presentation_utils,
146 $vocabulary
147 );
148 foreach ($labelled_values as $labelled_value) {
149 if ($limit !== null && $i >= $limit) {
150 $presentable_values[] = '...';
151 break;
152 }
153 $i++;
154
155 if (
156 $vocabulary->type() === VocabType::STANDARD ||
157 $vocabulary->type() === VocabType::COPYRIGHT
158 ) {
159 $presentable_values[] = $this->presentation_utils->sanitizeForHTML($labelled_value->label());
160 continue;
161 }
162
163 $presentable_value = $labelled_value->value();
164 if ($labelled_value->label() !== '') {
165 $presentable_value = $labelled_value->label() . ' (' . $presentable_value . ')';
166 }
167 $presentable_values[] = $this->presentation_utils->sanitizeForHTML($presentable_value);
168 }
169
170 return $presentable_values;
171 }
172
173 protected function translateConditionValue(
174 ConditionInterface $condition,
176 ): string {
177 $path_from_root = $this->path_factory->toElement(
178 $this->getStructureElementFromPath($condition->path(), $element),
179 );
180 $slot = $this->slot_handler->identiferFromPathAndCondition($path_from_root, null, null);
181 return (string) $this->vocab_presentation->presentableLabels(
182 $this->presentation_utils,
183 $slot,
184 false,
185 $condition->value()
186 )->current()?->label();
187 }
188
189 protected function findFirstCommonParent(
193 $a_supers = [];
194 while ($a) {
195 $a_supers[] = $a;
196 $a = $a->getSuperElement();
197 }
198 while ($b) {
199 if (in_array($b, $a_supers, true)) {
200 return $b;
201 }
202 $b = $b->getSuperElement();
203 }
204 return $a;
205 }
206
207 protected function getStructureElementFromPath(
211 return $this->navigator_factory->structureNavigator(
212 $path,
213 $start
214 )->elementAtFinalStep();
215 }
216}
txtFill(string $key, string ... $values)
translateConditionValue(ConditionInterface $condition, StructureElementInterface $element)
makeValuesPresentable(VocabularyInterface $vocabulary, ?int $limit=null)
findFirstCommonParent(StructureElementInterface $a, StructureElementInterface $b)
__construct(ElementsPresentation $elements_presentation, PresentationUtilities $presentation_utils, VocabPresentation $vocab_presentation, SlotHandler $slot_handler, Structure $structure, NavigatorFactory $navigator_factory, PathFactory $path_factory)
getStructureElementFromPath(PathInterface $path, StructureElementInterface $start)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$path
Definition: ltiservices.php:30
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples