Skip to content

Commit

Permalink
Merge pull request #2571 from oat-sa/feature/ADF-1780/create-necessar…
Browse files Browse the repository at this point in the history
…y-metadata

chore: add form modifiers
  • Loading branch information
gabrielfs7 authored Sep 13, 2024
2 parents 838caaa + 012223a commit b4b1c95
Show file tree
Hide file tree
Showing 11 changed files with 856 additions and 8 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"naneau/semver": "~0.0.7",
"oat-sa/generis": ">=15.36.4",
"oat-sa/tao-core": ">=54.22.0",
"oat-sa/extension-tao-item": ">=12.0.0",
"oat-sa/extension-tao-test": ">=16.0.0"
"oat-sa/tao-core": "dev-feat/ADF-1781/translations-feature as 99.99",
"oat-sa/extension-tao-item": "dev-feat/ADF-1779/add-translator-role as 99.99",
"oat-sa/extension-tao-test": "dev-feat/ADF-1779/add-translator-role as 99.99"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 6 additions & 5 deletions manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,25 @@
*
*/

use oat\tao\model\accessControl\func\AccessRule;
use oat\tao\model\user\TaoRoles;
use oat\taoItems\model\user\TaoItemsRoles;
use oat\taoQtiItem\controller\QtiCreator;
use oat\taoQtiItem\controller\QtiCssAuthoring;
use oat\taoQtiItem\controller\QtiPreview;
use oat\taoQtiItem\install\scripts\addValidationSettings;
use oat\taoQtiItem\install\scripts\ExtendConfigurationRegistry;
use oat\taoQtiItem\install\scripts\SetDragAndDropConfig;
use oat\taoQtiItem\install\scripts\setXMLParserConfig;
use oat\taoQtiItem\model\qti\CustomInteractionAsset\ServiceProvider\{
CustomInteractionAssetExtractorAllocatorServiceProvider
};
use oat\taoQtiItem\model\FeatureFlag\ServiceProvider\FeatureFlagFlaServiceProvider;
use oat\taoQtiItem\model\FeatureFlag\ServiceProvider\FeatureFlagQtiIdentifierServiceProvider;
// phpcs:ignore Generic.Files.LineLength
use oat\taoQtiItem\model\qti\CustomInteractionAsset\ServiceProvider\CustomInteractionAssetExtractorAllocatorServiceProvider;
use oat\taoQtiItem\model\qti\metadata\importer\MetaMetadataServiceProvider;
use oat\taoQtiItem\model\qti\ServiceProvider\IdentifierGenerationStrategyServiceProvider;
use oat\taoQtiItem\model\qti\ServiceProvider\ItemIdentifierValidatorServiceProvider;
use oat\taoQtiItem\model\qti\ServiceProvider\MetadataServiceProvider;
use oat\taoQtiItem\model\Translation\ServiceProvider\TranslationServiceProvider;
use oat\taoQtiItem\scripts\install\AddLabelInjectorForExport;
use oat\taoQtiItem\scripts\install\InitMetadataService;
use oat\taoQtiItem\scripts\install\ItemEventRegister;
Expand All @@ -47,8 +49,6 @@
use oat\taoQtiItem\scripts\install\SetupQtiMetadataImportExportService;
use oat\taoQtiItem\scripts\install\SetUpQueueTasks;
use oat\taoQtiItem\scripts\update\Updater;
use oat\taoItems\model\user\TaoItemsRoles;
use oat\tao\model\accessControl\func\AccessRule;

$extpath = __DIR__ . DIRECTORY_SEPARATOR;
$taopath = dirname(__FILE__, 2) . DIRECTORY_SEPARATOR . 'tao' . DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -209,5 +209,6 @@
MetaMetadataServiceProvider::class,
IdentifierGenerationStrategyServiceProvider::class,
FeatureFlagQtiIdentifierServiceProvider::class,
TranslationServiceProvider::class,
],
];
44 changes: 44 additions & 0 deletions migrations/Version202409111328111101_taoQtiItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace oat\taoQtiItem\migrations;

use Doctrine\DBAL\Schema\Schema;
use oat\oatbox\event\EventManager;
use oat\tao\scripts\tools\migrations\AbstractMigration;
use oat\taoItems\model\event\ItemUpdatedEvent;
use oat\taoQtiItem\model\Translation\Listener\ItemUpdatedEventListener;

/**
* Auto-generated Migration: Please modify to your needs!
*
* phpcs:disable Squiz.Classes.ValidClassName
*/
final class Version202409111328111101_taoQtiItem extends AbstractMigration
{
public function getDescription(): string
{
return 'Add new listener to populate translation properties';
}

public function up(Schema $schema): void
{
/** @var EventManager $eventManager */
$eventManager = $this->getServiceManager()->get(EventManager::SERVICE_ID);
$eventManager->attach(
ItemUpdatedEvent::class,
[ItemUpdatedEventListener::class, 'populateTranslationProperties']
);
}

public function down(Schema $schema): void
{
/** @var EventManager $eventManager */
$eventManager = $this->getServiceManager()->get(EventManager::SERVICE_ID);
$eventManager->detach(
ItemUpdatedEvent::class,
[ItemUpdatedEventListener::class, 'populateTranslationProperties']
);
}
}
69 changes: 69 additions & 0 deletions model/Translation/Form/Modifier/TranslationFormModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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\taoQtiItem\model\Translation\Form\Modifier;

use oat\generis\model\data\Ontology;
use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
use oat\tao\model\form\Modifier\AbstractFormModifier;
use oat\tao\model\TaoOntology;
use oat\taoQtiItem\model\Translation\Service\QtiIdentifierRetriever;
use tao_helpers_form_Form;
use tao_helpers_Uri;

class TranslationFormModifier extends AbstractFormModifier
{
private Ontology $ontology;
private QtiIdentifierRetriever $qtiIdentifierRetriever;
private FeatureFlagCheckerInterface $featureFlagChecker;

public function __construct(
Ontology $ontology,
QtiIdentifierRetriever $qtiIdentifierRetriever,
FeatureFlagCheckerInterface $featureFlagChecker
) {
$this->ontology = $ontology;
$this->qtiIdentifierRetriever = $qtiIdentifierRetriever;
$this->featureFlagChecker = $featureFlagChecker;
}

public function modify(tao_helpers_form_Form $form, array $options = []): void
{
if (!$this->featureFlagChecker->isEnabled('FEATURE_TRANSLATION_ENABLED')) {
return;
}

$encodedProperty = tao_helpers_Uri::encode(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER);
$uniqueIdValue = $form->getValue($encodedProperty);

if (!empty($uniqueIdValue)) {
return;
}

$instance = $this->ontology->getResource($form->getValue('uri'));
$identifier = $this->qtiIdentifierRetriever->retrieve($instance);

if ($identifier) {
$form->setValue($encodedProperty, $identifier);
}
}
}
75 changes: 75 additions & 0 deletions model/Translation/Listener/ItemUpdatedEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\taoQtiItem\model\Translation\Listener;

use oat\generis\model\data\Ontology;
use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
use oat\tao\model\TaoOntology;
use oat\taoItems\model\event\ItemUpdatedEvent;
use oat\taoQtiItem\model\Translation\Service\QtiIdentifierRetriever;
use Psr\Log\LoggerInterface;

class ItemUpdatedEventListener
{
private FeatureFlagCheckerInterface $featureFlagChecker;
private Ontology $ontology;
private QtiIdentifierRetriever $qtiIdentifierRetriever;
private LoggerInterface $logger;

public function __construct(
FeatureFlagCheckerInterface $featureFlagChecker,
Ontology $ontology,
QtiIdentifierRetriever $qtiIdentifierRetriever,
LoggerInterface $logger
) {
$this->featureFlagChecker = $featureFlagChecker;
$this->ontology = $ontology;
$this->qtiIdentifierRetriever = $qtiIdentifierRetriever;
$this->logger = $logger;
}

public function populateTranslationProperties(ItemUpdatedEvent $event): void
{
if (!$this->featureFlagChecker->isEnabled('FEATURE_TRANSLATION_ENABLED')) {
return;
}

$uniqueIdProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER);
$item = $this->ontology->getResource($event->getItemUri());

if ($item->getOnePropertyValue($uniqueIdProperty) !== null) {
$this->logger->info(
sprintf(
'The property "%s" for the item "%s" has already been set.',
$uniqueIdProperty->getUri(),
$item->getUri()
)
);

return;
}

$identifier = $this->qtiIdentifierRetriever->retrieve($item);
$item->setPropertyValue($uniqueIdProperty, $identifier);
}
}
55 changes: 55 additions & 0 deletions model/Translation/Service/QtiIdentifierRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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\taoQtiItem\model\Translation\Service;

use core_kernel_classes_Resource;
use oat\taoQtiItem\model\qti\Service;
use Psr\Log\LoggerInterface;
use Throwable;

class QtiIdentifierRetriever
{
private Service $qtiItemService;
private LoggerInterface $logger;

public function __construct(Service $qtiItemService, LoggerInterface $logger)
{
$this->qtiItemService = $qtiItemService;
$this->logger = $logger;
}

public function retrieve(core_kernel_classes_Resource $item): ?string
{
try {
$itemData = $this->qtiItemService->getDataItemByRdfItem($item);
} catch (Throwable $exception) {
$this->logger->error('An error occurred while retrieving item data: ' . $exception->getMessage());

throw $exception;
}

return $itemData
? $itemData->getIdentifier()
: null;
}
}
80 changes: 80 additions & 0 deletions model/Translation/ServiceProvider/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\taoQtiItem\model\Translation\ServiceProvider;

use oat\generis\model\data\Ontology;
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\oatbox\log\LoggerService;
use oat\tao\model\featureFlag\FeatureFlagChecker;
use oat\taoItems\model\Translation\Form\Modifier\TranslationFormModifierProxy;
use oat\taoQtiItem\model\qti\Service;
use oat\taoQtiItem\model\Translation\Form\Modifier\TranslationFormModifier;
use oat\taoQtiItem\model\Translation\Listener\ItemUpdatedEventListener;
use oat\taoQtiItem\model\Translation\Service\QtiIdentifierRetriever;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

class TranslationServiceProvider implements ContainerServiceProviderInterface
{
public function __invoke(ContainerConfigurator $configurator): void
{
$services = $configurator->services();

$services
->set(QtiIdentifierRetriever::class, QtiIdentifierRetriever::class)
->args([
service(Service::class),
service(LoggerService::SERVICE_ID),
]);

$services
->set(TranslationFormModifier::class, TranslationFormModifier::class)
->args([
service(Ontology::SERVICE_ID),
service(QtiIdentifierRetriever::class),
service(FeatureFlagChecker::class),
]);

$services = $configurator->services();

$services
->get(TranslationFormModifierProxy::class)
->call(
'addModifier',
[
service(TranslationFormModifier::class),
]
);

$services
->set(ItemUpdatedEventListener::class, ItemUpdatedEventListener::class)
->public()
->args([
service(FeatureFlagChecker::class),
service(Ontology::SERVICE_ID),
service(QtiIdentifierRetriever::class),
service(LoggerService::SERVICE_ID),
]);
}
}
Loading

0 comments on commit b4b1c95

Please sign in to comment.