Skip to content

Commit

Permalink
Merge pull request #2676 from oat-sa/fix/AUT-4073-add-property-checking
Browse files Browse the repository at this point in the history
fix: add property checking to items
  • Loading branch information
Karol-Stelmaczonek authored Feb 7, 2025
2 parents 338ed2d + ef350f7 commit 5266aec
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
29 changes: 28 additions & 1 deletion model/qti/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,24 @@ 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);
$notMatchingProperties = $this->checkMissingClassProperties(
$metadataValues,
$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,
Expand Down Expand Up @@ -974,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)
);
}
}
19 changes: 19 additions & 0 deletions model/qti/metadata/importer/MetadataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 array_unique($metadataUriList);
}

/**
* Allow to register, into the config, the current importer service
*/
Expand Down

0 comments on commit 5266aec

Please sign in to comment.