-
Notifications
You must be signed in to change notification settings - Fork 13
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 #2571 from oat-sa/feature/ADF-1780/create-necessar…
…y-metadata chore: add form modifiers
- Loading branch information
Showing
11 changed files
with
856 additions
and
8 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\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
69
model/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\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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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
80
model/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,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), | ||
]); | ||
} | ||
} |
Oops, something went wrong.