ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
4 include_once("./Services/Export/classes/class.ilXmlImporter.php");
5 
15 {
19  private $poolOBJ;
20 
27  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  {
37  $newObj = ilObjectFactory::getInstanceByObjId($new_id,false);
38  $newObj->setOnline(true);
39 
40  $_SESSION['qpl_import_subdir'] = $this->getImportPackageName();
41 
42  $newObj->setOnline(true);
43  }
44  else if ($new_id = $a_mapping->getMapping('Modules/TestQuestionPool','qpl', "new_id"))
45  {
46  $newObj = ilObjectFactory::getInstanceByObjId($new_id,false);
47  }
48  else
49  {
50  // Shouldn't happen
51  $GLOBALS['ilLog']->write(__METHOD__.': non container and no tax mapping, perhaps old qpl export' );
52  return false;
53  }
54 
55 
56 
57  list($xml_file,$qti_file) = $this->parseXmlFileNames();
58 
59  if(!@file_exists($xml_file))
60  {
61  $GLOBALS['ilLog']->write(__METHOD__.': Cannot find xml definition: '. $xml_file);
62  return false;
63  }
64  if(!@file_exists($qti_file))
65  {
66  $GLOBALS['ilLog']->write(__METHOD__.': Cannot find qti definition: '. $qti_file);
67  return false;
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  {
77  $newObj->setTitle($_POST["qpl_new"]);
78  }
79 
80  $newObj->update();
81  $newObj->saveToDb();
82 
83  // FIXME: Copied from ilObjQuestionPoolGUI::importVerifiedFileObject
84  // TODO: move all logic to ilObjQuestionPoolGUI::importVerifiedFile and call
85  // this method from ilObjQuestionPoolGUI and ilTestImporter
86 
87  $GLOBALS['ilLog']->write(__METHOD__.': xml file: '. $xml_file . ", qti file:" . $qti_file);
88 
89  if( isset($_SESSION["qpl_import_idents"]) )
90  {
91  $idents = $_SESSION["qpl_import_idents"];
92  unset($_SESSION["qpl_import_idents"]);
93  }
94  else
95  {
96  $idents = null;
97  }
98 
99  // start parsing of QTI files
100  include_once "./Services/QTI/classes/class.ilQTIParser.php";
101  $qtiParser = new ilQTIParser($qti_file, IL_MO_PARSE_QTI, $newObj->getId(), $idents);
102  $result = $qtiParser->startParsing();
103 
104  // import page data
105  if (strlen($xml_file))
106  {
107  include_once ("./Modules/LearningModule/classes/class.ilContObjParser.php");
108  $contParser = new ilContObjParser($newObj, $xml_file, basename($this->getImportDirectory()));
109  $contParser->setQuestionMapping($qtiParser->getImportMapping());
110  $contParser->startParsing();
111 
112  foreach ($qtiParser->getImportMapping() as $k => $v)
113  {
114  $oldQuestionId = substr($k, strpos($k, 'qst_')+strlen('qst_'));
115  $newQuestionId = $v['pool']; // yes, this is the new question id ^^
116 
117  $a_mapping->addMapping(
118  "Services/Taxonomy", "tax_item", "qpl:quest:$oldQuestionId", $newQuestionId
119  );
120 
121  $a_mapping->addMapping(
122  "Services/Taxonomy", "tax_item_obj_id", "qpl:quest:$oldQuestionId", $newObj->getId()
123  );
124 
125  $a_mapping->addMapping(
126  "Modules/TestQuestionPool", "quest", $oldQuestionId, $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 
146  function finalProcessing($a_mapping)
147  {
148  //echo "<pre>".print_r($a_mapping, true)."</pre>"; exit;
149  // get all glossaries of the import
150  include_once("./Services/Taxonomy/classes/class.ilObjTaxonomy.php");
151  $maps = $a_mapping->getMappingsOfEntity("Modules/TestQuestionPool", "qpl");
152  foreach ($maps as $old => $new)
153  {
154  if ($old != "new_id" && (int) $old > 0)
155  {
156  // get all new taxonomys of this object
157  $new_tax_ids = $a_mapping->getMapping("Services/Taxonomy", "tax_usage_of_obj", $old);
158  if($new_tax_ids !== false)
159  {
160  $tax_ids = explode(":", $new_tax_ids);
161  foreach($tax_ids as $tid)
162  {
164  }
165  }
166 
167  $taxMappings = $a_mapping->getMappingsOfEntity('Services/Taxonomy', 'tax');
168  foreach($taxMappings as $oldTaxId => $newTaxId)
169  {
170  if( $oldTaxId == $this->poolOBJ->getNavTaxonomyId() )
171  {
172  $this->poolOBJ->setNavTaxonomyId($newTaxId);
173  $this->poolOBJ->saveToDb();
174  break;
175  }
176  }
177  }
178  }
179  }
180 
185  protected function parseXmlFileNames()
186  {
187  $GLOBALS['ilLog']->write(__METHOD__.': '.$this->getImportDirectory());
188 
189  $basename = basename($this->getImportDirectory());
190 
191  $xml = $this->getImportDirectory().'/'.$basename.'.xml';
192  $qti = $this->getImportDirectory().'/'.preg_replace('/qpl/', 'qti', $basename).'.xml';
193 
194  return array($xml,$qti);
195  }
196 
197  private function getImportDirectoryContainer()
198  {
199  $dir = $this->getImportDirectory();
200  $dir = dirname($dir);
201  return $dir;
202  }
203 
204  private function getImportPackageName()
205  {
206  $dir = $this->getImportDirectory();
207  $name = basename($dir);
208  return $name;
209  }
210 
211  protected function importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId)
212  {
213  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentXmlParser.php';
215  $parser->startParsing();
216 
217  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImporter.php';
218  $importer = new ilAssQuestionSkillAssignmentImporter();
219  $importer->setTargetParentObjId($targetParentObjId);
220  $importer->setImportInstallationId($this->getInstallId());
221  $importer->setImportMappingRegistry($mappingRegistry);
222  $importer->setImportMappingComponent('Modules/TestQuestionPool');
223  $importer->setImportAssignmentList($parser->getAssignmentList());
224 
225  $importer->import();
226 
227  if( $importer->getFailedImportAssignmentList()->assignmentsExist() )
228  {
229  require_once 'Modules/TestQuestionPool/classes/questions/class.ilAssQuestionSkillAssignmentImportFails.php';
230  $qsaImportFails = new ilAssQuestionSkillAssignmentImportFails($targetParentObjId);
231  $qsaImportFails->registerFailedImports($importer->getFailedImportAssignmentList());
232 
233  $this->poolOBJ->setOnline(false);
234  }
235  }
236 }
237 
238 ?>
Importer class for question pools.
Content Object Parser.
$_SESSION["AccountId"]
$result
getImportDirectory()
Get import directory.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
Import XML.
const IL_MO_PARSE_QTI
parseXmlFileNames()
Create qti and xml file name.
static _setImportDirectory($a_import_dir=null)
set import directory
$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.
finalProcessing($a_mapping)
Final processing.
$parser
Definition: BPMN2Parser.php:24
static saveUsage($a_tax_id, $a_obj_id)
Save Usage.
getInstallId()
Get installation id.
importQuestionSkillAssignments($xmlFile, ilImportMapping $mappingRegistry, $targetParentObjId)
$_POST["username"]
Xml importer class.