ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilTestRandomQuestionSetSourcePoolDefinitionList.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  protected ilDBInterface $db;
28  protected ilObjTest $testOBJ;
29  private array $sourcePoolDefinitions = array();
31  protected array $lostPools = array();
32  protected array $trashedPools = array();
33 
34  public function __construct(ilDBInterface $db, ilObjTest $testOBJ, ilTestRandomQuestionSetSourcePoolDefinitionFactory $sourcePoolDefinitionFactory)
35  {
36  $this->db = $db;
37  $this->testOBJ = $testOBJ;
38  $this->sourcePoolDefinitionFactory = $sourcePoolDefinitionFactory;
39  }
40 
41  public function addDefinition(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition)
42  {
43  $this->sourcePoolDefinitions[ $sourcePoolDefinition->getId() ] = $sourcePoolDefinition;
44  }
45 
47  {
48  $this->lostPools[$lostPool->getId()] = $lostPool;
49  }
50 
51  public function isLostPool($poolId): bool
52  {
53  return isset($this->lostPools[$poolId]);
54  }
55 
56  public function hasLostPool(): bool
57  {
58  return (bool) count($this->lostPools);
59  }
60 
61  public function getLostPools(): array
62  {
63  return $this->lostPools;
64  }
65 
66  public function getLostPool($poolId)
67  {
68  if ($this->isLostPool($poolId)) {
69  return $this->lostPools[$poolId];
70  }
71 
72  return null;
73  }
74 
75  public function isTrashedPool($poolId): bool
76  {
77  return isset($this->trashedPools[$poolId]);
78  }
79 
80  public function hasTrashedPool(): bool
81  {
82  return (bool) count($this->trashedPools);
83  }
84 
85  public function getTrashedPools(): array
86  {
87  return $this->trashedPools;
88  }
89 
93  public function setTrashedPools($trashedPools)
94  {
95  $this->trashedPools = $trashedPools;
96  }
97 
98  // hey: fixRandomTestBuildable - provide single definitions, quantities distribution likes to deal with objects
99 
100  public function hasDefinition($sourcePoolDefinitionId): bool
101  {
102  return $this->getDefinition($sourcePoolDefinitionId) !== null;
103  }
104 
105  public function getDefinition($sourcePoolDefinitionId): ?ilTestRandomQuestionSetSourcePoolDefinition
106  {
107  if (isset($this->sourcePoolDefinitions[$sourcePoolDefinitionId])) {
108  return $this->sourcePoolDefinitions[$sourcePoolDefinitionId];
109  }
110 
111  return null;
112  }
113 
115  {
116  foreach ($this as $definition) {
117  if ($definition->getPoolId() != $sourcePoolId) {
118  continue;
119  }
120 
121  return $definition;
122  }
123 
124  throw new InvalidArgumentException('invalid source pool id given');
125  }
126 
127  public function getDefinitionIds(): array
128  {
129  return array_keys($this->sourcePoolDefinitions);
130  }
131 
132  public function getDefinitionCount(): int
133  {
134  return count($this->sourcePoolDefinitions);
135  }
136  // hey.
137 
138  public function loadDefinitions()
139  {
140  $query = "
141  SELECT tst_rnd_quest_set_qpls.*, odat.obj_id pool_id, odat.title actual_pool_title, tree.child
142  FROM tst_rnd_quest_set_qpls
143  LEFT JOIN object_data odat
144  ON odat.obj_id = pool_fi
145  LEFT JOIN object_reference oref
146  ON oref.obj_id = pool_fi
147  LEFT JOIN tree
148  ON tree = %s
149  AND child = oref.ref_id
150  WHERE test_fi = %s
151  ORDER BY sequence_pos ASC
152  ";
153 
154  $res = $this->db->queryF($query, array('integer', 'integer'), array(1, $this->testOBJ->getTestId()));
155 
156  $handledDefinitions = array();
157  $trashedPools = array();
158 
159  while ($row = $this->db->fetchAssoc($res)) {
160  $sourcePoolDefinition = $this->sourcePoolDefinitionFactory->getEmptySourcePoolDefinition();
161  $sourcePoolDefinition->initFromArray($row);
162 
163  if (!isset($handledDefinitions[$sourcePoolDefinition->getId()])) {
164  $this->addDefinition($sourcePoolDefinition);
165  $handledDefinitions[$sourcePoolDefinition->getId()] = $sourcePoolDefinition->getId();
166 
167  $trashedPool = new ilTestRandomQuestionSetNonAvailablePool();
168  $trashedPool->assignDbRow($row);
169 
170  $trashedPool->setUnavailabilityStatus(
172  );
173 
174  $trashedPools[$trashedPool->getId()] = $trashedPool;
175  }
176 
177  if (!$this->isLostPool($row['pool_id'])
178  && !$row['pool_id']) {
179  $lostPool = new ilTestRandomQuestionSetNonAvailablePool();
180  $lostPool->assignDbRow($row);
181 
182  $lostPool->setUnavailabilityStatus(
184  );
185 
186  $this->addLostPool($lostPool);
187 
188  if (isset($trashedPools[$lostPool->getId()])) {
189  unset($trashedPools[$lostPool->getId()]);
190  }
191  }
192 
193  if (isset($row['actual_pool_title'])
194  && $sourcePoolDefinition->getPoolTitle() !== $row['actual_pool_title']) {
195  $sourcePoolDefinition->setPoolTitle($row['actual_pool_title']);
196  $sourcePoolDefinition->saveToDb();
197  }
198 
199  if ($row['child']) {
200  unset($trashedPools[$row['pool_id']]);
201  }
202  }
203 
204  $this->setTrashedPools($trashedPools);
205  }
206 
207  public function saveDefinitions()
208  {
209  foreach ($this as $sourcePoolDefinition) {
211  $sourcePoolDefinition->saveToDb();
212  }
213  }
214 
215  public function cloneDefinitionsForTestId($testId): array
216  {
217  $definitionIdMap = array();
218 
219  foreach ($this as $definition) {
222  $originalId = $definition->getId();
223  $definition->cloneToDbForTestId($testId);
224  $cloneId = $definition->getId();
225 
226  $definitionIdMap[$originalId] = $cloneId;
227  }
228 
229  return $definitionIdMap;
230  }
231 
232  public function deleteDefinitions()
233  {
234  $query = "DELETE FROM tst_rnd_quest_set_qpls WHERE test_fi = %s";
235  $this->db->manipulateF($query, array('integer'), array($this->testOBJ->getTestId()));
236  }
237 
238  public function reindexPositions()
239  {
240  $positionIndex = array();
241 
242  foreach ($this as $definition) {
244  $positionIndex[ $definition->getId() ] = $definition->getSequencePosition();
245  }
246 
247  asort($positionIndex);
248 
249  $i = 1;
250 
251  foreach ($positionIndex as $definitionId => $definitionPosition) {
252  $positionIndex[$definitionId] = $i++;
253  }
254 
255  foreach ($this as $definition) {
256  $definition->setSequencePosition($positionIndex[$definition->getId()]);
257  }
258  }
259 
260  public function getNextPosition(): int
261  {
262  return (count($this->sourcePoolDefinitions) + 1);
263  }
264 
265  public function getInvolvedSourcePoolIds(): array
266  {
267  $involvedSourcePoolIds = array();
268 
269  foreach ($this as $definition) {
271  $involvedSourcePoolIds[ $definition->getPoolId() ] = $definition->getPoolId();
272  }
273 
274  return array_values($involvedSourcePoolIds);
275  }
276 
277  public function getQuestionAmount(): ?int
278  {
279  $questionAmount = 0;
280 
281  foreach ($this as $definition) {
283  $questionAmount += $definition->getQuestionAmount();
284  }
285 
286  return $questionAmount;
287  }
288 
292  public function savedDefinitionsExist(): bool
293  {
294  $query = "SELECT COUNT(*) cnt FROM tst_rnd_quest_set_qpls WHERE test_fi = %s";
295  $res = $this->db->queryF($query, array('integer'), array($this->testOBJ->getTestId()));
296 
297  $row = $this->db->fetchAssoc($res);
298 
299  return $row['cnt'] > 0;
300  }
301 
302  public function hasTaxonomyFilters(): bool
303  {
304  foreach ($this as $definition) {
306  // fau: taxFilter/typeFilter - new check for existing taxonomy filter
307  if (count($definition->getMappedTaxonomyFilter())) {
308  return true;
309  }
310  #if( $definition->getMappedFilterTaxId() && $definition->getMappedFilterTaxNodeId() )
311  #{
312  # return true;
313  #}
314  // fau.
315  }
316 
317  return false;
318  }
319 
320  // fau: taxFilter/typeFilter - check for existing type filters
321  public function hasTypeFilters(): bool
322  {
323  foreach ($this as $definition) {
324  if (count($definition->getTypeFilter())) {
325  return true;
326  }
327  }
328  return false;
329  }
330  // fau.
331 
332  public function areAllUsedPoolsAvailable(): bool
333  {
334  if ($this->hasLostPool()) {
335  return false;
336  }
337 
338  if ($this->hasTrashedPool()) {
339  return false;
340  }
341 
342  return true;
343  }
344 
348  public function rewind()
349  {
350  return reset($this->sourcePoolDefinitions);
351  }
352 
356  public function current()
357  {
358  return current($this->sourcePoolDefinitions);
359  }
360 
364  public function key()
365  {
366  return key($this->sourcePoolDefinitions);
367  }
368 
372  public function next()
373  {
374  return next($this->sourcePoolDefinitions);
375  }
376 
380  public function valid(): bool
381  {
382  return key($this->sourcePoolDefinitions) !== null;
383  }
384 
385  public function getNonAvailablePools(): array
386  {
387  return array_merge($this->getTrashedPools(), $this->getLostPools());
388  }
389 }
__construct(ilDBInterface $db, ilObjTest $testOBJ, ilTestRandomQuestionSetSourcePoolDefinitionFactory $sourcePoolDefinitionFactory)
$res
Definition: ltiservices.php:69
ilTestRandomQuestionSetSourcePoolDefinitionFactory $sourcePoolDefinitionFactory
addDefinition(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition)
$query
$i
Definition: metadata.php:41