ILIAS  release_8 Revision v8.24
class.ilTestImporter.php
Go to the documentation of this file.
1<?php
2
27{
31 public static $finallyProcessedTestsRegistry = array();
32
44 public function importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping): void
45 {
47
48 if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id)) {
49 // container content
50 $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
51 ilSession::set('tst_import_subdir', $this->getImportPackageName());
52 $newObj->saveToDb(); // this generates test id first time
53 $questionParentObjId = $newObj->getId();
54 } else {
55 // single object
56 $new_id = $a_mapping->getMapping('Modules/Test', 'tst', 'new_id');
57 $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
58
59 $questionParentObjId = ilSession::get('tst_import_qst_parent') ?? $newObj->getId();
60 }
61
62 $newObj->loadFromDb();
63
64 list($xml_file, $qti_file) = $this->parseXmlFileNames();
65
66 global $DIC; /* @var ILIAS\DI\Container $DIC */
67 if (!@file_exists($xml_file)) {
68 $DIC['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
69 return;
70 }
71 if (!@file_exists($qti_file)) {
72 $DIC['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $qti_file);
73 return;
74 }
75
76 /* @var ilObjTest $newObj */
77
78 // FIXME: Copied from ilObjTestGUI::importVerifiedFileObject
79 // TODO: move all logic to ilObjTest::importVerifiedFile and call
80 // this method from ilObjTestGUI and ilTestImporter
81 $newObj->mark_schema->flush();
82
83 $idents = ilSession::get('tst_import_idents');
84
85 // start parsing of QTI files
86 $qtiParser = new ilQTIParser($qti_file, ilQTIParser::IL_MO_PARSE_QTI, $questionParentObjId, $idents);
87 $qtiParser->setTestObject($newObj);
88 $qtiParser->startParsing();
89 $newObj = $qtiParser->getTestObject();
90
91 // import page data
92 $questionPageParser = new ilQuestionPageParser($newObj, $xml_file, basename($this->getImportDirectory()));
93 $questionPageParser->setQuestionMapping($qtiParser->getImportMapping());
94 $questionPageParser->startParsing();
95
96 $a_mapping = $this->addTexonomyAndQuestionsMapping($qtiParser->getQuestionIdMapping(), $newObj->getId(), $a_mapping);
97
98 if ($newObj->isRandomTest()) {
99 $this->importRandomQuestionSetConfig($newObj, $xml_file, $a_mapping);
100 }
101
102 // import test results
103 if (@file_exists(ilSession::get("tst_import_results_file"))) {
104 $results = new ilTestResultsImportParser(ilSession::get("tst_import_results_file"), $newObj);
105 $results->setQuestionIdMapping($a_mapping->getMappingsOfEntity('Modules/Test', 'quest'));
106 $results->setSrcPoolDefIdMapping($a_mapping->getMappingsOfEntity('Modules/Test', 'rnd_src_pool_def'));
107 $results->startParsing();
108 }
109
110 $newObj->saveToDb(); // this creates test_fi
111 $newObj->update(); // this saves ilObject data
112
113 // import skill assignments
114 $importedAssignmentList = $this->importQuestionSkillAssignments($a_mapping, $newObj, $xml_file);
115 $this->importSkillLevelThresholds($a_mapping, $importedAssignmentList, $newObj, $xml_file);
116
117 $a_mapping->addMapping("Modules/Test", "tst", $a_id, $newObj->getId());
118 }
119
120 public function addTexonomyAndQuestionsMapping(array $question_id_mapping, int $new_obj_id, ilImportMapping $mapping): ilImportMapping
121 {
122 foreach ($question_id_mapping as $oldQuestionId => $newQuestionId) {
123 $mapping->addMapping(
124 "Services/Taxonomy",
125 "tax_item",
126 "tst:quest:$oldQuestionId",
127 $newQuestionId
128 );
129
130 $mapping->addMapping(
131 "Services/Taxonomy",
132 "tax_item_obj_id",
133 "tst:quest:$oldQuestionId",
134 $new_obj_id
135 );
136
137 $mapping->addMapping(
138 "Modules/Test",
139 "quest",
140 $oldQuestionId,
141 $newQuestionId
142 );
143 }
144
145 return $mapping;
146 }
147
153 public function finalProcessing(ilImportMapping $a_mapping): void
154 {
155 $maps = $a_mapping->getMappingsOfEntity("Modules/Test", "tst");
156
157 foreach ($maps as $old => $new) {
158 if ($old == "new_id" || (int) $old <= 0) {
159 continue;
160 }
161
162 if (isset(self::$finallyProcessedTestsRegistry[$new])) {
163 continue;
164 }
165
166 /* @var ilObjTest $testOBJ */
167 $testOBJ = ilObjectFactory::getInstanceByObjId($new, false);
168 if ($testOBJ->isRandomTest()) {
169 $this->finalRandomTestTaxonomyProcessing($a_mapping, $old, $new, $testOBJ);
170 }
171
172 self::$finallyProcessedTestsRegistry[$new] = true;
173 }
174 }
175
176 protected function finalRandomTestTaxonomyProcessing(ilImportMapping $mapping, $oldTstObjId, $newTstObjId, ilObjTest $testOBJ)
177 {
178 $new_tax_ids = $mapping->getMapping(
179 'Services/Taxonomy',
180 'tax_usage_of_obj',
181 $oldTstObjId
182 );
183
184 if ($new_tax_ids !== false) {
185 $tax_ids = explode(":", $new_tax_ids);
186
187 foreach ($tax_ids as $tid) {
188 ilObjTaxonomy::saveUsage((int) $tid, (int) $newTstObjId);
189 }
190 }
191
192 // update all source pool definition's tax/taxNode ids with new mapped id
193 global $DIC; /* @var ILIAS\DI\Container $DIC */
194 $ilDB = $DIC['ilDB'];
195
197 $ilDB,
198 $testOBJ
199 );
200
202 $ilDB,
203 $testOBJ,
204 $srcPoolDefFactory
205 );
206
207 $srcPoolDefList->loadDefinitions();
208
209 foreach ($srcPoolDefList as $definition) {
210 // #21330
211 if (!is_array($definition->getMappedTaxonomyFilter()) || 0 === count($definition->getMappedTaxonomyFilter())) {
212 continue;
213 }
214
215 $definition->setMappedTaxonomyFilter(
217 $mapping,
218 $definition->getMappedTaxonomyFilter()
219 )
220 );
221 $definition->saveToDb();
222 }
223 }
224
230 protected function getNewMappedTaxonomyFilter(ilImportMapping $mapping, array $mappedFilter): array
231 {
232 $newMappedFilter = array();
233
234 foreach ($mappedFilter as $taxId => $taxNodes) {
235 $newTaxId = $mapping->getMapping(
236 'Services/Taxonomy',
237 'tax',
238 $taxId
239 );
240
241 if (!$newTaxId) {
242 continue;
243 }
244
245 $newMappedFilter[$newTaxId] = array();
246
247 foreach ($taxNodes as $taxNodeId) {
248 $newTaxNodeId = $mapping->getMapping(
249 'Services/Taxonomy',
250 'tax_tree',
251 $taxNodeId
252 );
253
254 if (!$newTaxNodeId) {
255 continue;
256 }
257
258 $newMappedFilter[$newTaxId][] = $newTaxNodeId;
259 }
260 }
261
262 return $newMappedFilter;
263 }
264
269 protected function parseXmlFileNames(): array
270 {
271 global $DIC; /* @var ILIAS\DI\Container $DIC */
272 $DIC['ilLog']->write(__METHOD__ . ': ' . $this->getImportDirectory());
273
274 $basename = basename($this->getImportDirectory());
275
276 $xml = $this->getImportDirectory() . '/' . $basename . '.xml';
277 $qti = $this->getImportDirectory() . '/' . preg_replace('/test|tst/', 'qti', $basename) . '.xml';
278
279 return array($xml,$qti);
280 }
281
282 private function getImportDirectoryContainer(): string
283 {
284 $dir = $this->getImportDirectory();
285 $dir = dirname($dir);
286 return $dir;
287 }
288
289 private function getImportPackageName(): string
290 {
291 $dir = $this->getImportDirectory();
292 $name = basename($dir);
293 return $name;
294 }
295
296 public function importRandomQuestionSetConfig(ilObjTest $testOBJ, $xmlFile, $a_mapping)
297 {
298 $testOBJ->questions = [];
299 $parser = new ilObjTestXMLParser($xmlFile);
300 $parser->setTestOBJ($testOBJ);
301 $parser->setImportMapping($a_mapping);
302 $parser->startParsing();
303 }
304
312 {
313 $parser = new ilAssQuestionSkillAssignmentXmlParser($xmlFile);
314 $parser->startParsing();
315
316 $importer = new ilAssQuestionSkillAssignmentImporter();
317 $importer->setTargetParentObjId($testOBJ->getId());
318 $importer->setImportInstallationId($this->getInstallId());
319 $importer->setImportMappingRegistry($mapping);
320 $importer->setImportMappingComponent('Modules/Test');
321 $importer->setImportAssignmentList($parser->getAssignmentList());
322
323 $importer->import();
324
325 if ($importer->getFailedImportAssignmentList()->assignmentsExist()) {
326 $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($testOBJ->getId());
327 $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
328
329 $testOBJ->setOnline(false);
330 }
331
332 return $importer->getSuccessImportAssignmentList();
333 }
334
341 protected function importSkillLevelThresholds(ilImportMapping $mapping, ilAssQuestionSkillAssignmentList $assignmentList, ilObjTest $testOBJ, $xmlFile)
342 {
343 $parser = new ilTestSkillLevelThresholdXmlParser($xmlFile);
344 $parser->startParsing();
345
346 $importer = new ilTestSkillLevelThresholdImporter();
347 $importer->setTargetTestId($testOBJ->getTestId());
348 $importer->setImportInstallationId($this->getInstallId());
349 $importer->setImportMappingRegistry($mapping);
350 $importer->setImportedQuestionSkillAssignmentList($assignmentList);
351 $importer->setImportThresholdList($parser->getSkillLevelThresholdImportList());
352 $importer->import();
353
354 if ($importer->getFailedThresholdImportSkillList()->skillsExist()) {
355 $sltImportFails = new ilTestSkillLevelThresholdImportFails($testOBJ->getId());
356 $sltImportFails->registerFailedImports($importer->getFailedThresholdImportSkillList());
357
358 $testOBJ->setOfflineStatus(true);
359 }
360 }
361}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
getMappingsOfEntity(string $a_comp, string $a_entity)
static saveUsage(int $a_tax_id, int $a_obj_id)
getTestId()
Gets the database id of the additional test data.
setOnline($a_online=true)
static _setImportDirectory($a_import_dir=null)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
setOfflineStatus(bool $status)
Legacy Content Object Parser.
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
finalRandomTestTaxonomyProcessing(ilImportMapping $mapping, $oldTstObjId, $newTstObjId, ilObjTest $testOBJ)
importQuestionSkillAssignments(ilImportMapping $mapping, ilObjTest $testOBJ, $xmlFile)
addTexonomyAndQuestionsMapping(array $question_id_mapping, int $new_obj_id, ilImportMapping $mapping)
finalProcessing(ilImportMapping $a_mapping)
Final processing.
importRandomQuestionSetConfig(ilObjTest $testOBJ, $xmlFile, $a_mapping)
importSkillLevelThresholds(ilImportMapping $mapping, ilAssQuestionSkillAssignmentList $assignmentList, ilObjTest $testOBJ, $xmlFile)
parseXmlFileNames()
Create qti and xml file name.
static $finallyProcessedTestsRegistry
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
Import XML.
getNewMappedTaxonomyFilter(ilImportMapping $mapping, array $mappedFilter)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
if($format !==null) $name
Definition: metadata.php:247
$xml
Definition: metadata.php:351
$results