ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.ilTestRandomQuestionSetSourcePoolDefinition.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
33{
34 private ?int $id = null;
35 private ?int $pool_id = null;
36 private ?int $pool_ref_id = null;
37 private string $pool_title = '';
38 private string $pool_path = '';
39 private ?int $pool_question_count = null;
40
44 private array $original_taxonomy_filter = [];
45
49 private array $mapped_taxonomy_filter = [];
50
51 private array $type_filter = [];
52 private array $lifecycle_filter = [];
53
54 private ?int $question_amount = null;
55
56 private ?int $sequence_position = null;
57
58 public function __construct(
59 protected ilDBInterface $db,
60 protected ilObjTest $test_obj
61 ) {
62 }
63
64 public function setId(int $id): void
65 {
66 $this->id = $id;
67 }
68
69 public function getId(): ?int
70 {
71 return $this->id;
72 }
73
74 public function setPoolId(int $pool_id): void
75 {
76 $this->pool_id = $pool_id;
77 }
78
79 public function getPoolId(): ?int
80 {
81 return $this->pool_id;
82 }
83
84 public function getPoolRefId(): ?int
85 {
86 return $this->pool_ref_id;
87 }
88
89 public function setPoolRefId(?int $pool_ref_id): void
90 {
91 $this->pool_ref_id = $pool_ref_id;
92 }
93
94 public function setPoolTitle(string $pool_title): void
95 {
96 $this->pool_title = $pool_title;
97 }
98
99 public function getPoolTitle(): string
100 {
101 return $this->pool_title;
102 }
103
104 public function setPoolPath(string $pool_path): void
105 {
106 $this->pool_path = $pool_path;
107 }
108
109 public function getPoolPath(): string
110 {
111 return $this->pool_path;
112 }
113
114 public function setPoolQuestionCount(?int $pool_question_count): void
115 {
116 $this->pool_question_count = $pool_question_count;
117 }
118
119 public function getPoolQuestionCount(): ?int
120 {
122 }
123
124 public function getOriginalTaxonomyFilter(): array
125 {
127 }
128
129 public function setOriginalTaxonomyFilter(array $filter = []): void
130 {
131 $this->original_taxonomy_filter = $filter;
132 }
133
134 private function getOriginalTaxonomyFilterForDbValue(): ?string
135 {
136 // TODO-RND2017: migrate to separate table for common selections by e.g. statistics
137 return empty($this->original_taxonomy_filter) ? null : serialize($this->original_taxonomy_filter);
138 }
139
140 private function setOriginalTaxonomyFilterFromDbValue(?string $value): void
141 {
142 // TODO-RND2017: migrate to separate table for common selections by e.g. statistics
143 $this->original_taxonomy_filter = empty($value) ? [] : unserialize($value);
144 }
145
150 public function getMappedTaxonomyFilter(): array
151 {
153 }
154
159 public function setMappedTaxonomyFilter(array $filter = []): void
160 {
161 $this->mapped_taxonomy_filter = array_filter(
162 $filter,
163 static fn(mixed $value, int|string $key): bool => (int) $key > 0 || !is_array($value),
164 ARRAY_FILTER_USE_BOTH
165 );
166 }
167
168 private function getMappedTaxonomyFilterForDbValue(): ?string
169 {
170 return empty($this->mapped_taxonomy_filter) ? null : serialize($this->mapped_taxonomy_filter);
171 }
172
173 private function setMappedTaxonomyFilterFromDbValue(?string $value): void
174 {
175 $this->mapped_taxonomy_filter = [];
176 if ($value === null || $value === '') {
177 return;
178 }
179
180 $filter = unserialize($value, ['allowed_classes' => false]);
181 if (!is_array($filter)) {
182 return;
183 }
184
186 foreach ($filter as $tax_id => $node_ids) {
187 $mapped_taxonomy_filter[$tax_id] = $node_ids;
188 }
189
190 $this->setMappedTaxonomyFilter($mapped_taxonomy_filter);
191 }
192
193 public function mapTaxonomyFilter(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxonomies_keys_map): void
194 {
195 $this->mapped_taxonomy_filter = [];
196 foreach ($this->original_taxonomy_filter as $tax_id => $node_ids) {
197 $mapped_node_ids = [];
198
199 $mapped_taxonomy_id = $taxonomies_keys_map->getMappedTaxonomyId($tax_id);
200 if ($mapped_taxonomy_id === null || $mapped_taxonomy_id < 1) {
201 continue;
202 }
203
204 foreach ($node_ids as $node_id) {
205 $mapped_node_id = $taxonomies_keys_map->getMappedTaxNodeId($node_id);
206 if ($mapped_node_id !== null) {
207 $mapped_node_ids[] = $mapped_node_id;
208 }
209 }
210 $this->mapped_taxonomy_filter[$mapped_taxonomy_id] = $mapped_node_ids;
211 }
212 }
213
214 public function setTypeFilter(array $type_filter = []): void
215 {
216 $this->type_filter = $type_filter;
217 }
218
219 public function getTypeFilter(): array
220 {
221 return $this->type_filter;
222 }
223
227 private function getTypeFilterForDbValue(): ?string
228 {
229 return empty($this->type_filter) ? null : serialize($this->type_filter);
230 }
231
235 private function setTypeFilterFromDbValue(?string $value): void
236 {
237 $this->type_filter = empty($value) ? [] : unserialize($value);
238 }
239
240 public function getLifecycleFilter(): array
241 {
243 }
244
245 public function setLifecycleFilter(array $lifecycle_filter): void
246 {
247 $this->lifecycle_filter = $lifecycle_filter;
248 }
249
250 public function getLifecycleFilterForDbValue(): ?string
251 {
252 return empty($this->lifecycle_filter) ? null : serialize($this->lifecycle_filter);
253 }
254
255 public function setLifecycleFilterFromDbValue(?string $db_value)
256 {
257 $this->lifecycle_filter = empty($db_value) ? [] : unserialize($db_value);
258 }
259
264 public function getTypeFilterAsTypeTags(): array
265 {
266 $map = [];
267 foreach (ilObjQuestionPool::_getQuestionTypes(true) as $row) {
268 $map[$row['question_type_id']] = $row['type_tag'];
269 }
270
271 $tags = [];
272 foreach ($this->type_filter as $type_id) {
273 if (isset($map[$type_id])) {
274 $tags[] = $map[$type_id];
275 }
276 }
277
278 return $tags;
279 }
280
285 public function setTypeFilterFromTypeTags(array $tags): void
286 {
287 $map = [];
288 foreach (ilObjQuestionPool::_getQuestionTypes(true) as $row) {
289 $map[$row['type_tag']] = $row['question_type_id'];
290 }
291
292 $this->type_filter = [];
293 foreach ($tags as $type_tag) {
294 if (isset($map[$type_tag])) {
295 $this->type_filter[] = $map[$type_tag];
296 }
297 }
298 }
299
300 public function setQuestionAmount(?int $question_amount): void
301 {
302 $this->question_amount = $question_amount;
303 }
304
305 public function getQuestionAmount(): ?int
306 {
308 }
309
310 public function setSequencePosition(int $sequence_position): void
311 {
312 $this->sequence_position = $sequence_position;
313 }
314
315 public function getSequencePosition(): ?int
316 {
318 }
319
320 // -----------------------------------------------------------------------------------------------------------------
321
322 public function initFromArray(array $data_array): void
323 {
324 foreach ($data_array as $field => $value) {
325 switch ($field) {
326 case 'def_id':
327 $this->setId($value);
328 break;
329 case 'pool_fi':
330 $this->setPoolId($value);
331 break;
332 case 'pool_ref_id':
333 $this->setPoolRefId($value ? (int) $value : null);
334 break;
335 case 'pool_title':
336 $this->setPoolTitle($value ?? '');
337 break;
338 case 'pool_path':
339 $this->setPoolPath($value);
340 break;
341 case 'pool_quest_count':
342 $this->setPoolQuestionCount($value);
343 break;
344 case 'origin_tax_filter':
346 break;
347 case 'mapped_tax_filter':
349 break;
350 case 'type_filter':
351 $this->setTypeFilterFromDbValue($value);
352 break;
353 case 'lifecycle_filter':
354 $this->setLifecycleFilterFromDbValue($value);
355 break;
356 // fau.
357 case 'quest_amount':
358 $this->setQuestionAmount($value);
359 break;
360 case 'sequence_pos':
361 $this->setSequencePosition($value);
362 break;
363 }
364 }
365 }
366
367 public function loadFromDb(int $id): bool
368 {
369 $res = $this->db->queryF(
370 "SELECT * FROM tst_rnd_quest_set_qpls WHERE def_id = %s",
371 ['integer'],
372 [$id]
373 );
374
375 while ($row = $this->db->fetchAssoc($res)) {
376 $this->initFromArray($row);
377
378 return true;
379 }
380
381 return false;
382 }
383
384 public function saveToDb(): void
385 {
386 if ($this->getId()) {
387 $this->updateDbRecord($this->test_obj->getTestId());
388 return;
389 }
390
391 $this->insertDbRecord($this->test_obj->getTestId());
392 }
393
394 public function cloneToDbForTestId(int $test_id): void
395 {
396 $this->insertDbRecord($test_id);
397 }
398
399 public function deleteFromDb(): void
400 {
401 $this->db->manipulateF(
402 "DELETE FROM tst_rnd_quest_set_qpls WHERE def_id = %s",
403 ['integer'],
404 [$this->getId()]
405 );
406 }
407
408 private function updateDbRecord(int $test_id): void
409 {
410 $this->db->update(
411 'tst_rnd_quest_set_qpls',
412 [
413 'test_fi' => ['integer', $test_id],
414 'pool_fi' => ['integer', $this->getPoolId()],
415 'pool_ref_id' => ['integer', $this->getPoolRefId()],
416 'pool_title' => ['text', $this->getPoolTitle()],
417 'pool_path' => ['text', $this->getPoolPath()],
418 'pool_quest_count' => ['integer', $this->getPoolQuestionCount()],
419 'origin_tax_filter' => ['text', $this->getOriginalTaxonomyFilterForDbValue()],
420 'mapped_tax_filter' => ['text', $this->getMappedTaxonomyFilterForDbValue()],
421 'type_filter' => ['text', $this->getTypeFilterForDbValue()],
422 'lifecycle_filter' => ['text', $this->getLifecycleFilterForDbValue()],
423 'quest_amount' => ['integer', $this->getQuestionAmount()],
424 'sequence_pos' => ['integer', $this->getSequencePosition()]
425 ],
426 [
427 'def_id' => ['integer', $this->getId()]
428 ]
429 );
430 }
431
432 private function insertDbRecord(int $test_id): void
433 {
434 $next_id = $this->db->nextId('tst_rnd_quest_set_qpls');
435
436 $this->db->insert('tst_rnd_quest_set_qpls', [
437 'def_id' => ['integer', $next_id],
438 'test_fi' => ['integer', $test_id],
439 'pool_fi' => ['integer', $this->getPoolId()],
440 'pool_ref_id' => ['integer', $this->getPoolRefId()],
441 'pool_title' => ['text', $this->getPoolTitle()],
442 'pool_path' => ['text', $this->getPoolPath()],
443 'pool_quest_count' => ['integer', $this->getPoolQuestionCount()],
444 'origin_tax_filter' => ['text', $this->getOriginalTaxonomyFilterForDbValue()],
445 'mapped_tax_filter' => ['text', $this->getMappedTaxonomyFilterForDbValue()],
446 'type_filter' => ['text', $this->getTypeFilterForDbValue()],
447 'lifecycle_filter' => ['text', $this->getLifecycleFilterForDbValue()],
448 'quest_amount' => ['integer', $this->getQuestionAmount()],
449 'sequence_pos' => ['integer', $this->getSequencePosition()]
450 ]);
451
452 $this->setId($next_id);
453 }
454
455 // -----------------------------------------------------------------------------------------------------------------
456
457 public function getPoolInfoLabel(ilLanguage $lng): string
458 {
459 $pool_path = $this->getPoolPath();
460 if (is_int($this->getPoolRefId()) && ilObject::_lookupObjId($this->getPoolRefId())) {
461 $path = new ilPathGUI();
462 $path->enableTextOnly(true);
463 $pool_path = $path->getPath(ROOT_FOLDER_ID, (int) $this->getPoolRefId());
464 }
465
466 $poolInfoLabel = sprintf(
467 $lng->txt('tst_random_question_set_source_questionpool_summary_string'),
468 $this->getPoolTitle(),
470 $this->getPoolQuestionCount()
471 );
472
473 return $poolInfoLabel;
474 }
475
476 // -----------------------------------------------------------------------------------------------------------------
477
478 #[\Override]
479 public function toNormalized(
480 Transformations $transformations,
481 array $context = []
482 ): array|float|bool|int|string|null {
483 $normalized = [
484 'id' => $transformations->normalize(new Id($this->getId(), 'rnd_src_pool_def')),
485 'pool_id' => $transformations->normalize(new Id($this->getPoolId(), 'qpl')),
486 'pool_title' => $this->getPoolTitle(),
487 'pool_path' => $this->getPoolPath(),
488 'quest_amount' => $this->getQuestionAmount(),
489 'pool_quest_count' => $this->getPoolQuestionCount(),
490 'position' => $this->getSequencePosition(),
491 'type_filter' => $this->getTypeFilterAsTypeTags(),
492 'lifecycle_filter' => $this->getLifecycleFilter(),
493 'taxonomy_filter' => [],
494 'mapped_taxonomy_filter' => [],
495 ];
496
497 foreach ($this->getOriginalTaxonomyFilter() as $tax_id => $node_ids) {
498 $normalized['taxonomy_filter'][] = [
499 'tax_id' => $transformations->normalize(new Id($tax_id, 'tax')),
500 'node_ids' => array_map(
501 static fn($node_id): mixed => $transformations->normalize(new Id($node_id, 'tax_node')),
502 $node_ids
503 ),
504 ];
505 }
506
507 foreach ($this->getMappedTaxonomyFilter() as $tax_id => $node_ids) {
508 $normalized['mapped_taxonomy_filter'][] = [
509 'tax_id' => $transformations->normalize(new Id($tax_id, 'mapped_tax')),
510 'node_ids' => array_map(
511 static fn($node_id): mixed => $transformations->normalize(new Id($node_id, 'mapped_tax_node')),
512 $node_ids
513 ),
514 ];
515 }
516
517 return $normalized;
518 }
519
520 #[\Override]
521 public function fromNormalized(
522 array $normalized,
523 Transformations $transformations
524 ): static {
525 $clone = clone $this;
526 $clone->setId($transformations->denormalize($normalized['id'], Id::class)->getId());
527 $clone->setPoolId($transformations->denormalize($normalized['pool_id'], Id::class)->getId());
528 $clone->setPoolTitle($transformations->string($normalized['pool_title']));
529 $clone->setPoolPath($transformations->string($normalized['pool_path']));
530 $clone->setQuestionAmount($transformations->nullableInt($normalized['quest_amount']));
531 $clone->setPoolQuestionCount($transformations->nullableInt($normalized['pool_quest_count']));
532 $clone->setSequencePosition($transformations->int($normalized['position']));
533 $clone->setTypeFilterFromTypeTags($normalized['type_filter']);
534 $clone->setLifecycleFilter($normalized['lifecycle_filter']);
535
536 $taxonomy_filter = [];
537 foreach ($normalized['taxonomy_filter'] as $item) {
538 $tax_id = $transformations->denormalize($item['tax_id'], Id::class)->getId();
539 $taxonomy_filter[$tax_id] = array_map(
540 static fn($node_id): mixed => $transformations->denormalize($node_id, Id::class)->getId(),
541 $item['node_ids']
542 );
543 }
544 $clone->setOriginalTaxonomyFilter($taxonomy_filter);
545
546 $mapped_taxonomy_filter = [];
547 foreach (($normalized['mapped_taxonomy_filter'] ?? []) as $item) {
548 $tax_id = $transformations->denormalize($item['tax_id'], Id::class)->getId();
549 $mapped_taxonomy_filter[$tax_id] = array_map(
550 static fn($node_id): mixed => $transformations->denormalize($node_id, Id::class)->getId(),
551 $item['node_ids']
552 );
553 }
554 $clone->setMappedTaxonomyFilter($mapped_taxonomy_filter);
555
556 return $clone;
557 }
558}
Wrapper object for an object identifier and an optional object type the identifier belongs to.
Definition: Id.php:33
Provides a set of transformations for normalizing and denormalizing values.
denormalize(array|float|bool|int|string|null $normalized, string|object $expected)
language handling
static _getQuestionTypes($all_tags=false, $fixOrder=false, $withDeprecatedTypes=true)
static _lookupObjId(int $ref_id)
setTypeFilterFromTypeTags(array $tags)
Set the type filter from a list of type tags.
setMappedTaxonomyFilter(array $filter=[])
set the original taxonomy filter condition
mapTaxonomyFilter(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxonomies_keys_map)
toNormalized(Transformations $transformations, array $context=[])
Convert the internal state of the implementing object into a language-neutral array structure.
__construct(protected ilDBInterface $db, protected ilObjTest $test_obj)
getTypeFilterForDbValue()
get the question type filter for insert into the database
fromNormalized(array $normalized, Transformations $transformations)
Restore the object from its normalized representation.
setTypeFilterFromDbValue(?string $value)
get the question type filter from database value
const ROOT_FOLDER_ID
Definition: constants.php:32
Interface ilDBInterface.
global $lng
Definition: privfeed.php:26
foreach($_POST as $key=> $value) $res