ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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
43 public function buildFormInputs(
44 array $pages,
45 Context $context,
46 ?\ilObjUser $user
47 ): array {
48 return array_reduce(
49 $this->getSettingsForPagesBySections($pages),
50 function (array $c, array $v) use ($pages, $context, $user): array {
51 $settings = $this->filterSettingsInSectionForAvailability($context, $v);
52 if ($settings === []) {
53 return $c;
54 }
55
56 $page = $settings[0]->getSettingsPage();
57 $section = $settings[0]->getSection();
58 $section_key = $this->buildSectionKey($settings[0]);
59
60 ['inputs' => $inputs, 'byline' => $byline, 'personalized' => $personalized] = $this->buildInputsForSection(
61 $settings,
62 $user
63 );
64
65 if (!in_array(AvailablePages::MainSettings, $pages)
66 || $page === AvailablePages::MainSettings && $section === AvailableSections::Main) {
67 return $c + $inputs;
68 }
69
70 $c[$section_key] = $this->ui_factory->input()->field()->optionalGroup(
71 $inputs,
72 $this->lng->txt("personalise_{$section_key}"),
73 $byline
74 );
75
76 if (!$personalized) {
77 $c[$section_key] = $c[$section_key]->withValue(null);
78 }
79
80 return $c;
81 },
82 []
83 );
84 }
85
89 public function addSectionsToLegacyForm(
90 \ilPropertyFormGUI $form,
91 array $pages,
92 Context $context,
93 ?\ilObjUser $user
95 return array_reduce(
96 $this->getSettingsForPagesBySections($pages),
97 fn(\ilPropertyFormGUI $c, array $v): \ilPropertyFormGUI => $this->addSectionToLegacyForm(
98 $c,
99 $user,
100 $this->filterSettingsInSectionForAvailability($context, $v)
101 ),
102 $form
103 );
104 }
105
106 public function performAdditionalChecks(
108 \ilPropertyFormGUI $form
109 ): bool {
110 return $this->checkStartingPointValue(
111 $tpl,
112 $form
113 );
114 }
115
122 public function saveForm(
123 \ilPropertyFormGUI|array $form,
124 array $pages,
125 Context $context,
126 \ilObjUser $user
127 ): \ilObjUser {
128 foreach ($this->getSettingsForPagesBySections($pages) as $section => $settings) {
129 $available_settings = $this->filterSettingsInSectionForAvailability($context, $settings);
130 $set_settings_to_default = false;
131 if ($section !== 0
132 && (
133 is_array($form) && $form[$section] === null
134 || $form instanceof \ilPropertyFormGUI
135 && (($input = $form->getInput($section)) === '' || $input === '0')
136 )
137 ) {
138 $set_settings_to_default = true;
139 }
140 foreach ($available_settings as $setting) {
141 $setting->persistUserInput(
142 $user,
143 $context,
144 $set_settings_to_default ? null : $this->retrieveValueFromInputs($form, $setting),
145 $form instanceof \ilPropertyFormGUI ? $form : null
146 );
147 }
148 }
149
150 $user->update();
151 return $user;
152 }
153
155 string $definition_class
156 ): Setting {
157 $setting = $this->user_settings_configuration_repository->getByDefinitionClass($definition_class);
158 if ($setting === null) {
159 throw new \UnexpectedValueException('No class by that name');
160 }
161 return $setting;
162 }
163
165 string $definition_class,
166 \ilPropertyFormGUI $form
167 ): mixed {
168 return $form->getInput(
169 $this->getSettingByDefinitionClass($definition_class)->getIdentifier()
170 );
171 }
172
173 public function settingAvailableToUser(
174 string $definition_class
175 ): bool {
176 return Context::User->isSettingAvailable(
177 $this->getSettingByDefinitionClass($definition_class)
178 );
179 }
180
181 public function getSettingValueFor(int $user_id, string $key): ?string
182 {
183 return $this->user_settings_data_repository->getFor($user_id)[$key] ?? null;
184 }
185
186 public function getExportableSettings(): array
187 {
188 $context = Context::Export;
189 return array_filter(
190 $this->user_settings_configuration_repository->get(),
191 fn(Setting $v): bool => $context->isSettingAvailable($v)
192 );
193 }
194
199 array $pages
200 ): array {
201 return $this->reorderSections(
202 array_reduce(
203 $this->user_settings_configuration_repository->get(),
204 function (array $c, Setting $v) use ($pages): array {
205 if (!in_array($v->getSettingsPage(), $pages)) {
206 return $c;
207 }
208
209 $section_key = $this->buildSectionKey($v);
210 if (!array_key_exists($section_key, $c)) {
211 $c[$section_key] = [];
212 }
213
214 $c[$section_key][] = $v;
215 return $c;
216 },
217 []
218 )
219 );
220 }
221
222 private function reorderSections(
223 array $sections
224 ): array {
225 if (isset($sections[AvailableSections::Main->value])) {
226 $default_section = $sections[AvailableSections::Main->value];
227 unset($sections[AvailableSections::Main->value]);
228 array_unshift($sections, $default_section);
229 }
230
231 if (isset($sections[AvailableSections::Additional->value])) {
232 $additional_section = $sections[AvailableSections::Additional->value];
233 unset($sections[AvailableSections::Additional->value]);
234 $sections[AvailableSections::Additional->value] = $additional_section;
235 }
236 return $sections;
237 }
238
239 private function buildInputsForSection(
240 array $settings,
241 ?\ilObjUser $user
242 ): array {
243 return array_reduce(
244 $settings,
245 function (array $c, Setting $v) use ($user): array {
246 $c['inputs'][$v->getIdentifier()] = $v->getInput(
247 $this->ui_factory->input()->field(),
248 $this->lng,
249 $this->refinery,
250 $this->settings,
251 $user
252 );
253 $c['byline'] .= "{$v->getLabel($this->lng)}: "
254 . "{$v->getDefaultValueForDisplay($this->lng, $this->settings)}; ";
255 if ($v->hasUserPersonalizedSetting($this->settings, $user)) {
256 $c['personalized'] = true;
257 }
258 return $c;
259 },
260 [
261 'inputs' => [],
262 'byline' => "{$this->lng->txt('default')}<br>",
263 'personalized' => false
264 ]
265 );
266 }
267
268 private function addSectionToLegacyForm(
269 \ilPropertyFormGUI $form,
270 ?\ilObjUser $user,
271 array $section
273 if ($section === []) {
274 return $form;
275 }
276
277 if ($section[0]->getSettingsPage() === AvailablePages::MainSettings
278 && $section[0]->getSection() === AvailableSections::Main) {
279 return $this->addDefaultInputsToLegacyForm($form, $user, $section);
280 }
281
282 return $this->addAdditionalInputsToLegacyForm($form, $user, $section);
283 }
284
286 \ilPropertyFormGUI $form,
287 ?\ilObjUser $user,
288 array $section
290 return array_reduce(
291 $section,
292 function (\ilPropertyFormGUI $c, Setting $v) use ($user): \ilPropertyFormGUI {
293 $input = $v->getLegacyInput($this->lng, $this->settings, $user);
294 $c->addItem($input);
295 return $c;
296 },
297 $form
298 );
299 }
300
302 \ilPropertyFormGUI $form,
303 ?\ilObjUser $user,
304 array $section
306 $section_key = $this->buildSectionKey($section[0]);
307 $values = array_reduce(
308 $section,
309 function (array $c, Setting $v) use ($user): array {
310 $input = $v->getLegacyInput($this->lng, $this->settings, $user);
311 $input->setPostVar($v->getIdentifier());
312 $c['checkbox']->addSubItem($input);
313 $c['defaults'] .= "{$v->getLabel($this->lng)}: "
314 . "{$v->getDefaultValueForDisplay($this->lng, $this->settings)}; ";
315 if ($v->hasUserPersonalizedSetting($this->settings, $user)) {
316 $c['has_personalization'] = true;
317 }
318 return $c;
319 },
320 [
321 'checkbox' => new \ilCheckboxInputGUI(
322 $this->lng->txt("personalise_{$section_key}"),
323 $section_key
324 ),
325 'defaults' => "{$this->lng->txt('default')}<br>",
326 'has_personalization' => false
327 ]
328 );
329 $values['checkbox']->setInfo(trim($values['defaults']));
330 $values['checkbox']->setChecked($values['has_personalization']);
331
332 $form->addItem($values['checkbox']);
333
334 return $form;
335 }
336
337 private function buildSectionKey(Setting $setting): string
338 {
339 return $setting->getSettingsPage() === AvailablePages::MainSettings
340 ? $setting->getSection()->value
341 : $setting->getSettingsPage()->value;
342 }
343
345 Context $context,
346 array $settings
347 ): array {
348 return array_values(
349 array_filter(
350 $settings,
351 static fn(Setting $v): bool => $context->isSettingAvailable($v)
352 )
353 );
354 }
355
356 private function retrieveValuefromInputs(
357 \ilPropertyFormGUI|array $form,
358 Setting $setting
359 ): mixed {
360 if ($form instanceof \ilPropertyFormGUI) {
361 return $form->getInput($setting->getIdentifier());
362 }
363
364 $section_key = $this->buildSectionKey($setting);
365
366 if ($section_key === AvailableSections::Main->value) {
367 return $form[$setting->getIdentifier()];
368 }
369 return $form[$section_key][$setting->getIdentifier()];
370 }
371
372 private function checkStartingPointValue(
374 \ilPropertyFormGUI $form
375 ): bool {
376 return $form->getInput('additional') === ''
377 || $this->user_settings_configuration_repository
378 ->getByIdentifier('starting_point')
379 ->validateUserChoice(
380 $tpl,
381 $this->lng,
382 $form
383 );
384 }
385}
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
__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)
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 ...
performAdditionalChecks(\ilGlobalTemplateInterface $tpl, \ilPropertyFormGUI $form)
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.
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'))