ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilCmiXapiContentUploadImporter.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
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 = [
56  self::CMI5_XML, self::TINCAN_XML
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 
67  protected ilObjCmiXapi $object;
68 
69  private \ILIAS\DI\Container $dic;
70 
74  public function __construct(ilObjCmiXapi $object)
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 
112  $this->sanitizeObjectDirectory();
113  }
114 
118  protected function handleFile(string $serverFile): void
119  {
120  $fileInfo = pathinfo($serverFile);
121 
122  switch ($fileInfo['extension']) {
123  case self::IMP_FILE_EXTENSION_XML:
124 
125  $this->handleXmlFile($serverFile);
126  break;
127 
128  case self::IMP_FILE_EXTENSION_ZIP:
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 
156  $this->sanitizeObjectDirectory();
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)) {
201  case self::IMP_FILE_EXTENSION_XML:
202 
203  $this->handleXmlFileFromUpload($uploadResult->getName(), $uploadResult->getPath());
204  break;
205 
206  case self::IMP_FILE_EXTENSION_ZIP:
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  // $zar = new \ZipArchive();
298  // $zar->open($uploadFilePath);
299  // $zar->extractTo($targetPath);
300  // $zar->close();
301  }
302 
303  protected function getAbsoluteObjectDirectory(): string
304  {
305  $dirs = [
306  ILIAS_ABSOLUTE_PATH,
307  'public/' . ILIAS_WEB_DIR . "/" . CLIENT_ID,
309  ];
310 
311  return implode(DIRECTORY_SEPARATOR, $dirs);
312  }
313 
314  public function getWebDataDirRelativeObjectDirectory(): string
315  {
316  return self::RELATIVE_CONTENT_DIRECTORY_NAMEBASE . $this->object->getId();
317  }
318 
319  protected function fetchFileExtension(FileUploadResult $uploadResult): string
320  {
321  return pathinfo($uploadResult->getName(), PATHINFO_EXTENSION);
322  }
323 
324  protected function hasStoredContentXml(): bool
325  {
326  return $this->getStoredContentXml() !== '';
327  }
328 
329  protected function getStoredContentXml(): string
330  {
331  foreach (self::$CONTENT_XML_FILENAMES as $xmlFileName) {
332  $xmlFilePath = $this->getWebDataDirRelativeObjectDirectory() . DIRECTORY_SEPARATOR . $xmlFileName;
333 
334  if ($this->dic->filesystem()->web()->has($xmlFilePath)) {
335  return $this->getAbsoluteObjectDirectory() . DIRECTORY_SEPARATOR . $xmlFileName;
336  }
337  }
338 
339  return '';
340  }
341 
342  protected function getXsdFilePath(string $xsdFileName): string
343  {
344  return ILIAS_ABSOLUTE_PATH . DIRECTORY_SEPARATOR . self::RELATIVE_XSD_DIRECTORY . DIRECTORY_SEPARATOR . $xsdFileName;
345  }
346 
347  protected function initObjectFromCmi5Xml(DOMDocument $dom): void
348  {
349  $xPath = new DOMXPath($dom);
350 
351  $courseNode = $xPath->query("//*[local-name()='course']")->item(0);
352  // TODO: multilanguage support
353  $title = $xPath->query("//*[local-name()='title']/*[local-name()='langstring']", $courseNode)->item(0)->nodeValue;
354  $this->object->setTitle(trim($title));
355 
356  $description = $xPath->query("//*[local-name()='description']/*[local-name()='langstring']", $courseNode)->item(0)->nodeValue;
357  $this->object->setDescription(trim($description));
358 
359  $publisherId = trim($courseNode->getAttribute('id'));
360  $this->object->setPublisherId($publisherId);
361 
362  $activityId = $this->generateActivityId($publisherId);
363  $this->object->setActivityId($activityId);
364 
365  $moveOn = '';
366 
367  foreach ($xPath->query("//*[local-name()='au']") as $assignedUnitNode) {
368  $relativeLaunchUrl = $xPath->query("//*[local-name()='url']", $assignedUnitNode)->item(0)->nodeValue;
369  if (!empty($xPath->query("//*[local-name()='launchParameters']", $assignedUnitNode)->item(0)->nodeValue)) {
370  $launchParameters = $xPath->query(
371  "//*[local-name()='launchParameters']",
372  $assignedUnitNode
373  )->item(0)->nodeValue;
374  }
375  if (!empty($assignedUnitNode->getAttribute('moveOn'))) {
376  $moveOn = trim($assignedUnitNode->getAttribute('moveOn'));
377  }
378  if (!empty($xPath->query("//*[local-name()='entitlementKey']", $assignedUnitNode)->item(0)->nodeValue)) {
379  $entitlementKey = $xPath->query(
380  "//*[local-name()='entitlementKey']",
381  $assignedUnitNode
382  )->item(0)->nodeValue;
383  }
384  if (!empty($assignedUnitNode->getAttribute('masteryScore'))) {
385  $masteryScore = trim($assignedUnitNode->getAttribute('masteryScore'));
386  }
387 
388  if (!empty($relativeLaunchUrl)) {
389  $this->object->setLaunchUrl(trim($relativeLaunchUrl));
390  }
391  if (!empty($launchParameters)) {
392  $this->object->setLaunchParameters(trim($launchParameters));
393  }
394  if (!empty($moveOn)) {
396  $moveOn = ilCmiXapiLP::MOVEON_PASSED;
397  }
398  $this->object->setMoveOn($moveOn);
399  }
400  if (!empty($entitlementKey)) {
401  $this->object->setEntitlementKey($entitlementKey);
402  }
403  if (!empty($masteryScore)) {
404  $this->object->setMasteryScore((float) $masteryScore);
405  } else {
406  $this->object->setMasteryScore(ilObjCmiXapi::LMS_MASTERY_SCORE);
407  }
408 
409  break; // TODO: manage multi au imports
410  }
411  $xml_str = $dom->saveXML();
412  $this->object->setXmlManifest($xml_str);
413  $this->object->update();
414  $this->object->save();
415 
416  $lpSettings = new ilLPObjSettings($this->object->getId());
418 
419  switch ($moveOn) {
422  break;
425  break;
428  break;
429  case ilCmiXapiLP::MOVEON_COMPLETED_AND_PASSED: // ich würde es noch implementieren
431  break;
432  }
433  $lpSettings->setMode($mode);
434  $lpSettings->update();
435  }
436 
437  protected function initObjectFromTincanXml(DOMDocument $dom): void
438  {
439  $xPath = new DOMXPath($dom);
440 
441  foreach ($xPath->query("//*[local-name()='activity']") as $activityNode) {
442  $title = $xPath->query("//*[local-name()='name']", $activityNode)->item(0)->nodeValue;
443  $this->object->setTitle(trim($title));
444 
445  $description = $xPath->query("//*[local-name()='description']", $activityNode)->item(0)->nodeValue;
446  $this->object->setDescription(trim($description));
447 
448  $activityId = $activityNode->getAttribute('id');
449  $this->object->setActivityId(trim($activityId));
450 
451  $relativeLaunchUrl = $xPath->query("//*[local-name()='launch']", $activityNode)->item(0)->nodeValue;
452  $this->object->setLaunchUrl(trim($relativeLaunchUrl));
453 
454  break; // TODO: manage multi activities imports
455  }
456 
457  $xml_str = $dom->saveXML();
458  $this->object->setXmlManifest($xml_str);
459  $this->object->update();
460  $this->object->save();
461  }
462 
463  private function generateActivityId(string $publisherId): string
464  {
465  global $DIC;
466  $objId = $this->object->getId();
467  return "https://ilias.de/cmi5/activityid/" . (new \Ramsey\Uuid\UuidFactory())->uuid3(ilCmiXapiUser::getIliasUuid(), $objId . '-' . $publisherId);
468  }
469 }
static getWebspaceDir(string $mode="filesystem")
get webspace directory
fetchFileExtension(FileUploadResult $uploadResult)
const MOVEON_COMPLETED_AND_PASSED
$objId
Definition: xapitoken.php:55
handleXmlFileFromUpload(string $xmlFileName, string $xmlFilePath)
static renameExecutables(string $a_dir)
__construct(ilObjCmiXapi $object)
ilCmiXapiContentUploadImporter constructor.
const CLIENT_ID
Definition: constants.php:41
global $DIC
Definition: shib_login.php:25
$results
const MOVEON_COMPLETED_OR_PASSED
const ILIAS_WEB_DIR
Definition: constants.php:45