Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
QtiTestRepository
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
5
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
 findByDelivery
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getFirstTestItemLanguage
100.00% covered (success)
100.00%
8 / 8
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) 2022 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiTest\model\Infrastructure;
24
25use core_kernel_classes_Resource;
26use oat\generis\model\data\Ontology;
27use oat\taoQtiItem\model\qti\Service;
28use oat\taoQtiTest\model\Domain\Model\QtiTest;
29use oat\taoQtiTest\model\Domain\Model\QtiTestRepositoryInterface;
30use oat\taoQtiTest\models\TestModelService;
31
32class QtiTestRepository implements QtiTestRepositoryInterface
33{
34    /** @var Ontology */
35    private $ontology;
36
37    /** @var TestModelService */
38    private $testModelService;
39
40    /** @var Service */
41    private $qtiItemService;
42
43    public function __construct(Ontology $ontology, TestModelService $testModelService, Service $qtiItemService = null)
44    {
45        $this->ontology = $ontology;
46        $this->testModelService = $testModelService;
47        $this->qtiItemService = $qtiItemService ?? Service::singleton();
48    }
49
50    public function findByDelivery(string $deliveryUri): ?QtiTest
51    {
52        $delivery = $this->ontology->getResource($deliveryUri);
53        $deliveryTest = $delivery->getProperty('http://www.tao.lu/Ontologies/TAODelivery.rdf#AssembledDeliveryOrigin');
54        $testId = $delivery->getOnePropertyValue($deliveryTest)->getUri();
55
56        return new QtiTest($testId, $this->getFirstTestItemLanguage($testId));
57    }
58
59    private function getFirstTestItemLanguage(string $testId): ?string
60    {
61        $items = $this->testModelService->getItems($this->ontology->getResource($testId));
62
63        $firstItem = current($items);
64
65        if (!$firstItem instanceof core_kernel_classes_Resource) {
66            return null;
67        }
68
69        $item = $this->qtiItemService->getDataItemByRdfItem($firstItem);
70
71        if ($item) {
72            return $item->getAttributeValue('xml:lang');
73        }
74
75        return null;
76    }
77}