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: unique identifiers #4124

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@
DynamicConfigServiceProvider::class,
TranslationServiceProvider::class,
TaoFormServiceProvider::class,
IdentifierGeneratorServiceProvider::class
IdentifierGeneratorServiceProvider::class,
],
'middlewares' => [
MiddlewareConfig::class,
Expand Down
74 changes: 74 additions & 0 deletions models/classes/Translation/Service/AbstractQtiIdentifierSetter.php
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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function create(CreateTranslationCommand $command): core_kernel_classes_R
);

foreach ($this->callables[$rootId] ?? [] as $callable) {
$clonedInstance = $callable($clonedInstance);
$callable($clonedInstance);
}

return $clonedInstance;
Expand Down
111 changes: 111 additions & 0 deletions models/classes/Translation/Service/TranslationUniqueIdSetter.php
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
Copy link
Contributor

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

Suggested change
public function __invoke(core_kernel_classes_Resource $item): void
public function __invoke(core_kernel_classes_Resource $resource): 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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
use oat\tao\model\Translation\Form\Modifier\TranslationFormModifier;
use oat\tao\model\Translation\Repository\ResourceTranslatableRepository;
use oat\tao\model\Translation\Repository\ResourceTranslationRepository;
use oat\tao\model\Translation\Service\Listener\ResourceDeletedEventListener;
use oat\tao\model\Translation\Service\ResourceLanguageRetriever;
use oat\tao\model\Translation\Service\ResourceMetadataPopulateService;
use oat\tao\model\Translation\Service\ResourceTranslatableRetriever;
use oat\tao\model\Translation\Service\ResourceTranslationRetriever;
use oat\tao\model\Translation\Service\TranslationCreationService;
use oat\tao\model\Translation\Service\TranslationDeletionService;
use oat\tao\model\Translation\Service\TranslationSyncService;
use oat\tao\model\Translation\Service\TranslationUniqueIdSetter;
use oat\tao\model\Translation\Service\TranslationUpdateService;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand Down Expand Up @@ -170,11 +170,9 @@ public function __invoke(ContainerConfigurator $configurator): void
->public();

$services
->set(ResourceDeletedEventListener::class, ResourceDeletedEventListener::class)
->set(TranslationUniqueIdSetter::class, TranslationUniqueIdSetter::class)
->args([
service(FeatureFlagChecker::class),
service(TranslationDeletionService::class),
])
->public();
]);
}
}