Skip to content

Commit

Permalink
chore: add form modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
shpran committed Sep 9, 2024
1 parent eb97437 commit 4a6ce7e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
4 changes: 3 additions & 1 deletion manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// phpcs:disable Generic.Files.LineLength
use oat\taoQtiTest\models\classes\render\CustomInteraction\ServiceProvider\CustomInteractionPostProcessingServiceProvider;
// phpcs:enable Generic.Files.LineLength
use oat\taoQtiTest\models\Form\ServiceProvider\FormServiceProvider;
use oat\taoQtiTest\models\render\ItemsReferencesServiceProvider;
use oat\taoQtiTest\models\TestSessionState\Container\TestSessionStateServiceProvider;
use oat\taoQtiTest\models\xmlEditor\XmlEditorInterface;
Expand Down Expand Up @@ -185,6 +186,7 @@
ItemsReferencesServiceProvider::class,
TestQtiServiceProvider::class,
TestSessionStateServiceProvider::class,
MetadataServiceProvider::class
MetadataServiceProvider::class,
FormServiceProvider::class,
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace oat\taoQtiTest\models\Form\Modifier;

use core_kernel_classes_Resource;
use oat\generis\model\data\Ontology;
use oat\tao\model\form\Modifier\FormModifierInterface;
use oat\tao\model\TaoOntology;
use tao_helpers_form_Form as Form;
use tao_helpers_Uri;
use taoQtiTest_models_classes_QtiTestService;

class EditTranslationInstanceFormModifier implements FormModifierInterface
{
public const ID = 'tao_qti_test.form_modifier.edit_translation_instance';

private Ontology $ontology;
private taoQtiTest_models_classes_QtiTestService $testQtiService;

public function __construct(Ontology $ontology, taoQtiTest_models_classes_QtiTestService $testQtiService)
{
$this->ontology = $ontology;
$this->testQtiService = $testQtiService;
}

public function supports(Form $form, array $options = []): bool
{
$instanceUri = $form->getValue(self::FORM_INSTANCE_URI);

if (!$instanceUri) {
return false;
}

$instance = $this->ontology->getResource($instanceUri);

// @TODO Check if FF for translation enabled
return $instance->isInstanceOf($this->ontology->getClass(TaoOntology::CLASS_URI_TEST));
}

public function modify(Form $form, array $options = []): void
{
$uniqueIdElement = $form->getElement(tao_helpers_Uri::encode(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER));

if (!$uniqueIdElement) {
return;
}

$instance = $this->ontology->getResource($form->getValue(self::FORM_INSTANCE_URI));
$jsonTest = $this->testQtiService->getJsonTest($instance);
$id = json_decode($jsonTest, true)['identifier'] ?? null;

if ($id) {
$uniqueIdElement->setValue($id);
}
}
}
36 changes: 36 additions & 0 deletions models/classes/Form/ServiceProvider/FormServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace oat\taoQtiTest\models\Form\ServiceProvider;

use oat\generis\model\data\Ontology;
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\tao\model\form\Modifier\FormModifierManager;
use oat\taoQtiTest\models\Form\Modifier\EditTranslationInstanceFormModifier;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use taoQtiTest_models_classes_QtiTestService;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

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

$services
->set(EditTranslationInstanceFormModifier::class, EditTranslationInstanceFormModifier::class)
->args([
service(Ontology::SERVICE_ID),
service(taoQtiTest_models_classes_QtiTestService::class),
]);

$formModifierManager = $services->get(FormModifierManager::class);
$formModifierManager
->call(
'add',
[
service(EditTranslationInstanceFormModifier::class),
EditTranslationInstanceFormModifier::ID,
]
);
}
}

0 comments on commit 4a6ce7e

Please sign in to comment.