Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_form_UserSettings | |
0.00% |
0 / 76 |
|
0.00% |
0 / 13 |
870 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| setUserTimezoneService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setContainer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setLanguageService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| initForm | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| initElements | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
30 | |||
| addInterfaceModeElement | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| addTimezoneEl | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getUserTimezoneService | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getLanguageService | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getFeatureFlagChecker | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getContainer | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getInterfaceModeOptions | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | use oat\generis\model\GenerisRdf; |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use oat\oatbox\user\UserLanguageServiceInterface; |
| 28 | use oat\oatbox\user\UserTimezoneServiceInterface; |
| 29 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 30 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 31 | use oat\tao\model\user\UserSettingsInterface; |
| 32 | use Psr\Container\ContainerInterface; |
| 33 | |
| 34 | /** |
| 35 | * This container initializes the settings form. |
| 36 | * |
| 37 | * @access public |
| 38 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 39 | * @package tao |
| 40 | */ |
| 41 | class tao_actions_form_UserSettings extends tao_helpers_form_FormContainer |
| 42 | { |
| 43 | public const OPTION_LANGUAGE_SERVICE = 'LanguageService'; |
| 44 | public const OPTION_CONTAINER_SERVICE = 'Container'; |
| 45 | public const OPTION_USERTIMEZONE_SERVICE = 'UserTimezoneService'; |
| 46 | |
| 47 | /** @var tao_models_classes_LanguageService */ |
| 48 | private $languageService; |
| 49 | |
| 50 | /** @var ContainerInterface */ |
| 51 | private $container; |
| 52 | |
| 53 | /** @var UserTimezoneServiceInterface */ |
| 54 | private $userTimezoneService; |
| 55 | |
| 56 | public function __construct(array $data = [], array $options = []) |
| 57 | { |
| 58 | if ( |
| 59 | isset($options[self::OPTION_LANGUAGE_SERVICE]) |
| 60 | && $options[self::OPTION_LANGUAGE_SERVICE] instanceof tao_models_classes_LanguageService |
| 61 | ) { |
| 62 | $this->setLanguageService($options[self::OPTION_LANGUAGE_SERVICE]); |
| 63 | } |
| 64 | |
| 65 | if ( |
| 66 | isset($options[self::OPTION_CONTAINER_SERVICE]) |
| 67 | && $options[self::OPTION_CONTAINER_SERVICE] instanceof ContainerInterface |
| 68 | ) { |
| 69 | $this->setContainer($options[self::OPTION_CONTAINER_SERVICE]); |
| 70 | } |
| 71 | |
| 72 | if ( |
| 73 | isset($options[self::OPTION_USERTIMEZONE_SERVICE]) |
| 74 | && $options[self::OPTION_USERTIMEZONE_SERVICE] instanceof UserTimezoneServiceInterface |
| 75 | ) { |
| 76 | $this->setUserTimezoneService($options[self::OPTION_USERTIMEZONE_SERVICE]); |
| 77 | } |
| 78 | |
| 79 | parent::__construct($data, $options); |
| 80 | } |
| 81 | |
| 82 | public function setUserTimezoneService(UserTimezoneServiceInterface $userTimezoneService): void |
| 83 | { |
| 84 | $this->userTimezoneService = $userTimezoneService; |
| 85 | } |
| 86 | |
| 87 | public function setContainer(ContainerInterface $container): void |
| 88 | { |
| 89 | $this->container = $container; |
| 90 | } |
| 91 | |
| 92 | public function setLanguageService(tao_models_classes_LanguageService $languageService): void |
| 93 | { |
| 94 | $this->languageService = $languageService; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @inheritdoc |
| 99 | * @throws common_Exception |
| 100 | * @throws Exception |
| 101 | */ |
| 102 | protected function initForm() |
| 103 | { |
| 104 | $this->form = tao_helpers_form_FormFactory::getForm('settings'); |
| 105 | |
| 106 | $actions = tao_helpers_form_FormFactory::getCommonActions('top'); |
| 107 | $this->form->setActions([], 'top'); |
| 108 | $this->form->setActions($actions); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @inheritdoc |
| 113 | * @throws common_Exception |
| 114 | * @throws common_exception_Error |
| 115 | */ |
| 116 | protected function initElements() |
| 117 | { |
| 118 | $langService = $this->getLanguageService(); |
| 119 | $userLangService = $this->getContainer()->get(UserLanguageServiceInterface::class); |
| 120 | |
| 121 | // Retrieve languages available for a GUI usage. |
| 122 | $guiUsage = new core_kernel_classes_Resource(tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_GUI); |
| 123 | $guiOptions = []; |
| 124 | foreach ($langService->getAvailableLanguagesByUsage($guiUsage) as $lang) { |
| 125 | $guiOptions[tao_helpers_Uri::encode($lang->getUri())] = $lang->getLabel(); |
| 126 | } |
| 127 | |
| 128 | // Retrieve languages available for a Data usage. |
| 129 | $dataUsage = new core_kernel_classes_Resource(tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_DATA); |
| 130 | $dataOptions = []; |
| 131 | foreach ($langService->getAvailableLanguagesByUsage($dataUsage) as $lang) { |
| 132 | $dataOptions[tao_helpers_Uri::encode($lang->getUri())] = $lang->getLabel(); |
| 133 | } |
| 134 | |
| 135 | $uiLangElement = tao_helpers_form_FormFactory::getElement('ui_lang', 'Combobox'); |
| 136 | $uiLangElement->setDescription(__('Interface language')); |
| 137 | $uiLangElement->setOptions($guiOptions); |
| 138 | |
| 139 | $this->form->addElement($uiLangElement); |
| 140 | |
| 141 | if ($userLangService->isDataLanguageEnabled()) { |
| 142 | $dataLangElement = tao_helpers_form_FormFactory::getElement('data_lang', 'Combobox'); |
| 143 | $dataLangElement->setDescription(__('Data language')); |
| 144 | $dataLangElement->setOptions($dataOptions); |
| 145 | $this->form->addElement($dataLangElement); |
| 146 | } |
| 147 | |
| 148 | $this->addTimezoneEl($this->form); |
| 149 | |
| 150 | if ( |
| 151 | $this->getFeatureFlagChecker()->isEnabled( |
| 152 | FeatureFlagCheckerInterface::FEATURE_FLAG_SOLAR_DESIGN_ENABLED |
| 153 | ) |
| 154 | ) { |
| 155 | $this->addInterfaceModeElement($this->form); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private function addInterfaceModeElement(tao_helpers_form_Form $form): void |
| 160 | { |
| 161 | $interfaceModeElement = tao_helpers_form_FormFactory::getElement( |
| 162 | UserSettingsInterface::INTERFACE_MODE, |
| 163 | 'Radiobox' |
| 164 | ); |
| 165 | $interfaceModeElement->setDescription(__('Interface Mode')); |
| 166 | $interfaceModeElement->setOptions($this->getInterfaceModeOptions()); |
| 167 | $form->addElement($interfaceModeElement); |
| 168 | } |
| 169 | |
| 170 | private function addTimezoneEl($form): void |
| 171 | { |
| 172 | if ($this->getUserTimezoneService()->isUserTimezoneEnabled()) { |
| 173 | $tzElement = tao_helpers_form_FormFactory::getElement('timezone', 'Combobox'); |
| 174 | $tzElement->setDescription(__('Time zone')); |
| 175 | |
| 176 | $options = []; |
| 177 | foreach (DateTimeZone::listIdentifiers() as $id) { |
| 178 | $options[$id] = $id; |
| 179 | } |
| 180 | $tzElement->setOptions($options); |
| 181 | |
| 182 | $form->addElement($tzElement); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private function getUserTimezoneService(): UserTimezoneServiceInterface |
| 187 | { |
| 188 | if (!$this->userTimezoneService) { |
| 189 | $this->userTimezoneService = $this->getContainer()->get( |
| 190 | UserTimezoneServiceInterface::SERVICE_ID |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | return $this->userTimezoneService; |
| 195 | } |
| 196 | |
| 197 | private function getLanguageService(): tao_models_classes_LanguageService |
| 198 | { |
| 199 | if (!$this->languageService) { |
| 200 | $this->languageService = tao_models_classes_LanguageService::singleton(); |
| 201 | } |
| 202 | |
| 203 | return $this->languageService; |
| 204 | } |
| 205 | |
| 206 | private function getFeatureFlagChecker(): FeatureFlagChecker |
| 207 | { |
| 208 | return $this |
| 209 | ->getContainer() |
| 210 | ->get(FeatureFlagChecker::class); |
| 211 | } |
| 212 | |
| 213 | private function getContainer(): ContainerInterface |
| 214 | { |
| 215 | if (!$this->container) { |
| 216 | $this->container = ServiceManager::getServiceManager()->getContainer(); |
| 217 | } |
| 218 | |
| 219 | return $this->container; |
| 220 | } |
| 221 | |
| 222 | private function getInterfaceModeOptions(): array |
| 223 | { |
| 224 | $options = []; |
| 225 | $property = new core_kernel_classes_Property(GenerisRdf::PROPERTY_USER_INTERFACE_MODE); |
| 226 | |
| 227 | foreach ($property->getRange()->getInstances(true) as $rangeInstance) { |
| 228 | $options[tao_helpers_Uri::encode($rangeInstance->getUri())] = $rangeInstance->getLabel(); |
| 229 | } |
| 230 | |
| 231 | krsort($options); |
| 232 | |
| 233 | return $options; |
| 234 | } |
| 235 | } |