ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilTestImporter.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Services/Export/classes/class.ilXmlImporter.php");
5 
14 {
19 
26  public function importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
27  {
28  // Container import => test object already created
29  include_once "./Modules/Test/classes/class.ilObjTest.php";
31 
32  if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id)) {
33  // container content
34  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
35  $_SESSION['tst_import_subdir'] = $this->getImportPackageName();
36  $newObj->saveToDb(); // this generates test id first time
37  $questionParentObjId = $newObj->getId();
38  $newObj->setOnline(true);
39  $questionParentObjId = $newObj->getId();
40  } else {
41  // single object
42  $new_id = $a_mapping->getMapping('Modules/Test', 'tst', 'new_id');
43  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
44 
45  if (isset($_SESSION['tst_import_qst_parent'])) {
46  $questionParentObjId = $_SESSION['tst_import_qst_parent'];
47  } else {
48  $questionParentObjId = $newObj->getId();
49  }
50  }
51 
52  $newObj->loadFromDb();
53 
54  list($xml_file, $qti_file) = $this->parseXmlFileNames();
55 
56  if (!@file_exists($xml_file)) {
57  $GLOBALS['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
58  return false;
59  }
60  if (!@file_exists($qti_file)) {
61  $GLOBALS['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $qti_file);
62  return false;
63  }
64 
65  /* @var ilObjTest $newObj */
66 
67  // FIXME: Copied from ilObjTestGUI::importVerifiedFileObject
68  // TODO: move all logic to ilObjTest::importVerifiedFile and call
69  // this method from ilObjTestGUI and ilTestImporter
70  $newObj->mark_schema->flush();
71 
72 
73  if (isset($_SESSION['tst_import_idents'])) {
74  $idents = $_SESSION['tst_import_idents'];
75  } else {
76  $idents = null;
77  }
78 
79  // start parsing of QTI files
80  include_once "./Services/QTI/classes/class.ilQTIParser.php";
81  $qtiParser = new ilQTIParser($qti_file, IL_MO_PARSE_QTI, $questionParentObjId, $idents);
82  $qtiParser->setTestObject($newObj);
83  $result = $qtiParser->startParsing();
84 
85  // import page data
86  include_once("./Modules/LearningModule/classes/class.ilContObjParser.php");
87  $contParser = new ilContObjParser($newObj, $xml_file, basename($this->getImportDirectory()));
88  $contParser->setQuestionMapping($qtiParser->getImportMapping());
89  $contParser->startParsing();
90 
91  foreach ($qtiParser->getQuestionIdMapping() as $oldQuestionId => $newQuestionId) {
92  $a_mapping->addMapping(
93  "Services/Taxonomy",
94  "tax_item",
95  "tst:quest:$oldQuestionId",
96  $newQuestionId
97  );
98 
99  $a_mapping->addMapping(
100  "Services/Taxonomy",
101  "tax_item_obj_id",
102  "tst:quest:$oldQuestionId",
103  $newObj->getId()
104  );
105 
106  $a_mapping->addMapping(
107  "Modules/Test",
108  "quest",
109  $oldQuestionId,
110  $newQuestionId
111  );
112  }
113 
114  if ($newObj->isRandomTest()) {
115  $newObj->questions = array();
116  $this->importRandomQuestionSetConfig($newObj, $xml_file, $a_mapping);
117  }
118 
119  // import test results
120  if (@file_exists($_SESSION["tst_import_results_file"])) {
121  include_once("./Modules/Test/classes/class.ilTestResultsImportParser.php");
122  $results = new ilTestResultsImportParser($_SESSION["tst_import_results_file"], $newObj);
123  $results->setQuestionIdMapping($a_mapping->getMappingsOfEntity('Modules/Test', 'quest'));
124  $results->setSrcPoolDefIdMapping($a_mapping->getMappingsOfEntity('Modules/Test', 'rnd_src_pool_def'));
125  $results->startParsing();
126  }
127 
128  $newObj->saveToDb(); // this creates test_fi
129  $newObj->update(); // this saves ilObject data
130 
131  // import skill assignments
132  $importedAssignmentList = $this->importQuestionSkillAssignments($a_mapping, $newObj, $xml_file);
133  $this->importSkillLevelThresholds($a_mapping, $importedAssignmentList, $newObj, $xml_file);
134 
135  $a_mapping->addMapping("Modules/Test", "tst", $a_id, $newObj->getId());
136 
138  }
139 
146  public function finalProcessing($a_mapping)
147  {
148  $maps = $a_mapping->getMappingsOfEntity("Modules/Test", "tst");
149 
150  foreach ($maps as $old => $new) {
151  if ($old == "new_id" || (int) $old <= 0) {
152  continue;
153  }
154 
155  if (isset(self::$finallyProcessedTestsRegistry[$new])) {
156  continue;
157  }
158 
159  /* @var ilObjTest $testOBJ */
160  $testOBJ = ilObjectFactory::getInstanceByObjId($new, false);
161  if ($testOBJ->isRandomTest()) {
162  $this->finalRandomTestTaxonomyProcessing($a_mapping, $old, $new, $testOBJ);
163  }
164 
165  self::$finallyProcessedTestsRegistry[$new] = true;
166  }
167  }
168 
169  protected function finalRandomTestTaxonomyProcessing(ilImportMapping $mapping, $oldTstObjId, $newTstObjId, ilObjTest $testOBJ)
170  {
171  require_once 'Services/Taxonomy/classes/class.ilObjTaxonomy.php';
172 
173  // get all new taxonomies of this object and store usage for test object
174 
175  $new_tax_ids = $mapping->getMapping(
176  'Services/Taxonomy',
177  'tax_usage_of_obj',
178  $oldTstObjId
179  );
180 
181  if ($new_tax_ids !== false) {
182  $tax_ids = explode(":", $new_tax_ids);
183 
184  foreach ($tax_ids as $tid) {
185  ilObjTaxonomy::saveUsage($tid, $newTstObjId);
186  }
187  }
188 
189  // update all source pool definition's tax/taxNode ids with new mapped id
190 
191  $ilDB = isset($GLOBALS['DIC']) ? $GLOBALS['DIC']['ilDB'] : $GLOBALS['ilDB'];
192 
193  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetSourcePoolDefinitionFactory.php';
195  $ilDB,
196  $testOBJ
197  );
198 
199  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetSourcePoolDefinitionList.php';
201  $ilDB,
202  $testOBJ,
203  $srcPoolDefFactory
204  );
205 
206  $srcPoolDefList->loadDefinitions();
207 
208  foreach ($srcPoolDefList as $definition) {
209  // #21330
210  if (!is_array($definition->getMappedTaxonomyFilter()) || 0 === count($definition->getMappedTaxonomyFilter())) {
211  continue;
212  }
213 
214  $definition->setMappedTaxonomyFilter(
216  $mapping,
217  $definition->getMappedTaxonomyFilter()
218  )
219  );
220  $definition->saveToDb();
221  }
222  }
223 
229  protected function getNewMappedTaxonomyFilter(ilImportMapping $mapping, array $mappedFilter)
230  {
231  $newMappedFilter = array();
232 
233  foreach ($mappedFilter as $taxId => $taxNodes) {
234  $newTaxId = $mapping->getMapping(
235  'Services/Taxonomy',
236  'tax',
237  $taxId
238  );
239 
240  if (!$newTaxId) {
241  continue;
242  }
243 
244  $newMappedFilter[$newTaxId] = array();
245 
246  foreach ($taxNodes as $taxNodeId) {
247  $newTaxNodeId = $mapping->getMapping(
248  'Services/Taxonomy',
249  'tax_tree',
250  $taxNodeId
251  );
252 
253  if (!$newTaxNodeId) {
254  continue;
255  }
256 
257  $newMappedFilter[$newTaxId][] = $newTaxNodeId;
258  }
259  }
260 
261  return $newMappedFilter;
262  }
263 
268  protected function parseXmlFileNames()
269  {
270  $GLOBALS['ilLog']->write(__METHOD__ . ': ' . $this->getImportDirectory());
271 
272  $basename = basename($this->getImportDirectory());
273 
274  $xml = $this->getImportDirectory() . '/' . $basename . '.xml';
275  $qti = $this->getImportDirectory() . '/' . preg_replace('/test|tst/', 'qti', $basename) . '.xml';
276 
277  return array($xml,$qti);
278  }
279 
280  private function getImportDirectoryContainer()
281  {
282  $dir = $this->getImportDirectory();
283  $dir = dirname($dir);
284  return $dir;
285  }
286 
287  private function getImportPackageName()
288  {
289  $dir = $this->getImportDirectory();
290  $name = basename($dir);
291  return $name;
292  }
293 
294  protected function importRandomQuestionSetConfig(ilObjTest $testOBJ, $xmlFile, $a_mapping)
295  {
296  require_once 'Modules/Test/classes/class.ilObjTestXMLParser.php';
297  $parser = new ilObjTestXMLParser($xmlFile);
298  $parser->setTestOBJ($testOBJ);
299  $parser->setImportMapping($a_mapping);
300  $parser->startParsing();
301  }
302 
309  protected function importQuestionSkillAssignments(ilImportMapping $mapping, ilObjTest $testOBJ, $xmlFile)
310  {
311  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentXmlParser.php';
313  $parser->startParsing();
314 
315  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImporter.php';
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  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImportFails.php';
327  $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($testOBJ->getId());
328  $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
329 
330  $testOBJ->setOnline(false);
331  }
332 
333  return $importer->getSuccessImportAssignmentList();
334  }
335 
342  protected function importSkillLevelThresholds(ilImportMapping $mapping, ilAssQuestionSkillAssignmentList $assignmentList, ilObjTest $testOBJ, $xmlFile)
343  {
344  require_once 'Modules/Test/classes/class.ilTestSkillLevelThresholdXmlParser.php';
346  $parser->startParsing();
347 
348  require_once 'Modules/Test/classes/class.ilTestSkillLevelThresholdImporter.php';
349  $importer = new ilTestSkillLevelThresholdImporter();
350  $importer->setTargetTestId($testOBJ->getTestId());
351  $importer->setImportInstallationId($this->getInstallId());
352  $importer->setImportMappingRegistry($mapping);
353  $importer->setImportedQuestionSkillAssignmentList($assignmentList);
354  $importer->setImportThresholdList($parser->getSkillLevelThresholdImportList());
355  $importer->import();
356 
357  if ($importer->getFailedThresholdImportSkillList()->skillsExist()) {
358  require_once 'Modules/Test/classes/class.ilTestSkillLevelThresholdImportFails.php';
359  $sltImportFails = new ilTestSkillLevelThresholdImportFails($testOBJ->getId());
360  $sltImportFails->registerFailedImports($importer->getFailedThresholdImportSkillList());
361 
362  $testOBJ->setOnline(false);
363  }
364  }
365 }
getMapping($a_comp, $a_entity, $a_old_id)
Get a mapping.
Content Object Parser.
setOnline($a_online=true)
$_SESSION["AccountId"]
$result
registerFailedImports(ilAssQuestionAssignedSkillList $skillList)
getImportDirectory()
Get import directory.
getTestId()
Gets the database id of the additional test data.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
getNewMappedTaxonomyFilter(ilImportMapping $mapping, array $mappedFilter)
parseXmlFileNames()
Create qti and xml file name.
$xml
Definition: metadata.php:240
importQuestionSkillAssignments(ilImportMapping $mapping, ilObjTest $testOBJ, $xmlFile)
if($format !==null) $name
Definition: metadata.php:146
Importer class for files.
getId()
get object id public
const IL_MO_PARSE_QTI
finalRandomTestTaxonomyProcessing(ilImportMapping $mapping, $oldTstObjId, $newTstObjId, ilObjTest $testOBJ)
importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
Import XML.
importRandomQuestionSetConfig(ilObjTest $testOBJ, $xmlFile, $a_mapping)
$old
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Create styles array
The data for the language used.
importSkillLevelThresholds(ilImportMapping $mapping, ilAssQuestionSkillAssignmentList $assignmentList, ilObjTest $testOBJ, $xmlFile)
finalProcessing($a_mapping)
Final processing.
$parser
Definition: BPMN2Parser.php:23
static saveUsage($a_tax_id, $a_obj_id)
Save Usage.
$results
Definition: svg-scanner.php:47
global $ilDB
getInstallId()
Get installation id.
static $finallyProcessedTestsRegistry
registerFailedImports(ilAssQuestionSkillAssignmentImportList $assignmentList)
static _setImportDirectory($a_import_dir=null)
set import directory
Xml importer class.