ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestImporter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
31 {
35  public static $finallyProcessedTestsRegistry = [];
36 
37  private ilLogger $log;
38  private ilDBInterface $db;
40 
41  public function __construct()
42  {
43  global $DIC;
44  $this->log = $DIC['ilLog'];
45  $this->db = $DIC['ilDB'];
46  $this->irss = $DIC['resource_storage'];
47 
49  }
50 
62  public function importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping): void
63  {
65 
66  if ($new_id = (int) $a_mapping->getMapping('Services/Container', 'objs', $a_id)) {
67  // container content
68  $new_obj = ilObjectFactory::getInstanceByObjId($new_id, false);
69  ilSession::set('tst_import_subdir', $this->getImportPackageName());
70  $new_obj->saveToDb(); // this generates test id first time
71  $question_parent_obj_id = $new_obj->getId();
72  } else {
73  // single object
74  $new_id = (int) $a_mapping->getMapping('Modules/Test', 'tst', 'new_id');
75  $new_obj = ilObjectFactory::getInstanceByObjId($new_id, false);
76 
77  $question_parent_obj_id = (int) (ilSession::get('tst_import_qst_parent') ?? $new_obj->getId());
78  }
79 
80  $new_obj->loadFromDb();
81 
82  list($xml_file, $qti_file) = $this->parseXmlFileNames();
83 
84  if (!@file_exists($xml_file)) {
85  $this->log->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
86  return;
87  }
88  if (!@file_exists($qti_file)) {
89  $this->log->write(__METHOD__ . ': Cannot find xml definition: ' . $qti_file);
90  return;
91  }
92 
93  /* @var ilObjTest $new_obj */
94 
95  // FIXME: Copied from ilObjTestGUI::importVerifiedFileObject
96  // TODO: move all logic to ilObjTest::importVerifiedFile and call
97  // this method from ilObjTestGUI and ilTestImporter
98  $new_obj->getMarkSchema()->flush();
99 
100  $idents = ilSession::get('tst_import_idents');
101 
102  // start parsing of QTI files
103  $qtiParser = new ilQTIParser(
104  $qti_file,
106  $question_parent_obj_id,
107  $idents,
108  $a_mapping->getAllMappings()
109  );
110  $qtiParser->setTestObject($new_obj);
111  $qtiParser->startParsing();
112  $new_obj = $qtiParser->getTestObject();
113 
114  // import page data
115  $questionPageParser = new ilQuestionPageParser($new_obj, $xml_file, basename($this->getImportDirectory()));
116  $questionPageParser->setQuestionMapping($qtiParser->getImportMapping());
117  $questionPageParser->startParsing();
118 
119  $a_mapping = $this->addTexonomyAndQuestionsMapping($qtiParser->getQuestionIdMapping(), $new_obj->getId(), $a_mapping);
120 
121  if ($new_obj->isRandomTest()) {
122  $this->importRandomQuestionSetConfig($new_obj, $xml_file, $a_mapping);
123  }
124 
125  $results_file_path = ilSession::get("tst_import_results_file");
126  // import test results
127  if ($results_file_path !== null && file_exists($results_file_path)) {
128  $results = new ilTestResultsImportParser($results_file_path, $new_obj, $this->db, $this->log, $this->irss);
129  $results->setQuestionIdMapping($a_mapping->getMappingsOfEntity('Modules/Test', 'quest'));
130  $results->setSrcPoolDefIdMapping($a_mapping->getMappingsOfEntity('Modules/Test', 'rnd_src_pool_def'));
131  $results->startParsing();
132  }
133 
134  $new_obj->saveToDb(); // this creates test_fi
135  $new_obj->update(); // this saves ilObject data
136 
137  // import skill assignments
138  $importedAssignmentList = $this->importQuestionSkillAssignments($a_mapping, $new_obj, $xml_file);
139  $this->importSkillLevelThresholds($a_mapping, $importedAssignmentList, $new_obj, $xml_file);
140 
141  $a_mapping->addMapping("Modules/Test", "tst", (string) $a_id, (string) $new_obj->getId());
142  }
143 
144  public function addTexonomyAndQuestionsMapping(array $question_id_mapping, int $new_obj_id, ilImportMapping $mapping): ilImportMapping
145  {
146  foreach ($question_id_mapping as $oldQuestionId => $newQuestionId) {
147  $mapping->addMapping(
148  "Services/Taxonomy",
149  "tax_item",
150  "tst:quest:$oldQuestionId",
151  (string) $newQuestionId
152  );
153 
154  $mapping->addMapping(
155  "Services/Taxonomy",
156  "tax_item_obj_id",
157  "tst:quest:$oldQuestionId",
158  (string) $new_obj_id
159  );
160 
161  $mapping->addMapping(
162  "Modules/Test",
163  "quest",
164  (string) $oldQuestionId,
165  (string) $newQuestionId
166  );
167  }
168 
169  return $mapping;
170  }
171 
177  public function finalProcessing(ilImportMapping $a_mapping): void
178  {
179  $maps = $a_mapping->getMappingsOfEntity("Modules/Test", "tst");
180 
181  foreach ($maps as $old => $new) {
182  if ($old == "new_id" || (int) $old <= 0) {
183  continue;
184  }
185 
186  if (isset(self::$finallyProcessedTestsRegistry[$new])) {
187  continue;
188  }
189 
190  $test_obj = ilObjectFactory::getInstanceByObjId((int) $new, false);
191  if ($test_obj->isRandomTest()) {
192  $this->finalRandomTestTaxonomyProcessing($a_mapping, (string) $old, $new, $test_obj);
193  }
194 
195  self::$finallyProcessedTestsRegistry[$new] = true;
196  }
197  }
198 
200  ilImportMapping $mapping,
201  string $oldTstObjId,
202  string $newTstObjId,
203  ilObjTest $test_obj
204  ): void {
205  $new_tax_ids = $mapping->getMapping(
206  'Services/Taxonomy',
207  'tax_usage_of_obj',
208  $oldTstObjId
209  );
210 
211  if ($new_tax_ids !== null) {
212  $tax_ids = explode(":", $new_tax_ids);
213 
214  foreach ($tax_ids as $tid) {
215  ilObjTaxonomy::saveUsage((int) $tid, (int) $newTstObjId);
216  }
217  }
218 
220  $this->db,
221  $test_obj
222  );
223 
225  $this->db,
226  $test_obj,
227  $srcPoolDefFactory
228  );
229 
230  $srcPoolDefList->loadDefinitions();
231 
232  foreach ($srcPoolDefList as $definition) {
233  // #21330
234  if (!is_array($definition->getMappedTaxonomyFilter()) || 0 === count($definition->getMappedTaxonomyFilter())) {
235  continue;
236  }
237 
238  $definition->setMappedTaxonomyFilter(
240  $mapping,
241  $definition->getMappedTaxonomyFilter()
242  )
243  );
244  $definition->saveToDb();
245  }
246  }
247 
253  protected function getNewMappedTaxonomyFilter(ilImportMapping $mapping, array $mappedFilter): array
254  {
255  $newMappedFilter = [];
256 
257  foreach ($mappedFilter as $taxId => $taxNodes) {
258  $newTaxId = $mapping->getMapping(
259  'Services/Taxonomy',
260  'tax',
261  (string) $taxId
262  );
263 
264  if (!$newTaxId) {
265  continue;
266  }
267 
268  $newMappedFilter[$newTaxId] = [];
269 
270  foreach ($taxNodes as $taxNodeId) {
271  $newTaxNodeId = $mapping->getMapping(
272  'Services/Taxonomy',
273  'tax_tree',
274  (string) $taxNodeId
275  );
276 
277  if (!$newTaxNodeId) {
278  continue;
279  }
280 
281  $newMappedFilter[$newTaxId][] = $newTaxNodeId;
282  }
283  }
284 
285  return $newMappedFilter;
286  }
287 
292  protected function parseXmlFileNames(): array
293  {
294  $this->log->write(__METHOD__ . ': ' . $this->getImportDirectory());
295 
296  $basename = basename($this->getImportDirectory());
297 
298  $xml = $this->getImportDirectory() . '/' . $basename . '.xml';
299  $qti = $this->getImportDirectory() . '/' . preg_replace('/test|tst/', 'qti', $basename) . '.xml';
300 
301  return [$xml,$qti];
302  }
303 
304  private function getImportDirectoryContainer(): string
305  {
306  $dir = $this->getImportDirectory();
307  $dir = dirname($dir);
308  return $dir;
309  }
310 
311  private function getImportPackageName(): string
312  {
313  $dir = $this->getImportDirectory();
314  $name = basename($dir);
315  return $name;
316  }
317 
318  public function importRandomQuestionSetConfig(ilObjTest $test_obj, $xmlFile, $a_mapping)
319  {
320  $test_obj->questions = [];
321  $parser = new ilObjTestXMLParser($xmlFile);
322  $parser->setTestOBJ($test_obj);
323  $parser->setImportMapping($a_mapping);
324  $parser->startParsing();
325  }
326 
334  {
335  $parser = new ilAssQuestionSkillAssignmentXmlParser($xmlFile);
336  $parser->startParsing();
337 
338  $importer = new ilAssQuestionSkillAssignmentImporter();
339  $importer->setTargetParentObjId($test_obj->getId());
340  $importer->setImportInstallationId((int) $this->getInstallId());
341  $importer->setImportMappingRegistry($mapping);
342  $importer->setImportMappingComponent('Modules/Test');
343  $importer->setImportAssignmentList($parser->getAssignmentList());
344 
345  $importer->import();
346 
347  if ($importer->getFailedImportAssignmentList()->assignmentsExist()) {
348  $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($test_obj->getId());
349  $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
350 
351  $test_obj->getObjectProperties()->storePropertyIsOnline(
352  $test_obj->getObjectProperties()->getPropertyIsOnline()->withOffline()
353  );
354  }
355 
356  return $importer->getSuccessImportAssignmentList();
357  }
358 
365  protected function importSkillLevelThresholds(ilImportMapping $mapping, ilAssQuestionSkillAssignmentList $assignmentList, ilObjTest $test_obj, $xmlFile)
366  {
367  $parser = new ilTestSkillLevelThresholdXmlParser($xmlFile);
368  $parser->initSkillLevelThresholdImportList();
369  $parser->startParsing();
370 
371  $importer = new ilTestSkillLevelThresholdImporter($this->db);
372  $importer->setTargetTestId($test_obj->getTestId());
373  $importer->setImportInstallationId((int) $this->getInstallId());
374  $importer->setImportMappingRegistry($mapping);
375  $importer->setImportedQuestionSkillAssignmentList($assignmentList);
376  $importer->setImportThresholdList($parser->getSkillLevelThresholdImportList());
377  $importer->import();
378 
379  if ($importer->getFailedThresholdImportSkillList()->skillsExist()) {
380  $sltImportFails = new ilTestSkillLevelThresholdImportFails($test_obj->getId());
381  $sltImportFails->registerFailedImports($importer->getFailedThresholdImportSkillList());
382 
383  $test_obj->setOfflineStatus(true);
384  }
385  }
386 }
static get(string $a_var)
finalRandomTestTaxonomyProcessing(ilImportMapping $mapping, string $oldTstObjId, string $newTstObjId, ilObjTest $test_obj)
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
Import XML.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
importSkillLevelThresholds(ilImportMapping $mapping, ilAssQuestionSkillAssignmentList $assignmentList, ilObjTest $test_obj, $xmlFile)
registerFailedImports(ilAssQuestionAssignedSkillList $skillList)
getTestId()
Gets the database id of the additional test data.
importQuestionSkillAssignments(ilImportMapping $mapping, ilObjTest $test_obj, $xmlFile)
getNewMappedTaxonomyFilter(ilImportMapping $mapping, array $mappedFilter)
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
parseXmlFileNames()
Create qti and xml file name.
global $DIC
Definition: feed.php:28
Importer class for files.
__construct(VocabulariesInterface $vocabularies)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
getMappingsOfEntity(string $a_comp, string $a_entity)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addTexonomyAndQuestionsMapping(array $question_id_mapping, int $new_obj_id, ilImportMapping $mapping)
Legacy Content Object Parser.
$results
setOfflineStatus(bool $status)
ResourceStorage $irss
static saveUsage(int $a_tax_id, int $a_obj_id)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
importRandomQuestionSetConfig(ilObjTest $test_obj, $xmlFile, $a_mapping)
finalProcessing(ilImportMapping $a_mapping)
Final processing.
static $finallyProcessedTestsRegistry
registerFailedImports(ilAssQuestionSkillAssignmentImportList $assignmentList)
static set(string $a_var, $a_val)
Set a value.
static _setImportDirectory($a_import_dir=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setTestObject(ilObjTest $a_tst_object)