ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSurveyImporter.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
11 {
15  protected $log;
16 
17 
21  public function __construct()
22  {
24  global $DIC;
25 
26  $this->log = $DIC["ilLog"];
27  }
28 
32  protected static $survey;
33 
37  protected $svy_log;
38 
45  public function init()
46  {
47  $this->ds = new ilSurveyDataSet();
48  $this->ds->setDSPrefix("ds");
49  $this->ds->setImport($this);
50 
51  $this->svy_log = ilLoggerFactory::getLogger("svy");
52  }
53 
54 
62  public function setSurvey(ilObjSurvey $a_val)
63  {
64  self::$survey = $a_val;
65  }
66 
72  public function getSurvey()
73  {
74  return self::$survey;
75  }
76 
89  public function importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
90  {
91  if ($a_entity == "svy") {
92  // Container import => test object already created
93  if ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id)) {
94  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
95  #$newObj->setImportDirectory(dirname(rtrim($this->getImportDirectory(),'/')));
96  } else { // case ii, non container
97  $new_id = $a_mapping->getMapping("Modules/Survey", "svy", 0);
98  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
99  }
100  $this->setSurvey($newObj);
101 
102 
103  list($xml_file) = $this->parseXmlFileNames();
104 
105  if (!@file_exists($xml_file)) {
106  $GLOBALS['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
107  return false;
108  }
109  $GLOBALS['ilLog']->write("getQuestionPoolID = " . $this->getImport()->getConfig("Modules/Survey")->getQuestionPoolID());
110 
111  $import = new SurveyImportParser($this->getImport()->getConfig("Modules/Survey")->getQuestionPoolID(), $xml_file, true, $a_mapping);
112 
113  $import->setSurveyObject($newObj);
114  $import->startParsing();
115 
116  $this->svy_log->debug("is array import_mob_xml: -" . is_array($_SESSION["import_mob_xhtml"]) . "-");
117 
118  // this is "written" by Services/Survey/classes/class.ilSurveyImportParser
119  if (is_array($_SESSION["import_mob_xhtml"])) {
120  foreach ($_SESSION["import_mob_xhtml"] as $mob) {
121  $this->svy_log->debug("import mob xhtml, type: " . $mob["type"] . ", id: " . $mob["mob"]);
122 
123  if (!$mob["type"]) {
124  $mob["type"] = "svy:html";
125  }
126 
127  $importfile = dirname($xml_file) . "/" . $mob["uri"];
128  $this->svy_log->debug("import file: " . $importfile);
129 
130  if (file_exists($importfile)) {
131  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
132 
133  // survey mob
134  if ($mob["type"] == "svy:html") {
135  ilObjMediaObject::_saveUsage($media_object->getId(), "svy:html", $newObj->getId());
136  $this->svy_log->debug("old introduction: " . $newObj->getIntroduction());
137  $newObj->setIntroduction(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $newObj->getIntroduction()));
138  $newObj->setOutro(str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $newObj->getOutro()));
139 
140  $this->svy_log->debug("new introduction: " . $newObj->getIntroduction());
141  } elseif ($import->questions[$mob["id"]]) {
142  $new_qid = $import->questions[$mob["id"]];
143  ilObjMediaObject::_saveUsage($media_object->getId(), $mob["type"], $new_qid);
144  $new_question = SurveyQuestion::_instanciateQuestion($new_qid);
145  $qtext = $new_question->getQuestiontext();
146 
147  $this->svy_log->debug("old question text: " . $qtext);
148 
149  $qtext = ilRTE::_replaceMediaObjectImageSrc($qtext, 0);
150  $qtext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $qtext);
151  $qtext = ilRTE::_replaceMediaObjectImageSrc($qtext, 1);
152  $new_question->setQuestiontext($qtext);
153  $new_question->saveToDb();
154 
155  $this->svy_log->debug("new question text: " . $qtext);
156 
157  // also fix existing original in pool
158  if ($new_question->getOriginalId()) {
159  $pool_question = SurveyQuestion::_instanciateQuestion($new_question->getOriginalId());
160  $pool_question->setQuestiontext($qtext);
161  $pool_question->saveToDb();
162  }
163  }
164  } else {
165  $this->svy_log->error("Error: Could not open XHTML mob file for test introduction during test import. File $importfile does not exist!");
166  }
167  }
168  $newObj->setIntroduction(ilRTE::_replaceMediaObjectImageSrc($newObj->getIntroduction(), 1));
169  $newObj->setOutro(ilRTE::_replaceMediaObjectImageSrc($newObj->getOutro(), 1));
170  $newObj->saveToDb();
171  }
172  $a_mapping->addMapping("Modules/Survey", "svy", (int) $a_id, (int) $newObj->getId());
173  return $newObj->getId();
174  } else {
175  $parser = new ilDataSetImportParser(
176  $a_entity,
177  $this->getSchemaVersion(),
178  $a_xml,
179  $this->ds,
180  $a_mapping
181  );
182  }
183 
184  return true;
185  }
186 
187 
192  protected function parseXmlFileNames()
193  {
194  $GLOBALS['ilLog']->write(__METHOD__ . ': ' . $this->getImportDirectory());
195 
196  $basename = basename($this->getImportDirectory());
197  $xml = $this->getImportDirectory() . '/' . $basename . '.xml';
198 
199  return array($xml);
200  }
201 }
getSchemaVersion()
Get schema version.
const IL_INST_ID
Definition: constants.php:38
$_SESSION["AccountId"]
getImportDirectory()
Get import directory.
Importer class for files.
Class ilObjSurvey.
getSurvey()
Get current survey object.
Survey Question Import Parser.
static _saveTempFileAsMediaObject($name, $tmp_name, $upload=true)
Create new media object and update page in db and return new media object.
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).
__construct()
Constructor.
getImport()
Get import.
parseXmlFileNames()
Create qti and xml file name.
Survey Data set class.
global $DIC
Definition: goto.php:24
importXmlRepresentation($a_entity, $a_id, $a_xml, $a_mapping)
Import XML.
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
$xml
Definition: metadata.php:332
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
__construct(Container $dic, ilPlugin $plugin)
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.