ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTestQuestionPoolImporter.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/Export/classes/class.ilXmlImporter.php");
5
15{
19 private $poolOBJ;
20
27 public function importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
28 {
29 /* @var ilObjQuestionPool $newObj */
30
31 include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
33
34 // Container import => pool object already created
35 if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id)) {
36 $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
37 $newObj->setOnline(true);
38
39 $_SESSION['qpl_import_subdir'] = $this->getImportPackageName();
40
41 $newObj->setOnline(true);
42 } elseif ($new_id = $a_mapping->getMapping('Modules/TestQuestionPool', 'qpl', "new_id")) {
43 $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
44 } else {
45 // Shouldn't happen
46 global $DIC; /* @var ILIAS\DI\Container $DIC */
47 $DIC['ilLog']->write(__METHOD__ . ': non container and no tax mapping, perhaps old qpl export');
48 return false;
49 }
50
51
52
53 list($xml_file, $qti_file) = $this->parseXmlFileNames();
54
55 global $DIC; /* @var ILIAS\DI\Container $DIC */
56 if (!@file_exists($xml_file)) {
57 $DIC['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
58 return false;
59 }
60 if (!@file_exists($qti_file)) {
61 $DIC['ilLog']->write(__METHOD__ . ': Cannot find qti definition: ' . $qti_file);
62 return false;
63 }
64
65 $this->poolOBJ = $newObj;
66
67 $newObj->fromXML($xml_file);
68
69 // set another question pool name (if possible)
70 if (isset($_POST["qpl_new"]) && strlen($_POST["qpl_new"])) {
71 $newObj->setTitle($_POST["qpl_new"]);
72 }
73
74 $newObj->update();
75 $newObj->saveToDb();
76
77 // FIXME: Copied from ilObjQuestionPoolGUI::importVerifiedFileObject
78 // TODO: move all logic to ilObjQuestionPoolGUI::importVerifiedFile and call
79 // this method from ilObjQuestionPoolGUI and ilTestImporter
80
81 global $DIC; /* @var ILIAS\DI\Container $DIC */
82 $DIC['ilLog']->write(__METHOD__ . ': xml file: ' . $xml_file . ", qti file:" . $qti_file);
83
84 if (isset($_SESSION["qpl_import_idents"])) {
85 $idents = $_SESSION["qpl_import_idents"];
86 unset($_SESSION["qpl_import_idents"]);
87 } else {
88 $idents = null;
89 }
90
91 // start parsing of QTI files
92 include_once "./Services/QTI/classes/class.ilQTIParser.php";
93 $qtiParser = new ilQTIParser($qti_file, IL_MO_PARSE_QTI, $newObj->getId(), $idents);
94 $result = $qtiParser->startParsing();
95
96 // import page data
97 if (strlen($xml_file)) {
98 include_once("./Modules/LearningModule/classes/class.ilContObjParser.php");
99 $contParser = new ilContObjParser($newObj, $xml_file, basename($this->getImportDirectory()));
100 $contParser->setQuestionMapping($qtiParser->getImportMapping());
101 $contParser->startParsing();
102
103 foreach ($qtiParser->getImportMapping() as $k => $v) {
104 $oldQuestionId = substr($k, strpos($k, 'qst_') + strlen('qst_'));
105 $newQuestionId = $v['pool']; // yes, this is the new question id ^^
106
107 $a_mapping->addMapping(
108 "Services/Taxonomy",
109 "tax_item",
110 "qpl:quest:$oldQuestionId",
111 $newQuestionId
112 );
113
114 $a_mapping->addMapping(
115 "Services/Taxonomy",
116 "tax_item_obj_id",
117 "qpl:quest:$oldQuestionId",
118 $newObj->getId()
119 );
120
121 $a_mapping->addMapping(
122 "Modules/TestQuestionPool",
123 "quest",
124 $oldQuestionId,
125 $newQuestionId
126 );
127 }
128 }
129
130 $this->importQuestionSkillAssignments($xml_file, $a_mapping, $newObj->getId());
131
132 $a_mapping->addMapping("Modules/TestQuestionPool", "qpl", $a_id, $newObj->getId());
133
135
136 $newObj->saveToDb();
137 }
138
145 public function finalProcessing($a_mapping)
146 {
147 //echo "<pre>".print_r($a_mapping, true)."</pre>"; exit;
148 // get all glossaries of the import
149 include_once("./Services/Taxonomy/classes/class.ilObjTaxonomy.php");
150 $maps = $a_mapping->getMappingsOfEntity("Modules/TestQuestionPool", "qpl");
151 foreach ($maps as $old => $new) {
152 if ($old != "new_id" && (int) $old > 0) {
153 // get all new taxonomys of this object
154 $new_tax_ids = $a_mapping->getMapping("Services/Taxonomy", "tax_usage_of_obj", $old);
155 if ($new_tax_ids !== false) {
156 $tax_ids = explode(":", $new_tax_ids);
157 foreach ($tax_ids as $tid) {
158 ilObjTaxonomy::saveUsage($tid, $new);
159 }
160 }
161
162 $taxMappings = $a_mapping->getMappingsOfEntity('Services/Taxonomy', 'tax');
163 foreach ($taxMappings as $oldTaxId => $newTaxId) {
164 if ($oldTaxId == $this->poolOBJ->getNavTaxonomyId()) {
165 $this->poolOBJ->setNavTaxonomyId($newTaxId);
166 $this->poolOBJ->saveToDb();
167 break;
168 }
169 }
170 }
171 }
172 }
173
178 protected function parseXmlFileNames()
179 {
180 global $DIC; /* @var ILIAS\DI\Container $DIC */
181 $DIC['ilLog']->write(__METHOD__ . ': ' . $this->getImportDirectory());
182
183 $basename = basename($this->getImportDirectory());
184
185 $xml = $this->getImportDirectory() . '/' . $basename . '.xml';
186 $qti = $this->getImportDirectory() . '/' . preg_replace('/qpl/', 'qti', $basename) . '.xml';
187
188 return array($xml,$qti);
189 }
190
192 {
193 $dir = $this->getImportDirectory();
194 $dir = dirname($dir);
195 return $dir;
196 }
197
198 private function getImportPackageName()
199 {
200 $dir = $this->getImportDirectory();
201 $name = basename($dir);
202 return $name;
203 }
204
205 protected function importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId)
206 {
207 require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentXmlParser.php';
209 $parser->startParsing();
210
211 require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImporter.php';
212 $importer = new ilAssQuestionSkillAssignmentImporter();
213 $importer->setTargetParentObjId($targetParentObjId);
214 $importer->setImportInstallationId($this->getInstallId());
215 $importer->setImportMappingRegistry($mappingRegistry);
216 $importer->setImportMappingComponent('Modules/TestQuestionPool');
217 $importer->setImportAssignmentList($parser->getAssignmentList());
218
219 $importer->import();
220
221 if ($importer->getFailedImportAssignmentList()->assignmentsExist()) {
222 require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImportFails.php';
223 $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($targetParentObjId);
224 $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
225
226 $this->poolOBJ->setOnline(false);
227 }
228 }
229}
$parser
Definition: BPMN2Parser.php:23
$result
$_POST["username"]
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
const IL_MO_PARSE_QTI
Content Object Parser.
static _setImportDirectory($a_import_dir=null)
set import directory
static saveUsage($a_tax_id, $a_obj_id)
Save Usage.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Importer class for question pools.
parseXmlFileNames()
Create qti and xml file name.
finalProcessing($a_mapping)
Final processing.
importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId)
importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
Import XML.
Xml importer class.
getInstallId()
Get installation id.
getImportDirectory()
Get import directory.
global $DIC
Definition: saml.php:7