Skip to content

Commit

Permalink
Merge pull request #2503 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 eb97437 + f97c561 commit f81583b
Show file tree
Hide file tree
Showing 11 changed files with 837 additions and 9 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"qtism/qtism": ">=0.28.3",
"oat-sa/generis": ">=15.36.4",
"oat-sa/tao-core": ">=54.21.0",
"oat-sa/extension-tao-item" : ">=12.1.0",
"oat-sa/extension-tao-itemqti" : ">=30.12.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-itemqti" : "dev-feature/ADF-1780/create-necessary-metadata as 99.99",
"oat-sa/extension-tao-test" : "dev-feat/ADF-1779/add-translator-role as 99.99",
"oat-sa/extension-tao-delivery" : ">=15.0.0",
"oat-sa/extension-tao-outcome" : ">=13.0.0",
"league/flysystem": "~1.0",
Expand Down
11 changes: 6 additions & 5 deletions manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@
use oat\tao\model\user\TaoRoles;
use oat\taoQtiTest\model\Container\TestQtiServiceProvider;
use oat\taoQtiTest\models\classes\metadata\MetadataServiceProvider;
// phpcs:disable Generic.Files.LineLength
// phpcs:ignore Generic.Files.LineLength
use oat\taoQtiTest\models\classes\render\CustomInteraction\ServiceProvider\CustomInteractionPostProcessingServiceProvider;
// phpcs:enable Generic.Files.LineLength
use oat\taoQtiTest\models\render\ItemsReferencesServiceProvider;
use oat\taoQtiTest\models\TestSessionState\Container\TestSessionStateServiceProvider;
use oat\taoQtiTest\models\Translation\ServiceProvider\TranslationServiceProvider;
use oat\taoQtiTest\models\xmlEditor\XmlEditorInterface;
use oat\taoQtiTest\scripts\install\RegisterResultTransmissionEventHandlers;
use oat\taoQtiTest\scripts\install\SetupProvider;
use oat\taoQtiTest\scripts\install\CreateTestSessionFilesystem;
use oat\taoQtiTest\scripts\install\DisableBRSinTestAuthoring;
use oat\taoQtiTest\scripts\install\RegisterCreatorServices;
use oat\taoQtiTest\scripts\install\RegisterFrontendPaths;
use oat\taoQtiTest\scripts\install\RegisterQtiCategoryPresetProviders;
use oat\taoQtiTest\scripts\install\RegisterQtiFlysystemManager;
use oat\taoQtiTest\scripts\install\RegisterQtiPackageExporter;
use oat\taoQtiTest\scripts\install\RegisterResultTransmissionEventHandlers;
use oat\taoQtiTest\scripts\install\RegisterSectionPauseService;
use oat\taoQtiTest\scripts\install\RegisterTestCategoryPresetProviderService;
use oat\taoQtiTest\scripts\install\RegisterTestContainer;
Expand All @@ -50,6 +49,7 @@
use oat\taoQtiTest\scripts\install\SetSynchronisationService;
use oat\taoQtiTest\scripts\install\SetupDefaultTemplateConfiguration;
use oat\taoQtiTest\scripts\install\SetupEventListeners;
use oat\taoQtiTest\scripts\install\SetupProvider;
use oat\taoQtiTest\scripts\install\SetUpQueueTasks;
use oat\taoQtiTest\scripts\install\SetupStateOffloadQueue;
use oat\taoQtiTest\scripts\install\SyncChannelInstaller;
Expand Down Expand Up @@ -185,6 +185,7 @@
ItemsReferencesServiceProvider::class,
TestQtiServiceProvider::class,
TestSessionStateServiceProvider::class,
MetadataServiceProvider::class
MetadataServiceProvider::class,
TranslationServiceProvider::class,
],
];
44 changes: 44 additions & 0 deletions migrations/Version202409111328132260_taoQtiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace oat\taoQtiTest\migrations;

use Doctrine\DBAL\Schema\Schema;
use oat\oatbox\event\EventManager;
use oat\tao\scripts\tools\migrations\AbstractMigration;
use oat\taoQtiTest\models\Translation\Listener\TestCreatedEventListener;
use oat\taoTests\models\event\TestCreatedEvent;

/**
* Auto-generated Migration: Please modify to your needs!
*
* phpcs:disable Squiz.Classes.ValidClassName
*/
final class Version202409111328132260_taoQtiTest 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(
TestCreatedEvent::class,
[TestCreatedEventListener::class, 'populateTranslationProperties']
);
}

public function down(Schema $schema): void
{
/** @var EventManager $eventManager */
$eventManager = $this->getServiceManager()->get(EventManager::SERVICE_ID);
$eventManager->detach(
TestCreatedEvent::class,
[TestCreatedEventListener::class, 'populateTranslationProperties']
);
}
}
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\taoQtiTest\models\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\taoQtiTest\models\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->featureFlagChecker = $featureFlagChecker;
$this->qtiIdentifierRetriever = $qtiIdentifierRetriever;
}

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);
}
}
}
76 changes: 76 additions & 0 deletions models/classes/Translation/Listener/TestCreatedEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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\taoQtiTest\models\Translation\Listener;

use oat\generis\model\data\Ontology;
use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
use oat\tao\model\TaoOntology;
use oat\taoQtiTest\models\Translation\Service\QtiIdentifierRetriever;
use oat\taoTests\models\event\TestCreatedEvent;
use Psr\Log\LoggerInterface;

class TestCreatedEventListener
{
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(TestCreatedEvent $event): void
{
if (!$this->featureFlagChecker->isEnabled('FEATURE_TRANSLATION_ENABLED')) {
return;
}

$test = $this->ontology->getResource($event->getTestUri());

$uniqueIdProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER);

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

return;
}

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

use core_kernel_classes_Resource;
use Psr\Log\LoggerInterface;
use taoQtiTest_models_classes_QtiTestService;
use Throwable;

class QtiIdentifierRetriever
{
private taoQtiTest_models_classes_QtiTestService $qtiTestService;
private LoggerInterface $logger;

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

public function retrieve(core_kernel_classes_Resource $test): ?string
{
try {
$jsonTest = $this->qtiTestService->getJsonTest($test);
$decodedTest = json_decode($jsonTest, true, 512, JSON_THROW_ON_ERROR);

return $decodedTest['identifier'] ?? null;
} catch (Throwable $exception) {
$this->logger->error('An error occurred while retrieving test data: ' . $exception->getMessage());

throw $exception;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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\taoQtiTest\models\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\taoQtiTest\models\Translation\Form\Modifier\TranslationFormModifier;
use oat\taoQtiTest\models\Translation\Listener\TestCreatedEventListener;
use oat\taoQtiTest\models\Translation\Service\QtiIdentifierRetriever;
use oat\taoTests\models\Translation\Form\Modifier\TranslationFormModifierProxy;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use taoQtiTest_models_classes_QtiTestService;

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(taoQtiTest_models_classes_QtiTestService::class),
service(LoggerService::SERVICE_ID),
]);

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

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

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

0 comments on commit f81583b

Please sign in to comment.