ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestRandomQuestionSetSourcePoolDefinition.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  private ?int $id = null;
30  private ?int $pool_id = null;
31  private ?int $pool_ref_id = null;
32  private ?string $pool_title = null;
33  private ?string $pool_path = null;
34  private ?int $pool_question_count = null;
35 
39  private array $original_taxonomy_filter = [];
40 
44  private array $mapped_taxonomy_filter = [];
45 
46  private array $type_filter = [];
47  private array $lifecycle_filter = [];
48 
49  private ?int $question_amount = null;
50 
51  private ?int $sequence_position = null;
52 
53  public function __construct(
54  protected ilDBInterface $db,
55  protected ilObjTest $test_obj
56  ) {
57  }
58 
59  public function setId(int $id): void
60  {
61  $this->id = $id;
62  }
63 
64  public function getId(): ?int
65  {
66  return $this->id;
67  }
68 
69  public function setPoolId(int $pool_id): void
70  {
71  $this->pool_id = $pool_id;
72  }
73 
74  public function getPoolId(): ?int
75  {
76  return $this->pool_id;
77  }
78 
79  public function getPoolRefId(): ?int
80  {
81  return $this->pool_ref_id;
82  }
83 
84  public function setPoolRefId(?int $pool_ref_id): void
85  {
86  $this->pool_ref_id = $pool_ref_id;
87  }
88 
89  public function setPoolTitle(string $pool_title): void
90  {
91  $this->pool_title = $pool_title;
92  }
93 
94  public function getPoolTitle(): string
95  {
96  return $this->pool_title;
97  }
98 
99  public function setPoolPath(?string $pool_path): void
100  {
101  $this->pool_path = $pool_path;
102  }
103 
104  public function getPoolPath(): ?string
105  {
106  return $this->pool_path;
107  }
108 
109  public function setPoolQuestionCount(?int $pool_question_count): void
110  {
111  $this->pool_question_count = $pool_question_count;
112  }
113 
114  public function getPoolQuestionCount(): ?int
115  {
117  }
118 
119  public function getOriginalTaxonomyFilter(): array
120  {
122  }
123 
124  public function setOriginalTaxonomyFilter(array $filter = []): void
125  {
126  $this->original_taxonomy_filter = $filter;
127  }
128 
129  private function getOriginalTaxonomyFilterForDbValue(): ?string
130  {
131  // TODO-RND2017: migrate to separate table for common selections by e.g. statistics
132  return empty($this->original_taxonomy_filter) ? null : serialize($this->original_taxonomy_filter);
133  }
134 
135  private function setOriginalTaxonomyFilterFromDbValue(?string $value): void
136  {
137  // TODO-RND2017: migrate to separate table for common selections by e.g. statistics
138  $this->original_taxonomy_filter = empty($value) ? [] : unserialize($value);
139  }
140 
145  public function getMappedTaxonomyFilter(): array
146  {
148  }
149 
154  public function setMappedTaxonomyFilter(array $filter = []): void
155  {
156  $this->mapped_taxonomy_filter = $filter;
157  }
158 
159  private function getMappedTaxonomyFilterForDbValue(): ?string
160  {
161  return empty($this->mapped_taxonomy_filter) ? null : serialize($this->mapped_taxonomy_filter);
162  }
163 
164  private function setMappedTaxonomyFilterFromDbValue(?string $value): void
165  {
166  $this->mapped_taxonomy_filter = empty($value) ? [] : unserialize($value);
167  }
168 
169  public function mapTaxonomyFilter(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxonomies_keys_map): void
170  {
171  $this->mapped_taxonomy_filter = [];
172  foreach ($this->original_taxonomy_filter as $tax_id => $node_ids) {
173  $mapped_taxonomy_id = $taxonomies_keys_map->getMappedTaxonomyId($tax_id);
174  if ($mapped_taxonomy_id === null) {
175  continue;
176  }
177 
178  foreach ($node_ids as $node_id) {
179  $mapped_node_id = $taxonomies_keys_map->getMappedTaxNodeId((int) $node_id);
180  if ($mapped_node_id !== null) {
181  $this->mapped_taxonomy_filter[$mapped_taxonomy_id][] = $mapped_node_id;
182  }
183  }
184  }
185  }
186 
187  public function setTypeFilter(array $type_filter = []): void
188  {
189  $this->type_filter = $type_filter;
190  }
191 
192  public function getTypeFilter(): array
193  {
194  return $this->type_filter;
195  }
196 
200  private function getTypeFilterForDbValue(): ?string
201  {
202  return empty($this->type_filter) ? null : serialize($this->type_filter);
203  }
204 
208  private function setTypeFilterFromDbValue(?string $value): void
209  {
210  $this->type_filter = empty($value) ? [] : unserialize($value);
211  }
212 
213  public function getLifecycleFilter(): array
214  {
216  }
217 
218  public function setLifecycleFilter(array $lifecycle_filter): void
219  {
220  $this->lifecycle_filter = $lifecycle_filter;
221  }
222 
223  public function getLifecycleFilterForDbValue(): ?string
224  {
225  return empty($this->lifecycle_filter) ? null : serialize($this->lifecycle_filter);
226  }
227 
228  public function setLifecycleFilterFromDbValue(?string $db_value)
229  {
230  $this->lifecycle_filter = empty($db_value) ? [] : unserialize($db_value);
231  }
232 
237  public function getTypeFilterAsTypeTags(): array
238  {
239  $map = [];
240  foreach (ilObjQuestionPool::_getQuestionTypes(true) as $row) {
241  $map[$row['question_type_id']] = $row['type_tag'];
242  }
243 
244  $tags = [];
245  foreach ($this->type_filter as $type_id) {
246  if (isset($map[$type_id])) {
247  $tags[] = $map[$type_id];
248  }
249  }
250 
251  return $tags;
252  }
253 
258  public function setTypeFilterFromTypeTags(array $tags): void
259  {
260  $map = [];
261  foreach (ilObjQuestionPool::_getQuestionTypes(true) as $row) {
262  $map[$row['type_tag']] = $row['question_type_id'];
263  }
264 
265  $this->type_filter = [];
266  foreach ($tags as $type_tag) {
267  if (isset($map[$type_tag])) {
268  $this->type_filter[] = $map[$type_tag];
269  }
270  }
271  }
272 
273  public function setQuestionAmount(?int $question_amount): void
274  {
275  $this->question_amount = $question_amount;
276  }
277 
278  public function getQuestionAmount(): ?int
279  {
280  return $this->question_amount;
281  }
282 
283  public function setSequencePosition(int $sequence_position): void
284  {
285  $this->sequence_position = $sequence_position;
286  }
287 
288  public function getSequencePosition(): ?int
289  {
291  }
292 
293  // -----------------------------------------------------------------------------------------------------------------
294 
295  public function initFromArray(array $data_array): void
296  {
297  foreach ($data_array as $field => $value) {
298  switch ($field) {
299  case 'def_id':
300  $this->setId($value);
301  break;
302  case 'pool_fi':
303  $this->setPoolId($value);
304  break;
305  case 'pool_ref_id':
306  $this->setPoolRefId($value ? (int) $value : null);
307  break;
308  case 'pool_title':
309  $this->setPoolTitle($value);
310  break;
311  case 'pool_path':
312  $this->setPoolPath($value);
313  break;
314  case 'pool_quest_count':
315  $this->setPoolQuestionCount($value);
316  break;
317  case 'origin_tax_filter':
319  break;
320  case 'mapped_tax_filter':
321  $this->setMappedTaxonomyFilterFromDbValue($value);
322  break;
323  case 'type_filter':
324  $this->setTypeFilterFromDbValue($value);
325  break;
326  case 'lifecycle_filter':
327  $this->setLifecycleFilterFromDbValue($value);
328  break;
329  // fau.
330  case 'quest_amount':
331  $this->setQuestionAmount($value);
332  break;
333  case 'sequence_pos':
334  $this->setSequencePosition($value);
335  break;
336  }
337  }
338  }
339 
340  public function loadFromDb(int $id): bool
341  {
342  $res = $this->db->queryF(
343  "SELECT * FROM tst_rnd_quest_set_qpls WHERE def_id = %s",
344  ['integer'],
345  [$id]
346  );
347 
348  while ($row = $this->db->fetchAssoc($res)) {
349  $this->initFromArray($row);
350 
351  return true;
352  }
353 
354  return false;
355  }
356 
357  public function saveToDb(): void
358  {
359  if ($this->getId()) {
360  $this->updateDbRecord($this->test_obj->getTestId());
361  return;
362  }
363 
364  $this->insertDbRecord($this->test_obj->getTestId());
365  }
366 
367  public function cloneToDbForTestId(int $test_id): void
368  {
369  $this->insertDbRecord($test_id);
370  }
371 
372  public function deleteFromDb(): void
373  {
374  $this->db->manipulateF(
375  "DELETE FROM tst_rnd_quest_set_qpls WHERE def_id = %s",
376  ['integer'],
377  [$this->getId()]
378  );
379  }
380 
381  private function updateDbRecord(int $test_id): void
382  {
383  $this->db->update(
384  'tst_rnd_quest_set_qpls',
385  [
386  'test_fi' => ['integer', $test_id],
387  'pool_fi' => ['integer', $this->getPoolId()],
388  'pool_ref_id' => ['integer', $this->getPoolRefId()],
389  'pool_title' => ['text', $this->getPoolTitle()],
390  'pool_path' => ['text', $this->getPoolPath()],
391  'pool_quest_count' => ['integer', $this->getPoolQuestionCount()],
392  'origin_tax_filter' => ['text', $this->getOriginalTaxonomyFilterForDbValue()],
393  'mapped_tax_filter' => ['text', $this->getMappedTaxonomyFilterForDbValue()],
394  'type_filter' => ['text', $this->getTypeFilterForDbValue()],
395  'lifecycle_filter' => ['text', $this->getLifecycleFilterForDbValue()],
396  'quest_amount' => ['integer', $this->getQuestionAmount()],
397  'sequence_pos' => ['integer', $this->getSequencePosition()]
398  ],
399  [
400  'def_id' => ['integer', $this->getId()]
401  ]
402  );
403  }
404 
405  private function insertDbRecord(int $test_id): void
406  {
407  $next_id = $this->db->nextId('tst_rnd_quest_set_qpls');
408 
409  $this->db->insert('tst_rnd_quest_set_qpls', [
410  'def_id' => ['integer', $next_id],
411  'test_fi' => ['integer', $test_id],
412  'pool_fi' => ['integer', $this->getPoolId()],
413  'pool_ref_id' => ['integer', $this->getPoolRefId()],
414  'pool_title' => ['text', $this->getPoolTitle()],
415  'pool_path' => ['text', $this->getPoolPath()],
416  'pool_quest_count' => ['integer', $this->getPoolQuestionCount()],
417  'origin_tax_filter' => ['text', $this->getOriginalTaxonomyFilterForDbValue()],
418  'mapped_tax_filter' => ['text', $this->getMappedTaxonomyFilterForDbValue()],
419  'type_filter' => ['text', $this->getTypeFilterForDbValue()],
420  'lifecycle_filter' => ['text', $this->getLifecycleFilterForDbValue()],
421  'quest_amount' => ['integer', $this->getQuestionAmount()],
422  'sequence_pos' => ['integer', $this->getSequencePosition()]
423  ]);
424 
425  $this->setId($next_id);
426  }
427 
428  // -----------------------------------------------------------------------------------------------------------------
429 
430  public function getPoolInfoLabel(ilLanguage $lng): string
431  {
432  $pool_path = $this->getPoolPath();
433  if (is_int($this->getPoolRefId()) && ilObject::_lookupObjId($this->getPoolRefId())) {
434  $path = new ilPathGUI();
435  $path->enableTextOnly(true);
436  $pool_path = $path->getPath(ROOT_FOLDER_ID, (int) $this->getPoolRefId());
437  }
438 
439  $poolInfoLabel = sprintf(
440  $lng->txt('tst_random_question_set_source_questionpool_summary_string'),
441  $this->getPoolTitle(),
442  $pool_path,
443  $this->getPoolQuestionCount()
444  );
445 
446  return $poolInfoLabel;
447  }
448 
449  // -----------------------------------------------------------------------------------------------------------------
450 }
mapTaxonomyFilter(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxonomies_keys_map)
Creates a path for a start and endnode.
$res
Definition: ltiservices.php:69
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
const ROOT_FOLDER_ID
Definition: constants.php:32
$path
Definition: ltiservices.php:32
setTypeFilterFromDbValue(?string $value)
get the question type filter from database value
static _lookupObjId(int $ref_id)
setMappedTaxonomyFilter(array $filter=[])
set the original taxonomy filter condition
__construct(protected ilDBInterface $db, protected ilObjTest $test_obj)
$lng
getTypeFilterForDbValue()
get the question type filter for insert into the database
setTypeFilterFromTypeTags(array $tags)
Set the type filter from a list of type tags.
static _getQuestionTypes($all_tags=false, $fixOrder=false, $withDeprecatedTypes=true)