ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilTestRandomQuestionSetSourcePoolDefinitionList.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 protected ilDBInterface $db;
31 private array $source_pool_definitions = [];
33 protected array $lost_pools = [];
34 protected array $trashed_pools = [];
35
37 {
38 $this->db = $db;
39 $this->test_obj = $test_obj;
40 $this->source_pool_definition_factory = $source_pool_definition_factory;
41 }
42
43 public function addDefinition(ilTestRandomQuestionSetSourcePoolDefinition $source_pool_definition)
44 {
45 $this->source_pool_definitions[ $source_pool_definition->getId() ] = $source_pool_definition;
46 }
47
49 {
50 $this->lost_pools[$lost_pool->getId()] = $lost_pool;
51 }
52
53 public function isLostPool(?int $pool_id): bool
54 {
55 return isset($this->lost_pools[$pool_id]);
56 }
57
58 public function hasLostPool(): bool
59 {
60 return (bool) count($this->lost_pools);
61 }
62
63 public function getLostPools(): array
64 {
65 return $this->lost_pools;
66 }
67
68 public function getLostPool(int $pool_id)
69 {
70 if ($this->isLostPool($pool_id)) {
71 return $this->lost_pools[$pool_id];
72 }
73
74 return null;
75 }
76
77 public function isTrashedPool(int $pool_id): bool
78 {
79 return isset($this->trashed_pools[$pool_id]);
80 }
81
82 public function hasTrashedPool(): bool
83 {
84 return (bool) count($this->trashed_pools);
85 }
86
87 public function getTrashedPools(): array
88 {
90 }
91
92 public function setTrashedPools(array $trashed_pools): void
93 {
94 $this->trashed_pools = $trashed_pools;
95 }
96
97 // hey: fixRandomTestBuildable - provide single definitions, quantities distribution likes to deal with objects
98
99 public function hasDefinition(int $source_pool_definition_id): bool
100 {
101 return $this->getDefinition($source_pool_definition_id) !== null;
102 }
103
104 public function getDefinition(int $source_pool_definition_id): ?ilTestRandomQuestionSetSourcePoolDefinition
105 {
106 if (isset($this->source_pool_definitions[$source_pool_definition_id])) {
107 return $this->source_pool_definitions[$source_pool_definition_id];
108 }
109
110 return null;
111 }
112
114 {
115 foreach ($this as $definition) {
116 if ($definition->getPoolId() != $source_pool_id) {
117 continue;
118 }
119
120 return $definition;
121 }
122
123 throw new InvalidArgumentException('invalid source pool id given');
124 }
125
126 public function getDefinitionIds(): array
127 {
128 return array_keys($this->source_pool_definitions);
129 }
130
131 public function getDefinitionCount(): int
132 {
133 return count($this->source_pool_definitions);
134 }
135 // hey.
136
137 public function loadDefinitions(): void
138 {
139 $query = "
140 SELECT tst_rnd_quest_set_qpls.*, odat.obj_id pool_id, odat.title actual_pool_title, tree.child
141 FROM tst_rnd_quest_set_qpls
142 LEFT JOIN object_data odat
143 ON odat.obj_id = pool_fi
144 LEFT JOIN object_reference oref
145 ON oref.obj_id = pool_fi
146 LEFT JOIN tree
147 ON tree = %s
148 AND child = oref.ref_id
149 WHERE test_fi = %s
150 ORDER BY sequence_pos ASC
151 ";
152
153 $res = $this->db->queryF($query, ['integer', 'integer'], [1, $this->test_obj->getTestId()]);
154
155 $handled_definitions = [];
156 $trashed_pools = [];
157
158 while ($row = $this->db->fetchAssoc($res)) {
159 $source_pool_definition = $this->source_pool_definition_factory->getEmptySourcePoolDefinition();
160 $source_pool_definition->initFromArray($row);
161
162 if (!isset($handled_definitions[$source_pool_definition->getId()])) {
163 $this->addDefinition($source_pool_definition);
164 $handled_definitions[$source_pool_definition->getId()] = $source_pool_definition->getId();
165
166 $trashedPool = new ilTestRandomQuestionSetNonAvailablePool();
167 $trashedPool->assignDbRow($row);
168
169 $trashedPool->setUnavailabilityStatus(
171 );
172
173 $trashed_pools[$trashedPool->getId()] = $trashedPool;
174 }
175
176 if (!$this->isLostPool($row['pool_fi'])
177 && !$row['pool_id']) {
179 $lost_pool->assignDbRow($row);
180
181 $lost_pool->setUnavailabilityStatus(
183 );
184
185 $this->addLostPool($lost_pool);
186
187 if (isset($trashed_pools[$lost_pool->getId()])) {
188 unset($trashed_pools[$lost_pool->getId()]);
189 }
190 }
191
192 if (isset($row['actual_pool_title'])
193 && $source_pool_definition->getPoolTitle() !== $row['actual_pool_title']) {
194 $source_pool_definition->setPoolTitle($row['actual_pool_title']);
195 $source_pool_definition->saveToDb();
196 }
197
198 if ($row['child']) {
199 unset($trashed_pools[$row['pool_id']]);
200 }
201 }
202
203 $this->setTrashedPools($trashed_pools);
204 }
205
206 public function saveDefinitions(): void
207 {
208 foreach ($this as $source_pool_definition) {
209 $source_pool_definition->saveToDb();
210 }
211 }
212
213 public function cloneDefinitionsForTestId(int $test_id): array
214 {
215 $definition_id_map = [];
216
217 foreach ($this as $definition) {
220 $original_id = $definition->getId();
221 $definition->cloneToDbForTestId($test_id);
222 $clone_id = $definition->getId();
223
224 $definition_id_map[$original_id] = $clone_id;
225 }
226
227 return $definition_id_map;
228 }
229
230 public function deleteDefinitions(): void
231 {
232 $query = "DELETE FROM tst_rnd_quest_set_qpls WHERE test_fi = %s";
233 $this->db->manipulateF($query, ['integer'], [$this->test_obj->getTestId()]);
234 }
235
236 public function reindexPositions(): void
237 {
238 $position_index = [];
239
240 foreach ($this as $definition) {
242 $position_index[ $definition->getId() ] = $definition->getSequencePosition();
243 }
244
245 asort($position_index);
246
247 $i = 1;
248
249 foreach (array_keys($position_index) as $definition_id) {
250 $position_index[$definition_id] = $i++;
251 }
252
253 foreach ($this as $definition) {
254 $definition->setSequencePosition($position_index[$definition->getId()]);
255 }
256 }
257
258 public function getNextPosition(): int
259 {
260 return (count($this->source_pool_definitions) + 1);
261 }
262
263 public function getInvolvedSourcePoolIds(): array
264 {
265 $involved_source_pool_ids = [];
266
267 foreach ($this as $definition) {
268 $involved_source_pool_ids[ $definition->getPoolId() ] = $definition->getPoolId();
269 }
270
271 return array_values($involved_source_pool_ids);
272 }
273
274 public function getQuestionAmount(): ?int
275 {
276 $question_amount = 0;
277
278 foreach ($this as $definition) {
279 $question_amount += $definition->getQuestionAmount();
280 }
281
282 return $question_amount;
283 }
284
288 public function savedDefinitionsExist(): bool
289 {
290 $query = "SELECT COUNT(*) cnt FROM tst_rnd_quest_set_qpls WHERE test_fi = %s";
291 $res = $this->db->queryF($query, ['integer'], [$this->test_obj->getTestId()]);
292
293 $row = $this->db->fetchAssoc($res);
294
295 return $row['cnt'] > 0;
296 }
297
298 public function hasTaxonomyFilters(): bool
299 {
300 foreach ($this as $definition) {
301 if (count($definition->getMappedTaxonomyFilter())) {
302 return true;
303 }
304 }
305
306 return false;
307 }
308
309 public function hasTypeFilters(): bool
310 {
311 foreach ($this as $definition) {
312 if (count($definition->getTypeFilter())) {
313 return true;
314 }
315 }
316 return false;
317 }
318
319 public function areAllUsedPoolsAvailable(): bool
320 {
321 if ($this->hasLostPool()) {
322 return false;
323 }
324
325 if ($this->hasTrashedPool()) {
326 return false;
327 }
328
329 return true;
330 }
331
332 public function rewind(): void
333 {
334 reset($this->source_pool_definitions);
335 }
336
338 {
339 $current = current($this->source_pool_definitions);
340 return $current !== false ? $current : null;
341 }
342
343 public function key(): ?int
344 {
345 return key($this->source_pool_definitions);
346 }
347
348 public function next(): void
349 {
350 next($this->source_pool_definitions);
351 }
352
353 public function valid(): bool
354 {
355 return key($this->source_pool_definitions) !== null;
356 }
357
358 public function getNonAvailablePools(): array
359 {
360 return array_merge($this->getTrashedPools(), $this->getLostPools());
361 }
362}
addDefinition(ilTestRandomQuestionSetSourcePoolDefinition $source_pool_definition)
ilTestRandomQuestionSetSourcePoolDefinitionFactory $source_pool_definition_factory
__construct(ilDBInterface $db, ilObjTest $test_obj, ilTestRandomQuestionSetSourcePoolDefinitionFactory $source_pool_definition_factory)
Interface ilDBInterface.
$res
Definition: ltiservices.php:69