Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
68.00% |
17 / 25 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| UserSettingsFormFactory | |
68.00% |
17 / 25 |
|
50.00% |
2 / 4 |
8.61 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| createFormFields | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
4.20 | |||
| createFormOptions | |
100.00% |
6 / 6 |
|
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) 2021 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\user; |
| 24 | |
| 25 | use common_Exception; |
| 26 | use tao_actions_form_UserSettings; |
| 27 | use tao_helpers_form_Form; |
| 28 | use tao_helpers_form_FormContainer; |
| 29 | use tao_models_classes_LanguageService; |
| 30 | |
| 31 | class UserSettingsFormFactory |
| 32 | { |
| 33 | public const PARAM_USE_CSRF_PROTECTION = 'useCSRFProtection'; |
| 34 | |
| 35 | /** @var tao_models_classes_LanguageService */ |
| 36 | private $languageService; |
| 37 | |
| 38 | public function __construct(tao_models_classes_LanguageService $languageService) |
| 39 | { |
| 40 | $this->languageService = $languageService; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @throws common_Exception |
| 45 | */ |
| 46 | public function create( |
| 47 | UserSettingsInterface $userSettings, |
| 48 | string $uiLanguage = null, |
| 49 | array $params = [] |
| 50 | ): tao_helpers_form_Form { |
| 51 | $formBuilder = new tao_actions_form_UserSettings( |
| 52 | $this->createFormFields($userSettings, $uiLanguage), |
| 53 | $this->createFormOptions($params) |
| 54 | ); |
| 55 | |
| 56 | return $formBuilder->getForm(); |
| 57 | } |
| 58 | |
| 59 | public function createFormFields( |
| 60 | UserSettingsInterface $userSettings, |
| 61 | string $uiLanguage = null |
| 62 | ): array { |
| 63 | $fields = [ |
| 64 | 'timezone' => trim($userSettings->getTimezone()), |
| 65 | 'ui_lang' => $uiLanguage, |
| 66 | ]; |
| 67 | |
| 68 | if (!empty($userSettings->getUILanguageCode())) { |
| 69 | $fields['ui_lang'] = $userSettings->getUILanguageCode(); |
| 70 | } |
| 71 | |
| 72 | if (!empty($userSettings->getDataLanguageCode())) { |
| 73 | $fields['data_lang'] = $userSettings->getDataLanguageCode(); |
| 74 | } |
| 75 | |
| 76 | if ($userSettings->getSetting(UserSettingsInterface::INTERFACE_MODE)) { |
| 77 | $fields[UserSettingsInterface::INTERFACE_MODE] = $userSettings->getSetting( |
| 78 | UserSettingsInterface::INTERFACE_MODE |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | return $fields; |
| 83 | } |
| 84 | |
| 85 | public function createFormOptions(array $params = []): array |
| 86 | { |
| 87 | return [ |
| 88 | tao_actions_form_UserSettings::OPTION_LANGUAGE_SERVICE => $this->languageService, |
| 89 | tao_helpers_form_FormContainer::CSRF_PROTECTION_OPTION => (bool) ( |
| 90 | $params[self::PARAM_USE_CSRF_PROTECTION] ?? true |
| 91 | ), |
| 92 | ]; |
| 93 | } |
| 94 | } |