Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Delete old assets on test overwrite #2393

Merged
merged 43 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
793c8f4
feat: Delete old assets on test overwrite
hectoras Jul 6, 2023
e7a080d
chore: Cleanup
hectoras Jul 6, 2023
d23842b
chore: Cleanup
hectoras Jul 6, 2023
6e0c88c
chore: Cleanup
hectoras Jul 6, 2023
1b35bfd
chore: Cleanup
hectoras Jul 6, 2023
67b11d2
chore: Cleanup
hectoras Jul 6, 2023
5b396b0
fix: Invert condition
hectoras Jul 6, 2023
13c9113
chore: Rollback code style changes
hectoras Jul 10, 2023
883af73
chore: Rollback coding style changes
hectoras Jul 10, 2023
7bfe57e
chore: Rollback coding style changes
hectoras Jul 10, 2023
1751447
chore: Rollback coding style changes
hectoras Jul 10, 2023
1a7cb58
chore: Rollback coding style changes
hectoras Jul 10, 2023
06a65c3
test: Update unit test test
hectoras Jul 10, 2023
d8c3221
chore: Remove dummy test
hectoras Jul 10, 2023
565f86c
chore: Use integration events to trigger deletion of related assets
hectoras Jul 10, 2023
7df8ec0
chore: Remove TestDeleter
hectoras Jul 10, 2023
ef42604
chore: Cleanup
hectoras Jul 10, 2023
dcbc2f9
chore: Cleanup
hectoras Jul 10, 2023
21145a7
chore: Cleanup
hectoras Jul 10, 2023
e838b14
chore: Cleanup
hectoras Jul 10, 2023
effa31a
fix: Fix broken test
hectoras Jul 11, 2023
83ea87b
chore: Fix PSR-12 issue
hectoras Jul 11, 2023
4c0687e
chore: Address linter issues related to method length
hectoras Jul 11, 2023
c1f66bd
chore: Cleanup
hectoras Jul 11, 2023
bf0d33a
feat: Remove possible duplicated URIs from QtiTestDeletedEvent params
hectoras Jul 11, 2023
4f9d728
chore: Add missing license header
hectoras Jul 12, 2023
286922e
chore: Add missing license header
hectoras Jul 12, 2023
a30eabd
chore: Coding style fixes
hectoras Jul 12, 2023
fc71942
refactor: Collect referenced resources and item classes to delete in …
hectoras Jul 12, 2023
21e1d65
chore: Shorten var name on auxiliary method
hectoras Jul 12, 2023
b7adabb
chore: Delete test before collecting its URI
hectoras Jul 12, 2023
012a459
chore: Add doc blocks with variable type hints
hectoras Jul 12, 2023
87436f8
chore: Use DI container to get services in QtiTestService
hectoras Jul 12, 2023
a1c24cf
feat: Rename event, trigger event before the deletions
hectoras Jul 12, 2023
0dceb0f
chore: Remove PHPDoc var type declarations
hectoras Jul 13, 2023
5d38559
feat: Remove logic to extract media references form this extension
hectoras Jul 13, 2023
5c48617
chore: Remove QtiTestsDeletedEvent that is not used anymore
hectoras Jul 13, 2023
237b442
chore: Move subclasses' items deletion to its own method
hectoras Jul 13, 2023
708ab7b
chore: Update oat-sa/extension-tao-item version in composer.json
hectoras Jul 14, 2023
4b90f13
fix: dont delete classes whose label don't match the expected label
hectoras Jul 14, 2023
cabc3f4
chore: force higher dependency version to fix pipeline
gabrielfs7 Jul 14, 2023
8e376d2
chore: use delete command
gabrielfs7 Jul 14, 2023
185b15d
chore: Point to released version of extension-tao-item
hectoras Jul 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion model/Container/TestQtiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\oatbox\event\EventManager;
use oat\oatbox\log\LoggerService;
use oat\taoQtiItem\model\qti\Service;
use oat\tao\model\resources\Service\ClassDeleter;
use oat\taoMediaManager\model\Specification\MediaClassSpecification;
use oat\taoQtiItem\model\qti\parser\ElementReferencesExtractor;
use oat\taoQtiItem\model\qti\Service as QtiItemService;
use oat\taoQtiTest\model\Domain\Model\ItemResponseRepositoryInterface;
use oat\taoQtiTest\model\Domain\Model\QtiTestRepositoryInterface;
use oat\taoQtiTest\model\Domain\Model\ToolsStateRepositoryInterface;
Expand All @@ -43,6 +46,7 @@
use oat\taoQtiTest\model\Service\StoreTraceVariablesService;
use oat\taoQtiTest\model\Service\TimeoutService;
use oat\taoQtiTest\models\runner\QtiRunnerService;
use oat\taoQtiTest\models\test\TestDeleter;
use oat\taoQtiTest\models\TestModelService;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand Down Expand Up @@ -156,5 +160,19 @@ public function __invoke(ContainerConfigurator $configurator): void
service(TestModelService::SERVICE_ID),
]
);

$services
->set(TestDeleter::class, TestDeleter::class)
->public()
->args(
[
service(LoggerService::SERVICE_ID),
service(Ontology::SERVICE_ID),
service(QtiItemService::class),
service(ClassDeleter::class),
service(ElementReferencesExtractor::class),
service(MediaClassSpecification::class)->nullOnInvalid(),
]
);
}
}
89 changes: 43 additions & 46 deletions models/classes/class.QtiTestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
* 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) 2013-2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* Copyright (c) 2013-2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
gabrielfs7 marked this conversation as resolved.
Show resolved Hide resolved
*/

use League\Flysystem\FileExistsException;
use oat\oatbox\filesystem\Directory;
use oat\oatbox\filesystem\File;
use oat\oatbox\filesystem\FileSystemService;
use oat\oatbox\service\ServiceNotFoundException;
use oat\tao\model\metadata\exception\MetadataImportException;
use oat\tao\model\resources\ResourceAccessDeniedException;
use oat\tao\model\resources\SecureResourceServiceInterface;
use oat\tao\model\TaoOntology;
use oat\taoBackOffice\model\tree\TreeService;
use oat\taoQtiItem\model\qti\ImportService;
use oat\taoQtiItem\model\qti\metadata\importer\MetadataImporter;
use oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue;
use oat\taoQtiItem\model\qti\metadata\MetadataGuardianResource;
use oat\taoQtiItem\model\qti\metadata\MetadataService;
use oat\taoQtiItem\model\qti\Resource;
Expand All @@ -39,25 +40,28 @@
use oat\taoQtiTest\models\metadata\MetadataTestContextAware;
use oat\taoQtiTest\models\render\QtiPackageImportPreprocessing;
use oat\taoQtiTest\models\test\AssessmentTestXmlFactory;
use oat\taoQtiTest\models\test\TestDeleter;
use oat\taoTests\models\event\TestUpdatedEvent;
use Psr\Container\ContainerInterface;
use qtism\common\utils\Format;
use qtism\data\AssessmentItemRef;
use qtism\data\QtiComponentCollection;
use qtism\data\SectionPartCollection;
use qtism\data\storage\StorageException;
use qtism\data\storage\xml\marshalling\MarshallingException;
use qtism\data\storage\xml\marshalling\UnmarshallingException;
use qtism\data\storage\xml\XmlDocument;
use qtism\data\storage\xml\XmlStorageException;
use taoTests_models_classes_TestsService as TestService;


/**
* the QTI TestModel service.
*
* @author Joel Bout <joel@taotesting.com>
* @author Bertrand Chevrier <bertrand@taotesting.com>
* @author Jerome Bogaerts <jerome@taotesting.com>
* @package taoQtiTest

*/
class taoQtiTest_models_classes_QtiTestService extends TestService
{
Expand Down Expand Up @@ -341,14 +345,14 @@ private function generateIdentifier(XmlDocument $doc, $qtiType, $offset = 1)
* @throws common_exception
* @throws common_exception_Error
* @throws common_exception_FileSystemError
* @throws oat\oatbox\service\ServiceNotFoundException
*/
public function importMultipleTests(
core_kernel_classes_Class $targetClass,
$file,
bool $overwriteTest = false,
?string $itemClassUri = null
) {

$testClass = $targetClass;
$report = new common_report_Report(common_report_Report::TYPE_INFO);
$validPackage = false;
Expand All @@ -362,7 +366,8 @@ public function importMultipleTests(
$report->add($preprocessingReport);
}

// Validate the given IMS Package itself (ZIP integrity, presence of an 'imsmanifest.xml' file.
// Validate the given IMS Package itself (ZIP integrity, presence of an 'imsmanifest.xml' file).
//
// phpcs:disable Generic.Files.LineLength
$invalidArchiveMsg = __("The provided archive is invalid. Make sure it is not corrupted and that it contains an 'imsmanifest.xml' file.");
// phpcs:enable Generic.Files.LineLength
Expand All @@ -375,8 +380,8 @@ public function importMultipleTests(
$report->add(common_report_Report::createFailure($invalidArchiveMsg));
}

// Validate the manifest (well formed XML, valid against the schema).
if ($validPackage === true) {
// Validate the manifest (well-formed XML, valid against the schema).
if ($validPackage) {
$folder = $qtiPackageParser->extract();

if (is_dir($folder) === false) {
Expand All @@ -394,9 +399,9 @@ public function importMultipleTests(
$tests = array_merge($tests, $qtiManifestParser->getResources($type));
}

$testsFound = (count($tests) !== 0);
$testsFound = (count($tests) > 0);

if ($testsFound !== true) {
if (!$testsFound) {
$report->add(
common_report_Report::createFailure(
// phpcs:disable Generic.Files.LineLength
Expand Down Expand Up @@ -520,13 +525,21 @@ public function clearRelatedResources(common_report_Report $report): void
* Import a QTI Test and its dependent Items into the TAO Platform.
*
* @param core_kernel_classes_Class $targetClass The RDFS Class where Ontology resources must be created.
* @param oat\taoQtiItem\model\qti\Resource $qtiTestResource The QTI Test Resource representing the IMS QTI Test to
* be imported.
* @param Resource $qtiTestResource The QTI Test Resource representing the IMS QTI Test to be imported.
* @param taoQtiTest_models_classes_ManifestParser $manifestParser The parser used to retrieve the IMS Manifest.
* @param string $folder The absolute path to the folder where the IMS archive containing the test content
* @param oat\taoQtiItem\model\qti\Resource[] $ignoreQtiResources An array of QTI Manifest Resources to be ignored
* at import time.
* @param Resource[] $ignoreQtiResources An array of QTI Manifest Resources to be ignored at import time.
* @param bool $overwriteTest
* @param string|null $itemClassUri
*
* @return common_report_Report A report about how the importation behaved.
*
* @throws ServiceNotFoundException
* @throws MetadataImportException
* @throws MarshallingException
* @throws common_Exception
* @throws common_exception_Error
* @throws taoQtiTest_models_classes_QtiTestServiceException
*/
protected function importTest(
core_kernel_classes_Class $targetClass,
Expand Down Expand Up @@ -613,14 +626,20 @@ protected function importTest(
if ($overwriteTest) {
$testLabel = $testDefinition->getDocumentComponent()->getTitle();
$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();

foreach ($reportCtx->testMetadata as $metadataValue) {
/** @var SimpleMetadataValue $metadataValue */
if (($metadataValue->getPath()[1] ?? '') === RDFS_LABEL) {
$testLabel = $metadataValue->getValue();
}
}

$this->deleteTestsFromClassByLabel($testLabel, $itemsClassLabel, $testClass, $itemClass);
$this->getTestDeleter()->deleteTestsFromClassByLabel(
$testLabel,
$itemsClassLabel,
$testClass,
$itemClass
);
}

$targetClass = $itemClass->createSubClass($testResource->getLabel());
Expand Down Expand Up @@ -727,7 +746,7 @@ protected function importTest(
// phpcs:enable Generic.Files.LineLength
}
} else {
// Ignored (possibily because imported in another test of the same package).
// Ignored (possibly because imported in another test of the same package).
$reportCtx->items[$assessmentItemRefId] = $ignoreQtiResources[$resourceIdentifier];
$report->add(
new common_report_Report(
Expand Down Expand Up @@ -866,28 +885,6 @@ protected function importTest(
return $report;
}

private function deleteTestsFromClassByLabel(
string $testLabel,
string $itemsClassLabel,
core_kernel_classes_Resource $testClass,
core_kernel_classes_Class $itemClass
): void {
$testService = $this->getTestService();
$itemTreeService = $this->getItemTreeService();

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

foreach ($itemClass->getSubClasses() as $subClass) {
if ($subClass->getLabel() === $itemsClassLabel) {
$itemTreeService->deleteClass($subClass);
}
}
}

/**
* 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 Expand Up @@ -1454,13 +1451,13 @@ private function getQtiPackageImportPreprocessing()
return $this->getServiceLocator()->get(QtiPackageImportPreprocessing::SERVICE_ID);
}

private function getItemTreeService(): taoItems_models_classes_ItemsService
private function getTestDeleter(): TestDeleter
{
return taoItems_models_classes_ItemsService::singleton();
return $this->getPsrContainer()->get(TestDeleter::class);
}

private function getTestService(): taoTests_models_classes_TestsService
private function getPsrContainer(): ContainerInterface
{
return taoTests_models_classes_TestsService::singleton();
return $this->getServiceLocator()->getContainer();
}
}
Loading