Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.88% |
31 / 32 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| LabelBasedLomOntologyClassificationExtractor | |
96.88% |
31 / 32 |
|
50.00% |
1 / 2 |
12 | |
0.00% |
0 / 1 |
| extract | |
96.00% |
24 / 25 |
|
0.00% |
0 / 1 |
7 | |||
| getResourceValue | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2024 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\qti\metadata\ontology; |
| 23 | |
| 24 | use core_kernel_classes_Resource as Resource; |
| 25 | use core_kernel_classes_Triple as Triple; |
| 26 | use oat\generis\model\OntologyAwareTrait; |
| 27 | use oat\generis\model\OntologyRdf; |
| 28 | use oat\taoQtiItem\model\qti\metadata\imsManifest\classificationMetadata\ClassificationEntryMetadataValue; |
| 29 | use oat\taoQtiItem\model\qti\metadata\imsManifest\classificationMetadata\ClassificationMetadataValue; |
| 30 | use oat\taoQtiItem\model\qti\metadata\imsManifest\classificationMetadata\ClassificationSourceMetadataValue; |
| 31 | use oat\taoQtiItem\model\qti\metadata\MetadataExtractionException; |
| 32 | use oat\taoQtiItem\model\qti\metadata\MetadataExtractor; |
| 33 | use tao_helpers_Uri; |
| 34 | use taoTests_models_classes_TestsService; |
| 35 | use oat\taoItems\model\TaoItemOntology; |
| 36 | |
| 37 | class LabelBasedLomOntologyClassificationExtractor implements MetadataExtractor |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | |
| 41 | public const EXCLUDED_PROPERTIES = [ |
| 42 | OntologyRdf::RDF_TYPE, |
| 43 | TaoItemOntology::PROPERTY_ITEM_CONTENT, |
| 44 | TaoItemOntology::PROPERTY_ITEM_MODEL, |
| 45 | taoTests_models_classes_TestsService::PROPERTY_TEST_TESTMODEL, |
| 46 | taoTests_models_classes_TestsService::PROPERTY_TEST_CONTENT, |
| 47 | ]; |
| 48 | |
| 49 | /** |
| 50 | * Extract resource metadata and transform it to ClassificationMetadataValue |
| 51 | * |
| 52 | * @param Resource $resource |
| 53 | * |
| 54 | * @return array |
| 55 | * |
| 56 | * @throws MetadataExtractionException |
| 57 | * @throws \oat\tao\model\metadata\exception\writer\MetadataWriterException |
| 58 | */ |
| 59 | public function extract($resource): array |
| 60 | { |
| 61 | if (!$resource instanceof Resource) { |
| 62 | throw new MetadataExtractionException( |
| 63 | __('The given target is not an instance of core_kernel_classes_Resource') |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | $resourceUri = $resource->getUri(); |
| 68 | $identifier = tao_helpers_Uri::getUniqueId($resourceUri); |
| 69 | $metadata = [ |
| 70 | $identifier => [] |
| 71 | ]; |
| 72 | |
| 73 | /** @var Triple $triple */ |
| 74 | foreach ($resource->getRdfTriples() as $triple) { |
| 75 | $property = $this->getProperty($triple->predicate); |
| 76 | $value = $this->getResourceValue($triple); |
| 77 | $propertyUri = $property->getUri(); |
| 78 | |
| 79 | if ( |
| 80 | trim($value) !== '' |
| 81 | && $property->isProperty() |
| 82 | && !in_array($propertyUri, self::EXCLUDED_PROPERTIES) |
| 83 | ) { |
| 84 | $metadata[$identifier][] = new ClassificationMetadataValue( |
| 85 | new ClassificationSourceMetadataValue($resourceUri, $propertyUri), |
| 86 | [ |
| 87 | new ClassificationEntryMetadataValue($resourceUri, $value) |
| 88 | ] |
| 89 | ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (empty($metadata[$identifier])) { |
| 94 | return []; |
| 95 | } |
| 96 | |
| 97 | return $metadata; |
| 98 | } |
| 99 | |
| 100 | private function getResourceValue(Triple $triple): string |
| 101 | { |
| 102 | if ($triple->object === null) { |
| 103 | return ''; |
| 104 | } |
| 105 | |
| 106 | if ( |
| 107 | !empty($triple->object) && |
| 108 | $this->getResource($triple->object)->exists() && |
| 109 | $this->getResource($triple->object)->getLabel() !== '' |
| 110 | ) { |
| 111 | return $this->getResource($triple->object)->getLabel(); |
| 112 | } |
| 113 | |
| 114 | return $triple->object; |
| 115 | } |
| 116 | } |