Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add property checking to items #2676

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading