ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilTestRandomQuestionSetConfig.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
33
35 private ?string $questionAmountConfigurationMode = null;
36 private ?int $questionAmountPerTest = null;
37 private ?int $lastQuestionSyncTimestamp = null;
38 private array $buildableMessages = [];
39
41 {
42 $this->requirePoolsWithHomogeneousScoredQuestions = $requirePoolsWithHomogeneousScoredQuestions;
43 }
44
46 {
48 }
49
51 {
52 $this->questionAmountConfigurationMode = $questionAmountConfigurationMode;
53 }
54
55 public function getQuestionAmountConfigurationMode(): ?string
56 {
58 }
59
61 {
63 }
64
66 {
68 }
69
70 public function isValidQuestionAmountConfigurationMode(string $amountMode): bool
71 {
72 switch ($amountMode) {
75
76 return true;
77 }
78
79 return false;
80 }
81
83 {
84 $this->questionAmountPerTest = $questionAmountPerTest;
85 }
86
87 public function getQuestionAmountPerTest(): ?int
88 {
90 }
91
93 {
94 $this->lastQuestionSyncTimestamp = $lastQuestionSyncTimestamp;
95 }
96
98 {
100 }
101
102 public function getBuildableMessages(): array
103 {
105 }
106
107 public function initFromArray(array $data_array): void
108 {
109 foreach ($data_array as $field => $value) {
110 switch ($field) {
111 case 'req_pools_homo_scored': $this->setPoolsWithHomogeneousScoredQuestionsRequired((bool) $value);
112 break;
113 case 'quest_amount_cfg_mode': $this->setQuestionAmountConfigurationMode($value);
114 break;
115 case 'quest_amount_per_test': $this->setQuestionAmountPerTest($value);
116 break;
117 case 'quest_sync_timestamp': $this->setLastQuestionSyncTimestamp($value);
118 break;
119 }
120 }
121 }
122
123 public function loadFromDb(): void
124 {
125 $res = $this->db->queryF(
126 "SELECT * FROM tst_rnd_quest_set_cfg WHERE test_fi = %s",
127 ['integer'],
128 [$this->test_obj->getTestId()]
129 );
130
131 $row = $this->db->fetchAssoc($res);
132 if ($row !== null) {
133 $this->initFromArray($row);
134 }
135 }
136
137 public function saveToDb(): void
138 {
139 if ($this->dbRecordExists($this->test_obj->getTestId())) {
140 $this->updateDbRecord($this->test_obj->getTestId());
141 return;
142 }
143
144 $this->insertDbRecord($this->test_obj->getTestId());
145 }
146
147 public function cloneToDbForTestId(int $test_id): void
148 {
149 $this->insertDbRecord($test_id);
150 }
151
152 public function deleteFromDb(): void
153 {
154 $this->db->manipulateF(
155 "DELETE FROM tst_rnd_quest_set_cfg WHERE test_fi = %s",
156 ['integer'],
157 [$this->test_obj->getTestId()]
158 );
159 }
160
161 private function dbRecordExists(int $test_id): bool
162 {
163 $res = $this->db->queryF(
164 "SELECT COUNT(*) cnt FROM tst_rnd_quest_set_cfg WHERE test_fi = %s",
165 ['integer'],
166 [$test_id]
167 );
168
169 $row = $this->db->fetchAssoc($res);
170
171 return (bool) $row['cnt'];
172 }
173
174 private function updateDbRecord(int $test_id): void
175 {
176 $this->db->update(
177 'tst_rnd_quest_set_cfg',
178 [
179 'req_pools_homo_scored' => ['integer', (int) $this->arePoolsWithHomogeneousScoredQuestionsRequired()],
180 'quest_amount_cfg_mode' => ['text', $this->getQuestionAmountConfigurationMode()],
181 'quest_amount_per_test' => ['integer', (int) $this->getQuestionAmountPerTest()],
182 'quest_sync_timestamp' => ['integer', (int) $this->getLastQuestionSyncTimestamp()]
183 ],
184 [
185 'test_fi' => ['integer', $test_id]
186 ]
187 );
188 }
189
190 private function insertDbRecord(int $test_id): void
191 {
192 $this->db->insert(
193 'tst_rnd_quest_set_cfg',
194 [
195 'test_fi' => ['integer', $test_id],
196 'req_pools_homo_scored' => ['integer', (int) $this->arePoolsWithHomogeneousScoredQuestionsRequired()],
197 'quest_amount_cfg_mode' => ['text', $this->getQuestionAmountConfigurationMode()],
198 'quest_amount_per_test' => ['integer', (int) $this->getQuestionAmountPerTest()],
199 'quest_sync_timestamp' => ['integer', (int) $this->getLastQuestionSyncTimestamp()]
200 ]
201 );
202 }
203
204 public function isQuestionSetConfigured(): bool
205 {
206 return $this->getLastQuestionSyncTimestamp() != 0
208 && $this->hasSourcePoolDefinitions()
209 && $this->isQuestionSetBuildable();
210 }
211
212 public function isQuestionAmountConfigComplete(): bool
213 {
215 $sourcePoolDefinitionList = $this->buildSourcePoolDefinitionList($this->test_obj);
216
217 $sourcePoolDefinitionList->loadDefinitions();
218
219 foreach ($sourcePoolDefinitionList as $definition) {
220 if ($definition->getQuestionAmount() < 1) {
221 return false;
222 }
223 }
224 } elseif ($this->getQuestionAmountPerTest() < 1) {
225 return false;
226 }
227
228 return true;
229 }
230
231 public function hasSourcePoolDefinitions(): bool
232 {
233 $sourcePoolDefinitionList = $this->buildSourcePoolDefinitionList($this->test_obj);
234
235 return $sourcePoolDefinitionList->savedDefinitionsExist();
236 }
237
238 public function isQuestionSetBuildable(): bool
239 {
240 $sourcePoolDefinitionList = $this->buildSourcePoolDefinitionList($this->test_obj);
241 $sourcePoolDefinitionList->loadDefinitions();
242
243 $stagingPoolQuestionList = new ilTestRandomQuestionSetStagingPoolQuestionList($this->db, $this->component_repository);
244
246 $this->db,
247 $this->lng,
248 $this->logger,
249 $this->test_obj,
250 $this,
251 $sourcePoolDefinitionList,
252 $stagingPoolQuestionList
253 );
254
255 $buildable = $questionSetBuilder->checkBuildable();
256 $this->buildableMessages = $questionSetBuilder->getCheckMessages();
257 return $buildable;
258 }
259
260 public function doesQuestionSetRelatedDataExist(): bool
261 {
262 if ($this->dbRecordExists($this->test_obj->getTestId())) {
263 return true;
264 }
265
266 $sourcePoolDefinitionList = $this->buildSourcePoolDefinitionList($this->test_obj);
267
268 if ($sourcePoolDefinitionList->savedDefinitionsExist()) {
269 return true;
270 }
271
272 return false;
273 }
274
275 public function removeQuestionSetRelatedData(): void
276 {
277 $sourcePoolDefinitionList = $this->buildSourcePoolDefinitionList($this->test_obj);
278 $sourcePoolDefinitionList->deleteDefinitions();
279
281 $this->db,
282 $this->logger,
283 $this->test_obj
284 );
285 $stagingPool->reset();
286
287 $this->deleteFromDb();
288 }
289
290 public function cloneQuestionSetRelatedData(ilObjTest $clone_test_obj): void
291 {
292 $this->loadFromDb();
293 $this->cloneToDbForTestId($clone_test_obj->getTestId());
294
295 // clone source pool definitions (selection rules)
296
297 $source_pool_definition_list_orig = $this->buildSourcePoolDefinitionList($this->test_obj);
298 $source_pool_definition_list_orig->loadDefinitions();
299 $definition_id_map = $source_pool_definition_list_orig->cloneDefinitionsForTestId($clone_test_obj->getTestId());
300 $this->registerClonedSourcePoolDefinitionIdMapping($clone_test_obj, $definition_id_map);
301
302 // build new question stage for cloned test
303
304 $source_pool_definition_list_clone = $this->buildSourcePoolDefinitionList($clone_test_obj);
305 $staging_pool = $this->buildStagingPoolBuilder($clone_test_obj);
306
307 $source_pool_definition_list_clone->loadDefinitions();
308 $staging_pool->rebuild($source_pool_definition_list_clone);
309 $source_pool_definition_list_clone->saveDefinitions();
310
311 $this->updateLastQuestionSyncTimestampForTestId($clone_test_obj->getTestId(), time());
312 }
313
314 private function registerClonedSourcePoolDefinitionIdMapping(ilObjTest $cloneTestOBJ, array $definitionIdMap): void
315 {
317
318 foreach ($definitionIdMap as $originalDefinitionId => $cloneDefinitionId) {
319 $originalKey = $this->test_obj->getRefId() . '_rndSelDef_' . $originalDefinitionId;
320 $mappedKey = $cloneTestOBJ->getRefId() . '_rndSelDef_' . $cloneDefinitionId;
321 $cwo->appendMapping($originalKey, $mappedKey);
322 $this->logger->info(__METHOD__ . ": Added random selection definition id mapping $originalKey <-> $mappedKey");
323 }
324 }
325
327 {
329 $this->db,
330 $test_obj,
332 $this->db,
333 $test_obj
334 )
335 );
336 }
337
339 {
340 $stagingPool = new ilTestRandomQuestionSetStagingPoolBuilder($this->db, $this->logger, $test_obj);
341
342 return $stagingPool;
343 }
344
345 public function updateLastQuestionSyncTimestampForTestId(int $test_id, int $timestamp): void
346 {
347 $this->db->update(
348 'tst_rnd_quest_set_cfg',
349 [
350 'quest_sync_timestamp' => ['integer', (int) $timestamp]
351 ],
352 [
353 'test_fi' => ['integer', $test_id]
354 ]
355 );
356 }
357
358 public function isResultTaxonomyFilterSupported(): bool
359 {
360 return true;
361 }
362
363 public function getSelectableQuestionPools(): array
364 {
365 return $this->test_obj->getAvailableQuestionpools(
366 true,
368 false,
369 true,
370 true
371 );
372 }
373
374 public function doesSelectableQuestionPoolsExist(): bool
375 {
376 return (bool) count($this->getSelectableQuestionPools());
377 }
378
380 {
381 $definitionList = $this->buildSourcePoolDefinitionList($this->test_obj);
382 $definitionList->loadDefinitions();
383
384 $poolTitles = [];
385
386 foreach ($definitionList as $definition) {
387 $refId = current(ilObject::_getAllReferences($definition->getPoolId()));
388 $href = ilLink::_getLink($refId, 'qpl');
389 $title = $definition->getPoolTitle();
390
391 $poolTitles[$definition->getPoolId()] = "<a href=\"$href\" alt=\"$title\">$title</a>";
392 }
393
394 return implode(', ', $poolTitles);
395 }
396}
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
static _getInstance(int $a_copy_id)
getTestId()
Gets the database id of the additional test data.
static _getAllReferences(int $id)
get all reference ids for object ID
static getInstance(ilDBInterface $db, ilLanguage $lng, TestLogger $logger, ilObjTest $testOBJ, ilTestRandomQuestionSetConfig $questionSetConfig, ilTestRandomQuestionSetSourcePoolDefinitionList $sourcePoolDefinitionList, ilTestRandomQuestionSetStagingPoolQuestionList $stagingPoolQuestionList)
updateLastQuestionSyncTimestampForTestId(int $test_id, int $timestamp)
setQuestionAmountConfigurationMode(?string $questionAmountConfigurationMode)
registerClonedSourcePoolDefinitionIdMapping(ilObjTest $cloneTestOBJ, array $definitionIdMap)
setLastQuestionSyncTimestamp(int $lastQuestionSyncTimestamp)
setQuestionAmountPerTest(?int $questionAmountPerTest)
setPoolsWithHomogeneousScoredQuestionsRequired(bool $requirePoolsWithHomogeneousScoredQuestions)
$res
Definition: ltiservices.php:69
$refId
Definition: xapitoken.php:58