-
Notifications
You must be signed in to change notification settings - Fork 13
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
chore: add form modifiers #2571
Merged
gabrielfs7
merged 12 commits into
feat/HKD-6/integration
from
feature/ADF-1780/create-necessary-metadata
Sep 13, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
16dda70
chore: add form modifiers
shpran 1c72ed8
chore: rename modifier, extend abstract class
shpran 9567f22
chore: do not replace unique ID
shpran 7d2c22b
chore: use proxies instead of manager
shpran f641023
chore: fix ci
shpran f83f2c6
chore: add unit tests for modifier
shpran 5d05800
chore: add dev-dependencies
shpran 7e8439d
chore: add event listener to set unique identifier
shpran a729df0
chore: register listener
shpran c6f864e
chore: use direct event class, add unit test
shpran 730b32d
chore: update unit tests in accordance with latest changes, add new u…
shpran 012223a
chore: throw an exception during retrieving, update unit test
shpran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
gabrielfs7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 | ||
gabrielfs7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.