ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
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 UIFactory $ui_factory,
34 private readonly Refinery $refinery,
35 private readonly ConfigurationRepository $user_settings_configuration_repository,
36 private readonly DataRepository $user_settings_data_repository
37 ) {
38 }
39
44 public function buildFormInputs(
45 array $pages,
46 Context $context,
47 ?\ilObjUser $user
48 ): array {
49 return array_reduce(
50 $this->getSettingsForPagesBySections($pages),
51 function (array $c, array $v) use ($pages, $context, $user): array {
52 $settings = $this->filterSettingsInSectionForAvailability($context, $v);
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(
109 \ilPropertyFormGUI $form
110 ): bool {
111 return $this->checkStartingPointValue(
112 $tpl,
113 $form
114 );
115 }
116
123 public function saveForm(
124 \ilPropertyFormGUI|array $form,
125 array $pages,
126 Context $context,
127 \ilObjUser $user
128 ): \ilObjUser {
129 foreach ($this->getSettingsForPagesBySections($pages) as $section => $settings) {
130 $available_settings = $this->filterSettingsInSectionForAvailability($context, $settings);
131 $set_settings_to_default = false;
132 if ($section !== 0
133 && (
134 is_array($form) && $form[$section] === null
135 || $form instanceof \ilPropertyFormGUI
136 && (($input = $form->getInput($section)) === '' || $input === '0')
137 )
138 ) {
139 $set_settings_to_default = true;
140 }
141 foreach ($available_settings as $setting) {
142 $setting->persistUserInput(
143 $user,
144 $context,
145 $set_settings_to_default ? null : $this->retrieveValueFromInputs($form, $setting),
146 $form instanceof \ilPropertyFormGUI ? $form : null
147 );
148 }
149 }
150
151 $user->update();
152 return $user;
153 }
154
156 string $definition_class
157 ): Setting {
158 $setting = $this->user_settings_configuration_repository->getByDefinitionClass($definition_class);
159 if ($setting === null) {
160 throw new \UnexpectedValueException('No class by that name');
161 }
162 return $setting;
163 }
164
166 string $definition_class,
167 \ilPropertyFormGUI $form
168 ): mixed {
169 return $form->getInput(
170 $this->getSettingByDefinitionClass($definition_class)->getIdentifier()
171 );
172 }
173
174 public function settingAvailableToUser(
175 string $definition_class
176 ): bool {
177 return Context::User->isSettingAvailable(
178 $this->getSettingByDefinitionClass($definition_class)
179 );
180 }
181
182 public function getSettingValueFor(int $user_id, string $key): ?string
183 {
184 return $this->user_settings_data_repository->getFor($user_id)[$key] ?? null;
185 }
186
190 public function getExportableSettings(): array
191 {
192 $context = Context::Export;
193 return array_filter(
194 $this->user_settings_configuration_repository->get(),
195 static fn(Setting $v): bool => $context->isSettingAvailable($v)
196 );
197 }
198
203 array $pages
204 ): array {
205 return $this->reorderSections(
206 array_reduce(
207 $this->user_settings_configuration_repository->get(),
208 function (array $c, Setting $v) use ($pages): array {
209 if (!in_array($v->getSettingsPage(), $pages)) {
210 return $c;
211 }
212
213 $section_key = $this->buildSectionKey($v);
214 if (!array_key_exists($section_key, $c)) {
215 $c[$section_key] = [];
216 }
217
218 $c[$section_key][] = $v;
219 return $c;
220 },
221 []
222 )
223 );
224 }
225
226 private function reorderSections(
227 array $sections
228 ): array {
229 if (isset($sections[AvailableSections::Main->value])) {
230 $default_section = $sections[AvailableSections::Main->value];
231 unset($sections[AvailableSections::Main->value]);
232 array_unshift($sections, $default_section);
233 }
234
235 if (isset($sections[AvailableSections::Additional->value])) {
236 $additional_section = $sections[AvailableSections::Additional->value];
237 unset($sections[AvailableSections::Additional->value]);
238 $sections[AvailableSections::Additional->value] = $additional_section;
239 }
240 return $sections;
241 }
242
243 private function buildInputsForSection(
244 array $settings,
245 ?\ilObjUser $user
246 ): array {
247 return array_reduce(
248 $settings,
249 function (array $c, Setting $v) use ($user): array {
250 $c['inputs'][$v->getIdentifier()] = $v->getInput(
251 $this->ui_factory->input()->field(),
252 $this->lng,
253 $this->refinery,
254 $this->settings,
255 $user
256 );
257 $c['byline'] .= "{$v->getLabel($this->lng)}: "
258 . "{$v->getDefaultValueForDisplay($this->lng, $this->settings)}; ";
259 if ($v->hasUserPersonalizedSetting($this->settings, $user)) {
260 $c['personalized'] = true;
261 }
262 return $c;
263 },
264 [
265 'inputs' => [],
266 'byline' => "{$this->lng->txt('default')}<br>",
267 'personalized' => false
268 ]
269 );
270 }
271
272 private function addSectionToLegacyForm(
273 \ilPropertyFormGUI $form,
274 ?\ilObjUser $user,
275 array $section
277 if ($section === []) {
278 return $form;
279 }
280
281 if ($section[0]->getSettingsPage() === AvailablePages::MainSettings
282 && $section[0]->getSection() === AvailableSections::Main) {
283 return $this->addDefaultInputsToLegacyForm($form, $user, $section);
284 }
285
286 return $this->addAdditionalInputsToLegacyForm($form, $user, $section);
287 }
288
290 \ilPropertyFormGUI $form,
291 ?\ilObjUser $user,
292 array $section
294 return array_reduce(
295 $section,
296 function (\ilPropertyFormGUI $c, Setting $v) use ($user): \ilPropertyFormGUI {
297 $input = $v->getLegacyInput($this->lng, $this->settings, $user);
298 $c->addItem($input);
299 return $c;
300 },
301 $form
302 );
303 }
304
306 \ilPropertyFormGUI $form,
307 ?\ilObjUser $user,
308 array $section
310 $section_key = $this->buildSectionKey($section[0]);
311 $values = array_reduce(
312 $section,
313 function (array $c, Setting $v) use ($user): array {
314 $input = $v->getLegacyInput($this->lng, $this->settings, $user);
315 $input->setPostVar($v->getIdentifier());
316 $c['checkbox']->addSubItem($input);
317 $c['defaults'] .= "{$v->getLabel($this->lng)}: "
318 . "{$v->getDefaultValueForDisplay($this->lng, $this->settings)}; ";
319 if ($v->hasUserPersonalizedSetting($this->settings, $user)) {
320 $c['has_personalization'] = true;
321 }
322 return $c;
323 },
324 [
325 'checkbox' => new \ilCheckboxInputGUI(
326 $this->lng->txt("personalise_{$section_key}"),
327 $section_key
328 ),
329 'defaults' => "{$this->lng->txt('default')}<br>",
330 'has_personalization' => false
331 ]
332 );
333 $values['checkbox']->setInfo(trim($values['defaults']));
334 $values['checkbox']->setChecked($values['has_personalization']);
335
336 $form->addItem($values['checkbox']);
337
338 return $form;
339 }
340
341 private function buildSectionKey(Setting $setting): string
342 {
343 return $setting->getSettingsPage() === AvailablePages::MainSettings
344 ? $setting->getSection()->value
345 : $setting->getSettingsPage()->value;
346 }
347
349 Context $context,
350 array $settings
351 ): array {
352 return array_values(
353 array_filter(
354 $settings,
355 static fn(Setting $v): bool => $context->isSettingAvailable($v)
356 )
357 );
358 }
359
360 private function retrieveValueFromInputs(
361 \ilPropertyFormGUI|array $form,
362 Setting $setting
363 ): mixed {
364 if ($form instanceof \ilPropertyFormGUI) {
365 return $form->getInput($setting->getIdentifier());
366 }
367
368 $section_key = $this->buildSectionKey($setting);
369
370 if ($section_key === AvailableSections::Main->value) {
371 return $form[$setting->getIdentifier()];
372 }
373 return $form[$section_key][$setting->getIdentifier()];
374 }
375
376 private function checkStartingPointValue(
378 \ilPropertyFormGUI $form
379 ): bool {
380 return $form->getInput('additional') === ''
381 || $this->user_settings_configuration_repository
382 ->getByIdentifier('starting_point')
383 ->validateUserChoice(
384 $tpl,
385 $this->lng,
386 $form
387 );
388 }
389}
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:155
getLegacyInput(Language $lng, \ilSetting $settings, ?\ilObjUser $user=null)
Definition: Setting.php:104
__construct(private readonly Language $lng, private readonly \ilSetting $settings, private readonly UIFactory $ui_factory, private readonly Refinery $refinery, private readonly ConfigurationRepository $user_settings_configuration_repository, private readonly DataRepository $user_settings_data_repository)
addSectionsToLegacyForm(\ilPropertyFormGUI $form, array $pages, Context $context, ?\ilObjUser $user)
buildInputsForSection(array $settings, ?\ilObjUser $user)
checkStartingPointValue(\ilGlobalTemplateInterface $tpl, \ilPropertyFormGUI $form)
buildFormInputs(array $pages, Context $context, ?\ilObjUser $user)
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 ...
performAdditionalChecks(\ilGlobalTemplateInterface $tpl, \ilPropertyFormGUI $form)
addDefaultInputsToLegacyForm(\ilPropertyFormGUI $form, ?\ilObjUser $user, array $section)
filterSettingsInSectionForAvailability(Context $context, array $settings)
getValueFromLegacyFormByDefinitionClass(string $definition_class, \ilPropertyFormGUI $form)
retrieveValueFromInputs(\ilPropertyFormGUI|array $form, Setting $setting)
User class.
This class represents a property form user interface.
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:26
if(!file_exists('../ilias.ini.php'))