ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilCmiXapiContentUploadImporter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\FileUpload\DTO\UploadResult as FileUploadResult;
22use ILIAS\FileUpload\DTO\ProcessingStatus as FileUploadProcessingStatus;
23use ILIAS\FileUpload\Location as FileUploadResultLocation;
27
38{
39 public const RELATIVE_CONTENT_DIRECTORY_NAMEBASE = 'lm_data/lm_';
40
41 public const RELATIVE_XSD_DIRECTORY = 'components/ILIAS/CmiXapi/xml/contentschema';
42
43 public const IMP_FILE_EXTENSION_XML = 'xml';
44 public const IMP_FILE_EXTENSION_ZIP = 'zip';
45
46 public const CMI5_XML = 'cmi5.xml';
47 public const CMI5_XSD = 'cmi5_v1_CourseStructure.xsd';
48
49 public const TINCAN_XML = 'tincan.xml';
50 public const TINCAN_XSD = 'tincan.xsd';
51
55 protected static array $CONTENT_XML_FILENAMES = [
57 ];
58
62 protected static array $CONTENT_XSD_FILENAMES = [
63 self::CMI5_XML => self::CMI5_XSD,
64 self::TINCAN_XML => self::TINCAN_XSD
65 ];
66
68
69 private \ILIAS\DI\Container $dic;
70
75 {
76 global $DIC;
77 $this->dic = $DIC;
78 $this->object = $object;
79 }
80
84 public function ensureCreatedObjectDirectory(): void
85 {
86 global $DIC; /* @var \ILIAS\DI\Container $DIC */
87 if (!$DIC->filesystem()->web()->has($this->getWebDataDirRelativeObjectDirectory())) {
88 $DIC->filesystem()->web()->createDir($this->getWebDataDirRelativeObjectDirectory());
89 }
90 }
91
92 protected function sanitizeObjectDirectory(): void
93 {
95 implode(DIRECTORY_SEPARATOR, [
98 ])
99 );
100 }
101
106 public function importServerFile(string $serverFile): void
107 {
109
110 $this->handleFile($serverFile);
111
113 }
114
118 protected function handleFile(string $serverFile): void
119 {
120 $fileInfo = pathinfo($serverFile);
121
122 switch ($fileInfo['extension']) {
124
125 $this->handleXmlFile($serverFile);
126 break;
127
129
130 $this->handleZipContentUpload($serverFile);
131
132 if ($this->hasStoredContentXml()) {
133 $this->handleXmlFile($this->getStoredContentXml());
134 }
135
136 break;
137 }
138 }
139
145 public function importFormUpload(array $fileData): void
146 {
147 global $DIC;
149
150 $uploadResult = $this->getUpload(
151 $fileData['tmp_name']
152 );
153
154 $this->handleUpload($uploadResult);
155
157 }
158
163 protected function getUpload(?string $uploadFilePath): FileUploadResult
164 {
165 global $DIC; /* @var \ILIAS\DI\Container $DIC */
166
167 if ($DIC->upload()->hasUploads()) {
168 if (!$DIC->upload()->hasBeenProcessed()) {
169 $DIC->upload()->process();
170 }
171
172 /* @var FileUploadResult $result */
173
174 $results = $DIC->upload()->getResults();
175
176 if (isset($results[$uploadFilePath])) {
177 $result = $results[$uploadFilePath];
178
179 if ($result->isOK()) {
180 return $result;
181 }
182
184 'upload processing failed with message ' .
185 '"' . $result->getStatus()->getMessage() . '"'
186 );
187 }
188
189 throw new ilCmiXapiInvalidUploadContentException('upload lost during processing!');
190 }
191
192 throw new ilCmiXapiInvalidUploadContentException('no upload provided!');
193 }
194
198 protected function handleUpload(FileUploadResult $uploadResult): void
199 {
200 switch ($this->fetchFileExtension($uploadResult)) {
202
203 $this->handleXmlFileFromUpload($uploadResult->getName(), $uploadResult->getPath());
204 break;
205
207
208 $this->handleZipContentUpload($uploadResult->getPath());
209
210 if ($this->hasStoredContentXml()) {
211 $this->handleXmlFile($this->getStoredContentXml());
212 }
213
214 break;
215 }
216 }
217
221 protected function handleXmlFile(string $xmlFilePath): void
222 {
223 $dom = new DOMDocument();
224 $dom->load($xmlFilePath);
225
226 switch (basename($xmlFilePath)) {
227 case self::CMI5_XML:
228
229 $xsdFilePath = $this->getXsdFilePath(self::CMI5_XSD);
230 $this->validateXmlFile($dom, $xsdFilePath);
231
232 $this->initObjectFromCmi5Xml($dom);
233
234 break;
235
236 case self::TINCAN_XML:
237
238 $xsdFilePath = $this->getXsdFilePath(self::TINCAN_XSD);
239 $this->validateXmlFile($dom, $xsdFilePath);
240
241 $this->initObjectFromTincanXml($dom);
242
243 break;
244 }
245 }
246
250 protected function handleXmlFileFromUpload(string $xmlFileName, string $xmlFilePath): void
251 {
252 $dom = new DOMDocument();
253 $dom->load($xmlFilePath);
254 switch (basename($xmlFileName)) {
255 case self::CMI5_XML:
256
257 $xsdFilePath = $this->getXsdFilePath(self::CMI5_XSD);
258 $this->validateXmlFile($dom, $xsdFilePath);
259
260 $this->initObjectFromCmi5Xml($dom);
261
262 break;
263
264 case self::TINCAN_XML:
265
266 $xsdFilePath = $this->getXsdFilePath(self::TINCAN_XSD);
267 $this->validateXmlFile($dom, $xsdFilePath);
268
269 $this->initObjectFromTincanXml($dom);
270
271 break;
272 }
273 }
274
278 protected function validateXmlFile(DOMDocument $dom, $xsdFilePath): void
279 {
280 if (!$dom->schemaValidate($xsdFilePath)) {
281 throw new ilCmiXapiInvalidUploadContentException('invalid content xml given!');
282 }
283 }
284
285 protected function handleZipContentUpload(string $uploadFilePath): void
286 {
287 $targetPath = $this->getAbsoluteObjectDirectory();
288 $archives = new Archives();
289 $unzip = $archives->unzip(
290 Streams::ofResource(fopen($uploadFilePath, 'rb')),
291 $archives->unzipOptions()
292 ->withZipOutputPath($targetPath)
293 ->withOverwrite(true)
294 // ->withDirectoryHandling(ZipDirectoryHandling::FLAT_STRUCTURE)
295 );
296 $unzip->extract();
297 }
298
299 protected function getAbsoluteObjectDirectory(): string
300 {
301 $dirs = [
302 ILIAS_ABSOLUTE_PATH,
303 'public/' . ILIAS_WEB_DIR . "/" . CLIENT_ID,
305 ];
306
307 return implode(DIRECTORY_SEPARATOR, $dirs);
308 }
309
310 public function getWebDataDirRelativeObjectDirectory(): string
311 {
312 return self::RELATIVE_CONTENT_DIRECTORY_NAMEBASE . $this->object->getId();
313 }
314
315 protected function fetchFileExtension(FileUploadResult $uploadResult): string
316 {
317 return pathinfo($uploadResult->getName(), PATHINFO_EXTENSION);
318 }
319
320 protected function hasStoredContentXml(): bool
321 {
322 return $this->getStoredContentXml() !== '';
323 }
324
325 protected function getStoredContentXml(): string
326 {
327 foreach (self::$CONTENT_XML_FILENAMES as $xmlFileName) {
328 $xmlFilePath = $this->getWebDataDirRelativeObjectDirectory() . DIRECTORY_SEPARATOR . $xmlFileName;
329
330 if ($this->dic->filesystem()->web()->has($xmlFilePath)) {
331 return $this->getAbsoluteObjectDirectory() . DIRECTORY_SEPARATOR . $xmlFileName;
332 }
333 }
334
335 return '';
336 }
337
338 protected function getXsdFilePath(string $xsdFileName): string
339 {
340 return ILIAS_ABSOLUTE_PATH . DIRECTORY_SEPARATOR . self::RELATIVE_XSD_DIRECTORY . DIRECTORY_SEPARATOR . $xsdFileName;
341 }
342
343 protected function initObjectFromCmi5Xml(DOMDocument $dom): void
344 {
345 $xPath = new DOMXPath($dom);
346
347 $courseNode = $xPath->query("//*[local-name()='course']")->item(0);
348 // TODO: multilanguage support
349 $title = $xPath->query("//*[local-name()='title']/*[local-name()='langstring']", $courseNode)->item(0)->nodeValue;
350 $this->object->setTitle(trim($title));
351
352 $description = $xPath->query("//*[local-name()='description']/*[local-name()='langstring']", $courseNode)->item(0)->nodeValue;
353 $this->object->setDescription(trim($description));
354
355 $publisherId = trim($courseNode->getAttribute('id'));
356 $this->object->setPublisherId($publisherId);
357
358 $activityId = $this->generateActivityId($publisherId);
359 $this->object->setActivityId($activityId);
360
361 $moveOn = '';
362
363 foreach ($xPath->query("//*[local-name()='au']") as $assignedUnitNode) {
364 $relativeLaunchUrl = $xPath->query("//*[local-name()='url']", $assignedUnitNode)->item(0)->nodeValue;
365 if (!empty($xPath->query("//*[local-name()='launchParameters']", $assignedUnitNode)->item(0)->nodeValue)) {
366 $launchParameters = $xPath->query(
367 "//*[local-name()='launchParameters']",
368 $assignedUnitNode
369 )->item(0)->nodeValue;
370 }
371 if (!empty($assignedUnitNode->getAttribute('moveOn'))) {
372 $moveOn = trim($assignedUnitNode->getAttribute('moveOn'));
373 }
374 if (!empty($xPath->query("//*[local-name()='entitlementKey']", $assignedUnitNode)->item(0)->nodeValue)) {
375 $entitlementKey = $xPath->query(
376 "//*[local-name()='entitlementKey']",
377 $assignedUnitNode
378 )->item(0)->nodeValue;
379 }
380 if (!empty($assignedUnitNode->getAttribute('masteryScore'))) {
381 $masteryScore = trim($assignedUnitNode->getAttribute('masteryScore'));
382 }
383
384 if (!empty($relativeLaunchUrl)) {
385 $this->object->setLaunchUrl(trim($relativeLaunchUrl));
386 }
387 if (!empty($launchParameters)) {
388 $this->object->setLaunchParameters(trim($launchParameters));
389 }
390 if (!empty($moveOn)) {
393 }
394 $this->object->setMoveOn($moveOn);
395 }
396 if (!empty($entitlementKey)) {
397 $this->object->setEntitlementKey($entitlementKey);
398 }
399 if (!empty($masteryScore)) {
400 $this->object->setMasteryScore((float) $masteryScore);
401 } else {
402 $this->object->setMasteryScore(ilObjCmiXapi::LMS_MASTERY_SCORE);
403 }
404
405 break; // TODO: manage multi au imports
406 }
407 $xml_str = $dom->saveXML();
408 $this->object->setXmlManifest($xml_str);
409 $this->object->update();
410 $this->object->save();
411
412 $lpSettings = new ilLPObjSettings($this->object->getId());
414
415 switch ($moveOn) {
418 break;
421 break;
424 break;
425 case ilCmiXapiLP::MOVEON_COMPLETED_AND_PASSED: // ich würde es noch implementieren
427 break;
428 }
429 $lpSettings->setMode($mode);
430 $lpSettings->update();
431 }
432
433 protected function initObjectFromTincanXml(DOMDocument $dom): void
434 {
435 $xPath = new DOMXPath($dom);
436
437 foreach ($xPath->query("//*[local-name()='activity']") as $activityNode) {
438 $title = $xPath->query("//*[local-name()='name']", $activityNode)->item(0)->nodeValue;
439 $this->object->setTitle(trim($title));
440
441 $description = $xPath->query("//*[local-name()='description']", $activityNode)->item(0)->nodeValue;
442 $this->object->setDescription(trim($description));
443
444 $activityId = $activityNode->getAttribute('id');
445 $this->object->setActivityId(trim($activityId));
446
447 $relativeLaunchUrl = $xPath->query("//*[local-name()='launch']", $activityNode)->item(0)->nodeValue;
448 $this->object->setLaunchUrl(trim($relativeLaunchUrl));
449
450 break; // TODO: manage multi activities imports
451 }
452
453 $xml_str = $dom->saveXML();
454 $this->object->setXmlManifest($xml_str);
455 $this->object->update();
456 $this->object->save();
457 }
458
459 private function generateActivityId(string $publisherId): string
460 {
461 global $DIC;
462 $objId = $this->object->getId();
463 return "https://ilias.de/cmi5/activityid/" . (new \Ramsey\Uuid\UuidFactory())->uuid3(ilCmiXapiUser::getIliasUuid(), $objId . '-' . $publisherId);
464 }
465}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
__construct(ilObjCmiXapi $object)
ilCmiXapiContentUploadImporter constructor.
fetchFileExtension(FileUploadResult $uploadResult)
handleXmlFileFromUpload(string $xmlFileName, string $xmlFilePath)
const MOVEON_COMPLETED_OR_PASSED
const MOVEON_COMPLETED_AND_PASSED
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static renameExecutables(string $a_dir)
const CLIENT_ID
Definition: constants.php:41
const ILIAS_WEB_DIR
Definition: constants.php:45
Interface Location.
Definition: Location.php:33
$results
global $DIC
Definition: shib_login.php:26
$objId
Definition: xapitoken.php:55