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: avoid property duplication in meta metadata #2578

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
40 changes: 33 additions & 7 deletions models/classes/metadata/MetadataLomService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@

use DOMDocument;
use DOMNodeList;
use DOMXPath;

class MetadataLomService
{
public const FEATURE_FLAG = 'FEATURE_FLAG_METADATA_LOM_SERVICE';

public function addPropertiesToMetadataBlock(array $properties, DOMDocument $manifest): void
{
/** @var DOMNodeList $metadataBlock */
Expand All @@ -39,13 +38,30 @@ public function addPropertiesToMetadataBlock(array $properties, DOMDocument $man
$manifest->documentElement->appendChild($metadataBlock);
}

$metadataBlock->item(0)
->appendChild($manifest->createElement('imsmd:lom'))
->appendChild($manifest->createElement('imsmd:metaMetadata'))
->appendChild($manifest->createElement('extension'))
->appendChild($manifest->createElement('customProperties'));
$customProperties = $this->getCustomProperties($manifest);

// If there is no custom properties means that it is initial loop and we need to create customProperties node
if (empty($customProperties)) {
$metadataBlock->item(0)
->appendChild($manifest->createElement('imsmd:lom'))
->appendChild($manifest->createElement('imsmd:metaMetadata'))
->appendChild($manifest->createElement('extension'))
->appendChild($manifest->createElement('customProperties'));
}

foreach ($properties as $property) {
$propertyExists = false;
if (!empty($customProperties)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it should not be empty already cause you created them above, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->getCustomProperties($manifest); should return empty array on a first loop.

//Check if custom property DomElement array contain object where nodeValue is equal to $property['uri']
$propertyExists = !empty(array_filter($customProperties, function ($customProperty) use ($property) {
return $customProperty->nodeValue === $property['uri'];
}));
}

if ($propertyExists) {
continue;
}

$propertyNode = $manifest->createElement('property');
foreach ($property as $key => $value) {
$propertyNode->appendChild($manifest->createElement($key, $value));
Expand All @@ -56,4 +72,14 @@ public function addPropertiesToMetadataBlock(array $properties, DOMDocument $man
->appendChild($propertyNode);
}
}

private function getCustomProperties(DOMDocument $manifest): array
{
$xpath = new DOMXPath($manifest);
$xpath->registerNamespace('manifest', 'http://www.imsglobal.org/xsd/imscp_v1p1');
$xpath->registerNamespace('imsmd', 'http://ltsc.ieee.org/xsd/LOM');

$customProperties = $xpath->query('//customProperties/property/uri');
return iterator_to_array($customProperties);
}
}