Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UniqueNumericQtiIdentifierReplacer
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 replace
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
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\qti\parser;
24
25use DOMXPath;
26use oat\tao\model\featureFlag\FeatureFlagChecker;
27use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorInterface;
28use oat\taoQtiItem\helpers\QtiXmlLoader;
29
30class UniqueNumericQtiIdentifierReplacer
31{
32    private FeatureFlagChecker $featureFlagChecker;
33    private QtiXmlLoader $qtiXmlLoader;
34    private IdentifierGeneratorInterface $identifierGenerator;
35
36    public function __construct(
37        FeatureFlagChecker $featureFlagChecker,
38        QtiXmlLoader $qtiXmlLoader,
39        IdentifierGeneratorInterface $identifierGenerator
40    ) {
41        $this->featureFlagChecker = $featureFlagChecker;
42        $this->qtiXmlLoader = $qtiXmlLoader;
43        $this->identifierGenerator = $identifierGenerator;
44    }
45    public function replace(string $qti): string
46    {
47        if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_UNIQUE_NUMERIC_QTI_IDENTIFIER')) {
48            return $qti;
49        }
50        $doc = $this->qtiXmlLoader->load($qti);
51
52        $xpath = new DOMXpath($doc);
53        $xpath->registerNamespace('qti', 'http://www.imsglobal.org/xsd/imsqti_v2p2');
54        $identifierNodes = $xpath->query('//qti:assessmentItem/@identifier');
55
56        foreach ($identifierNodes as $identifierNode) {
57            $identifierNode->nodeValue = $this->identifierGenerator->generate();
58        }
59
60        return $doc->saveXML();
61    }
62}