ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestQuestionPoolImporter.php
Go to the documentation of this file.
1 <?php
2 
28 {
32  private $poolOBJ;
33 
39  public function importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping): void
40  {
42 
43  // Container import => pool object already created
44  if (($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id)) !== null) {
45  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
46  $newObj->getObjectProperties()->storePropertyIsOnline($newObj->getObjectProperties()->getPropertyIsOnline()->withOffline()); // sets Question pools to always online
47 
48  ilSession::set('qpl_import_subdir', $this->getImportPackageName());
49  } elseif (($new_id = $a_mapping->getMapping('Modules/TestQuestionPool', 'qpl', "new_id")) !== null) {
50  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
51  } else {
52  // Shouldn't happen
53  global $DIC; /* @var ILIAS\DI\Container $DIC */
54  $DIC['ilLog']->write(__METHOD__ . ': non container and no tax mapping, perhaps old qpl export');
55  return;
56  }
57 
58  list($xml_file, $qti_file) = $this->parseXmlFileNames();
59 
60  global $DIC; /* @var ILIAS\DI\Container $DIC */
61  if (!@file_exists($xml_file)) {
62  $DIC['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
63  return;
64  }
65  if (!@file_exists($qti_file)) {
66  $DIC['ilLog']->write(__METHOD__ . ': Cannot find qti definition: ' . $qti_file);
67  return;
68  }
69 
70  $this->poolOBJ = $newObj;
71 
72  $newObj->fromXML($xml_file);
73 
74  // set another question pool name (if possible)
75  if (isset($_POST["qpl_new"]) && strlen($_POST["qpl_new"])) {
76  $newObj->setTitle($_POST["qpl_new"]);
77  }
78 
79  $newObj->update();
80  $newObj->saveToDb();
81 
82  // FIXME: Copied from ilObjQuestionPoolGUI::importVerifiedFileObject
83  // TODO: move all logic to ilObjQuestionPoolGUI::importVerifiedFile and call
84  // this method from ilObjQuestionPoolGUI and ilTestImporter
85 
86  global $DIC; /* @var ILIAS\DI\Container $DIC */
87  $DIC['ilLog']->write(__METHOD__ . ': xml file: ' . $xml_file . ", qti file:" . $qti_file);
88 
89  if (ilSession::get("qpl_import_idents") !== null) {
90  $idents = ilSession::get("qpl_import_idents");
91  ilSession::clear("qpl_import_idents");
92  } else {
93  $idents = null;
94  }
95 
96  $qtiParser = new ilQTIParser($qti_file, ilQTIParser::IL_MO_PARSE_QTI, $newObj->getId(), $idents);
97  $qtiParser->startParsing();
98 
99  if (strlen($xml_file)) {
100  $questionPageParser = new ilQuestionPageParser($newObj, $xml_file, basename($this->getImportDirectory()));
101  $questionPageParser->setQuestionMapping($qtiParser->getImportMapping());
102  $questionPageParser->startParsing();
103 
104  foreach ($qtiParser->getImportMapping() as $k => $v) {
105  $oldQuestionId = substr($k, strpos($k, 'qst_') + strlen('qst_'));
106  $newQuestionId = $v['pool']; // yes, this is the new question id ^^
107 
108  $a_mapping->addMapping(
109  "Services/Taxonomy",
110  "tax_item",
111  "qpl:quest:$oldQuestionId",
112  $newQuestionId
113  );
114 
115  $a_mapping->addMapping(
116  "Services/Taxonomy",
117  "tax_item_obj_id",
118  "qpl:quest:$oldQuestionId",
119  $newObj->getId()
120  );
121 
122  $a_mapping->addMapping(
123  "Modules/TestQuestionPool",
124  "quest",
125  $oldQuestionId,
126  $newQuestionId
127  );
128  }
129  }
130 
131  $this->importQuestionSkillAssignments($xml_file, $a_mapping, $newObj->getId());
132 
133  $a_mapping->addMapping("Modules/TestQuestionPool", "qpl", $a_id, $newObj->getId());
134 
136 
137  $newObj->saveToDb();
138  }
139 
145  public function finalProcessing(ilImportMapping $a_mapping): void
146  {
147  $maps = $a_mapping->getMappingsOfEntity("Modules/TestQuestionPool", "qpl");
148  foreach ($maps as $old => $new) {
149  if ($old != "new_id" && (int) $old > 0) {
150  // get all new taxonomys of this object
151  $new_tax_ids = $a_mapping->getMapping("Services/Taxonomy", "tax_usage_of_obj", $old);
152  if ($new_tax_ids !== null) {
153  $tax_ids = explode(":", $new_tax_ids);
154  foreach ($tax_ids as $tid) {
155  ilObjTaxonomy::saveUsage((int) $tid, $new);
156  }
157  }
158  }
159  }
160  }
161 
166  protected function parseXmlFileNames(): array
167  {
168  global $DIC; /* @var ILIAS\DI\Container $DIC */
169  $DIC['ilLog']->write(__METHOD__ . ': ' . $this->getImportDirectory());
170 
171  $basename = basename($this->getImportDirectory());
172 
173  $xml = $this->getImportDirectory() . '/' . $basename . '.xml';
174  $qti = $this->getImportDirectory() . '/' . preg_replace('/qpl/', 'qti', $basename) . '.xml';
175 
176  return array($xml,$qti);
177  }
178 
179  private function getImportDirectoryContainer(): string
180  {
181  $dir = $this->getImportDirectory();
182  $dir = dirname($dir);
183  return $dir;
184  }
185 
186  private function getImportPackageName(): string
187  {
188  $dir = $this->getImportDirectory();
189  $name = basename($dir);
190  return $name;
191  }
192 
193  protected function importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId): void
194  {
195  $parser = new ilAssQuestionSkillAssignmentXmlParser($xmlFile);
196  $parser->startParsing();
197 
198  $importer = new ilAssQuestionSkillAssignmentImporter();
199  $importer->setTargetParentObjId($targetParentObjId);
200  $importer->setImportInstallationId($this->getInstallId());
201  $importer->setImportMappingRegistry($mappingRegistry);
202  $importer->setImportMappingComponent('Modules/TestQuestionPool');
203  $importer->setImportAssignmentList($parser->getAssignmentList());
204 
205  $importer->import();
206 
207  if ($importer->getFailedImportAssignmentList()->assignmentsExist()) {
208  $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($targetParentObjId);
209  $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
210 
211  $this->poolOBJ->getObjectProperties()->storePropertyIsOnline($this->poolOBJ->getObjectProperties()->getPropertyIsOnline()->withOffline());
212  }
213  }
214 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static get(string $a_var)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
Import XML.
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
finalProcessing(ilImportMapping $a_mapping)
Final processing.
global $DIC
Definition: feed.php:28
getMapping(string $a_comp, string $a_entity, string $a_old_id)
getMappingsOfEntity(string $a_comp, string $a_entity)
parseXmlFileNames()
Create qti and xml file name.
Legacy Content Object Parser.
static _setImportDirectory($a_import_dir=null)
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
importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId)
static clear(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...