ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilSurveyImporter.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected ilSurveyDataSet $ds;
26  protected ilLogger $log;
27  protected static ilObjSurvey $survey;
28  protected ilLogger $svy_log;
29  protected \ILIAS\SurveyQuestionPool\Export\ImportManager $spl_import_manager;
30 
31  public function __construct()
32  {
34  global $DIC;
35 
36  $this->log = $DIC["ilLog"];
37 
38  $this->spl_import_manager = $DIC->surveyQuestionPool()
39  ->internal()
40  ->domain()
41  ->import();
42  }
43 
44 
45  public function init(): void
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 
60  public function setSurvey(ilObjSurvey $a_val): void
61  {
62  self::$survey = $a_val;
63  }
64 
65  public function getSurvey(): ilObjSurvey
66  {
67  return self::$survey;
68  }
69 
76  public function importXmlRepresentation(
77  string $a_entity,
78  string $a_id,
79  string $a_xml,
80  ilImportMapping $a_mapping
81  ): void {
82  if ($a_entity === "svy") {
83  // Container import => test object already created
84  if (!($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id))) { // case ii, non container
85  $new_id = $a_mapping->getMapping("Modules/Survey", "svy", 0);
86  }
88  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
89  $this->setSurvey($newObj);
90 
91 
92  [$xml_file] = $this->parseXmlFileNames();
93 
94  if (!file_exists($xml_file)) {
95  $GLOBALS['ilLog']->write(__METHOD__ . ': Cannot find xml definition: ' . $xml_file);
96  return;
97  }
98  $GLOBALS['ilLog']->write("getQuestionPoolID = " . $this->getImport()->getConfig("Modules/Survey")->getQuestionPoolID());
99 
100  $import = new SurveyImportParser(
101  $this->getImport()->getConfig("Modules/Survey")->getQuestionPoolID(),
102  $xml_file,
103  true,
104  $a_mapping
105  );
106 
107  $import->setSurveyObject($newObj);
108  $import->startParsing();
109 
110 
111  // this is "written" by Services/Survey/classes/class.ilSurveyImportParser
112  $mobs = $this->spl_import_manager->getMobs();
113  if (count($mobs) > 0) {
114  foreach ($mobs as $mob) {
115  $this->svy_log->debug("import mob xhtml, type: " . $mob["type"] . ", id: " . $mob["mob"]);
116 
117  if (!isset($mob["type"])) {
118  $mob["type"] = "svy:html";
119  }
120 
121  $importfile = dirname($xml_file) . "/" . $mob["uri"];
122  $this->svy_log->debug("import file: " . $importfile);
123 
124  if (file_exists($importfile)) {
125  $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
126 
127  // survey mob
128  if ($mob["type"] === "svy:html") {
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  } elseif ($import->questions[$mob["id"]]) {
136  $new_qid = $import->questions[$mob["id"]];
137  ilObjMediaObject::_saveUsage($media_object->getId(), $mob["type"], $new_qid);
138  $new_question = SurveyQuestion::_instanciateQuestion($new_qid);
139  $qtext = $new_question->getQuestiontext();
140 
141  $this->svy_log->debug("old question text: " . $qtext);
142 
143  $qtext = ilRTE::_replaceMediaObjectImageSrc($qtext, 0);
144  $qtext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $qtext);
145  $qtext = ilRTE::_replaceMediaObjectImageSrc($qtext, 1);
146  $new_question->setQuestiontext($qtext);
147  $new_question->saveToDb();
148 
149  $this->svy_log->debug("new question text: " . $qtext);
150 
151  // also fix existing original in pool
152  if ($new_question->getOriginalId()) {
153  $pool_question = SurveyQuestion::_instanciateQuestion($new_question->getOriginalId());
154  $pool_question->setQuestiontext($qtext);
155  $pool_question->saveToDb();
156  }
157  }
158  } else {
159  $this->svy_log->error("Error: Could not open XHTML mob file for test introduction during test import. File $importfile does not exist!");
160  }
161  }
162  $newObj->setIntroduction(ilRTE::_replaceMediaObjectImageSrc($newObj->getIntroduction(), 1));
163  $newObj->setOutro(ilRTE::_replaceMediaObjectImageSrc($newObj->getOutro(), 1));
164  $newObj->saveToDb();
165  }
166  $a_mapping->addMapping("Modules/Survey", "svy", (int) $a_id, $newObj->getId());
167  } else {
168  $parser = new ilDataSetImportParser(
169  $a_entity,
170  $this->getSchemaVersion(),
171  $a_xml,
172  $this->ds,
173  $a_mapping
174  );
175  }
176  }
177 
178 
182  protected function parseXmlFileNames(): array
183  {
184  $GLOBALS['ilLog']->write(__METHOD__ . ': ' . $this->getImportDirectory());
185 
186  $basename = basename($this->getImportDirectory());
187  $xml = $this->getImportDirectory() . '/' . $basename . '.xml';
188 
189  return array($xml);
190  }
191 }
static _replaceMediaObjectImageSrc(string $a_text, int $a_direction=0, string $nic='')
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
const IL_INST_ID
Definition: constants.php:40
static getLogger(string $a_component_id)
Get component logger.
$mobs
Definition: imgupload.php:70
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ilImportMapping $a_mapping)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
Manifest parser for ILIAS standard export files.
setSurvey(ilObjSurvey $a_val)
Set current survey object (being imported).
global $DIC
Definition: feed.php:28
getMapping(string $a_comp, string $a_entity, string $a_old_id)
static ilObjSurvey $survey
parseXmlFileNames()
Create qti and xml file name.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
$xml
Definition: metadata.php:351
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
Create new media object and update page in db and return new media object.
__construct(Container $dic, ilPlugin $plugin)
ILIAS SurveyQuestionPool Export ImportManager $spl_import_manager
static _instanciateQuestion(int $question_id)
Get question object.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...