Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
QtiLanguageSetter
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
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.
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiItem\model\Translation\Service;
24
25use core_kernel_classes_Resource;
26use oat\generis\model\data\Ontology;
27use oat\tao\model\TaoOntology;
28use oat\taoQtiItem\model\qti\Service;
29use Psr\Log\LoggerInterface;
30use tao_models_classes_LanguageService;
31use Throwable;
32
33class QtiLanguageSetter
34{
35    private Service $qtiItemService;
36    private LoggerInterface $logger;
37    private Ontology $ontology;
38    private tao_models_classes_LanguageService $languageService;
39
40    public function __construct(
41        Service $qtiItemService,
42        LoggerInterface $logger,
43        Ontology $ontology,
44        tao_models_classes_LanguageService $languageService
45    ) {
46        $this->qtiItemService = $qtiItemService;
47        $this->logger = $logger;
48        $this->ontology = $ontology;
49        $this->languageService = $languageService;
50    }
51
52    public function __invoke(core_kernel_classes_Resource $item): core_kernel_classes_Resource
53    {
54        try {
55            $itemData = $this->qtiItemService->getDataItemByRdfItem($item);
56        } catch (Throwable $exception) {
57            $this->logger->error(
58                sprintf(
59                    'An error occurred while retrieving item data: %s. Trace: %s',
60                    $exception->getMessage(),
61                    $exception->getTraceAsString()
62                )
63            );
64
65            throw $exception;
66        }
67
68        if (!$itemData) {
69            $this->logger->info(sprintf('There is no item data for item %s.', $item->getUri()));
70
71            return $item;
72        }
73
74        $languageProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_LANGUAGE);
75        $language = $item->getOnePropertyValue($languageProperty);
76
77        if (empty($language)) {
78            $this->logger->info(sprintf('There is no language for item %s.', $item->getUri()));
79
80            return $item;
81        }
82
83        $localeCode = str_replace(TaoOntology::LANGUAGE_PREFIX, '', $language->getUri());
84
85        if ($this->languageService->isRtlLanguage($localeCode)) {
86            $itemData->getBody()->setAttribute('dir', 'rtl');
87        }
88
89        $itemData->setAttribute('xml:lang', $localeCode);
90
91        $this->qtiItemService->saveDataItemToRdfItem($itemData, $item);
92
93        return $item;
94    }
95}