ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Repository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31
33{
34 protected DatabaseWrapper $db;
36 protected SlotHandler $slot_handler;
37
38 public function __construct(
39 DatabaseWrapper $db,
41 SlotHandler $slot_handler
42 ) {
43 $this->db = $db;
44 $this->factory = $factory;
45 $this->slot_handler = $slot_handler;
46 }
47
51 public function create(
52 SlotIdentifier $slot,
53 string $source
54 ): string {
55 if ($source === FactoryInterface::STANDARD_SOURCE) {
56 throw new \ilMDVocabulariesException(FactoryInterface::STANDARD_SOURCE . ' is reserved as a source.');
57 }
58 if ($source === '') {
59 throw new \ilMDVocabulariesException('Source cannot be empty.');
60 }
61 if (!$this->isSlotValid($slot)) {
62 throw new \ilMDVocabulariesException(
63 'Slot ' . $slot->value . ' is not available for controlled vocabularies.'
64 );
65 }
66
67 $id = $this->db->nextId('il_md_vocab_contr');
68
69 $this->db->insert(
70 'il_md_vocab_contr',
71 [
72 'id' => [\ilDBConstants::T_INTEGER, $id],
73 'slot' => [\ilDBConstants::T_TEXT, $slot->value],
74 'source' => [\ilDBConstants::T_TEXT, $source],
75 'active' => [\ilDBConstants::T_INTEGER, 1],
76 'custom_input' => [\ilDBConstants::T_INTEGER, 1]
77 ]
78 );
79
80 return (string) $id;
81 }
82
83 public function addValueToVocabulary(
84 string $vocab_id,
85 string $value,
86 string $label = ''
87 ): void {
88 if ($value === '') {
89 return;
90 }
91 $this->db->insert(
92 'il_md_vocab_contr_vals',
93 [
94 'vocab_id' => [\ilDBConstants::T_INTEGER, $vocab_id],
95 'value' => [\ilDBConstants::T_TEXT, $value],
96 'label' => [\ilDBConstants::T_TEXT, $label]
97 ]
98 );
99 }
100
105 SlotIdentifier $slot,
106 string ...$values
107 ): \Generator {
108 $result = $this->db->query(
109 'SELECT value FROM il_md_vocab_contr_vals JOIN il_md_vocab_contr ON id = vocab_id
110 WHERE slot = ' . $this->db->quoteAsString($slot->value) . ' AND ' .
111 $this->db->in('value', ...$values)
112 );
113
114 foreach ($result as $row) {
115 yield (string) $row['value'];
116 }
117 }
118
119 public function getVocabulary(string $vocab_id): VocabularyInterface
120 {
121 $result = $this->db->query(
122 'SELECT id, slot, source, active, custom_input FROM il_md_vocab_contr
123 WHERE id = ' . $this->db->quoteAsInteger($vocab_id)
124 );
125
126 foreach ($result as $row) {
127 return $this->getVocabularyFromRow($row);
128 }
129 return $this->factory->null();
130 }
131
135 public function getVocabulariesForSlots(SlotIdentifier ...$slots): \Generator
136 {
137 yield from $this->readVocabularies(false, ...$slots);
138 }
139
140 public function countActiveVocabulariesForSlot(SlotIdentifier $slot): int
141 {
142 $result = $this->db->query(
143 'SELECT COUNT(*) AS count FROM il_md_vocab_contr WHERE active = 1 AND slot = ' .
144 $this->db->quoteAsString($slot->value)
145 );
146
147 foreach ($result as $row) {
148 return (int) $row['count'];
149 }
150 return 0;
151 }
152
156 public function getActiveVocabulariesForSlots(SlotIdentifier ...$slots): \Generator
157 {
158 yield from $this->readVocabularies(true, ...$slots);
159 }
160
164 public function getLabelsForValues(
165 SlotIdentifier $slot,
166 bool $only_active,
167 string ...$values
168 ): \Generator {
169 if (!$this->isSlotValid($slot)) {
170 return;
171 }
172
173 $active_where = '';
174 if ($only_active) {
175 $active_where = 'active = 1 AND ';
176 }
177
178 $result = $this->db->query(
179 'SELECT value, label FROM il_md_vocab_contr_vals JOIN il_md_vocab_contr ON id = vocab_id
180 WHERE slot = ' . $this->db->quoteAsString($slot->value) . ' AND ' . $active_where .
181 $this->db->in('value', ...$values)
182 );
183
184 $labels_by_value = [];
185 foreach ($result as $row) {
186 $labels_by_value[(string) $row['value']] = (string) $row['label'];
187 }
188 foreach ($values as $value) {
189 if (!array_key_exists($value, $labels_by_value)) {
190 continue;
191 }
192 yield new LabelledValue(
193 $value,
194 $labels_by_value[$value]
195 );
196 }
197 }
198
199 public function setActiveForVocabulary(
200 string $vocab_id,
201 bool $active
202 ): void {
203 $this->db->update(
204 'il_md_vocab_contr',
205 ['active' => [\ilDBConstants::T_INTEGER, (int) $active]],
206 ['id' => [\ilDBConstants::T_INTEGER, $vocab_id]]
207 );
208 }
209
211 string $vocab_id,
212 bool $custom_inputs
213 ): void {
214 $this->db->update(
215 'il_md_vocab_contr',
216 ['custom_input' => [\ilDBConstants::T_INTEGER, (int) $custom_inputs]],
217 ['id' => [\ilDBConstants::T_INTEGER, $vocab_id]]
218 );
219 }
220
221 public function deleteVocabulary(string $vocab_id): void
222 {
223 $this->db->manipulate(
224 'DELETE FROM il_md_vocab_contr_vals WHERE vocab_id = ' .
225 $this->db->quoteAsString($vocab_id)
226 );
227 $this->db->manipulate(
228 'DELETE FROM il_md_vocab_contr WHERE id = ' .
229 $this->db->quoteAsString($vocab_id)
230 );
231 }
232
236 protected function readVocabularies(bool $only_active, SlotIdentifier ...$slots): \Generator
237 {
238 $slot_values = [];
239 foreach ($slots as $slot) {
240 if (!$this->isSlotValid($slot)) {
241 continue;
242 }
243 $slot_values[] = $slot->value;
244 }
245
246 $where_active = '';
247 if ($only_active) {
248 $where_active = ' AND active = 1';
249 }
250 $result = $this->db->query(
251 'SELECT id, slot, source, active, custom_input FROM il_md_vocab_contr
252 WHERE ' . $this->db->in('slot', ...$slot_values) .
253 $where_active
254 );
255
256 foreach ($result as $row) {
257 yield $this->getVocabularyFromRow($row);
258 }
259 }
260
261 protected function getVocabularyFromRow(array $row): VocabularyInterface
262 {
263 $slot = SlotIdentifier::from((string) $row['slot']);
264 $id = (string) $row['id'];
265 $source = (string) $row['source'];
266 $values = $this->readVocabularyValues((string) $row['id']);
267
268 if ($this->slot_handler->dataTypeForSlot($slot) === DataType::VOCAB_VALUE) {
269 $builder = $this->factory->controlledVocabValue($slot, $id, $source, ...$values);
270 } else {
271 $builder = $this->factory->controlledString($slot, $id, $source, ...$values);
272 }
273
274 return $builder->withIsDeactivated(!$row['active'])
275 ->withDisallowsCustomInputs(!$row['custom_input'])
276 ->get();
277 }
278
282 protected function readVocabularyValues(string $vocab_id): \Generator
283 {
284 $result = $this->db->query(
285 'SELECT value FROM il_md_vocab_contr_vals WHERE vocab_id = ' .
286 $this->db->quoteAsInteger($vocab_id) . " ORDER BY COALESCE(NULLIF(label,''), value) ASC"
287 );
288 foreach ($result as $row) {
289 yield (string) $row['value'];
290 }
291 }
292
293 protected function isSlotValid(SlotIdentifier $slot): bool
294 {
295 $valid_slots = [
296 SlotIdentifier::GENERAL_STRUCTURE,
297 SlotIdentifier::GENERAL_AGGREGATION_LEVEL,
298 SlotIdentifier::GENERAL_COVERAGE,
299 SlotIdentifier::GENERAL_IDENTIFIER_CATALOG,
300 SlotIdentifier::LIFECYCLE_STATUS,
301 SlotIdentifier::LIFECYCLE_CONTRIBUTE_PUBLISHER,
302 SlotIdentifier::METAMETADATA_IDENTIFIER_CATALOG,
303 SlotIdentifier::METAMETADATA_SCHEMA,
304 SlotIdentifier::TECHNICAL_REQUIREMENT_BROWSER,
305 SlotIdentifier::TECHNICAL_REQUIREMENT_OS,
306 SlotIdentifier::TECHNICAL_OTHER_PLATFORM_REQUIREMENTS,
307 SlotIdentifier::TECHNICAL_FORMAT,
308 SlotIdentifier::EDUCATIONAL_INTERACTIVITY_TYPE,
309 SlotIdentifier::EDUCATIONAL_LEARNING_RESOURCE_TYPE,
310 SlotIdentifier::EDUCATIONAL_INTERACTIVITY_LEVEL,
311 SlotIdentifier::EDUCATIONAL_SEMANTIC_DENSITY,
312 SlotIdentifier::EDCUCATIONAL_INTENDED_END_USER_ROLE,
313 SlotIdentifier::EDUCATIONAL_CONTEXT,
314 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
315 SlotIdentifier::EDUCATIONAL_TYPICAL_AGE_RANGE,
316 SlotIdentifier::RIGHTS_COST,
317 SlotIdentifier::RIGHTS_CP_AND_OTHER_RESTRICTIONS,
318 SlotIdentifier::RELATION_KIND,
319 SlotIdentifier::RELATION_RESOURCE_IDENTIFIER_CATALOG,
320 SlotIdentifier::CLASSIFICATION_PURPOSE,
321 SlotIdentifier::CLASSIFICATION_KEYWORD,
322 SlotIdentifier::CLASSIFICATION_TAXPATH_SOURCE,
324 ];
325
326 return in_array($slot, $valid_slots, true);
327 }
328}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
factory()
findAlreadyExistingValues(SlotIdentifier $slot, string ... $values)
Definition: Repository.php:104
getLabelsForValues(SlotIdentifier $slot, bool $only_active, string ... $values)
Definition: Repository.php:164
setActiveForVocabulary(string $vocab_id, bool $active)
Definition: Repository.php:199
getActiveVocabulariesForSlots(SlotIdentifier ... $slots)
Definition: Repository.php:156
getVocabulariesForSlots(SlotIdentifier ... $slots)
Definition: Repository.php:135
addValueToVocabulary(string $vocab_id, string $value, string $label='')
The value, vocab_id tuple must be unique! Before using this, check with findAlreadyExistingValues.
Definition: Repository.php:83
__construct(DatabaseWrapper $db, FactoryInterface $factory, SlotHandler $slot_handler)
Definition: Repository.php:38
setCustomInputsAllowedForVocabulary(string $vocab_id, bool $custom_inputs)
Definition: Repository.php:210
create(SlotIdentifier $slot, string $source)
Definition: Repository.php:51
readVocabularies(bool $only_active, SlotIdentifier ... $slots)
Definition: Repository.php:236
Class ilDBConstants.
if(!file_exists('../ilias.ini.php'))