diff --git a/actions/class.RestQtiTests.php b/actions/class.RestQtiTests.php index 859f50824..d875f78c8 100644 --- a/actions/class.RestQtiTests.php +++ b/actions/class.RestQtiTests.php @@ -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'; /** @@ -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) { @@ -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 */ @@ -155,8 +184,9 @@ public function importDeferred() $this->isMetadataValidatorsEnabled(), $this->isItemMustExistEnabled(), $this->isItemMustBeOverwrittenEnabled(), - $this->getOverwriteTestUri(), - $this->getItemClassUri() + $this->isOverwriteTest(), + $this->getItemClassUri(), + $this->getOverwriteTestUri() ); $result = [ diff --git a/models/classes/class.CrudQtiTestsService.php b/models/classes/class.CrudQtiTestsService.php index 45c9c73b9..b70c4d1b8 100644 --- a/models/classes/class.CrudQtiTestsService.php +++ b/models/classes/class.CrudQtiTestsService.php @@ -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 @@ -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; diff --git a/models/classes/class.QtiTestService.php b/models/classes/class.QtiTestService.php index 5f81a9e86..3fcee714c 100644 --- a/models/classes/class.QtiTestService.php +++ b/models/classes/class.QtiTestService.php @@ -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); @@ -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); @@ -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); @@ -623,8 +626,9 @@ 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) { @@ -632,8 +636,10 @@ protected function importTest( } } - $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); @@ -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. diff --git a/models/classes/import/QtiTestImporter.php b/models/classes/import/QtiTestImporter.php index 1fd5bb6c2..f1cb86bd5 100644 --- a/models/classes/import/QtiTestImporter.php +++ b/models/classes/import/QtiTestImporter.php @@ -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( @@ -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, @@ -64,8 +66,9 @@ public function import( $enableValidators, $itemMustExist, $itemMustBeOverwritten, - $overwriteTestUri, - $itemClassUri + $overwriteTest, + $itemClassUri, + $overwriteTestUri ); } } diff --git a/models/classes/tasks/ImportQtiTest.php b/models/classes/tasks/ImportQtiTest.php index 8cb1f903f..bf2b7e321 100644 --- a/models/classes/tasks/ImportQtiTest.php +++ b/models/classes/tasks/ImportQtiTest.php @@ -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; @@ -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; @@ -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()); @@ -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()) );