ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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  return empty($this->original_taxonomy_filter) ? null : serialize($this->original_taxonomy_filter);
132  }
133 
134  private function setOriginalTaxonomyFilterFromDbValue(?string $value): void
135  {
136  $this->original_taxonomy_filter = empty($value) ? [] : unserialize($value);
137  }
138 
142  public function getMappedTaxonomyFilter(): array
143  {
145  }
146 
150  public function setMappedTaxonomyFilter(array $filter = []): void
151  {
152  $this->mapped_taxonomy_filter = $filter;
153  }
154 
155  private function getMappedTaxonomyFilterForDbValue(): ?string
156  {
157  return empty($this->mapped_taxonomy_filter) ? null : serialize($this->mapped_taxonomy_filter);
158  }
159 
160  private function setMappedTaxonomyFilterFromDbValue(?string $value): void
161  {
162  $this->mapped_taxonomy_filter = empty($value) ? [] : unserialize($value);
163  }
164 
165  public function mapTaxonomyFilter(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxonomies_keys_map): void
166  {
167  $this->mapped_taxonomy_filter = [];
168  foreach ($this->original_taxonomy_filter as $tax_id => $node_ids) {
169  $mapped_taxonomy_id = $taxonomies_keys_map->getMappedTaxonomyId($tax_id);
170  if ($mapped_taxonomy_id === null) {
171  continue;
172  }
173 
174  foreach ($node_ids as $node_id) {
175  $mapped_node_id = $taxonomies_keys_map->getMappedTaxNodeId((int) $node_id);
176  if ($mapped_node_id !== null) {
177  $this->mapped_taxonomy_filter[$mapped_taxonomy_id][] = $mapped_node_id;
178  }
179  }
180  }
181  }
182 
183  public function setTypeFilter(array $type_filter = []): void
184  {
185  $this->type_filter = $type_filter;
186  }
187 
188  public function getTypeFilter(): array
189  {
190  return $this->type_filter;
191  }
192 
196  private function getTypeFilterForDbValue(): ?string
197  {
198  return empty($this->type_filter) ? null : serialize($this->type_filter);
199  }
200 
204  private function setTypeFilterFromDbValue(?string $value): void
205  {
206  $this->type_filter = empty($value) ? [] : unserialize($value);
207  }
208 
209  public function getLifecycleFilter(): array
210  {
212  }
213 
214  public function setLifecycleFilter(array $lifecycle_filter): void
215  {
216  $this->lifecycle_filter = $lifecycle_filter;
217  }
218 
219  public function getLifecycleFilterForDbValue(): ?string
220  {
221  return empty($this->lifecycle_filter) ? null : serialize($this->lifecycle_filter);
222  }
223 
224  public function setLifecycleFilterFromDbValue(?string $db_value)
225  {
226  $this->lifecycle_filter = empty($db_value) ? [] : unserialize($db_value);
227  }
228 
233  public function getTypeFilterAsTypeTags(): array
234  {
235  $map = [];
236  foreach (ilObjQuestionPool::_getQuestionTypes(true) as $row) {
237  $map[$row['question_type_id']] = $row['type_tag'];
238  }
239 
240  $tags = [];
241  foreach ($this->type_filter as $type_id) {
242  if (isset($map[$type_id])) {
243  $tags[] = $map[$type_id];
244  }
245  }
246 
247  return $tags;
248  }
249 
254  public function setTypeFilterFromTypeTags(array $tags): void
255  {
256  $map = [];
257  foreach (ilObjQuestionPool::_getQuestionTypes(true) as $row) {
258  $map[$row['type_tag']] = $row['question_type_id'];
259  }
260 
261  $this->type_filter = [];
262  foreach ($tags as $type_tag) {
263  if (isset($map[$type_tag])) {
264  $this->type_filter[] = $map[$type_tag];
265  }
266  }
267  }
268 
269  public function setQuestionAmount(?int $question_amount): void
270  {
271  $this->question_amount = $question_amount;
272  }
273 
274  public function getQuestionAmount(): ?int
275  {
276  return $this->question_amount;
277  }
278 
279  public function setSequencePosition(int $sequence_position): void
280  {
281  $this->sequence_position = $sequence_position;
282  }
283 
284  public function getSequencePosition(): ?int
285  {
287  }
288 
289  // -----------------------------------------------------------------------------------------------------------------
290 
291  public function initFromArray(array $data_array): void
292  {
293  foreach ($data_array as $field => $value) {
294  switch ($field) {
295  case 'def_id':
296  $this->setId($value);
297  break;
298  case 'pool_fi':
299  $this->setPoolId($value);
300  break;
301  case 'pool_ref_id':
302  $this->setPoolRefId($value ? (int) $value : null);
303  break;
304  case 'pool_title':
305  $this->setPoolTitle($value);
306  break;
307  case 'pool_path':
308  $this->setPoolPath($value);
309  break;
310  case 'pool_quest_count':
311  $this->setPoolQuestionCount($value);
312  break;
313  case 'origin_tax_filter':
315  break;
316  case 'mapped_tax_filter':
317  $this->setMappedTaxonomyFilterFromDbValue($value);
318  break;
319  case 'type_filter':
320  $this->setTypeFilterFromDbValue($value);
321  break;
322  case 'lifecycle_filter':
323  $this->setLifecycleFilterFromDbValue($value);
324  break;
325  // fau.
326  case 'quest_amount':
327  $this->setQuestionAmount($value);
328  break;
329  case 'sequence_pos':
330  $this->setSequencePosition($value);
331  break;
332  }
333  }
334  }
335 
336  public function loadFromDb(int $id): bool
337  {
338  $res = $this->db->queryF(
339  "SELECT * FROM tst_rnd_quest_set_qpls WHERE def_id = %s",
340  ['integer'],
341  [$id]
342  );
343 
344  while ($row = $this->db->fetchAssoc($res)) {
345  $this->initFromArray($row);
346 
347  return true;
348  }
349 
350  return false;
351  }
352 
353  public function saveToDb(): void
354  {
355  if ($this->getId()) {
356  $this->updateDbRecord($this->test_obj->getTestId());
357  return;
358  }
359 
360  $this->insertDbRecord($this->test_obj->getTestId());
361  }
362 
363  public function cloneToDbForTestId(int $test_id): void
364  {
365  $this->insertDbRecord($test_id);
366  }
367 
368  public function deleteFromDb(): void
369  {
370  $this->db->manipulateF(
371  "DELETE FROM tst_rnd_quest_set_qpls WHERE def_id = %s",
372  ['integer'],
373  [$this->getId()]
374  );
375  }
376 
377  private function updateDbRecord(int $test_id): void
378  {
379  $this->db->update(
380  'tst_rnd_quest_set_qpls',
381  [
382  'test_fi' => ['integer', $test_id],
383  'pool_fi' => ['integer', $this->getPoolId()],
384  'pool_ref_id' => ['integer', $this->getPoolRefId()],
385  'pool_title' => ['text', $this->getPoolTitle()],
386  'pool_path' => ['text', $this->getPoolPath()],
387  'pool_quest_count' => ['integer', $this->getPoolQuestionCount()],
388  'origin_tax_filter' => ['text', $this->getOriginalTaxonomyFilterForDbValue()],
389  'mapped_tax_filter' => ['text', $this->getMappedTaxonomyFilterForDbValue()],
390  'type_filter' => ['text', $this->getTypeFilterForDbValue()],
391  'lifecycle_filter' => ['text', $this->getLifecycleFilterForDbValue()],
392  'quest_amount' => ['integer', $this->getQuestionAmount()],
393  'sequence_pos' => ['integer', $this->getSequencePosition()]
394  ],
395  [
396  'def_id' => ['integer', $this->getId()]
397  ]
398  );
399  }
400 
401  private function insertDbRecord(int $test_id): void
402  {
403  $next_id = $this->db->nextId('tst_rnd_quest_set_qpls');
404 
405  $this->db->insert('tst_rnd_quest_set_qpls', [
406  'def_id' => ['integer', $next_id],
407  'test_fi' => ['integer', $test_id],
408  'pool_fi' => ['integer', $this->getPoolId()],
409  'pool_ref_id' => ['integer', $this->getPoolRefId()],
410  'pool_title' => ['text', $this->getPoolTitle()],
411  'pool_path' => ['text', $this->getPoolPath()],
412  'pool_quest_count' => ['integer', $this->getPoolQuestionCount()],
413  'origin_tax_filter' => ['text', $this->getOriginalTaxonomyFilterForDbValue()],
414  'mapped_tax_filter' => ['text', $this->getMappedTaxonomyFilterForDbValue()],
415  'type_filter' => ['text', $this->getTypeFilterForDbValue()],
416  'lifecycle_filter' => ['text', $this->getLifecycleFilterForDbValue()],
417  'quest_amount' => ['integer', $this->getQuestionAmount()],
418  'sequence_pos' => ['integer', $this->getSequencePosition()]
419  ]);
420 
421  $this->setId($next_id);
422  }
423 
424  // -----------------------------------------------------------------------------------------------------------------
425 
426  public function getPoolInfoLabel(ilLanguage $lng): string
427  {
428  $pool_path = $this->getPoolPath();
429  if (is_int($this->getPoolRefId()) && ilObject::_lookupObjId($this->getPoolRefId())) {
430  $path = new ilPathGUI();
431  $path->enableTextOnly(true);
432  $pool_path = $path->getPath(ROOT_FOLDER_ID, (int) $this->getPoolRefId());
433  }
434 
435  $poolInfoLabel = sprintf(
436  $lng->txt('tst_random_question_set_source_questionpool_summary_string'),
437  $this->getPoolTitle(),
438  $pool_path,
439  $this->getPoolQuestionCount()
440  );
441 
442  return $poolInfoLabel;
443  }
444 
445  // -----------------------------------------------------------------------------------------------------------------
446 }
mapTaxonomyFilter(ilQuestionPoolDuplicatedTaxonomiesKeysMap $taxonomies_keys_map)
$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:30
setTypeFilterFromDbValue(?string $value)
get the question type filter from database value
static _lookupObjId(int $ref_id)
__construct(protected ilDBInterface $db, protected ilObjTest $test_obj)
global $lng
Definition: privfeed.php:32
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)