From 4c0eab2ea5b665b077e00cc232e3d33abc468d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Marsza=C5=82?= Date: Mon, 25 Nov 2024 10:37:57 +0100 Subject: [PATCH 1/2] fix: when no metadata in package is included use assesment title as label --- models/classes/class.QtiTestService.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/models/classes/class.QtiTestService.php b/models/classes/class.QtiTestService.php index f7de74c2f..8e393053d 100644 --- a/models/classes/class.QtiTestService.php +++ b/models/classes/class.QtiTestService.php @@ -629,7 +629,7 @@ protected function importTest( // If any, assessmentSectionRefs will be resolved and included as part of the main test definition. $testDefinition->includeAssessmentSectionRefs(true); - $testLabel = $packageLabel ?? $this->getTestLabel($reportCtx->testMetadata); + $testLabel = $packageLabel ?? $this->getTestLabel($reportCtx->testMetadata, $testDefinition); if ($overwriteTestUri || $overwriteTest) { $itemsClassLabel = $testLabel; @@ -1571,16 +1571,25 @@ private function createTestIdentifier(core_kernel_classes_Resource $test): strin return str_replace('_', '-', Format::sanitizeIdentifier($identifier)); } - private function getTestLabel(array $testMetadata): string + private function getTestLabel(array $testMetadata, XmlDocument $testDefinition): string { $labelMetadata = array_filter($testMetadata, function ($metadata) { return in_array(RDFS_LABEL, $metadata->getPath()); }); + if (count($labelMetadata) === 0) { + if ($testDefinition->getDocumentComponent() === null) { + throw new Exception('No metadata label found for test and no title in the test definition.'); + } + common_Logger::w('No metadata label found for test. Using the title from the test definition.'); + return $testDefinition->getDocumentComponent()->getTitle(); + } + if (count($labelMetadata) > 1) { common_Logger::w('Multiple labels found for test. Using the first one.'); } + return reset($labelMetadata)->getValue(); } } From 96af807e8a5142e4af1508e40997aab0a9d4e460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Marsza=C5=82?= Date: Mon, 25 Nov 2024 10:51:15 +0100 Subject: [PATCH 2/2] fix: code style, small improvements --- models/classes/class.QtiTestService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/classes/class.QtiTestService.php b/models/classes/class.QtiTestService.php index 8e393053d..54e341cc5 100644 --- a/models/classes/class.QtiTestService.php +++ b/models/classes/class.QtiTestService.php @@ -1577,10 +1577,11 @@ private function getTestLabel(array $testMetadata, XmlDocument $testDefinition): return in_array(RDFS_LABEL, $metadata->getPath()); }); - if (count($labelMetadata) === 0) { + if (empty($labelMetadata)) { if ($testDefinition->getDocumentComponent() === null) { throw new Exception('No metadata label found for test and no title in the test definition.'); } + common_Logger::w('No metadata label found for test. Using the title from the test definition.'); return $testDefinition->getDocumentComponent()->getTitle(); } @@ -1589,7 +1590,6 @@ private function getTestLabel(array $testMetadata, XmlDocument $testDefinition): common_Logger::w('Multiple labels found for test. Using the first one.'); } - return reset($labelMetadata)->getValue(); } }