-
Notifications
You must be signed in to change notification settings - Fork 35
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: unique identifiers #4124
Merged
Merged
fix: unique identifiers #4124
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
models/classes/Translation/Service/AbstractQtiIdentifierSetter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2024 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\tao\model\Translation\Service; | ||
|
||
use core_kernel_classes_Resource; | ||
use InvalidArgumentException; | ||
use Psr\Log\LoggerInterface; | ||
use Throwable; | ||
|
||
abstract class AbstractQtiIdentifierSetter | ||
{ | ||
public const OPTION_RESOURCE = 'resource'; | ||
public const OPTION_IDENTIFIER = 'identifier'; | ||
|
||
private LoggerInterface $logger; | ||
|
||
public function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function set(array $options): void | ||
{ | ||
try { | ||
if (!isset($options[self::OPTION_RESOURCE], $options[self::OPTION_IDENTIFIER])) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'Options %s and %s are required to set QTI Identifier.', | ||
self::OPTION_RESOURCE, | ||
self::OPTION_IDENTIFIER | ||
) | ||
); | ||
} | ||
|
||
$this->applyIdentifier($options); | ||
} catch (Throwable $exception) { | ||
$this->logger->error('An error occurred while setting QTI identifier: ' . $exception->getMessage()); | ||
|
||
throw $exception; | ||
} | ||
} | ||
|
||
abstract protected function applyIdentifier(array $options): void; | ||
|
||
protected function getResource(array $options): core_kernel_classes_Resource | ||
{ | ||
return $options[self::OPTION_RESOURCE]; | ||
} | ||
|
||
protected function getIdentifier(array $options): string | ||
{ | ||
return $options[self::OPTION_IDENTIFIER]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
models/classes/Translation/Service/TranslationUniqueIdSetter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
/** | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License | ||
* as published by the Free Software Foundation; under version 2 | ||
* of the License (non-upgradable). | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2024 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\tao\model\Translation\Service; | ||
|
||
use core_kernel_classes_Resource; | ||
use InvalidArgumentException; | ||
use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; | ||
use oat\tao\model\TaoOntology; | ||
|
||
class TranslationUniqueIdSetter | ||
{ | ||
private FeatureFlagCheckerInterface $featureFlagChecker; | ||
private array $qtiIdentifierSetters = []; | ||
|
||
public function __construct(FeatureFlagCheckerInterface $featureFlagChecker) | ||
{ | ||
$this->featureFlagChecker = $featureFlagChecker; | ||
} | ||
|
||
public function addQtiIdentifierSetter(AbstractQtiIdentifierSetter $qtiIdentifierSetter, string $resourceType): void | ||
{ | ||
if (isset($this->qtiIdentifierSetters[$resourceType])) { | ||
throw new InvalidArgumentException( | ||
'QTI Identifier setter already exists for resource type ' . $resourceType | ||
); | ||
} | ||
|
||
$this->qtiIdentifierSetters[$resourceType] = $qtiIdentifierSetter; | ||
} | ||
|
||
public function __invoke(core_kernel_classes_Resource $item): void | ||
{ | ||
if ( | ||
!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_UNIQUE_NUMERIC_QTI_IDENTIFIER') | ||
&& !$this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED') | ||
) { | ||
return; | ||
} | ||
|
||
$originalResource = $this->getOriginalResource($item); | ||
$uniqueIdentifier = $this->getUniqueId($originalResource); | ||
|
||
$item->editPropertyValues($item->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER), $uniqueIdentifier); | ||
$this->getQtiIdentifierSetter($item)->set([ | ||
AbstractQtiIdentifierSetter::OPTION_RESOURCE => $item, | ||
AbstractQtiIdentifierSetter::OPTION_IDENTIFIER => $uniqueIdentifier, | ||
]); | ||
} | ||
|
||
private function getOriginalResource(core_kernel_classes_Resource $resource): core_kernel_classes_Resource | ||
{ | ||
$property = $resource->getProperty(TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI); | ||
$originalResourceUri = $resource->getOnePropertyValue($property); | ||
|
||
if (empty($originalResourceUri)) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'Resource %s is not a translation - original resource URI is empty', | ||
$originalResourceUri | ||
) | ||
); | ||
} | ||
|
||
return $resource->getResource($originalResourceUri); | ||
} | ||
|
||
private function getUniqueId(core_kernel_classes_Resource $resource): ?string | ||
{ | ||
$property = $resource->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER); | ||
$uniqueId = $resource->getOnePropertyValue($property); | ||
|
||
if (empty($uniqueId)) { | ||
throw new InvalidArgumentException('Unique ID must exists for resource URI ' . $resource->getUri()); | ||
} | ||
|
||
return $uniqueId->literal; | ||
} | ||
|
||
private function getQtiIdentifierSetter(core_kernel_classes_Resource $resource): AbstractQtiIdentifierSetter | ||
{ | ||
$resourceType = $resource->getRootId(); | ||
|
||
if (!isset($this->qtiIdentifierSetters[$resourceType])) { | ||
throw new InvalidArgumentException( | ||
'QTI Identifier setter does not exist for resource type ' . $resourceType | ||
); | ||
} | ||
|
||
return $this->qtiIdentifierSetters[$resourceType]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should call it more agnostically
$resource