-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2503 from oat-sa/feature/ADF-1780/create-necessar…
…y-metadata chore: add form modifiers
- Loading branch information
Showing
11 changed files
with
837 additions
and
9 deletions.
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
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
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,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'] | ||
); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
models/classes/Translation/Form/Modifier/TranslationFormModifier.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,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
76
models/classes/Translation/Listener/TestCreatedEventListener.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,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
54
models/classes/Translation/Service/QtiIdentifierRetriever.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,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; | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
models/classes/Translation/ServiceProvider/TranslationServiceProvider.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,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), | ||
]); | ||
} | ||
} |
Oops, something went wrong.