Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
UserLanguageService | |
100.00% |
14 / 14 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
getDefaultLanguage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDataLanguage | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getInterfaceLanguage | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
isDataLanguageEnabled | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getAuthoringLanguage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setCustomInterfaceLanguage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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) 2018-2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | */ |
20 | |
21 | namespace oat\oatbox\user; |
22 | |
23 | use oat\oatbox\service\ConfigurableService; |
24 | use oat\generis\model\GenerisRdf; |
25 | |
26 | class UserLanguageService extends ConfigurableService implements UserLanguageServiceInterface |
27 | { |
28 | public const OPTION_LOCK_DATA_LANGUAGE = 'lock_data_language'; |
29 | public const OPTION_AUTHORING_LANGUAGE = 'authoring_language'; |
30 | public const OPTION_INTERFACE_LANGUAGE = 'interface_language'; |
31 | |
32 | /** @var ?string */ |
33 | private $customInterfaceLanguage; |
34 | |
35 | /** |
36 | * {@inheritDoc} |
37 | * @see \oat\oatbox\user\UserLanguageServiceInterface::getDefaultLanguage() |
38 | */ |
39 | public function getDefaultLanguage() |
40 | { |
41 | return DEFAULT_LANG; |
42 | } |
43 | |
44 | /** |
45 | * @inheritdoc |
46 | */ |
47 | public function getDataLanguage(User $user) |
48 | { |
49 | $result = $this->getDefaultLanguage(); |
50 | if ($this->isDataLanguageEnabled()) { |
51 | $lang = $user->getPropertyValues(GenerisRdf::PROPERTY_USER_DEFLG); |
52 | $result = empty($lang) ? $this->getDefaultLanguage() : (string)current($lang); |
53 | } |
54 | return $result; |
55 | } |
56 | |
57 | /** |
58 | * @inheritdoc |
59 | */ |
60 | public function getInterfaceLanguage(User $user) |
61 | { |
62 | if (!empty($this->customInterfaceLanguage)) { |
63 | return $this->customInterfaceLanguage; |
64 | } |
65 | |
66 | $lang = $user->getPropertyValues(GenerisRdf::PROPERTY_USER_UILG); |
67 | |
68 | return empty($lang) ? $this->getOption(self::OPTION_INTERFACE_LANGUAGE, DEFAULT_LANG) : (string)current($lang); |
69 | } |
70 | |
71 | /** |
72 | * @inheritdoc |
73 | */ |
74 | public function isDataLanguageEnabled() |
75 | { |
76 | return !$this->hasOption(self::OPTION_LOCK_DATA_LANGUAGE) |
77 | || $this->getOption(self::OPTION_LOCK_DATA_LANGUAGE) === false; |
78 | } |
79 | |
80 | public function getAuthoringLanguage(): string |
81 | { |
82 | return $this->getOption(self::OPTION_AUTHORING_LANGUAGE, $this->getDefaultLanguage()); |
83 | } |
84 | |
85 | /** |
86 | * @inheritdoc |
87 | */ |
88 | public function setCustomInterfaceLanguage(?string $customInterfaceLanguage): void |
89 | { |
90 | $this->customInterfaceLanguage = $customInterfaceLanguage; |
91 | } |
92 | } |