ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  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  {
34  // container content
35  $newObj = ilObjectFactory::getInstanceByObjId($new_id,false);
36  $_SESSION['tst_import_subdir'] = $this->getImportPackageName();
37  $newObj->saveToDb(); // this generates test id first time
38  $questionParentObjId = $newObj->getId();
39  $newObj->setOnline(true);
40  $questionParentObjId = $newObj->getId();
41  }
42  else
43  {
44  // single object
45  $new_id = $a_mapping->getMapping('Modules/Test', 'tst', 'new_id');
46  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
47 
48  if( isset($_SESSION['tst_import_qst_parent']) )
49  {
50  $questionParentObjId = $_SESSION['tst_import_qst_parent'];
51  }
52  else
53  {
54  $questionParentObjId = $newObj->getId();
55  }
56  }
57 
58  $newObj->loadFromDb();
59 
60  list($xml_file,$qti_file) = $this->parseXmlFileNames();
61 
62  if(!@file_exists($xml_file))
63  {
64  $GLOBALS['ilLog']->write(__METHOD__.': Cannot find xml definition: '. $xml_file);
65  return false;
66  }
67  if(!@file_exists($qti_file))
68  {
69  $GLOBALS['ilLog']->write(__METHOD__.': Cannot find xml definition: '. $qti_file);
70  return false;
71  }
72 
73  /* @var ilObjTest $newObj */
74 
75  // FIXME: Copied from ilObjTestGUI::importVerifiedFileObject
76  // TODO: move all logic to ilObjTest::importVerifiedFile and call
77  // this method from ilObjTestGUI and ilTestImporter
78  $newObj->mark_schema->flush();
79 
80 
81  if( isset($_SESSION['tst_import_idents']) )
82  {
83  $idents = $_SESSION['tst_import_idents'];
84  }
85  else
86  {
87  $idents = null;
88  }
89 
90  // start parsing of QTI files
91  include_once "./Services/QTI/classes/class.ilQTIParser.php";
92  $qtiParser = new ilQTIParser($qti_file, IL_MO_PARSE_QTI, $questionParentObjId, $idents);
93  $qtiParser->setTestObject($newObj);
94  $result = $qtiParser->startParsing();
95 
96  // import page data
97  include_once ("./Modules/LearningModule/classes/class.ilContObjParser.php");
98  $contParser = new ilContObjParser($newObj, $xml_file, basename($this->getImportDirectory()));
99  $contParser->setQuestionMapping($qtiParser->getImportMapping());
100  $contParser->startParsing();
101 
102  foreach($qtiParser->getQuestionIdMapping() as $oldQuestionId => $newQuestionId)
103  {
104  $a_mapping->addMapping(
105  "Services/Taxonomy", "tax_item", "tst:quest:$oldQuestionId", $newQuestionId
106  );
107 
108  $a_mapping->addMapping(
109  "Services/Taxonomy", "tax_item_obj_id", "tst:quest:$oldQuestionId", $newObj->getId()
110  );
111 
112  $a_mapping->addMapping(
113  "Modules/Test", "quest", $oldQuestionId, $newQuestionId
114  );
115  }
116 
117  if( $newObj->isRandomTest() )
118  {
119  $newObj->questions = array();
120  $this->importRandomQuestionSetConfig($newObj, $xml_file, $a_mapping);
121  }
122 
123  // import test results
124  if(@file_exists($_SESSION["tst_import_results_file"]))
125  {
126  include_once("./Modules/Test/classes/class.ilTestResultsImportParser.php");
127  $results = new ilTestResultsImportParser($_SESSION["tst_import_results_file"], $newObj);
128  $results->setQuestionIdMapping($a_mapping->getMappingsOfEntity('Modules/Test', 'quest'));
129  $results->setSrcPoolDefIdMapping($a_mapping->getMappingsOfEntity('Modules/Test', 'rnd_src_pool_def'));
130  $results->startParsing();
131  }
132 
133  $newObj->saveToDb(); // this creates test_fi
134  $newObj->update(); // this saves ilObject data
135 
136  // import skill assignments
137  $importedAssignmentList = $this->importQuestionSkillAssignments($a_mapping, $newObj, $xml_file);
138  $this->importSkillLevelThresholds($a_mapping, $importedAssignmentList, $newObj, $xml_file);
139 
140  $a_mapping->addMapping("Modules/Test", "tst", $a_id, $newObj->getId());
141 
143  }
144 
151  function finalProcessing($a_mapping)
152  {
153  $maps = $a_mapping->getMappingsOfEntity("Modules/Test", "tst");
154 
155  foreach ($maps as $old => $new)
156  {
157  if ($old == "new_id" || (int)$old <= 0)
158  {
159  continue;
160  }
161 
162  if( isset(self::$finallyProcessedTestsRegistry[$new]) )
163  {
164  continue;
165  }
166 
167  /* @var ilObjTest $testOBJ */
168  $testOBJ = ilObjectFactory::getInstanceByObjId($new, false);
169  if( $testOBJ->isRandomTest() )
170  {
171  $this->finalRandomTestTaxonomyProcessing($a_mapping, $old, $new, $testOBJ);
172  }
173 
174  self::$finallyProcessedTestsRegistry[$new] = true;
175  }
176  }
177 
178  protected function finalRandomTestTaxonomyProcessing(ilImportMapping $mapping, $oldTstObjId, $newTstObjId, ilObjTest $testOBJ)
179  {
180  require_once 'Services/Taxonomy/classes/class.ilObjTaxonomy.php';
181 
182  // get all new taxonomies of this object and store usage for test object
183 
184  $new_tax_ids = $mapping->getMapping(
185  'Services/Taxonomy', 'tax_usage_of_obj', $oldTstObjId
186  );
187 
188  if($new_tax_ids !== false)
189  {
190  $tax_ids = explode(":", $new_tax_ids);
191 
192  foreach($tax_ids as $tid)
193  {
194  ilObjTaxonomy::saveUsage($tid, $newTstObjId);
195  }
196  }
197 
198  // update all source pool definition's tax/taxNode ids with new mapped id
199 
200  $ilDB = isset($GLOBALS['DIC']) ? $GLOBALS['DIC']['ilDB'] : $GLOBALS['ilDB'];
201 
202  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetSourcePoolDefinitionFactory.php';
204  $ilDB, $testOBJ
205  );
206 
207  require_once 'Modules/Test/classes/class.ilTestRandomQuestionSetSourcePoolDefinitionList.php';
209  $ilDB, $testOBJ, $srcPoolDefFactory
210  );
211 
212  $srcPoolDefList->loadDefinitions();
213 
214  foreach($srcPoolDefList as $definition)
215  {
216  if( !$definition->getMappedFilterTaxId() && !$definition->getMappedFilterTaxNodeId() )
217  {
218  continue;
219  }
220 
221  $newTaxId = $mapping->getMapping(
222  'Services/Taxonomy', 'tax', $definition->getMappedFilterTaxId()
223  );
224 
225  $definition->setMappedFilterTaxId($newTaxId ? $newTaxId : null);
226 
227  $newTaxNodeId = $mapping->getMapping(
228  'Services/Taxonomy', 'tax_tree', $definition->getMappedFilterTaxNodeId()
229  );
230 
231  $definition->setMappedFilterTaxNodeId($newTaxNodeId ? $newTaxNodeId : null);
232 
233  $definition->saveToDb();
234  }
235  }
236 
241  protected function parseXmlFileNames()
242  {
243  $GLOBALS['ilLog']->write(__METHOD__.': '.$this->getImportDirectory());
244 
245  $basename = basename($this->getImportDirectory());
246 
247  $xml = $this->getImportDirectory().'/'.$basename.'.xml';
248  $qti = $this->getImportDirectory().'/'.preg_replace('/test|tst/', 'qti', $basename).'.xml';
249 
250  return array($xml,$qti);
251  }
252 
253  private function getImportDirectoryContainer()
254  {
255  $dir = $this->getImportDirectory();
256  $dir = dirname($dir);
257  return $dir;
258  }
259 
260  private function getImportPackageName()
261  {
262  $dir = $this->getImportDirectory();
263  $name = basename($dir);
264  return $name;
265  }
266 
267  protected function importRandomQuestionSetConfig(ilObjTest $testOBJ, $xmlFile, $a_mapping)
268  {
269  require_once 'Modules/Test/classes/class.ilObjTestXMLParser.php';
270  $parser = new ilObjTestXMLParser($xmlFile);
271  $parser->setTestOBJ($testOBJ);
272  $parser->setImportMapping($a_mapping);
273  $parser->startParsing();
274  }
275 
282  protected function importQuestionSkillAssignments(ilImportMapping $mapping, ilObjTest $testOBJ, $xmlFile)
283  {
284  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentXmlParser.php';
286  $parser->startParsing();
287 
288  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImporter.php';
289  $importer = new ilAssQuestionSkillAssignmentImporter();
290  $importer->setTargetParentObjId($testOBJ->getId());
291  $importer->setImportInstallationId($this->getInstallId());
292  $importer->setImportMappingRegistry($mapping);
293  $importer->setImportMappingComponent('Modules/Test');
294  $importer->setImportAssignmentList($parser->getAssignmentList());
295 
296  $importer->import();
297 
298  if( $importer->getFailedImportAssignmentList()->assignmentsExist() )
299  {
300  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImportFails.php';
301  $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($testOBJ->getId());
302  $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
303 
304  $testOBJ->setOnline(false);
305  }
306 
307  return $importer->getSuccessImportAssignmentList();
308  }
309 
316  protected function importSkillLevelThresholds(ilImportMapping $mapping, ilAssQuestionSkillAssignmentList $assignmentList, ilObjTest $testOBJ, $xmlFile)
317  {
318  require_once 'Modules/Test/classes/class.ilTestSkillLevelThresholdXmlParser.php';
320  $parser->startParsing();
321 
322  require_once 'Modules/Test/classes/class.ilTestSkillLevelThresholdImporter.php';
323  $importer = new ilTestSkillLevelThresholdImporter();
324  $importer->setTargetTestId($testOBJ->getTestId());
325  $importer->setImportInstallationId($this->getInstallId());
326  $importer->setImportMappingRegistry($mapping);
327  $importer->setImportedQuestionSkillAssignmentList($assignmentList);
328  $importer->setImportThresholdList($parser->getSkillLevelThresholdImportList());
329  $importer->import();
330 
331  if( $importer->getFailedThresholdImportSkillList()->skillsExist() )
332  {
333  require_once 'Modules/Test/classes/class.ilTestSkillLevelThresholdImportFails.php';
334  $sltImportFails = new ilTestSkillLevelThresholdImportFails($testOBJ->getId());
335  $sltImportFails->registerFailedImports($importer->getFailedThresholdImportSkillList());
336 
337  $testOBJ->setOnline(false);
338  }
339  }
340 }
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.
parseXmlFileNames()
Create qti and xml file name.
importQuestionSkillAssignments(ilImportMapping $mapping, ilObjTest $testOBJ, $xmlFile)
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)
$results
$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:24
static saveUsage($a_tax_id, $a_obj_id)
Save Usage.
global $ilDB
getInstallId()
Get installation id.
static $finallyProcessedTestsRegistry
registerFailedImports(ilAssQuestionSkillAssignmentImportList $assignmentList)
static _setImportDirectory($a_import_dir=null)
set import directory
Xml importer class.