ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
SettingsImplementation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\User\Settings;
22
25use ILIAS\UI\Factory as UIFactory;
26use ILIAS\Refinery\Factory as Refinery;
27
29{
30 public function __construct(
31 private readonly Language $lng,
32 private readonly \ilSetting $settings,
33 private readonly \ilGlobalTemplateInterface $tpl,
34 private readonly UIFactory $ui_factory,
35 private readonly Refinery $refinery,
36 private readonly ConfigurationRepository $user_settings_configuration_repository,
37 private readonly DataRepository $user_settings_data_repository
38 ) {
39 }
40
44 public function buildFormInputs(
45 array $pages,
47 ?\ilObjUser $user
48 ): array {
49 return array_reduce(
50 $this->getSettingsForPagesBySections($pages),
51 function (array $c, array $v) use ($pages, $context, $user): array {
53 if ($settings === []) {
54 return $c;
55 }
56
57 $page = $settings[0]->getSettingsPage();
58 $section = $settings[0]->getSection();
59 $section_key = $this->buildSectionKey($settings[0]);
60
61 ['inputs' => $inputs, 'byline' => $byline, 'personalized' => $personalized] = $this->buildInputsForSection(
62 $settings,
63 $user
64 );
65
66 if (!in_array(AvailablePages::MainSettings, $pages)
67 || $page === AvailablePages::MainSettings && $section === AvailableSections::Main) {
68 return $c + $inputs;
69 }
70
71 $c[$section_key] = $this->ui_factory->input()->field()->optionalGroup(
72 $inputs,
73 $this->lng->txt("personalise_{$section_key}"),
74 $byline
75 );
76
77 if (!$personalized) {
78 $c[$section_key] = $c[$section_key]->withValue(null);
79 }
80
81 return $c;
82 },
83 []
84 );
85 }
86
90 public function addSectionsToLegacyForm(
91 \ilPropertyFormGUI $form,
92 array $pages,
93 Context $context,
94 ?\ilObjUser $user
96 return array_reduce(
97 $this->getSettingsForPagesBySections($pages),
98 fn(\ilPropertyFormGUI $c, array $v): \ilPropertyFormGUI => $this->addSectionToLegacyForm(
99 $c,
100 $user,
101 $this->filterSettingsInSectionForAvailability($context, $v)
102 ),
103 $form
104 );
105 }
106
107 public function performAdditionalChecks(
108 \ilPropertyFormGUI $form
109 ): bool {
110 return $this->checkStartingPointValue($form);
111 }
112
119 public function saveForm(
120 \ilPropertyFormGUI|array $form,
121 array $pages,
122 Context $context,
123 \ilObjUser $user
124 ): \ilObjUser {
125 foreach ($this->getSettingsForPagesBySections($pages) as $section => $settings) {
126 $available_settings = $this->filterSettingsInSectionForAvailability($context, $settings);
127 $set_settings_to_default = false;
128 if ($section !== 0
129 && (
130 is_array($form) && $form[$section] === null
131 || $form instanceof \ilPropertyFormGUI
132 && (($input = $form->getInput($section)) === '' || $input === '0')
133 )
134 ) {
135 $set_settings_to_default = true;
136 }
137 foreach ($available_settings as $setting) {
138 $setting->persistUserInput(
139 $user,
140 $context,
141 $set_settings_to_default ? null : $this->retrieveValueFromInputs($form, $setting),
142 $form instanceof \ilPropertyFormGUI ? $form : null
143 );
144 }
145 }
146
147 $user->update();
148 return $user;
149 }
150
152 string $definition_class
153 ): Setting {
154 $setting = $this->user_settings_configuration_repository->getByDefinitionClass($definition_class);
155 if ($setting === null) {
156 throw new \UnexpectedValueException('No class by that name');
157 }
158 return $setting;
159 }
160
162 string $definition_class,
163 \ilPropertyFormGUI $form
164 ): mixed {
165 return $form->getInput(
166 $this->getSettingByDefinitionClass($definition_class)->getIdentifier()
167 );
168 }
169
170 public function settingAvailableToUser(
171 string $definition_class
172 ): bool {
173 return Context::User->isSettingAvailable(
174 $this->getSettingByDefinitionClass($definition_class)
175 );
176 }
177
178 public function getSettingValueFor(int $user_id, string $key): ?string
179 {
180 return $this->user_settings_data_repository->getFor($user_id)[$key] ?? null;
181 }
182
183 public function getExportableSettings(): array
184 {
185 $context = Context::Export;
186 return array_filter(
187 $this->user_settings_configuration_repository->get(),
188 fn(Setting $v): bool => $context->isSettingAvailable($v)
189 );
190 }
191
196 array $pages
197 ): array {
198 return $this->reorderSections(
199 array_reduce(
200 $this->user_settings_configuration_repository->get(),
201 function (array $c, Setting $v) use ($pages): array {
202 if (!in_array($v->getSettingsPage(), $pages)) {
203 return $c;
204 }
205
206 $section_key = $this->buildSectionKey($v);
207 if (!array_key_exists($section_key, $c)) {
208 $c[$section_key] = [];
209 }
210
211 $c[$section_key][] = $v;
212 return $c;
213 },
214 []
215 )
216 );
217 }
218
219 private function reorderSections(
220 array $sections
221 ): array {
222 if (isset($sections[AvailableSections::Main->value])) {
223 $default_section = $sections[AvailableSections::Main->value];
224 unset($sections[AvailableSections::Main->value]);
225 array_unshift($sections, $default_section);
226 }
227
228 if (isset($sections[AvailableSections::Additional->value])) {
229 $additional_section = $sections[AvailableSections::Additional->value];
230 unset($sections[AvailableSections::Additional->value]);
231 $sections[AvailableSections::Additional->value] = $additional_section;
232 }
233 return $sections;
234 }
235
236 private function buildInputsForSection(
237 array $settings,
238 ?\ilObjUser $user
239 ): array {
240 return array_reduce(
241 $settings,
242 function (array $c, Setting $v) use ($user): array {
243 $c['inputs'][$v->getIdentifier()] = $v->getInput(
244 $this->ui_factory->input()->field(),
245 $this->lng,
246 $this->refinery,
247 $this->settings,
248 $user
249 );
250 $c['byline'] .= "{$v->getLabel($this->lng)}: "
251 . "{$v->getDefaultValueForDisplay($this->lng, $this->settings)}; ";
252 if ($v->hasUserPersonalizedSetting($this->settings, $user)) {
253 $c['personalized'] = true;
254 }
255 return $c;
256 },
257 [
258 'inputs' => [],
259 'byline' => "{$this->lng->txt('default')}<br>",
260 'personalized' => false
261 ]
262 );
263 }
264
265 private function addSectionToLegacyForm(
266 \ilPropertyFormGUI $form,
267 ?\ilObjUser $user,
268 array $section
270 if ($section === []) {
271 return $form;
272 }
273
274 if ($section[0]->getSettingsPage() === AvailablePages::MainSettings
275 && $section[0]->getSection() === AvailableSections::Main) {
276 return $this->addDefaultInputsToLegacyForm($form, $user, $section);
277 }
278
279 return $this->addAdditionalInputsToLegacyForm($form, $user, $section);
280 }
281
283 \ilPropertyFormGUI $form,
284 ?\ilObjUser $user,
285 array $section
287 return array_reduce(
288 $section,
289 function (\ilPropertyFormGUI $c, Setting $v) use ($user): \ilPropertyFormGUI {
290 $input = $v->getLegacyInput($this->lng, $this->settings, $user);
291 $c->addItem($input);
292 return $c;
293 },
294 $form
295 );
296 }
297
299 \ilPropertyFormGUI $form,
300 ?\ilObjUser $user,
301 array $section
303 $section_key = $this->buildSectionKey($section[0]);
304 $values = array_reduce(
305 $section,
306 function (array $c, Setting $v) use ($user): array {
307 $input = $v->getLegacyInput($this->lng, $this->settings, $user);
308 $input->setPostVar($v->getIdentifier());
309 $c['checkbox']->addSubItem($input);
310 $c['defaults'] .= "{$v->getLabel($this->lng)}: "
311 . "{$v->getDefaultValueForDisplay($this->lng, $this->settings)}; ";
312 if ($v->hasUserPersonalizedSetting($this->settings, $user)) {
313 $c['has_personalization'] = true;
314 }
315 return $c;
316 },
317 [
318 'checkbox' => new \ilCheckboxInputGUI(
319 $this->lng->txt("personalise_{$section_key}"),
320 $section_key
321 ),
322 'defaults' => "{$this->lng->txt('default')}<br>",
323 'has_personalization' => false
324 ]
325 );
326 $values['checkbox']->setInfo(trim($values['defaults']));
327 $values['checkbox']->setChecked($values['has_personalization']);
328
329 $form->addItem($values['checkbox']);
330
331 return $form;
332 }
333
334 private function buildSectionKey(Setting $setting): string
335 {
336 return $setting->getSettingsPage() === AvailablePages::MainSettings
337 ? $setting->getSection()->value
338 : $setting->getSettingsPage()->value;
339 }
340
342 Context $context,
343 array $settings
344 ): array {
345 return array_values(
346 array_filter(
347 $settings,
348 static fn(Setting $v): bool => $context->isSettingAvailable($v)
349 )
350 );
351 }
352
353 private function retrieveValuefromInputs(
354 \ilPropertyFormGUI|array $form,
355 Setting $setting
356 ): mixed {
357 if ($form instanceof \ilPropertyFormGUI) {
358 return $form->getInput($setting->getIdentifier());
359 }
360
361 $section_key = $this->buildSectionKey($setting);
362
363 if ($section_key === AvailableSections::Main->value) {
364 return $form[$setting->getIdentifier()];
365 }
366 return $form[$section_key][$setting->getIdentifier()];
367 }
368
369 private function checkStartingPointValue(\ilPropertyFormGUI $form): bool
370 {
371 return $form->getInput('additional') === ''
372 || $this->user_settings_configuration_repository->getByIdentifier('starting_point')->validateUserChoice($this->tpl, $this->lng, $form);
373 }
374}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
hasUserPersonalizedSetting(\ilSetting $settings, ?\ilObjUser $user)
Definition: Setting.php:152
getLegacyInput(Language $lng, \ilSetting $settings, ?\ilObjUser $user=null)
Definition: Setting.php:104
addSectionsToLegacyForm(\ilPropertyFormGUI $form, array $pages, Context $context, ?\ilObjUser $user)
buildInputsForSection(array $settings, ?\ilObjUser $user)
__construct(private readonly Language $lng, private readonly \ilSetting $settings, private readonly \ilGlobalTemplateInterface $tpl, private readonly UIFactory $ui_factory, private readonly Refinery $refinery, private readonly ConfigurationRepository $user_settings_configuration_repository, private readonly DataRepository $user_settings_data_repository)
buildFormInputs(array $pages, Context $context, ?\ilObjUser $user)
retrieveValuefromInputs(\ilPropertyFormGUI|array $form, Setting $setting)
addSectionToLegacyForm(\ilPropertyFormGUI $form, ?\ilObjUser $user, array $section)
addAdditionalInputsToLegacyForm(\ilPropertyFormGUI $form, ?\ilObjUser $user, array $section)
saveForm(\ilPropertyFormGUI|array $form, array $pages, Context $context, \ilObjUser $user)
If it is possible to set the preference on the user, this is what will be done, the user needs to be ...
addDefaultInputsToLegacyForm(\ilPropertyFormGUI $form, ?\ilObjUser $user, array $section)
filterSettingsInSectionForAvailability(Context $context, array $settings)
getValueFromLegacyFormByDefinitionClass(string $definition_class, \ilPropertyFormGUI $form)
User class.
This class represents a property form user interface.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
ILIAS Setting Class.
$c
Definition: deliver.php:25
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
get(string $class_name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))
$context
Definition: webdav.php:31