Skip to content

Commit

Permalink
Merge pull request #2575 from oat-sa/feat/HKD-6/integration
Browse files Browse the repository at this point in the history
Feat/hkd 6/integration
  • Loading branch information
shpran authored Nov 11, 2024
2 parents a7469e6 + 3b1b170 commit 9d6aa41
Show file tree
Hide file tree
Showing 55 changed files with 2,604 additions and 1,011 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
"oat-sa/lib-tao-qti": "^7.8.1",
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"naneau/semver": "~0.0.7",
"oat-sa/generis": ">=15.36.4",
"oat-sa/tao-core": ">=54.22.0",
"oat-sa/extension-tao-item": ">=12.0.0",
"oat-sa/extension-tao-test": ">=16.0.0"
"oat-sa/generis": ">=15.39.0",
"oat-sa/tao-core": ">=54.23.0",
"oat-sa/extension-tao-item": ">=12.4.0",
"oat-sa/extension-tao-test": ">=16.3.0"
},
"autoload": {
"psr-4": {
Expand Down
39 changes: 34 additions & 5 deletions controller/QtiCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
use oat\generis\model\data\event\ResourceUpdated;
use oat\generis\model\OntologyAwareTrait;
use oat\oatbox\event\EventManager;
use oat\tao\model\featureFlag\FeatureFlagChecker;
use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
use oat\tao\model\featureFlag\FeatureFlagConfigSwitcher;
use oat\tao\model\http\HttpJsonResponseTrait;
use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorInterface;
use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorProxy;
use oat\tao\model\media\MediaService;
use oat\tao\model\TaoOntology;
use oat\taoItems\model\event\ItemCreatedEvent;
Expand All @@ -41,7 +45,6 @@
use oat\taoQtiItem\model\ItemModel;
use oat\taoQtiItem\model\qti\event\UpdatedItemEventDispatcher;
use oat\taoQtiItem\model\qti\exception\QtiModelException;
use oat\taoQtiItem\model\qti\Item;
use oat\taoQtiItem\model\qti\parser\XmlToItemParser;
use oat\taoQtiItem\model\qti\Service;
use oat\taoQtiItem\model\qti\validator\ItemIdentifierValidator;
Expand Down Expand Up @@ -161,7 +164,6 @@ public function getMediaSources()

public function getItemData()
{

$returnValue = [
'itemData' => null
];
Expand All @@ -174,9 +176,9 @@ public function getItemData()
// do not resolve xinclude here, leave it to the client side
$item = Service::singleton()->getDataItemByRdfItem($itemResource, $lang, false);

if (!is_null($item)) {
$returnValue['itemData'] = $item->toArray();
}
$returnValue['itemData'] = $item
? $item->toArray()
: ['identifier' => $this->getItemIdentifier($itemResource)];

$availableLangs = \tao_helpers_I18n::getAvailableLangsByUsage(
new core_kernel_classes_Resource(TaoOntology::PROPERTY_STANCE_LANGUAGE_USAGE_DATA)
Expand Down Expand Up @@ -291,6 +293,10 @@ protected function getCreatorConfig(core_kernel_classes_Resource $item)
$lang = \common_session_SessionManager::getSession()->getDataLanguage();
$config->setProperty('lang', $lang);

// Add support for translation and side-by-side view
$config->setProperty('translation', $this->getRequestParameter('translation'));
$config->setProperty('originResourceUri', $this->getRequestParameter('originResourceUri'));

//base url:
$url = tao_helpers_Uri::url('getFile', 'QtiCreator', 'taoQtiItem', [
'uri' => $item->getUri(),
Expand Down Expand Up @@ -365,4 +371,27 @@ private function validateXmlInput(string $xml)
}
\tao_helpers_Xml::getSimpleXml($xml);
}

private function getItemIdentifier(core_kernel_classes_Resource $item): ?string
{
if ($this->getFeatureFlagChecker()->isEnabled('FEATURE_FLAG_UNIQUE_NUMERIC_QTI_IDENTIFIER')) {
$uniqueId = $item->getOnePropertyValue($this->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER));

if (!empty($uniqueId)) {
return $uniqueId;
}
}

return $this->getIdentifierGenerator()->generate([IdentifierGeneratorInterface::OPTION_RESOURCE => $item]);
}

private function getIdentifierGenerator(): IdentifierGeneratorInterface
{
return $this->getServiceLocator()->getContainer()->get(IdentifierGeneratorProxy::class);
}

private function getFeatureFlagChecker(): FeatureFlagCheckerInterface
{
return $this->getServiceLocator()->getContainer()->get(FeatureFlagChecker::class);
}
}
15 changes: 10 additions & 5 deletions manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@
*
* Copyright (c) 2013-2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* phpcs:disable Generic.Files.LineLength
*/

use oat\tao\model\accessControl\func\AccessRule;
use oat\tao\model\user\TaoRoles;
use oat\taoItems\model\user\TaoItemsRoles;
use oat\taoQtiItem\controller\QtiCreator;
use oat\taoQtiItem\controller\QtiCssAuthoring;
use oat\taoQtiItem\controller\QtiPreview;
use oat\taoQtiItem\install\scripts\addValidationSettings;
use oat\taoQtiItem\install\scripts\ExtendConfigurationRegistry;
use oat\taoQtiItem\install\scripts\SetDragAndDropConfig;
use oat\taoQtiItem\install\scripts\setXMLParserConfig;
use oat\taoQtiItem\model\qti\CustomInteractionAsset\ServiceProvider\{
CustomInteractionAssetExtractorAllocatorServiceProvider
};
use oat\taoQtiItem\model\FeatureFlag\ServiceProvider\FeatureFlagFlaServiceProvider;
use oat\taoQtiItem\model\FeatureFlag\ServiceProvider\FeatureFlagQtiIdentifierServiceProvider;
use oat\taoQtiItem\model\qti\CustomInteractionAsset\ServiceProvider\CustomInteractionAssetExtractorAllocatorServiceProvider;
use oat\taoQtiItem\model\qti\metadata\importer\MetaMetadataServiceProvider;
use oat\taoQtiItem\model\qti\ServiceProvider\IdentifierGenerationStrategyServiceProvider;
use oat\taoQtiItem\model\qti\ServiceProvider\ItemIdentifierValidatorServiceProvider;
use oat\taoQtiItem\model\qti\ServiceProvider\MetadataServiceProvider;
use oat\taoQtiItem\model\qti\ServiceProvider\QtiServiceProvider;
use oat\taoQtiItem\model\Translation\ServiceProvider\TranslationServiceProvider;
use oat\taoQtiItem\model\UniqueId\ServiceProvider\UniqueIdServiceProvider;
use oat\taoQtiItem\scripts\install\AddLabelInjectorForExport;
use oat\taoQtiItem\scripts\install\InitMetadataService;
use oat\taoQtiItem\scripts\install\ItemEventRegister;
Expand All @@ -47,8 +51,6 @@
use oat\taoQtiItem\scripts\install\SetupQtiMetadataImportExportService;
use oat\taoQtiItem\scripts\install\SetUpQueueTasks;
use oat\taoQtiItem\scripts\update\Updater;
use oat\taoItems\model\user\TaoItemsRoles;
use oat\tao\model\accessControl\func\AccessRule;

$extpath = __DIR__ . DIRECTORY_SEPARATOR;
$taopath = dirname(__FILE__, 2) . DIRECTORY_SEPARATOR . 'tao' . DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -209,5 +211,8 @@
MetaMetadataServiceProvider::class,
IdentifierGenerationStrategyServiceProvider::class,
FeatureFlagQtiIdentifierServiceProvider::class,
UniqueIdServiceProvider::class,
TranslationServiceProvider::class,
QtiServiceProvider::class,
],
];
75 changes: 75 additions & 0 deletions migrations/Version202410311205211101_taoQtiItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace oat\taoQtiItem\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\taoItems\model\event\ItemCreatedEvent;
use oat\taoItems\model\event\ItemDuplicatedEvent;
use oat\taoQtiItem\model\event\ItemImported;
use oat\taoQtiItem\model\UniqueId\Listener\ItemCreationListener;

/**
* phpcs:disable Squiz.Classes.ValidClassName
*/
final class Version202410311205211101_taoQtiItem extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
/** @var EventManager $eventManager */
$eventManager = $this->getServiceManager()->get(EventManager::SERVICE_ID);

$eventManager->attach(
ItemCreatedEvent::class,
[ItemCreationListener::class, 'populateUniqueId']
);
$eventManager->attach(
ItemImported::class,
[ItemCreationListener::class, 'populateUniqueId']
);
$eventManager->attach(
ItemDuplicatedEvent::class,
[ItemCreationListener::class, 'populateUniqueId']
);
$eventManager->attach(
InstanceCopiedEvent::class,
[ItemCreationListener::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(
ItemCreatedEvent::class,
[ItemCreationListener::class, 'populateUniqueId']
);
$eventManager->detach(
ItemImported::class,
[ItemCreationListener::class, 'populateUniqueId']
);
$eventManager->detach(
ItemDuplicatedEvent::class,
[ItemCreationListener::class, 'populateUniqueId']
);
$eventManager->detach(
InstanceCopiedEvent::class,
[ItemCreationListener::class, 'populateUniqueId']
);

$this->getServiceManager()->register(EventManager::SERVICE_ID, $eventManager);
}
}
61 changes: 61 additions & 0 deletions model/Translation/Service/QtiLanguageRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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 QtiLanguageRetriever
{
private Service $qtiItemService;
private LoggerInterface $logger;

public function __construct(Service $qtiItemService, LoggerInterface $logger)
{
$this->qtiItemService = $qtiItemService;
$this->logger = $logger;
}

public function __invoke(core_kernel_classes_Resource $item): ?string
{
try {
$itemData = $this->qtiItemService->getDataItemByRdfItem($item);
} catch (Throwable $exception) {
$this->logger->error(
sprintf(
'An error occurred while retrieving item data: %s. Trace: %s',
$exception->getMessage(),
$exception->getTraceAsString()
)
);

throw $exception;
}

return $itemData && $itemData->hasAttribute('xml:lang')
? $itemData->getAttributeValue('xml:lang')
: null;
}
}
95 changes: 95 additions & 0 deletions model/Translation/Service/QtiLanguageSetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\generis\model\data\Ontology;
use oat\tao\model\TaoOntology;
use oat\taoQtiItem\model\qti\Service;
use Psr\Log\LoggerInterface;
use tao_models_classes_LanguageService;
use Throwable;

class QtiLanguageSetter
{
private Service $qtiItemService;
private LoggerInterface $logger;
private Ontology $ontology;
private tao_models_classes_LanguageService $languageService;

public function __construct(
Service $qtiItemService,
LoggerInterface $logger,
Ontology $ontology,
tao_models_classes_LanguageService $languageService
) {
$this->qtiItemService = $qtiItemService;
$this->logger = $logger;
$this->ontology = $ontology;
$this->languageService = $languageService;
}

public function __invoke(core_kernel_classes_Resource $item): core_kernel_classes_Resource
{
try {
$itemData = $this->qtiItemService->getDataItemByRdfItem($item);
} catch (Throwable $exception) {
$this->logger->error(
sprintf(
'An error occurred while retrieving item data: %s. Trace: %s',
$exception->getMessage(),
$exception->getTraceAsString()
)
);

throw $exception;
}

if (!$itemData) {
$this->logger->info(sprintf('There is no item data for item %s.', $item->getUri()));

return $item;
}

$languageProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_LANGUAGE);
$language = $item->getOnePropertyValue($languageProperty);

if (empty($language)) {
$this->logger->info(sprintf('There is no language for item %s.', $item->getUri()));

return $item;
}

$localeCode = str_replace(TaoOntology::LANGUAGE_PREFIX, '', $language->getUri());

if ($this->languageService->isRtlLanguage($localeCode)) {
$itemData->getBody()->setAttribute('dir', 'rtl');
}

$itemData->setAttribute('xml:lang', $localeCode);

$this->qtiItemService->saveDataItemToRdfItem($itemData, $item);

return $item;
}
}
Loading

0 comments on commit 9d6aa41

Please sign in to comment.