ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilTestRandomQuestionSetSourcePoolDefinitionList.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetNonAvailablePool.php';
12{
18 protected $db = null;
19
25 protected $testOBJ = null;
26
30 private $sourcePoolDefinitions = array();
31
36
40 protected $lostPools = array();
41
45 protected $trashedPools = array();
46
54 {
55 $this->db = $db;
56 $this->testOBJ = $testOBJ;
57 $this->sourcePoolDefinitionFactory = $sourcePoolDefinitionFactory;
58 }
59
60 public function addDefinition(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition)
61 {
62 $this->sourcePoolDefinitions[ $sourcePoolDefinition->getId() ] = $sourcePoolDefinition;
63 }
64
66 {
67 $this->lostPools[$lostPool->getId()] = $lostPool;
68 }
69
70 public function isLostPool($poolId)
71 {
72 return isset($this->lostPools[$poolId]);
73 }
74
75 public function hasLostPool()
76 {
77 return (bool)count($this->lostPools);
78 }
79
80 public function getLostPools()
81 {
82 return $this->lostPools;
83 }
84
85 public function getLostPool($poolId)
86 {
87 if( $this->isLostPool($poolId) )
88 {
89 return $this->lostPools[$poolId];
90 }
91
92 return null;
93 }
94
95 public function isTrashedPool($poolId)
96 {
97 return isset($this->trashedPools[$poolId]);
98 }
99
100 public function hasTrashedPool()
101 {
102 return (bool)count($this->trashedPools);
103 }
104
108 public function getTrashedPools()
109 {
110 return $this->trashedPools;
111 }
112
117 {
118 $this->trashedPools = $trashedPools;
119 }
120
121 // hey: fixRandomTestBuildable - provide single definitions, quantities distribution likes to deal with objects
122
123 public function hasDefinition($sourcePoolDefinitionId)
124 {
125 return $this->getDefinition($sourcePoolDefinitionId) !== null;
126 }
127
128 public function getDefinition($sourcePoolDefinitionId)
129 {
130 if( isset($this->sourcePoolDefinitions[$sourcePoolDefinitionId]) )
131 {
132 return $this->sourcePoolDefinitions[$sourcePoolDefinitionId];
133 }
134
135 return null;
136 }
137
138 public function getDefinitionBySourcePoolId($sourcePoolId)
139 {
140 foreach($this as $definition)
141 {
142 if($definition->getPoolId() != $sourcePoolId)
143 {
144 continue;
145 }
146
147 return $definition;
148 }
149
150 throw new InvalidArgumentException('invalid source pool id given');
151 }
152
153 public function getDefinitionIds()
154 {
155 return array_keys($this->sourcePoolDefinitions);
156 }
157
158 public function getDefinitionCount()
159 {
160 return count($this->sourcePoolDefinitions);
161 }
162 // hey.
163
164 public function loadDefinitions()
165 {
166 $query = "
167 SELECT tst_rnd_quest_set_qpls.*, odat.obj_id pool_id, tree.child
168 FROM tst_rnd_quest_set_qpls
169 LEFT JOIN object_data odat
170 ON odat.obj_id = pool_fi
171 LEFT JOIN object_reference oref
172 ON oref.obj_id = pool_fi
173 LEFT JOIN tree
174 ON tree = %s
175 AND child = oref.ref_id
176 WHERE test_fi = %s
177 ORDER BY sequence_pos ASC
178 ";
179
180 $res = $this->db->queryF($query, array('integer', 'integer'), array(1, $this->testOBJ->getTestId()));
181
182 $handledDefinitions = array();
183 $trashedPools = array();
184
185 while( $row = $this->db->fetchAssoc($res) )
186 {
187 $sourcePoolDefinition = $this->sourcePoolDefinitionFactory->getEmptySourcePoolDefinition();
188 $sourcePoolDefinition->initFromArray($row);
189
190 if( !isset($handledDefinitions[$sourcePoolDefinition->getId()]) )
191 {
192 $this->addDefinition($sourcePoolDefinition);
193 $handledDefinitions[$sourcePoolDefinition->getId()] = $sourcePoolDefinition->getId();
194
195 $trashedPool = new ilTestRandomQuestionSetNonAvailablePool();
196 $trashedPool->assignDbRow($row);
197
198 $trashedPool->setUnavailabilityStatus(
200 );
201
202 $trashedPools[$trashedPool->getId()] = $trashedPool;
203 }
204
205 if( !$this->isLostPool($row['pool_id']) )
206 {
207 if( !$row['pool_id'] )
208 {
210 $lostPool->assignDbRow($row);
211
212 $lostPool->setUnavailabilityStatus(
214 );
215
216 $this->addLostPool($lostPool);
217
218 if( isset($trashedPools[$lostPool->getId()]) )
219 {
220 unset($trashedPools[$lostPool->getId()]);
221 }
222 }
223 }
224
225 if( $row['child'] )
226 {
227 unset($trashedPools[$row['pool_id']]);
228 }
229 }
230
232 }
233
234 public function saveDefinitions()
235 {
236 foreach($this as $sourcePoolDefinition)
237 {
239 $sourcePoolDefinition->saveToDb();
240 }
241 }
242
243 public function cloneDefinitionsForTestId($testId)
244 {
245 $definitionIdMap = array();
246
247 foreach($this as $definition)
248 {
251 $originalId = $definition->getId();
252 $definition->cloneToDbForTestId($testId);
253 $cloneId = $definition->getId();
254
255 $definitionIdMap[$originalId] = $cloneId;
256 }
257
258 return $definitionIdMap;
259 }
260
261 public function deleteDefinitions()
262 {
263 $query = "DELETE FROM tst_rnd_quest_set_qpls WHERE test_fi = %s";
264 $this->db->manipulateF($query, array('integer'), array($this->testOBJ->getTestId()));
265 }
266
267 public function reindexPositions()
268 {
269 $positionIndex = array();
270
271 foreach($this as $definition)
272 {
274 $positionIndex[ $definition->getId() ] = $definition->getSequencePosition();
275 }
276
277 asort($positionIndex);
278
279 $i = 1;
280
281 foreach($positionIndex as $definitionId => $definitionPosition)
282 {
283 $positionIndex[$definitionId] = $i++;
284 }
285
286 foreach($this as $definition)
287 {
288 $definition->setSequencePosition( $positionIndex[$definition->getId()] );
289 }
290 }
291
292 public function getNextPosition()
293 {
294 return ( count($this->sourcePoolDefinitions) + 1 );
295 }
296
297 public function getInvolvedSourcePoolIds()
298 {
299 $involvedSourcePoolIds = array();
300
301 foreach($this as $definition)
302 {
304 $involvedSourcePoolIds[ $definition->getPoolId() ] = $definition->getPoolId();
305 }
306
307 return array_values($involvedSourcePoolIds);
308 }
309
310 public function getQuestionAmount()
311 {
312 $questionAmount = 0;
313
314 foreach($this as $definition)
315 {
317 $questionAmount += $definition->getQuestionAmount();
318 }
319
320 return $questionAmount;
321 }
322
326 public function savedDefinitionsExist()
327 {
328 $query = "SELECT COUNT(*) cnt FROM tst_rnd_quest_set_qpls WHERE test_fi = %s";
329 $res = $this->db->queryF($query, array('integer'), array($this->testOBJ->getTestId()));
330
331 $row = $this->db->fetchAssoc($res);
332
333 return $row['cnt'] > 0;
334 }
335
336 public function hasTaxonomyFilters()
337 {
338 foreach($this as $definition)
339 {
341 if( $definition->getMappedFilterTaxId() && $definition->getMappedFilterTaxNodeId() )
342 {
343 return true;
344 }
345 }
346
347 return false;
348 }
349
350 public function areAllUsedPoolsAvailable()
351 {
352 if( $this->hasLostPool() )
353 {
354 return false;
355 }
356
357 if( $this->hasTrashedPool() )
358 {
359 return false;
360 }
361
362 return true;
363 }
364
368 public function rewind()
369 {
370 return reset($this->sourcePoolDefinitions);
371 }
372
376 public function current()
377 {
378 return current($this->sourcePoolDefinitions);
379 }
380
384 public function key()
385 {
386 return key($this->sourcePoolDefinitions);
387 }
388
392 public function next()
393 {
394 return next($this->sourcePoolDefinitions);
395 }
396
400 public function valid()
401 {
402 return key($this->sourcePoolDefinitions) !== null;
403 }
404
405 public function getNonAvailablePools()
406 {
407 echo get_class($this->getTrashedPools()[0]);
408 return array_merge($this->getTrashedPools(), $this->getLostPools());
409 }
410}
An exception for terminatinating execution or to throw for unit testing.
addDefinition(ilTestRandomQuestionSetSourcePoolDefinition $sourcePoolDefinition)
__construct(ilDBInterface $db, ilObjTest $testOBJ, ilTestRandomQuestionSetSourcePoolDefinitionFactory $sourcePoolDefinitionFactory)
Constructor.
Interface ilDBInterface.