ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 Repository $user_settings_repository
37 ) {
38 }
39
43 public function buildFormInputs(
44 array $pages,
46 ?\ilObjUser $user
47 ): array {
48 return array_reduce(
49 $this->getSettingsForPagesBySections($pages),
50 function (array $c, array $v) use ($pages, $context, $user): array {
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(
107 \ilPropertyFormGUI $form
108 ): bool {
109 return $this->checkStartingPointValue($form);
110 }
111
118 public function saveForm(
119 \ilPropertyFormGUI|array $form,
120 array $pages,
121 Context $context,
122 \ilObjUser $user
123 ): \ilObjUser {
124 foreach ($this->getSettingsForPagesBySections($pages) as $section => $settings) {
125 $available_settings = $this->filterSettingsInSectionForAvailability($context, $settings);
126 $set_settings_to_default = false;
127 if ($section !== 0
128 && (
129 is_array($form) && $form[$section] === null
130 || $form instanceof \ilPropertyFormGUI
131 && (($input = $form->getInput($section)) === '' || $input === '0')
132 )
133 ) {
134 $set_settings_to_default = true;
135 }
136 foreach ($available_settings as $setting) {
137 $setting->persistUserInput(
138 $user,
139 $context,
140 $set_settings_to_default ? null : $this->retrieveValueFromInputs($form, $setting),
141 $form instanceof \ilPropertyFormGUI ? $form : null
142 );
143 }
144 }
145
146 $user->update();
147 return $user;
148 }
149
151 string $definition_class
152 ): Setting {
153 $setting = $this->user_settings_repository->getByDefinitionClass($definition_class);
154 if ($setting === null) {
155 throw new \UnexpectedValueException('No class by that name');
156 }
157 return $setting;
158 }
159
161 string $definition_class,
162 \ilPropertyFormGUI $form
163 ): mixed {
164 return $form->getInput(
165 $this->getSettingByDefinitionClass($definition_class)->getIdentifier()
166 );
167 }
168
169 public function settingAvailableToUser(
170 string $definition_class
171 ): bool {
172 return Context::User->isSettingAvailableInType(
173 $this->getSettingByDefinitionClass($definition_class)
174 );
175 }
176
181 array $pages
182 ): array {
183 return $this->reorderSections(
184 array_reduce(
185 $this->user_settings_repository->get(),
186 function (array $c, Setting $v) use ($pages): array {
187 if (!in_array($v->getSettingsPage(), $pages)) {
188 return $c;
189 }
190
191 $section_key = $this->buildSectionKey($v);
192 if (!array_key_exists($section_key, $c)) {
193 $c[$section_key] = [];
194 }
195
196 $c[$section_key][] = $v;
197 return $c;
198 },
199 []
200 )
201 );
202 }
203
204 private function reorderSections(
205 array $sections
206 ): array {
207 if (isset($sections[AvailableSections::Main->value])) {
208 $default_section = $sections[AvailableSections::Main->value];
209 unset($sections[AvailableSections::Main->value]);
210 array_unshift($sections, $default_section);
211 }
212
213 if (isset($sections[AvailableSections::Additional->value])) {
214 $additional_section = $sections[AvailableSections::Additional->value];
215 unset($sections[AvailableSections::Additional->value]);
216 $sections[AvailableSections::Additional->value] = $additional_section;
217 }
218 return $sections;
219 }
220
221 private function buildInputsForSection(
222 array $settings,
223 ?\ilObjUser $user
224 ): array {
225 return array_reduce(
226 $settings,
227 function (array $c, Setting $v) use ($user): array {
228 $c['inputs'][$v->getIdentifier()] = $v->getInput(
229 $this->ui_factory->input()->field(),
230 $this->lng,
231 $this->refinery,
232 $this->settings,
233 $user
234 );
235 $c['byline'] .= "{$v->getLabel($this->lng)}: "
236 . "{$v->getDefaultValueForDisplay($this->lng, $this->settings)}; ";
237 if ($v->hasUserPersonalizedSetting($this->settings, $user)) {
238 $c['personalized'] = true;
239 }
240 return $c;
241 },
242 [
243 'inputs' => [],
244 'byline' => "{$this->lng->txt('default')}<br>",
245 'personalized' => false
246 ]
247 );
248 }
249
250 private function addSectionToLegacyForm(
251 \ilPropertyFormGUI $form,
252 ?\ilObjUser $user,
253 array $section
255 if ($section === []) {
256 return $form;
257 }
258
259 if ($section[0]->getSettingsPage() === AvailablePages::MainSettings
260 && $section[0]->getSection() === AvailableSections::Main) {
261 return $this->addDefaultInputsToLegacyForm($form, $user, $section);
262 }
263
264 return $this->addAdditionalInputsToLegacyForm($form, $user, $section);
265 }
266
268 \ilPropertyFormGUI $form,
269 ?\ilObjUser $user,
270 array $section
272 return array_reduce(
273 $section,
274 function (\ilPropertyFormGUI $c, Setting $v) use ($user): \ilPropertyFormGUI {
275 $input = $v->getLegacyInput($this->lng, $this->settings, $user);
276 $c->addItem($input);
277 return $c;
278 },
279 $form
280 );
281 }
282
284 \ilPropertyFormGUI $form,
285 ?\ilObjUser $user,
286 array $section
288 $section_key = $this->buildSectionKey($section[0]);
289 $values = array_reduce(
290 $section,
291 function (array $c, Setting $v) use ($user): array {
292 $input = $v->getLegacyInput($this->lng, $this->settings, $user);
293 $input->setPostVar($v->getIdentifier());
294 $c['checkbox']->addSubItem($input);
295 $c['defaults'] .= "{$v->getLabel($this->lng)}: "
296 . "{$v->getDefaultValueForDisplay($this->lng, $this->settings)}; ";
297 if ($v->hasUserPersonalizedSetting($this->settings, $user)) {
298 $c['has_personalization'] = true;
299 }
300 return $c;
301 },
302 [
303 'checkbox' => new \ilCheckboxInputGUI(
304 $this->lng->txt("personalise_{$section_key}"),
305 $section_key
306 ),
307 'defaults' => "{$this->lng->txt('default')}<br>",
308 'has_personalization' => false
309 ]
310 );
311 $values['checkbox']->setInfo(trim($values['defaults']));
312 $values['checkbox']->setChecked($values['has_personalization']);
313
314 $form->addItem($values['checkbox']);
315
316 return $form;
317 }
318
319 private function buildSectionKey(Setting $setting): string
320 {
321 return $setting->getSettingsPage() === AvailablePages::MainSettings
322 ? $setting->getSection()->value
323 : $setting->getSettingsPage()->value;
324 }
325
327 Context $context,
328 array $settings
329 ): array {
330 return array_values(
331 array_filter(
332 $settings,
333 static fn(Setting $v): bool => $context->isSettingAvailableInType($v)
334 )
335 );
336 }
337
338 private function retrieveValuefromInputs(
339 \ilPropertyFormGUI|array $form,
340 Setting $setting
341 ): mixed {
342 if ($form instanceof \ilPropertyFormGUI) {
343 return $form->getInput($setting->getIdentifier());
344 }
345
346 $section_key = $this->buildSectionKey($setting);
347
348 if ($section_key === AvailableSections::Main->value) {
349 return $form[$setting->getIdentifier()];
350 }
351 return $form[$section_key][$setting->getIdentifier()];
352 }
353
354 private function checkStartingPointValue(\ilPropertyFormGUI $form): bool
355 {
356 return $form->getInput('additional') === ''
357 || $this->user_settings_repository->getByIdentifier('starting_point')->validateUserChoice($this->tpl, $this->lng, $form);
358 }
359}
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 Repository $user_settings_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