Skip to content

Commit

Permalink
Merge pull request #2677 from oat-sa/fix/AUT-4078/import-items
Browse files Browse the repository at this point in the history
fix: improve importing items using cli and queue. Link translated ite…
  • Loading branch information
tikhanovichA authored Feb 13, 2025
2 parents 61906ea + 6b1de72 commit 8aaf638
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 11 deletions.
10 changes: 7 additions & 3 deletions model/tasks/ImportQtiItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ImportQtiItem extends AbstractTaskAction implements \JsonSerializable
public const PARAM_VALIDATORS = 'enableMetadataValidators';
public const PARAM_ITEM_MUST_EXIST = 'itemMustExist';
public const PARAM_ITEM_MUST_BE_OVERWRITTEN = 'itemMustBeOverwritten';
public const PARAM_ITEM_METADATA = 'importMetadata';

protected $service;

Expand Down Expand Up @@ -71,7 +72,8 @@ public function __invoke($params)
(isset($params[self::PARAM_GUARDIANS])) ? $params[self::PARAM_GUARDIANS] : true,
(isset($params[self::PARAM_VALIDATORS])) ? $params[self::PARAM_VALIDATORS] : true,
(isset($params[self::PARAM_ITEM_MUST_EXIST])) ? $params[self::PARAM_ITEM_MUST_EXIST] : false,
$params[self::PARAM_ITEM_MUST_BE_OVERWRITTEN] ?? false
$params[self::PARAM_ITEM_MUST_BE_OVERWRITTEN] ?? false,
$params[self::PARAM_ITEM_METADATA] ?? false
);
}

Expand Down Expand Up @@ -102,7 +104,8 @@ public static function createTask(
$enableMetadataGuardians = true,
$enableMetadataValidators = true,
$itemMustExist = false,
$itemMustBeOverwritten = false
$itemMustBeOverwritten = false,
$itemMetadata = false
) {
$action = new self();
$action->setServiceLocator($serviceManager);
Expand All @@ -120,7 +123,8 @@ public static function createTask(
self::PARAM_GUARDIANS => $enableMetadataGuardians,
self::PARAM_VALIDATORS => $enableMetadataValidators,
self::PARAM_ITEM_MUST_EXIST => $itemMustExist,
self::PARAM_ITEM_MUST_BE_OVERWRITTEN => $itemMustBeOverwritten
self::PARAM_ITEM_MUST_BE_OVERWRITTEN => $itemMustBeOverwritten,
self::PARAM_ITEM_METADATA => $itemMetadata
],
__('Import QTI ITEM into "%s"', $class->getLabel())
);
Expand Down
43 changes: 35 additions & 8 deletions scripts/cli/importItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use oat\taoQtiItem\model\qti\exception\ExtractException;
use oat\taoQtiItem\model\qti\exception\ParsingException;
use oat\taoQtiItem\model\qti\ImportService;
use oat\taoQtiItem\model\tasks\ImportQtiItem;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

Expand All @@ -56,6 +57,7 @@ class importItems implements Action, ServiceLocatorAwareInterface
protected $recurse = false;
protected $directoryToClass = false;
protected $processed = 0;
protected $async = false;

/**
* @param array $params
Expand Down Expand Up @@ -101,6 +103,10 @@ public function __invoke($params = [])
$this->rollbackOnWarning = true;
break;

case '-a':
$this->async = true;
break;

default:
if (file_exists($param)) {
$fileName = $param;
Expand All @@ -122,6 +128,7 @@ public function __invoke($params = [])
. "\t -e\t\t Rollback on error\n"
. "\t -w\t\t Rollback on warning\n"
. "\t -h\t\t Show this help\n"
. "\t -a\t\t Async import\n"
);
}

Expand Down Expand Up @@ -285,13 +292,33 @@ protected function importPackage($fileName, $class)

try {
$importService = ImportService::singleton();
$report = $importService->importQTIPACKFile(
$fileName,
$class,
true,
$this->rollbackOnError,
$this->rollbackOnWarning
);
if (!$this->async) {
$report = $importService->importQTIPACKFile(
$fileName,
$class,
true,
$this->rollbackOnError,
$this->rollbackOnWarning,
true,
true,
false,
false,
true
);
} else {
$task = ImportQtiItem::createTask(
$fileName,
$class,
$this->getServiceLocator(),
true,
true,
false,
false,
true
);

$report = new Report(Report::TYPE_INFO, printf('Task %s created', $task->getId()));
}
} catch (ExtractException $e) {
$report = common_report_Report::createFailure(
__('The ZIP archive containing the IMS QTI Item cannot be extracted.')
Expand All @@ -309,7 +336,7 @@ protected function importPackage($fileName, $class)
helpers_TimeOutHelper::reset();

$this->showReport($report);
$this->processed ++;
$this->processed++;

return new Report($report->getType());
}
Expand Down
195 changes: 195 additions & 0 deletions scripts/cli/linkTranslatedItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?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) 2025 (original work) Open Assessment Technologies SA ;
*
*/

namespace oat\taoQtiItem\scripts\cli;

use oat\generis\model\GenerisRdf;
use oat\oatbox\reporting\Report;
use oat\generis\model\OntologyAwareTrait;
use oat\generis\model\OntologyRdf;
use oat\oatbox\extension\script\ScriptAction;
use oat\tao\model\TaoOntology;
use taoItems_models_classes_ItemsService as ItemService;

/**
* Class linkTranslatedItems
*
* php index.php '\oat\taoQtiItem\scripts\cli\linkTranslatedItems'
*
* @package oat\taoQtiItem\scripts\cli
*/
class linkTranslatedItems extends ScriptAction
{
use OntologyAwareTrait;

private $wetRun = false;
private $classUri = '';
private $mainItemLanguage = '';
private $report;

protected function provideOptions()
{
return [
'wet-run' => [
'prefix' => 'w',
'flag' => true,
'longPrefix' => 'wet-run',
'description' => 'Find and remove all orphan triples related to files for removed items.',
],
'class' => [
'prefix' => 'c',
'longPrefix' => 'class',
'description' => 'Class to link translations',
'required' => true
],
'main-language' => [
'prefix' => 'm',
'longPrefix' => 'main-language',
'description' => 'Item with this language will be considered as main item',
'required' => false,
'default' => 'en-US'
]

];
}

protected function provideDescription()
{
return 'Tool to remove orphan files attached to removed items. By default in dry-run';
}

protected function provideUsage()
{
return [
'prefix' => 'h',
'longPrefix' => 'help',
'description' => 'Prints a help statement'
];
}
public function run()
{
$this->report = Report::createInfo('Starting linking translations');
$this->init();

$class = $this->getClass($this->classUri);

$this->report->add(new Report(Report::TYPE_INFO, 'Linking translations for class ' . $class->getLabel()));

$aliasProperty = $this->getProperty(GenerisRdf::PROPERTY_ALIAS);
$classProperties = $class->getProperties(true);

foreach ($classProperties as $property) {
$aliasName = (string)$property->getOnePropertyValue($aliasProperty);

if (empty($aliasName)) {
continue;
}

if ($aliasName === 'taoImportedUniqueIdentifier') {
$uniqueIdentifierProperty = $property->getUri();
break;
}
}

$items = $this->getItemService()->getRootClass()->searchInstances(
[
TaoOntology::PROPERTY_TRANSLATION_TYPE => TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_ORIGINAL,
TaoOntology::PROPERTY_LANGUAGE => TaoOntology::LANGUAGE_PREFIX . $this->mainItemLanguage
],
['like' => false, 'recursive' => true]
);
$mainItem = 0;
$translations = 0;

foreach ($items as $item) {
$importedUniqueId = $item->getOnePropertyValue($this->getProperty($uniqueIdentifierProperty));
if (!$this->wetRun) {
$item->editPropertyValues(
$this->getProperty(TaoOntology::PROPERTY_TRANSLATION_STATUS),
TaoOntology::PROPERTY_VALUE_TRANSLATION_STATUS_READY
);
$item->editPropertyValues(
$this->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER),
$importedUniqueId
);
}
$mainItem++;
$translatedLanguages = $item->getPropertyValues(
$this->getProperty(TaoOntology::PROPERTY_TRANSLATED_INTO_LANGUAGES)
);
foreach ($translatedLanguages as $lang) {
$linkedItems = $this->getItemService()->getRootClass()->searchInstances(
[
TaoOntology::PROPERTY_LANGUAGE => $lang,
$uniqueIdentifierProperty => $importedUniqueId
],
['like' => false, 'recursive' => true]
);
foreach ($linkedItems as $linkedItem) {
$translations++;
if ($this->wetRun) {
$linkedItem->editPropertyValues(
$this->getProperty(TaoOntology::PROPERTY_TRANSLATION_STATUS),
TaoOntology::PROPERTY_VALUE_TRANSLATION_STATUS_READY
);
$linkedItem->editPropertyValues(
$this->getProperty(TaoOntology::PROPERTY_TRANSLATION_TYPE),
TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_TRANSLATION
);
$linkedItem->setPropertyValue(
$this->getProperty(TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI),
$item->getUri()
);
$linkedItem->setPropertyValue(
$this->getProperty(TaoOntology::PROPERTY_TRANSLATION_PROGRESS),
TaoOntology::PROPERTY_VALUE_TRANSLATION_PROGRESS_TRANSLATED
);
$linkedItem->editPropertyValues(
$this->getProperty(OntologyRdf::RDF_TYPE),
TaoOntology::CLASS_URI_ITEM
);
}
}
}
}
$this->report->add(
new Report(
Report::TYPE_SUCCESS,
'Linked ' . $translations . ' translations to ' . $mainItem . ' main items'
)
);
return $this->report;
}

private function init()
{
if ($this->getOption('wet-run')) {
$this->wetRun = true;
}
$this->classUri = $this->getOption('class');
$this->mainItemLanguage = $this->getOption('main-language');
}

protected function getItemService(): ItemService
{
return $this->getServiceLocator()->get(ItemService::class);
}
}

0 comments on commit 8aaf638

Please sign in to comment.