ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilSurveyImporter.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Services/Export/classes/class.ilXmlImporter.php");
5 
14 {
18  protected static $survey;
19 
23  protected $svy_log;
24 
31  function init()
32  {
33  include_once("./Modules/Survey/classes/class.ilSurveyDataSet.php");
34  $this->ds = new ilSurveyDataSet();
35  $this->ds->setDSPrefix("ds");
36  $this->ds->setImport($this);
37 
38  $this->svy_log = ilLoggerFactory::getLogger("svy");
39  }
40 
41 
49  function setSurvey(ilObjSurvey $a_val)
50  {
51  self::$survey = $a_val;
52  }
53 
59  function getSurvey()
60  {
61  return self::$survey;
62  }
63 
70  function importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
71  {
72  if ($a_entity == "svy")
73  {
74  // Container import => test object already created
75  if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id))
76  {
77  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
78  #$newObj->setImportDirectory(dirname(rtrim($this->getImportDirectory(),'/')));
79  } else // case ii, non container
80  {
81  $new_id = $a_mapping->getMapping("Modules/Survey", "svy", 0);
82  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
83  }
84  $this->setSurvey($newObj);
85 
86  include_once "./Services/Survey/classes/class.SurveyImportParser.php";
87 
88  list($xml_file) = $this->parseXmlFileNames();
89 
90  if (!@file_exists($xml_file))
91  {
92  $GLOBALS['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
93  return false;
94  }
95  $GLOBALS['ilLog']->write("getQuestionPoolID = ".$this->getImport()->getConfig("Modules/Survey")->getQuestionPoolID());
96 
97  $import = new SurveyImportParser($this->getImport()->getConfig("Modules/Survey")->getQuestionPoolID(), $xml_file, true, $a_mapping);
98 
99  $import->setSurveyObject($newObj);
100  $import->startParsing();
101 
102  $this->svy_log->debug("is array import_mob_xml: -".is_array($_SESSION["import_mob_xhtml"])."-");
103 
104  // this is "written" by Services/Survey/classes/class.ilSurveyImportParser
105  if (is_array($_SESSION["import_mob_xhtml"]))
106  {
107  include_once "./Services/MediaObjects/classes/class.ilObjMediaObject.php";
108  include_once "./Services/RTE/classes/class.ilRTE.php";
109  include_once "./Modules/TestQuestionPool/classes/class.ilObjQuestionPool.php";
110  foreach ($_SESSION["import_mob_xhtml"] as $mob)
111  {
112  $this->svy_log->debug("import mob xhtml, type: ".$mob["type"].", id: ".$mob["mob"]);
113 
114  if (!$mob["type"])
115  {
116  $mob["type"] = "svy:html";
117  }
118 
119  $importfile = dirname($xml_file) . "/" . $mob["uri"];
120  $this->svy_log->debug("import file: ".$importfile);
121 
122  if (file_exists($importfile))
123  {
124  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
125 
126  // survey mob
127  if ($mob["type"] == "svy:html")
128  {
129  ilObjMediaObject::_saveUsage($media_object->getId(), "svy:html", $newObj->getId());
130  $this->svy_log->debug("old introduction: ".$newObj->getIntroduction());
131  $newObj->setIntroduction(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $newObj->getIntroduction()));
132  $newObj->setOutro(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $newObj->getOutro()));
133 
134  $this->svy_log->debug("new introduction: ".$newObj->getIntroduction());
135  }
136  else if($import->questions[$mob["id"]])
137  {
138  $new_qid = $import->questions[$mob["id"]];
139  ilObjMediaObject::_saveUsage($media_object->getId(), $mob["type"], $new_qid);
140  $new_question = SurveyQuestion::_instanciateQuestion($new_qid);
141  $qtext = $new_question->getQuestiontext();
142 
143  $this->svy_log->debug("old question text: ".$qtext);
144 
145  $qtext = ilRTE::_replaceMediaObjectImageSrc($qtext, 0);
146  $qtext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $qtext);
147  $qtext = ilRTE::_replaceMediaObjectImageSrc($qtext, 1);
148  $new_question->setQuestiontext($qtext);
149  $new_question->saveToDb();
150 
151  $this->svy_log->debug("new question text: ".$qtext);
152 
153  // also fix existing original in pool
154  if($new_question->getOriginalId())
155  {
156  $pool_question = SurveyQuestion::_instanciateQuestion($new_question->getOriginalId());
157  $pool_question->setQuestiontext($qtext);
158  $pool_question->saveToDb();
159  }
160  }
161  }
162  else
163  {
164  $this->svy_log->error("Error: Could not open XHTML mob file for test introduction during test import. File $importfile does not exist!");
165  }
166  }
167  $newObj->setIntroduction(ilRTE::_replaceMediaObjectImageSrc($newObj->getIntroduction(), 1));
168  $newObj->setOutro(ilRTE::_replaceMediaObjectImageSrc($newObj->getOutro(), 1));
169  $newObj->saveToDb();
170  }
171 
172  $a_mapping->addMapping("Modules/Survey", "svy", $a_id, $newObj->getId());
173  }
174  else
175  {
176  include_once("./Services/DataSet/classes/class.ilDataSetImportParser.php");
177  $parser = new ilDataSetImportParser($a_entity, $this->getSchemaVersion(),
178  $a_xml, $this->ds, $a_mapping);
179  }
180 
181  return true;
182 
183  }
184 
185 
190  protected function parseXmlFileNames()
191  {
192  $GLOBALS['ilLog']->write(__METHOD__.': '.$this->getImportDirectory());
193 
194  $basename = basename($this->getImportDirectory());
195  $xml = $this->getImportDirectory().'/'.$basename.'.xml';
196 
197  return array($xml);
198  }
199 }
200 
201 ?>
getSchemaVersion()
Get schema version.
$_SESSION["AccountId"]
getImportDirectory()
Get import directory.
Importer class for files.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
getSurvey()
Get current survey object.
Survey Question Import Parser.
Manifest parser for ILIAS standard export files.
static _replaceMediaObjectImageSrc($a_text, $a_direction=0, $nic=IL_INST_ID)
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
setSurvey(ilObjSurvey $a_val)
Set current survey object (being imported).
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=TRUE)
Create new media object and update page in db and return new media object.
getImport()
Get import.
parseXmlFileNames()
Create qti and xml file name.
Survey Data set class.
importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
Import XML.
static _instanciateQuestion($question_id)
Creates an instance of a question with a given question id.
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.
$parser
Definition: BPMN2Parser.php:24
static getLogger($a_component_id)
Get component logger.
static _saveUsage($a_mob_id, $a_type, $a_id, $a_usage_hist_nr=0, $a_lang="-")
Save usage of mob within another container (e.g.
Xml importer class.