Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_actions_UserSettings
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 10
380
0.00% covered (danger)
0.00%
0 / 1
 password
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 properties
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
56
 getExtensionManager
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserSettingsService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserSettingsFormFactory
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUserLanguageService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLanguageService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFeatureFlagChecker
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 isDemoMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg
19 *                         (under the project TAO & TAO2);
20 *               2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung
21 *                         (under the project TAO-TRANSFER);
22 *               2009-2012 (update and modification) Public Research Centre Henri Tudor
23 *                         (under the project TAO-SUSTAIN & TAO-DEV);
24 *               2013-2021 (update and modification) Open Assessment Technologies SA;
25 *
26 */
27
28declare(strict_types=1);
29
30use oat\generis\model\GenerisRdf;
31use oat\generis\model\OntologyAwareTrait;
32use oat\oatbox\user\UserLanguageServiceInterface;
33use oat\tao\model\featureFlag\FeatureFlagChecker;
34use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
35use oat\tao\model\service\ApplicationService;
36use oat\tao\model\user\UserSettingsFormFactory;
37use oat\tao\model\user\UserSettingsInterface;
38use oat\tao\model\user\UserSettingsServiceInterface;
39use oat\tao\model\user\implementation\UserSettingsService;
40use tao_helpers_form_FormContainer as FormContainer;
41use tao_helpers_Display as DisplayHelper;
42
43/**
44 * This controller provides actions to manage the user settings
45 *
46 * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu}
47 * @license GPLv2  http://www.opensource.org/licenses/gpl-2.0.php
48 * @package tao
49 */
50class tao_actions_UserSettings extends tao_actions_CommonModule
51{
52    use OntologyAwareTrait;
53
54    /**
55     * Action dedicated to change the password of the user currently connected.
56     */
57    public function password()
58    {
59        $this->setData('formTitle', __("Change password"));
60
61        if ($this->isDemoMode()) {
62            $this->setData('myForm', __('Unable to change passwords in demo mode'));
63        } else {
64            $passwordFormContainer = new tao_actions_form_UserPassword(
65                [],
66                [FormContainer::CSRF_PROTECTION_OPTION => true]
67            );
68            $passwordForm = $passwordFormContainer->getForm();
69
70            if ($passwordForm->isSubmited() && $passwordForm->isValid()) {
71                $newPassword = $passwordForm->getValue('newpassword');
72                $userService = $this->getUserService();
73                $userService->setPassword($userService->getCurrentUser(), $newPassword);
74
75                $this->setData('message', __('Password changed'));
76            }
77
78            $this->setData('settingsForm', $passwordForm->render());
79        }
80
81        $this->setView('form/settings_user.tpl');
82    }
83
84    /**
85     * Action dedicated to change the settings of the user (language, ...)
86     */
87    public function properties()
88    {
89        $userResource = $this->getUserService()->getCurrentUser();
90        $uiLanguage = $this->getLanguageService()->getLanguageByCode(
91            $this->getUserLanguageService()->getInterfaceLanguage(
92                $this->getSession()->getUser()
93            )
94        );
95
96        $settingsForm = $this->getUserSettingsFormFactory()->create(
97            $this->getUserSettingsService()->get($userResource),
98            $uiLanguage ? $uiLanguage->getUri() : null,
99            [
100                UserSettingsFormFactory::PARAM_USE_CSRF_PROTECTION => true
101            ]
102        );
103
104        if ($settingsForm->isSubmited() && $settingsForm->isValid()) {
105            $uiLang = $this->getResource($settingsForm->getValue('ui_lang'));
106            $userSettingsData = [
107                GenerisRdf::PROPERTY_USER_TIMEZONE => $settingsForm->getValue('timezone'),
108                GenerisRdf::PROPERTY_USER_UILG => $uiLang->getUri(),
109            ];
110
111            if ($this->getUserLanguageService()->isDataLanguageEnabled()) {
112                $dataLang = $this->getResource($settingsForm->getValue('data_lang'));
113                $userSettingsData[GenerisRdf::PROPERTY_USER_DEFLG] = $dataLang->getUri();
114            }
115
116            if (
117                $this->getFeatureFlagChecker()->isEnabled(
118                    FeatureFlagCheckerInterface::FEATURE_FLAG_SOLAR_DESIGN_ENABLED
119                )
120            ) {
121                $interfaceMode = $this->getResource($settingsForm->getValue(UserSettingsInterface::INTERFACE_MODE));
122                $userSettingsData[GenerisRdf::PROPERTY_USER_INTERFACE_MODE] = $interfaceMode->getUri();
123            }
124
125            $binder = new tao_models_classes_dataBinding_GenerisFormDataBinder($userResource);
126
127            if ($binder->bind($userSettingsData)) {
128                $this->getSession()->refresh();
129
130                $uiLangCode = $this->getLanguageService()->getCode($uiLang);
131                $extension = $this->getExtensionManager()->getExtensionById('tao');
132                tao_helpers_I18n::init($extension, $uiLangCode);
133
134                $this->setData('message', __('Settings updated'));
135                $this->setData('reload', true);
136            }
137        }
138
139        $this->setData(
140            'formTitle',
141            __('My settings (%s)', DisplayHelper::htmlEscape($userResource->getLabel()))
142        );
143
144        $this->setData('settingsForm', $settingsForm->render());
145
146        $this->setView('form/settings_user.tpl');
147    }
148
149    private function getExtensionManager(): common_ext_ExtensionsManager
150    {
151        return $this->getPsrContainer()->get(common_ext_ExtensionsManager::SERVICE_ID);
152    }
153
154    private function getUserService(): tao_models_classes_UserService
155    {
156        return $this->getPsrContainer()->get(tao_models_classes_UserService::class);
157    }
158
159    private function getUserSettingsService(): UserSettingsServiceInterface
160    {
161        return $this->getPsrContainer()->get(UserSettingsService::class);
162    }
163
164    private function getUserSettingsFormFactory(): UserSettingsFormFactory
165    {
166        return $this->getPsrContainer()->get(UserSettingsFormFactory::class);
167    }
168
169    private function getUserLanguageService(): UserLanguageServiceInterface
170    {
171        return $this->getPsrContainer()->get(UserLanguageServiceInterface::class);
172    }
173
174    private function getLanguageService(): tao_models_classes_LanguageService
175    {
176        return tao_models_classes_LanguageService::singleton();
177    }
178
179    private function getFeatureFlagChecker(): FeatureFlagCheckerInterface
180    {
181        return $this
182            ->getPsrContainer()
183            ->get(FeatureFlagChecker::class);
184    }
185
186    private function isDemoMode(): bool
187    {
188        return $this->getPsrContainer()->get(ApplicationService::SERVICE_ID)->isDemo();
189    }
190}