ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilScormAiccImporter.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 include_once("./Services/Export/classes/class.ilXmlImporter.php");
5 {
6  public function __construct()
7  {
8  require_once "./Modules/ScormAicc/classes/class.ilScormAiccDataSet.php";
9  $this->dataset = new ilScormAiccDataSet();
10  //todo: at the moment restricted to one module in xml file, extend?
11  $this->moduleProperties = [];
12  $this->manifest = [];
13  }
14 
15  public function init()
16  {
17  }
18 
25  public function importXmlRepresentation($a_entity, $a_id, $a_import_dirname, $a_mapping)
26  {
27  global $DIC;
28  $ilLog = $DIC['ilLog'];
29 
30  if ($this->handleEditableLmXml($a_entity, $a_id, $a_import_dirname, $a_mapping)) {
31  return true;
32  }
33  // case i container
34  if ($a_id != null && $new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id)) {
35  $newObj = ilObjectFactory::getInstanceByObjId($new_id, false);
36 
37  // require_once("./Services/Export/classes/class.ilExport.php");
38  // $exportDir = ilExport::_getExportDirectory($a_id,"xml","sahs");
39  // $tempFile = dirname($exportDir) . '/export/' . basename($this->getImportDirectory()) . '.zip';
40 
41  // $timeStamp = time();
42  // $lmDir = ilUtil::getWebspaceDir("filesystem") . "/lm_data/";
43  // $lmTempDir = $lmDir . $timeStamp;
44  // $importDir = $this->getImportDirectory();
45  // $a_import_dirname = $lmTempDir . '/' . basename($importDir);
46  // if (!file_exists($tempFile)) {
47  // $tempFile = $importDir . '/content.zip';
48  // $a_import_dirname = $importDir;
49  // }
50  // if (!file_exists($lmTempDir)) {
51  // mkdir($lmTempDir, 0755, true);
52  // }
53  // $zar = new ZipArchive();
54  // $zar->open($tempFile);
55  // $zar->extractTo($lmTempDir);
56  // $zar->close();
57  $a_import_dirname = $this->getImportDirectory();
58  }
59 
60  // $ilLog->write($a_import_dirname);
61  $result = false;
62  if (file_exists($a_import_dirname)) {
63  $manifestFile = $a_import_dirname . "/manifest.xml";
64  if (file_exists($manifestFile)) {
65  $manifest = file_get_contents($manifestFile);
66  if (isset($manifest)) {
67  $propertiesFile = $a_import_dirname . "/properties.xml";
68  $xml = file_get_contents($propertiesFile);
69 
70  if (isset($xml)) {
71  $use_internal_errors = libxml_use_internal_errors(true);
72  $xmlRoot = simplexml_load_string(trim($xml));
73  libxml_use_internal_errors($use_internal_errors);
74  if (!$xmlRoot instanceof SimpleXMLElement) {
75  $ilLog->write('XML for SCORM Import is: ' . $xml . $xmlRoot);
76  return false;
77  }
78 
79  foreach ($this->dataset->properties as $key => $value) {
80  $this->moduleProperties[$key] = $xmlRoot->$key;
81  }
82  $this->moduleProperties["Title"] = $xmlRoot->Title;
83  $this->moduleProperties["Description"] = $xmlRoot->Description;
84 
85  foreach ($this->moduleProperties as $key => $xmlRoot) {
86  $xmlRootValue = $xmlRoot->__toString();
87  $filteredValue = preg_replace('%\s%', '', $xmlRootValue);
88  $this->moduleProperties[$key] = ilUtil::stripSlashes($filteredValue);
89  }
90 
91  if ($a_id != null && $new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id)) {
92  $this->dataset->writeData("sahs", "5.1.0", $newObj->getId(), $this->moduleProperties);
93 
94  $newObj->createReference();
95 
96  $scormFile = "content.zip";
97  $scormFilePath = $a_import_dirname . "/" . $scormFile;
98  $targetPath = $newObj->getDataDirectory() . "/" . $scormFile;
99  $file_path = $targetPath;
100 
101  ilFileUtils::rename($scormFilePath, $targetPath);
102  ilUtil::unzip($file_path);
103  unlink($file_path);
104  // ilUtil::delDir($lmTempDir, false);
105  // $ilLog->write($scormFilePath.'----'. $targetPath);
106  ilUtil::renameExecutables($newObj->getDataDirectory());
107 
108  $newId = $newObj->getRefId();
109  // $newObj->putInTree($newId);
110  // $newObj->setPermissions($newId);
111  $subType = $this->moduleProperties["SubType"];
112  if ($subType == "scorm") {
113  include_once("./Modules/ScormAicc/classes/class.ilObjSCORMLearningModule.php");
114  $newObj = new ilObjSCORMLearningModule($newId);
115  } else {
116  include_once("./Modules/Scorm2004/classes/class.ilObjSCORM2004LearningModule.php");
117  $newObj = new ilObjSCORM2004LearningModule($newId);
118  }
119  $title = $newObj->readObject();
120  //auto set learning progress settings
121  $newObj->setLearningProgressSettingsAtUpload();
122  }
123 
124 
125  $result = true;
126  } else {
127  $ilLog->write("error parsing xml file for scorm import");
128  //error xml parsing
129  }
130  } else {
131  $ilLog->write("error reading manifest file");
132  }
133  } else {
134  $ilLog->write("error no manifest file found");
135  }
136  } else {
137  $ilLog->write("error file lost while importing");
138  //error non existing file
139  }
140  return $result;
141  }
142 
143  public function writeData($a_entity, $a_version, $a_id)
144  {
145  $this->dataset->writeData($a_entity, $a_version, $a_id, $this->moduleProperties);
146  }
147 
157  public function handleEditableLmXml($a_entity, $a_id, $a_xml, $a_mapping)
158  {
159  // if editable...
160  if (is_int(strpos($a_xml, "<Editable>1</Editable>"))) {
161  // ...use ilScorm2004DataSet for import
162  include_once("./Modules/Scorm2004/classes/class.ilScorm2004DataSet.php");
163  $dataset = new ilScorm2004DataSet();
164  $dataset->setDSPrefix("ds");
165  $dataset->setImportDirectory($this->getImportDirectory());
166 
167  include_once("./Services/DataSet/classes/class.ilDataSetImportParser.php");
168  $parser = new ilDataSetImportParser(
169  $a_entity,
170  $this->getSchemaVersion(),
171  $a_xml,
172  $dataset,
173  $a_mapping
174  );
175  return true;
176  }
177  return false;
178  }
179 }
getSchemaVersion()
Get schema version.
$result
getImportDirectory()
Get import directory.
Manifest parser for ILIAS standard export files.
handleEditableLmXml($a_entity, $a_id, $a_xml, $a_mapping)
Handle editable (authoring) scorm lms.
static rename($a_source, $a_target)
Rename a file.
importXmlRepresentation($a_entity, $a_id, $a_import_dirname, $a_mapping)
Import XML.
global $DIC
Definition: goto.php:24
static unzip(string $path_to_zip_file, bool $overwrite_existing=false, bool $unpack_flat=false)
$xml
Definition: metadata.php:332
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static renameExecutables($a_dir)
Rename uploaded executables for security reasons.
Class ilObjSCORM2004LearningModule.
writeData($a_entity, $a_version, $a_id)
Class ilObjSCORMLearningModule.
Xml importer class.