diff --git a/models/classes/class.QtiTestService.php b/models/classes/class.QtiTestService.php index f7de74c2f..54e341cc5 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,12 +1571,21 @@ 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 (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(); + } + if (count($labelMetadata) > 1) { common_Logger::w('Multiple labels found for test. Using the first one.'); }