Skip to content

Commit

Permalink
chore: rollback old parameter to avoid breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
shpran committed Aug 5, 2024
1 parent 9baafd9 commit 1bdcdee
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
36 changes: 33 additions & 3 deletions actions/class.RestQtiTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@
class taoQtiTest_actions_RestQtiTests extends AbstractRestQti
{
public const PARAM_PACKAGE_NAME = 'qtiPackage';

private const PARAM_TEST_URI = 'testUri';

private const ITEM_CLASS_URI = 'itemClassUri';

/**
* @deprecated Use taoQtiTest_actions_RestQtiTests::OVERWRITE_TEST_URI instead with the URI of the test to be
* replaced
*/
private const OVERWRITE_TEST = 'overwriteTest';

private const OVERWRITE_TEST_URI = 'overwriteTestUri';

/**
Expand Down Expand Up @@ -98,8 +106,9 @@ public function import()
$this->isMetadataValidatorsEnabled(),
$this->isItemMustExistEnabled(),
$this->isItemMustBeOverwrittenEnabled(),
$this->isOverwriteTest(),
$this->getItemClassUri(),
$this->getOverwriteTestUri(),
$this->getItemClassUri()
);

if ($report->getType() === common_report_Report::TYPE_SUCCESS) {
Expand Down Expand Up @@ -129,6 +138,26 @@ protected function getItemClassUri(): ?string
return $this->getPostParameter(self::ITEM_CLASS_URI);
}

/**
* @throws common_exception_RestApi
*/
protected function isOverwriteTest(): bool
{
$isOverwriteTest = $this->getPostParameter(self::OVERWRITE_TEST);

if (is_null($isOverwriteTest)) {
return false;
}

if (!in_array($isOverwriteTest, ['true', 'false'])) {
throw new \common_exception_RestApi(
'isOverwriteTest parameter should be boolean (true or false).'
);
}

return filter_var($isOverwriteTest, FILTER_VALIDATE_BOOLEAN);
}

/**
* @inheritdoc
*/
Expand All @@ -155,8 +184,9 @@ public function importDeferred()
$this->isMetadataValidatorsEnabled(),
$this->isItemMustExistEnabled(),
$this->isItemMustBeOverwrittenEnabled(),
$this->getOverwriteTestUri(),
$this->getItemClassUri()
$this->isOverwriteTest(),
$this->getItemClassUri(),
$this->getOverwriteTestUri()
);

$result = [
Expand Down
13 changes: 10 additions & 3 deletions models/classes/class.CrudQtiTestsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public function importQtiTest(
$enableMetadataValidators = true,
$itemMustExist = false,
$itemMustBeOverwritten = false,
?string $overwriteTestUri = null,
?string $itemClassUri = null
bool $overwriteTest = false,
?string $itemClassUri = null,
?string $overwriteTestUri = null
) {
try {
//The zip extraction is a long process that can exceed the 30s timeout
Expand All @@ -91,7 +92,13 @@ public function importQtiTest(
$importer->enableItemMustBeOverwritten();
}

$report = $importer->importMultipleTests($class, $uploadedFile, $overwriteTestUri, $itemClassUri);
$report = $importer->importMultipleTests(
$class,
$uploadedFile,
$overwriteTest,
$itemClassUri,
$overwriteTestUri
);
helpers_TimeOutHelper::reset();

return $report;
Expand Down
36 changes: 28 additions & 8 deletions models/classes/class.QtiTestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,10 @@ private function generateIdentifier(XmlDocument $doc, $qtiType, $offset = 1)
public function importMultipleTests(
core_kernel_classes_Class $targetClass,
$file,
?string $overwriteTestUri = null,
bool $overwriteTest = false,
?string $itemClassUri = null,
array $form = []
array $form = [],
?string $overwriteTestUri = null
) {
$testClass = $targetClass;
$report = new common_report_Report(common_report_Report::TYPE_INFO);
Expand Down Expand Up @@ -423,9 +424,10 @@ public function importMultipleTests(
$qtiManifestParser,
$folder,
$alreadyImportedQtiResources,
$overwriteTestUri,
$overwriteTest,
$itemClassUri,
!empty($form[TestImportForm::METADATA_FORM_ELEMENT_NAME]) ?? false
!empty($form[TestImportForm::METADATA_FORM_ELEMENT_NAME]) ?? false,
$overwriteTestUri,
);
$report->add($importTestReport);

Expand Down Expand Up @@ -544,9 +546,10 @@ protected function importTest(
taoQtiTest_models_classes_ManifestParser $manifestParser,
$folder,
array $ignoreQtiResources = [],
?string $overwriteTestUri = null,
bool $overwriteTest = false,
?string $itemClassUri = null,
bool $importMetadata = false
bool $importMetadata = false,
?string $overwriteTestUri = null
) {
/** @var ImportService $itemImportService */
$itemImportService = $this->getServiceLocator()->get(ImportService::SERVICE_ID);
Expand Down Expand Up @@ -623,17 +626,20 @@ protected function importTest(
$testDefinition->includeAssessmentSectionRefs(true);
$testLabel = $testDefinition->getDocumentComponent()->getTitle();

if ($overwriteTestUri) {
if ($overwriteTestUri || $overwriteTest) {
$itemsClassLabel = $testLabel;

/** @var oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue $m */
foreach ($reportCtx->testMetadata as $singleMetadata) {
if (($singleMetadata->getPath()[1] ?? '') === RDFS_LABEL) {
$testLabel = $singleMetadata->getValue();
}
}

$this->getTestService()->deleteTest(new core_kernel_classes_Resource($overwriteTestUri));
$this->deleteItemSubclassesByLabel($itemParentClass, $itemsClassLabel);
$overwriteTestUri
? $this->getTestService()->deleteTest(new core_kernel_classes_Resource($overwriteTestUri))
: $this->deleteTestsFromClassByLabel($testLabel, $testClass);
}

$targetItemClass = $itemParentClass->createSubClass(self::IN_PROGRESS_LABEL);
Expand Down Expand Up @@ -906,6 +912,20 @@ protected function importTest(
return $report;
}

/**
* @throws common_Exception
*/
private function deleteTestsFromClassByLabel(string $testLabel, core_kernel_classes_Resource $testClass): void
{
$testService = $this->getTestService();

foreach ($testClass->getInstances() as $testInstance) {
if ($testInstance->getLabel() === $testLabel) {
$testService->deleteTest($testInstance);
}
}
}

/**
* Import the Test itself by importing its QTI-XML definition into the system, after
* the QTI Items composing the test were also imported.
Expand Down
13 changes: 8 additions & 5 deletions models/classes/import/QtiTestImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class QtiTestImporter extends AbstractTestImporter
* @param bool $enableValidators
* @param bool $itemMustExist
* @param bool $itemMustBeOverwritten
* @param string|null $overwriteTestUri
* @param bool $overwriteTest
* @param string|null $itemClassUri
* @param string|null $overwriteTestUri
* @return common_report_Report
*/
public function import(
Expand All @@ -54,8 +55,9 @@ public function import(
bool $enableValidators = true,
bool $itemMustExist = false,
bool $itemMustBeOverwritten = false,
?string $overwriteTestUri = null,
?string $itemClassUri = null
bool $overwriteTest = false,
?string $itemClassUri = null,
?string $overwriteTestUri = null
) {
return taoQtiTest_models_classes_CrudQtiTestsService::singleton()->importQtiTest(
$file,
Expand All @@ -64,8 +66,9 @@ public function import(
$enableValidators,
$itemMustExist,
$itemMustBeOverwritten,
$overwriteTestUri,
$itemClassUri
$overwriteTest,
$itemClassUri,
$overwriteTestUri
);
}
}
16 changes: 12 additions & 4 deletions models/classes/tasks/ImportQtiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class ImportQtiTest extends AbstractTaskAction implements \JsonSerializable
public const PARAM_ITEM_MUST_EXIST = 'item_must_exist';
public const PARAM_ITEM_MUST_BE_OVERWRITTEN = 'item_must_be_overwritten';
public const PARAM_ITEM_CLASS_URI = 'item_class_uri';
/**
* @deprecated Use oat\taoQtiTest\models\tasks\ImportQtiTest::PARAM_OVERWRITE_TEST_URI instead with the URI of the
* test to be replaced
*/
public const PARAM_OVERWRITE_TEST = 'overwrite_test';
public const PARAM_OVERWRITE_TEST_URI = 'overwrite_test_uri';

protected $service;
Expand Down Expand Up @@ -88,8 +93,9 @@ public function __invoke($params)
$params[self::PARAM_ENABLE_VALIDATORS] ?? true,
$params[self::PARAM_ITEM_MUST_EXIST] ?? false,
$params[self::PARAM_ITEM_MUST_BE_OVERWRITTEN] ?? false,
$params[self::PARAM_OVERWRITE_TEST] ?? false,
$params[self::PARAM_ITEM_CLASS_URI] ?? false,
$params[self::PARAM_OVERWRITE_TEST_URI] ?? null,
$params[self::PARAM_ITEM_CLASS_URI] ?? false
);

return $report;
Expand Down Expand Up @@ -121,8 +127,9 @@ public static function createTask(
$enableValidators = true,
$itemMustExist = false,
$itemMustBeOverwritten = false,
?string $overwriteTestUri = null,
?string $itemClassUri = null
bool $overwriteTest = false,
?string $itemClassUri = null,
?string $overwriteTestUri = null
) {
$action = new self();
$action->setServiceLocator(ServiceManager::getServiceManager());
Expand All @@ -141,8 +148,9 @@ public static function createTask(
self::PARAM_ENABLE_VALIDATORS => $enableValidators,
self::PARAM_ITEM_MUST_EXIST => $itemMustExist,
self::PARAM_ITEM_MUST_BE_OVERWRITTEN => $itemMustBeOverwritten,
self::PARAM_OVERWRITE_TEST_URI => $overwriteTestUri,
self::PARAM_OVERWRITE_TEST => $overwriteTest,
self::PARAM_ITEM_CLASS_URI => $itemClassUri,
self::PARAM_OVERWRITE_TEST_URI => $overwriteTestUri,
],
__('Import QTI TEST into "%s"', $class->getLabel())
);
Expand Down

0 comments on commit 1bdcdee

Please sign in to comment.