Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.00% covered (warning)
72.00%
18 / 25
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Qti3XsdValidator
72.00% covered (warning)
72.00%
18 / 25
75.00% covered (warning)
75.00%
3 / 4
10.78
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isQtiElementName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 isMathElementName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadQtiElementNames
65.00% covered (warning)
65.00%
13 / 20
0.00% covered (danger)
0.00%
0 / 1
6.07
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.
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiItem\model\Export\Qti3Package;
24
25use oat\taoQtiItem\model\ValidationService;
26use RuntimeException;
27use SimpleXMLElement;
28
29class Qti3XsdValidator
30{
31    private const QTI3_NAMESPACE = 'http://www.imsglobal.org/xsd/imsqtiasi_v3p0';
32
33    private ?array $qtiElementNames = null;
34    private ValidationService $validationService;
35
36    public function __construct(ValidationService $validationService = null)
37    {
38        $this->validationService = $validationService ?? new ValidationService();
39    }
40
41    public function isQtiElementName(string $elementName): bool
42    {
43        if ($this->qtiElementNames === null) {
44            $this->qtiElementNames = $this->loadQtiElementNames();
45        }
46
47        return in_array($elementName, $this->qtiElementNames, true);
48    }
49
50    public function isMathElementName(string $elementName): bool
51    {
52        return str_starts_with($elementName, 'm:');
53    }
54
55    public function loadQtiElementNames(): array
56    {
57        $schemaFiles = $this->validationService->getContentValidationSchema(self::QTI3_NAMESPACE);
58
59        if (empty($schemaFiles)) {
60            throw new RuntimeException('QTI3 XSD schema files not found');
61        }
62
63        $mainSchema = $schemaFiles[0]; // Use the first schema file
64
65        if (!file_exists($mainSchema)) {
66            throw new RuntimeException(
67                sprintf(
68                    'QTI3 XSD schema file not found at path: %s',
69                    $mainSchema
70                )
71            );
72        }
73
74        $xml = new SimpleXMLElement(file_get_contents($mainSchema));
75        $xml->registerXPathNamespace('xs', 'http://www.w3.org/2001/XMLSchema');
76
77        $elements = $xml->xpath('//xs:element[@name]');
78
79        $qtiElementNames = [];
80        foreach ($elements as $element) {
81            $name = (string)$element['name'];
82            if (str_starts_with($name, 'qti-')) {
83                $qtiElementNames[] = $name;
84            }
85        }
86
87        return $qtiElementNames;
88    }
89}