From 082c3bb79fcdb2d3a6d4a7a29df784196acc8f2a Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Fri, 31 Jan 2025 16:39:39 +0100 Subject: [PATCH 1/3] fix: add property checking to items --- model/qti/ImportService.php | 15 ++++++++++++++- .../metadata/importer/MetadataImporter.php | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/model/qti/ImportService.php b/model/qti/ImportService.php index 4e9b6cfc25..e3632f372d 100755 --- a/model/qti/ImportService.php +++ b/model/qti/ImportService.php @@ -329,12 +329,25 @@ public function importQTIPACKFile( $qtiItemResources = $this->createQtiManifest($folder . 'imsmanifest.xml'); if ($importMetadataEnabled) { - $metadataValues = $this->getMetadataImporter()->extract($domManifest); $metaMetadataValues = $this->getMetaMetadataExtractor()->extract($domManifest); $mappedMetadataValues = $this->getMetaMetadataImportMapper()->mapMetaMetadataToProperties( $metaMetadataValues, $itemClass ); + $metadataValues = $this->getMetadataImporter()->extract($domManifest); + $metadataValueUris = $this->getMetadataImporter()->metadataValueUris($metadataValues); + $notMatchingProperties = array_diff( + $metadataValueUris, + array_keys($mappedMetadataValues['itemProperties']) + ); + if (!empty($notMatchingProperties)) { + return Report::createError( + sprintf( + __('Target class is missing the following metadata properties: %s'), + implode(', ', $notMatchingProperties) + ) + ); + } if (empty($mappedMetadataValues)) { $mappedMetadataValues = $this->getMetaMetadataImportMapper()->mapMetadataToProperties( $metadataValues, diff --git a/model/qti/metadata/importer/MetadataImporter.php b/model/qti/metadata/importer/MetadataImporter.php index 38076d82a0..0424f404f2 100644 --- a/model/qti/metadata/importer/MetadataImporter.php +++ b/model/qti/metadata/importer/MetadataImporter.php @@ -29,6 +29,7 @@ use oat\taoQtiItem\model\qti\metadata\ContextualMetadataGuardian; use oat\taoQtiItem\model\qti\metadata\MetadataService; use oat\taoQtiItem\model\qti\metadata\MetadataValidator; +use oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue; class MetadataImporter extends AbstractMetadataService { @@ -231,6 +232,24 @@ public function unregister($key, $name) return parent::unregister($key, $name); } + public function metadataValueUris($metadata): array + { + $metadataUriList = []; + foreach ($metadata as $resourceIdentifier => $metadataValueCollection) { + foreach ($metadataValueCollection as $metadataValue) { + if (!$metadataValue instanceof SimpleMetadataValue) { + continue; + } + $uri = $metadataValue->getPath()[1]; + if (!empty($uri)) { + $metadataUriList[] = $uri; + } + } + } + + return $metadataUriList; + } + /** * Allow to register, into the config, the current importer service */ From 9fc366540f646064d7a5791014a3c007ac87feed Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Tue, 4 Feb 2025 14:12:07 +0100 Subject: [PATCH 2/3] fix: add property checking to test --- model/qti/metadata/importer/MetadataImporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/qti/metadata/importer/MetadataImporter.php b/model/qti/metadata/importer/MetadataImporter.php index 0424f404f2..2959c41d3a 100644 --- a/model/qti/metadata/importer/MetadataImporter.php +++ b/model/qti/metadata/importer/MetadataImporter.php @@ -247,7 +247,7 @@ public function metadataValueUris($metadata): array } } - return $metadataUriList; + return array_unique($metadataUriList); } /** From ef350f732233a651c9b87903126ac4760c7797b0 Mon Sep 17 00:00:00 2001 From: Karol Stelmaczonek Date: Tue, 4 Feb 2025 15:12:55 +0100 Subject: [PATCH 3/3] fix: introduce new method checkMissingClassProperties --- model/qti/ImportService.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/model/qti/ImportService.php b/model/qti/ImportService.php index e3632f372d..53b7243be6 100755 --- a/model/qti/ImportService.php +++ b/model/qti/ImportService.php @@ -335,10 +335,9 @@ public function importQTIPACKFile( $itemClass ); $metadataValues = $this->getMetadataImporter()->extract($domManifest); - $metadataValueUris = $this->getMetadataImporter()->metadataValueUris($metadataValues); - $notMatchingProperties = array_diff( - $metadataValueUris, - array_keys($mappedMetadataValues['itemProperties']) + $notMatchingProperties = $this->checkMissingClassProperties( + $metadataValues, + $mappedMetadataValues['itemProperties'] ); if (!empty($notMatchingProperties)) { return Report::createError( @@ -987,4 +986,19 @@ private function getItemConverter(): ItemConverter { return $this->getServiceManager()->getContainer()->get(ItemConverter::class); } + + /** + * Checks if target class has all the properties needed to import the metadata. + * @param array $metadataValues + * @param $itemProperties + * @return array + */ + private function checkMissingClassProperties(array $metadataValues, $itemProperties): array + { + $metadataValueUris = $this->getMetadataImporter()->metadataValueUris($metadataValues); + return array_diff( + $metadataValueUris, + array_keys($itemProperties) + ); + } }