ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilScormAiccImporter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\Data\Factory as DataTypeFactory;
22
24{
26 private \ILIAS\Data\Result $result;
27 private DataTypeFactory $df;
31 private array $module_properties = [];
32
33 public function __construct()
34 {
35 $this->dataset = new ilScormAiccDataSet();
36 $this->df = new DataTypeFactory();
37 $this->initResult();
38
40 }
41
42 private function initResult(): void
43 {
44 $this->publishResult($this->df->error('No XML parsed, yet'));
45 $this->module_properties = [];
46 }
47
48 private function publishResult(\ILIAS\Data\Result $result): \ILIAS\Data\Result
49 {
50 $this->result = $result;
51 return $this->result;
52 }
53
57 public function getResult(): \ILIAS\Data\Result
58 {
59 return $this->result;
60 }
61
62 public function init(): void
63 {
64 }
65
71 public function importXmlRepresentation(
72 string $a_entity,
73 string $a_id,
74 string $a_xml,
75 ?ilImportMapping $a_mapping
76 ): void {
77 global $DIC;
78
79 $this->initResult();
80
81 $xml_directory = $a_xml;
82 $new_object = null;
83
84 $this->publishResult(
85 $this->df
86 ->ok('Parsing started')
87 ->then(
88 function (string $message) use (
89 &$new_object,
90 &$xml_directory,
91 $a_id,
92 $a_mapping
93 ): ?\ILIAS\Data\Result {
94 if ($a_id !== '' &&
95 $a_mapping !== null &&
96 ($new_id = $a_mapping->getMapping('Services/Container', 'objs', $a_id))) {
97 $new_object = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
98 $xml_directory = $this->getImportDirectory();
99 }
100
101 return $this->df->ok($xml_directory);
102 }
103 )
104 ->then(function (string $xml_directory): ?\ILIAS\Data\Result {
105 if (!is_dir($xml_directory)) {
106 return $this->df->error(
107 sprintf('Directory lost while importing: %s', $xml_directory)
108 );
109 }
110
111 return null;
112 })
113 ->then(function (string $xml_directory): ?\ILIAS\Data\Result {
114 $manifest_file = $xml_directory . '/manifest.xml';
115 if (!file_exists($manifest_file)) {
116 return $this->df->error(
117 sprintf(
118 'No manifest file found in import directory "%s": %s',
119 $xml_directory,
120 $manifest_file
121 )
122 );
123 }
124
125 return $this->df->ok($manifest_file);
126 })
127 ->then(function (string $manifest_file): ?\ILIAS\Data\Result {
128 $manifest_file_content = file_get_contents($manifest_file);
129 if (!is_string($manifest_file_content) || $manifest_file_content === '') {
130 return $this->df->error(
131 sprintf(
132 'Could not read content from manifest file: %s',
133 $manifest_file
134 )
135 );
136 }
137
138 return $this->df->ok($manifest_file_content);
139 })
140 ->then(function (string $manifest_file_content) use ($xml_directory): ?\ILIAS\Data\Result {
141 $properties_file = $xml_directory . '/properties.xml';
142 $properties_file_content = file_get_contents($properties_file);
143 if (!is_string($properties_file_content) || $properties_file_content === '') {
144 return $this->df->error(
145 sprintf(
146 'Could not read file: %s',
147 $properties_file
148 )
149 );
150 }
151
152 return $this->df->ok($properties_file_content);
153 })
154 ->then(function (string $properties_file_content): ?\ILIAS\Data\Result {
155 return (new ilScormImportParser($this->df))->parse($properties_file_content);
156 })
157 ->then(
158 function (SimpleXMLElement $properties_xml_doc): ?\ILIAS\Data\Result {
159 try {
160 foreach ($this->dataset->properties as $key => $value) {
161 $this->module_properties[$key] = $properties_xml_doc->{$key};
162 }
163
164 $this->module_properties['Title'] = $properties_xml_doc->Title;
165 $this->module_properties['Description'] = $properties_xml_doc->Description;
166
167 foreach ($this->module_properties as $key => $property_node) {
168 $property_value = $property_node->__toString();
169 $filteredValue = preg_replace('%\s%', '', $property_value);
170 $this->module_properties[$key] = ilUtil::stripSlashes($filteredValue);
171 }
172
173 return $this->df->ok($this->module_properties);
174 } catch (Exception $exception) {
175 return $this->df->error($exception);
176 }
177 }
178 )->then(function (array $module_properties) use (
179 $xml_directory,
180 $a_id,
181 $a_mapping,
182 $new_object,
183 $DIC
184 ): ?\ILIAS\Data\Result {
185 if ($a_id !== '' &&
186 $a_mapping !== null &&
187 ($new_id = $a_mapping->getMapping(
188 'Services/Container',
189 'objs',
190 $a_id
191 ))) {
192 $this->dataset->writeData(
193 'sahs',
194 '5.1.0',
195 $new_object->getId(),
196 $this->module_properties
197 );
198
199 $new_object->createReference();
200
201 $scormFile = 'content.zip';
202 $scormFilePath = $xml_directory . '/' . $scormFile;
203 $targetPath = $new_object->getDataDirectory() . '/' . $scormFile;
204 $file_path = $targetPath;
205
206 ilFileUtils::rename($scormFilePath, $targetPath);
207 $DIC->legacyArchives()->unzip($file_path);
208 unlink($file_path);
209 ilFileUtils::renameExecutables($new_object->getDataDirectory());
210
211 $new_ref_id = $new_object->getRefId();
212 $subType = $module_properties['SubType'];
213 if ($subType === 'scorm') {
214 $new_object = new ilObjSCORMLearningModule($new_ref_id);
215 } else {
216 $new_object = new ilObjSCORM2004LearningModule($new_ref_id);
217 }
218
219 $title = $new_object->readObject();
220 $new_object->setLearningProgressSettingsAtUpload();
221 }
222
223 return null;
224 })
225 );
226 }
227
228 public function writeData(string $a_entity, string $a_version, int $a_id): void
229 {
230 $this->dataset->writeData($a_entity, $a_version, $a_id, $this->module_properties);
231 }
232}
Builds data types.
Definition: Factory.php:36
static rename(string $a_source, string $a_target)
static renameExecutables(string $a_dir)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
Class ilObjSCORM2004LearningModule.
Class ilObjSCORMLearningModule.
publishResult(\ILIAS\Data\Result $result)
writeData(string $a_entity, string $a_version, int $a_id)
importXmlRepresentation(string $a_entity, string $a_id, string $a_xml, ?ilImportMapping $a_mapping)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
Xml importer class.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
try
handle Lrs Init
Definition: xapiproxy.php:86
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$message
Definition: xapiexit.php:31