-
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.
- Loading branch information
Showing
54 changed files
with
2,789 additions
and
361 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
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace oat\taoQtiTest\migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use oat\oatbox\event\EventManager; | ||
use oat\tao\model\resources\Event\InstanceCopiedEvent; | ||
use oat\tao\scripts\tools\migrations\AbstractMigration; | ||
use oat\taoQtiTest\models\classes\event\TestImportedEvent; | ||
use oat\taoQtiTest\models\UniqueId\Listener\TestCreationListener; | ||
use oat\taoTests\models\event\TestCreatedEvent; | ||
use oat\taoTests\models\event\TestDuplicatedEvent; | ||
|
||
/** | ||
* phpcs:disable Squiz.Classes.ValidClassName | ||
*/ | ||
final class Version202409111328132261_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, | ||
[TestCreationListener::class, 'populateUniqueId'] | ||
); | ||
$eventManager->attach( | ||
TestDuplicatedEvent::class, | ||
[TestCreationListener::class, 'populateUniqueId'] | ||
); | ||
$eventManager->attach( | ||
TestImportedEvent::class, | ||
[TestCreationListener::class, 'populateUniqueId'] | ||
); | ||
$eventManager->attach( | ||
InstanceCopiedEvent::class, | ||
[TestCreationListener::class, 'populateUniqueId'] | ||
); | ||
|
||
$this->getServiceManager()->register(EventManager::SERVICE_ID, $eventManager); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
/** @var EventManager $eventManager */ | ||
$eventManager = $this->getServiceManager()->get(EventManager::SERVICE_ID); | ||
|
||
$eventManager->detach( | ||
TestCreatedEvent::class, | ||
[TestCreationListener::class, 'populateUniqueId'] | ||
); | ||
$eventManager->detach( | ||
TestDuplicatedEvent::class, | ||
[TestCreationListener::class, 'populateUniqueId'] | ||
); | ||
$eventManager->detach( | ||
TestImportedEvent::class, | ||
[TestCreationListener::class, 'populateUniqueId'] | ||
); | ||
$eventManager->detach( | ||
InstanceCopiedEvent::class, | ||
[TestCreationListener::class, 'populateUniqueId'] | ||
); | ||
$this->getServiceManager()->register(EventManager::SERVICE_ID, $eventManager); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
models/classes/IdentifierGenerator/Generator/QtiIdentifierGenerator.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,51 @@ | ||
<?php | ||
|
||
namespace oat\taoQtiTest\models\IdentifierGenerator\Generator; | ||
|
||
use core_kernel_classes_Resource; | ||
use InvalidArgumentException; | ||
use oat\generis\model\data\Ontology; | ||
use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorInterface; | ||
use qtism\common\utils\Format; | ||
|
||
class QtiIdentifierGenerator implements IdentifierGeneratorInterface | ||
{ | ||
private Ontology $ontology; | ||
|
||
public function __construct(Ontology $ontology) | ||
{ | ||
$this->ontology = $ontology; | ||
} | ||
|
||
public function generate(array $options = []): string | ||
{ | ||
$resource = $this->getResource($options); | ||
$label = $resource->getLabel(); | ||
|
||
$identifier = null; | ||
|
||
if (preg_match('/^\d/', $label)) { | ||
$identifier = 't_' . $label; | ||
} | ||
|
||
return str_replace('_', '-', Format::sanitizeIdentifier($identifier)); | ||
} | ||
|
||
private function getResource(array $options): core_kernel_classes_Resource | ||
{ | ||
if (isset($options[self::OPTION_RESOURCE_ID]) && is_string($options[self::OPTION_RESOURCE_ID])) { | ||
return $this->ontology->getResource($options[self::OPTION_RESOURCE_ID]); | ||
} | ||
|
||
if ( | ||
isset($options[self::OPTION_RESOURCE]) | ||
&& $options[self::OPTION_RESOURCE] instanceof core_kernel_classes_Resource | ||
) { | ||
return $options[self::OPTION_RESOURCE]; | ||
} | ||
|
||
throw new InvalidArgumentException( | ||
'Test QTI Identifier generation failure: resource is required' | ||
); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
models/classes/IdentifierGenerator/ServiceProvider/IdentifierGeneratorServiceProvider.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,36 @@ | ||
<?php | ||
|
||
namespace oat\taoQtiTest\models\IdentifierGenerator\ServiceProvider; | ||
|
||
use oat\generis\model\data\Ontology; | ||
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface; | ||
use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorProxy; | ||
use oat\tao\model\TaoOntology; | ||
use oat\taoQtiTest\models\IdentifierGenerator\Generator\QtiIdentifierGenerator; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
use function Symfony\Component\DependencyInjection\Loader\Configurator\service; | ||
|
||
class IdentifierGeneratorServiceProvider implements ContainerServiceProviderInterface | ||
{ | ||
public function __invoke(ContainerConfigurator $configurator): void | ||
{ | ||
$services = $configurator->services(); | ||
|
||
$services | ||
->set(QtiIdentifierGenerator::class, QtiIdentifierGenerator::class) | ||
->args([ | ||
service(Ontology::SERVICE_ID), | ||
]); | ||
|
||
$services | ||
->get(IdentifierGeneratorProxy::class) | ||
->call( | ||
'addIdentifierGenerator', | ||
[ | ||
service(QtiIdentifierGenerator::class), | ||
TaoOntology::CLASS_URI_TEST, | ||
] | ||
); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
models/classes/Qti/Identifier/Service/QtiIdentifierSetter.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,50 @@ | ||
<?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\Qti\Identifier\Service; | ||
|
||
use oat\tao\model\Translation\Service\AbstractQtiIdentifierSetter; | ||
use Psr\Log\LoggerInterface; | ||
use taoQtiTest_models_classes_QtiTestService; | ||
|
||
class QtiIdentifierSetter extends AbstractQtiIdentifierSetter | ||
{ | ||
private taoQtiTest_models_classes_QtiTestService $qtiTestService; | ||
|
||
public function __construct(taoQtiTest_models_classes_QtiTestService $qtiTestService, LoggerInterface $logger) | ||
{ | ||
parent::__construct($logger); | ||
|
||
$this->qtiTestService = $qtiTestService; | ||
} | ||
|
||
protected function applyIdentifier(array $options): void | ||
{ | ||
$test = $this->getResource($options); | ||
$jsonTest = $this->qtiTestService->getJsonTest($test); | ||
|
||
$decodedTest = json_decode($jsonTest, true, 512, JSON_THROW_ON_ERROR); | ||
$decodedTest['identifier'] = $this->getIdentifier($options); | ||
|
||
$this->qtiTestService->saveJsonTest($test, json_encode($decodedTest)); | ||
} | ||
} |
Oops, something went wrong.