ILIAS  trunk Revision v11.0_alpha-1749-g1a06bdef097
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Repository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
30 
32 {
36 
37  public function __construct(
38  GatewayInterface $gateway,
39  FactoryInterface $factory,
40  AssignmentsInterface $assignments
41  ) {
42  $this->gateway = $gateway;
43  $this->factory = $factory;
44  $this->assignments = $assignments;
45  }
46 
47  public function deactivateVocabulary(SlotIdentifier $slot): void
48  {
49  if (!$this->isSlotValid($slot)) {
50  return;
51  }
52 
53  if (!$this->gateway->doesDeactivationEntryExistForSlot($slot)) {
54  $this->gateway->createDeactivationEntry($slot);
55  }
56  }
57 
58  public function activateVocabulary(SlotIdentifier $slot): void
59  {
60  if (!$this->isSlotValid($slot)) {
61  return;
62  }
63 
64  $this->gateway->deleteDeactivationEntry($slot);
65  }
66 
67  public function isVocabularyActive(SlotIdentifier $slot): bool
68  {
69  return !$this->gateway->doesDeactivationEntryExistForSlot($slot);
70  }
71 
73  {
74  if (!$this->assignments->doesSlotHaveValues($slot)) {
75  return $this->factory->null();
76  }
77  return $this->factory->standard(
78  $slot,
79  ...$this->assignments->valuesForSlot($slot)
80  )->withIsDeactivated(!$this->isVocabularyActive($slot))->get();
81  }
82 
86  public function getVocabularies(SlotIdentifier ...$slots): \Generator
87  {
88  foreach ($slots as $slot) {
89  if (!$this->assignments->doesSlotHaveValues($slot)) {
90  continue;
91  }
92  yield $this->factory->standard(
93  $slot,
94  ...$this->assignments->valuesForSlot($slot)
95  )->withIsDeactivated(!$this->isVocabularyActive($slot))->get();
96  }
97  }
98 
102  public function getActiveVocabularies(SlotIdentifier ...$slots): \Generator
103  {
104  foreach ($slots as $slot) {
105  if (
106  !$this->assignments->doesSlotHaveValues($slot) ||
107  !$this->isVocabularyActive($slot)
108  ) {
109  continue;
110  }
111  yield $this->factory->standard(
112  $slot,
113  ...$this->assignments->valuesForSlot($slot)
114  )->get();
115  }
116  }
117 
121  public function getLabelsForValues(
122  PresentationUtilities $presentation_utilities,
123  SlotIdentifier $slot,
124  bool $only_active,
125  string ...$values
126  ): \Generator {
127  if ($only_active) {
128  $vocabularies = $this->getActiveVocabularies($slot);
129  } else {
130  $vocabularies = $this->getVocabularies($slot);
131  }
132 
133  $vocab_values = [];
134  foreach ($vocabularies as $vocabulary) {
135  $vocab_values = array_merge(
136  $vocab_values,
137  iterator_to_array($vocabulary->values())
138  );
139  }
140 
141  foreach ($values as $value) {
142  if (!in_array($value, $vocab_values, true)) {
143  continue;
144  }
145  yield new LabelledValue(
146  $value,
147  $this->getLabelForValue($presentation_utilities, $value)
148  );
149  }
150  }
151 
152  protected function getLabelForValue(
153  PresentationUtilities $presentation_utilities,
154  string $value
155  ): string {
156  $value = $this->camelCaseToSpaces($value);
157  $exceptions = [
158  'ispartof' => 'is_part_of', 'haspart' => 'has_part',
159  'isversionof' => 'is_version_of', 'hasversion' => 'has_version',
160  'isformatof' => 'is_format_of', 'hasformat' => 'has_format',
161  'references' => 'references',
162  'isreferencedby' => 'is_referenced_by',
163  'isbasedon' => 'is_based_on', 'isbasisfor' => 'is_basis_for',
164  'requires' => 'requires', 'isrequiredby' => 'is_required_by',
165  'graphical designer' => 'graphicaldesigner',
166  'technical implementer' => 'technicalimplementer',
167  'content provider' => 'contentprovider',
168  'technical validator' => 'technicalvalidator',
169  'educational validator' => 'educationalvalidator',
170  'script writer' => 'scriptwriter',
171  'instructional designer' => 'instructionaldesigner',
172  'subject matter expert' => 'subjectmatterexpert',
173  'diagram' => 'diagramm'
174  ];
175  if (array_key_exists($value, $exceptions)) {
176  $value = $exceptions[$value];
177  }
178 
179  return $presentation_utilities->txt('meta_' . $this->fillSpaces($value));
180  }
181 
182  protected function camelCaseToSpaces(string $string): string
183  {
184  $string = preg_replace('/(?<=[a-z])(?=[A-Z])/', ' ', $string);
185  return strtolower($string);
186  }
187 
188  protected function fillSpaces(string $string): string
189  {
190  $string = str_replace(' ', '_', $string);
191  return strtolower($string);
192  }
193 
194  protected function isSlotValid(SlotIdentifier $slot): bool
195  {
196  $valid_slots = [
197  SlotIdentifier::GENERAL_STRUCTURE,
198  SlotIdentifier::GENERAL_AGGREGATION_LEVEL,
199  SlotIdentifier::LIFECYCLE_STATUS,
200  SlotIdentifier::LIFECYCLE_CONTRIBUTE_ROLE,
201  SlotIdentifier::METAMETADATA_CONTRIBUTE_ROLE,
202  SlotIdentifier::TECHNICAL_REQUIREMENT_TYPE,
203  SlotIdentifier::TECHNICAL_REQUIREMENT_BROWSER,
204  SlotIdentifier::TECHNICAL_REQUIREMENT_OS,
205  SlotIdentifier::EDUCATIONAL_INTERACTIVITY_TYPE,
206  SlotIdentifier::EDUCATIONAL_LEARNING_RESOURCE_TYPE,
207  SlotIdentifier::EDUCATIONAL_INTERACTIVITY_LEVEL,
208  SlotIdentifier::EDUCATIONAL_SEMANTIC_DENSITY,
209  SlotIdentifier::EDCUCATIONAL_INTENDED_END_USER_ROLE,
210  SlotIdentifier::EDUCATIONAL_CONTEXT,
211  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
212  SlotIdentifier::RIGHTS_COST,
213  SlotIdentifier::RIGHTS_CP_AND_OTHER_RESTRICTIONS,
214  SlotIdentifier::RELATION_KIND,
215  SlotIdentifier::CLASSIFICATION_PURPOSE
216  ];
217 
218  return in_array($slot, $valid_slots, true);
219  }
220 }
getLabelForValue(PresentationUtilities $presentation_utilities, string $value)
Definition: Repository.php:152
factory()
getVocabularies(SlotIdentifier ... $slots)
Definition: Repository.php:86
getActiveVocabularies(SlotIdentifier ... $slots)
Definition: Repository.php:102
getLabelsForValues(PresentationUtilities $presentation_utilities, SlotIdentifier $slot, bool $only_active, string ... $values)
Definition: Repository.php:121
__construct(GatewayInterface $gateway, FactoryInterface $factory, AssignmentsInterface $assignments)
Definition: Repository.php:37