Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImsManifestClassificationMetadataExtractor
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 5
380
0.00% covered (danger)
0.00%
0 / 1
 setClassificationMapping
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
 extract
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
110
 getClassificationMetadata
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getSourcePath
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getEntryPath
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
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) 2017 (original work) Open Assessment Technologies SA
19 *
20 */
21
22namespace oat\taoQtiItem\model\qti\metadata\imsManifest\classificationMetadata;
23
24use oat\taoQtiItem\model\qti\metadata\imsManifest\ImsManifestMetadataExtractor;
25use oat\taoQtiItem\model\qti\metadata\imsManifest\ImsManifestMetadataValue;
26use oat\taoQtiItem\model\qti\metadata\MetadataExtractionException;
27use oat\taoQtiItem\model\qti\metadata\MetadataValue;
28use oat\taoQtiItem\model\qti\metadata\simple\SimpleMetadataValue;
29
30class ImsManifestClassificationMetadataExtractor extends ImsManifestMetadataExtractor
31{
32    /**
33     * @var array Map of property-uri => map-value to extract from classification metadata
34     */
35    protected $classification = [];
36
37    /**
38     * Set the association between property uri and value to find into classification metadata source
39     *
40     * @param $propertyUri
41     * @param $classificationValue
42     * @throws MetadataExtractionException
43     */
44    public function setClassificationMapping($propertyUri, $classificationValue)
45    {
46        if (empty($propertyUri) || ! is_string($propertyUri)) {
47            throw new MetadataExtractionException(__('PropertyUri to map has to a valid uri'));
48        }
49        if (empty($classificationValue) || ! is_string($classificationValue)) {
50            throw new MetadataExtractionException(__('Classification value to map has to a string'));
51        }
52        $this->classification[$propertyUri] = $classificationValue;
53    }
54
55    /**
56     * @see ImsManifestDataExtractor::extract()
57     */
58    public function extract($manifest)
59    {
60        $values = parent::extract($manifest);
61        $newValues = [];
62
63        foreach ($values as $resourceIdentifier => $metadataValueCollection) {
64            $i = 0;
65            $classificationMetadataValue = null;
66
67            /** @var ImsManifestMetadataValue $metadataValue */
68            foreach ($metadataValueCollection as $metadataValue) {
69                foreach ($this->getClassificationMetadata() as $property => $sourceValue) {
70                    if (
71                        $metadataValue->getValue() === ''
72                        || $metadataValue->getValue() != $sourceValue
73                        || $metadataValue->getPath() !== $this->getSourcePath()
74                    ) {
75                        continue;
76                    }
77
78                    // LOM Classification case.
79                    if (isset($metadataValueCollection[$i + 1])) {
80                        /** @var MetadataValue $metadata */
81                        $metadata = $metadataValueCollection[$i + 1];
82
83                        if ($metadata->getPath() === $this->getEntryPath()) {
84                            if ($metadata->getValue() !== '') {
85                                $newValues[$resourceIdentifier][] = new SimpleMetadataValue(
86                                    $resourceIdentifier,
87                                    ['http://ltsc.ieee.org/xsd/LOM#lom', $property],
88                                    $metadata->getValue()
89                                );
90                            }
91                        }
92                    }
93                }
94
95                $i++;
96            }
97        }
98
99        return $newValues;
100    }
101
102    /**
103     * Get an array to map classification metadata to extract
104     *
105     * @return array
106     * @throws MetadataExtractionException
107     */
108    protected function getClassificationMetadata()
109    {
110        if (empty($this->classification)) {
111            throw new MetadataExtractionException(__('Classification metadata is required to be extracted.'));
112        }
113        return $this->classification;
114    }
115
116    /**
117     * Get default source path into DomDocument
118     *
119     * @return array
120     */
121    protected function getSourcePath()
122    {
123        return [
124            'http://ltsc.ieee.org/xsd/LOM#lom',
125            'http://ltsc.ieee.org/xsd/LOM#classification',
126            'http://ltsc.ieee.org/xsd/LOM#taxonPath',
127            'http://ltsc.ieee.org/xsd/LOM#source',
128            'http://ltsc.ieee.org/xsd/LOM#string'
129        ];
130    }
131
132    /**
133     * Get default entry path into DomDocument
134     *
135     * @return array
136     */
137    protected function getEntryPath()
138    {
139        return [
140            'http://ltsc.ieee.org/xsd/LOM#lom',
141            'http://ltsc.ieee.org/xsd/LOM#classification',
142            'http://ltsc.ieee.org/xsd/LOM#taxonPath',
143            'http://ltsc.ieee.org/xsd/LOM#taxon',
144            'http://ltsc.ieee.org/xsd/LOM#entry',
145            'http://ltsc.ieee.org/xsd/LOM#string'
146        ];
147    }
148}