ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilTestQuestionPoolImporter.php
Go to the documentation of this file.
1 <?php
2 
19 include_once("./Services/Export/classes/class.ilXmlImporter.php");
20 
30 {
34  private $poolOBJ;
35 
41  public function importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping): void
42  {
43  /* @var ilObjQuestionPool $newObj */
44 
45  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
47 
48  // Container import => pool object already created
49  if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id)) {
50  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
51  $newObj->setOnline(true); // sets Question pools to always online
52 
53  ilSession::set('qpl_import_subdir', $this->getImportPackageName());
54  } elseif ($new_id = $a_mapping->getMapping('Modules/TestQuestionPool', 'qpl', "new_id")) {
55  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
56  } else {
57  // Shouldn't happen
58  global $DIC; /* @var ILIAS\DI\Container $DIC */
59  $DIC['ilLog']->write(__METHOD__ . ': non container and no tax mapping, perhaps old qpl export');
60  return;
61  }
62 
63 
64 
65  list($xml_file, $qti_file) = $this->parseXmlFileNames();
66 
67  global $DIC; /* @var ILIAS\DI\Container $DIC */
68  if (!@file_exists($xml_file)) {
69  $DIC['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
70  return;
71  }
72  if (!@file_exists($qti_file)) {
73  $DIC['ilLog']->write(__METHOD__ . ': Cannot find qti definition: ' . $qti_file);
74  return;
75  }
76 
77  $this->poolOBJ = $newObj;
78 
79  $newObj->fromXML($xml_file);
80 
81  // set another question pool name (if possible)
82  if (isset($_POST["qpl_new"]) && strlen($_POST["qpl_new"])) {
83  $newObj->setTitle($_POST["qpl_new"]);
84  }
85 
86  $newObj->update();
87  $newObj->saveToDb();
88 
89  // FIXME: Copied from ilObjQuestionPoolGUI::importVerifiedFileObject
90  // TODO: move all logic to ilObjQuestionPoolGUI::importVerifiedFile and call
91  // this method from ilObjQuestionPoolGUI and ilTestImporter
92 
93  global $DIC; /* @var ILIAS\DI\Container $DIC */
94  $DIC['ilLog']->write(__METHOD__ . ': xml file: ' . $xml_file . ", qti file:" . $qti_file);
95 
96  if (ilSession::get("qpl_import_idents") !== null) {
97  $idents = ilSession::get("qpl_import_idents");
98  ilSession::clear("qpl_import_idents");
99  } else {
100  $idents = null;
101  }
102 
103  // start parsing of QTI files
104  include_once "./Services/QTI/classes/class.ilQTIParser.php";
105  $qtiParser = new ilQTIParser($qti_file, ilQTIParser::IL_MO_PARSE_QTI, $newObj->getId(), $idents);
106  $qtiParser->startParsing();
107 
108  // import page data
109  if (strlen($xml_file)) {
110  $questionPageParser = new ilQuestionPageParser($newObj, $xml_file, basename($this->getImportDirectory()));
111  $questionPageParser->setQuestionMapping($qtiParser->getImportMapping());
112  $questionPageParser->startParsing();
113 
114  foreach ($qtiParser->getImportMapping() as $k => $v) {
115  $oldQuestionId = substr($k, strpos($k, 'qst_') + strlen('qst_'));
116  $newQuestionId = $v['pool']; // yes, this is the new question id ^^
117 
118  $a_mapping->addMapping(
119  "Services/Taxonomy",
120  "tax_item",
121  "qpl:quest:$oldQuestionId",
122  $newQuestionId
123  );
124 
125  $a_mapping->addMapping(
126  "Services/Taxonomy",
127  "tax_item_obj_id",
128  "qpl:quest:$oldQuestionId",
129  $newObj->getId()
130  );
131 
132  $a_mapping->addMapping(
133  "Modules/TestQuestionPool",
134  "quest",
135  $oldQuestionId,
136  $newQuestionId
137  );
138  }
139  }
140 
141  $this->importQuestionSkillAssignments($xml_file, $a_mapping, $newObj->getId());
142 
143  $a_mapping->addMapping("Modules/TestQuestionPool", "qpl", $a_id, $newObj->getId());
144 
146 
147  $newObj->saveToDb();
148  }
149 
155  public function finalProcessing(ilImportMapping $a_mapping): void
156  {
157  //echo "<pre>".print_r($a_mapping, true)."</pre>"; exit;
158  // get all glossaries of the import
159  include_once("./Services/Taxonomy/classes/class.ilObjTaxonomy.php");
160  $maps = $a_mapping->getMappingsOfEntity("Modules/TestQuestionPool", "qpl");
161  foreach ($maps as $old => $new) {
162  if ($old != "new_id" && (int) $old > 0) {
163  // get all new taxonomys of this object
164  $new_tax_ids = $a_mapping->getMapping("Services/Taxonomy", "tax_usage_of_obj", $old);
165  if ($new_tax_ids !== false) {
166  $tax_ids = explode(":", $new_tax_ids);
167  foreach ($tax_ids as $tid) {
168  ilObjTaxonomy::saveUsage((int) $tid, $new);
169  }
170  }
171 
172  $taxMappings = $a_mapping->getMappingsOfEntity('Services/Taxonomy', 'tax');
173  foreach ($taxMappings as $oldTaxId => $newTaxId) {
174  if ($oldTaxId == $this->poolOBJ->getNavTaxonomyId()) {
175  $this->poolOBJ->setNavTaxonomyId($newTaxId);
176  $this->poolOBJ->saveToDb();
177  break;
178  }
179  }
180  }
181  }
182  }
183 
188  protected function parseXmlFileNames(): array
189  {
190  global $DIC; /* @var ILIAS\DI\Container $DIC */
191  $DIC['ilLog']->write(__METHOD__ . ': ' . $this->getImportDirectory());
192 
193  $basename = basename($this->getImportDirectory());
194 
195  $xml = $this->getImportDirectory() . '/' . $basename . '.xml';
196  $qti = $this->getImportDirectory() . '/' . preg_replace('/qpl/', 'qti', $basename) . '.xml';
197 
198  return array($xml,$qti);
199  }
200 
201  private function getImportDirectoryContainer(): string
202  {
203  $dir = $this->getImportDirectory();
204  $dir = dirname($dir);
205  return $dir;
206  }
207 
208  private function getImportPackageName(): string
209  {
210  $dir = $this->getImportDirectory();
211  $name = basename($dir);
212  return $name;
213  }
214 
215  protected function importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId): void
216  {
217  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentXmlParser.php';
218  $parser = new ilAssQuestionSkillAssignmentXmlParser($xmlFile);
219  $parser->startParsing();
220 
221  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImporter.php';
222  $importer = new ilAssQuestionSkillAssignmentImporter();
223  $importer->setTargetParentObjId($targetParentObjId);
224  $importer->setImportInstallationId($this->getInstallId());
225  $importer->setImportMappingRegistry($mappingRegistry);
226  $importer->setImportMappingComponent('Modules/TestQuestionPool');
227  $importer->setImportAssignmentList($parser->getAssignmentList());
228 
229  $importer->import();
230 
231  if ($importer->getFailedImportAssignmentList()->assignmentsExist()) {
232  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImportFails.php';
233  $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($targetParentObjId);
234  $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
235 
236  $this->poolOBJ->setOnline(false);
237  }
238  }
239 }
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
if($format !==null) $name
Definition: metadata.php:247
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)
set import directory
$xml
Definition: metadata.php:351
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...