ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
ProfileImplementation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\User\Profile;
22
24use ILIAS\User\Profile\Fields\Field as ProfileField;
25use ILIAS\User\Profile\Fields\AvailableSections as AvailableProfileSections;
26use ILIAS\User\Profile\Fields\ConfigurationRepository as FieldsConfigurationRepository;
29
31{
35 private array $user_fields;
36
37 public function __construct(
38 private readonly Language $lng,
39 private readonly FieldsConfigurationRepository $profile_fields_repository,
40 private readonly DataRepository $profile_data_repository
41 ) {
42 $this->user_fields = $this->profile_fields_repository->get();
43 }
44
45 public function getFields(
46 array $sections_to_skip = [],
47 array $fields_to_skip = []
48 ): array {
49 return array_reduce(
50 $this->user_fields,
51 static function (array $c, ProfileField $v) use ($sections_to_skip, $fields_to_skip): array {
52 if (!in_array($v->getSection(), $sections_to_skip)
53 && !in_array(get_class($v->getDefinition()), $fields_to_skip)) {
54 $c[$v->getIdentifier()] = $v;
55 }
56 return $c;
57 },
58 []
59 );
60 }
61
62 public function getVisibleFields(
63 Context $context,
64 ?\ilObjUser $user = null,
65 array $sections_to_skip = [],
66 array $fields_to_skip = []
67 ): array {
68 return array_filter(
69 $this->user_fields,
70 static fn(ProfileField $v) => !in_array($v->getSection(), $sections_to_skip)
71 && !in_array($v->getDefinition()::class, $fields_to_skip)
72 && $context->isFieldVisible($v, $user)
73 ? true : false
74 );
75 }
76
77 public function getFieldByIdentifier(string $identifier): ?ProfileField
78 {
79 return $this->profile_fields_repository->getByIdentifier($identifier);
80 }
81
82 public function getFieldByClass(string $class): ?ProfileField
83 {
84 return $this->profile_fields_repository->getByClass($class);
85 }
86
87 public function addFieldsToForm(
88 \ilPropertyFormGUI $form,
89 Context $context,
90 bool $do_require,
91 ?\ilObjUser $user,
92 array $fields_to_skip = []
94 return array_reduce(
95 $this->getVisibleFieldsBySection($context, $user, $fields_to_skip),
96 function (\ilPropertyFormGUI $c, array $v) use ($context, $user, $do_require): \ilPropertyFormGUI {
97 $section_header = new \ilFormSectionHeaderGUI();
98 $section_header->setTitle($this->lng->txt($v[0]->getSection()->value));
99 $c->addItem($section_header);
100 return $this->addSectionFieldsToForm($c, $context, $do_require, $user, $v);
101 },
102 $form
103 );
104 }
105
106 public function addFormValuesToUser(
107 \ilPropertyFormGUI $form,
108 Context $context,
109 \ilObjUser $user,
110 array $skip_fields = []
111 ): \ilObjUser {
112 return array_reduce(
113 $this->getVisibleFields($context, $user, [], $skip_fields),
114 static function (\ilObjUser $c, ProfileField $v) use ($form, $context, $user): \ilObjUser {
115 if ($form->getItemByPostVar($v->getIdentifier())->getDisabled()) {
116 return $c;
117 }
118 return $v->addValueToUserObject(
119 $c,
120 $context,
121 $form->getInput($v->getIdentifier()),
122 $form
123 );
124 },
125 $user
126 );
127 }
128
129 public function getDataFor(int $usr_id): Data
130 {
131 return $this->profile_data_repository->getSingle($usr_id);
132 }
133
134 public function getDataForMultiple(
135 array $usr_ids
136 ): \Generator {
137 return $this->profile_data_repository->getMultiple($usr_ids);
138 }
139
140 public function isProfileIncomplete(\ilObjUser $user): bool
141 {
142 foreach ($this->user_fields as $field) {
143 if (!$field->isVisibleToUser()) {
144 continue;
145 }
146
147 if ($field->isRequired() && empty($field->retrieveValueFromUser($user))) {
148 return true;
149 }
150 }
151
152 return false;
153 }
154
155 public function userFieldVisibleToUser(
156 string $setting_identifier
157 ): bool {
158 $field = $this->profile_fields_repository->getByIdentifier($setting_identifier);
159 if ($field === null) {
160 return false;
161 }
162
163 return $field->isVisibleToUser();
164 }
165
166 public function userFieldEditableByUser(string $setting): bool
167 {
168 $field = $this->profile_fields_repository->getByIdentifier($setting);
169 if ($field === null) {
170 return false;
171 }
172 return $field->isVisibleToUser() && $field->isChangeableByUser();
173 }
174
175 public function getIgnorableRequiredFields(): array
176 {
177 return array_reduce(
178 $this->user_fields,
179 static function (array $c, ProfileField $v): array {
180 if ($v->getIdentifier() === 'username'
181 || $v->getIdentifier() === 'password'
182 || $v->isRequired()
183 || !$v->isChangeableByUser()) {
184 return $c;
185 }
186 $c[] = $v;
187 return $c;
188 },
189 []
190 );
191 }
192
193 public function getAllUserDefinedFields(): array
194 {
195 return array_reduce(
196 $this->user_fields,
197 static function (array $c, ProfileField $v): array {
198 if ($v->isCustom()) {
199 $c[$v->getIdentifier()] = $v;
200 }
201 return $c;
202 },
203 []
204 );
205 }
206
208 Context $context
209 ): array {
210 return array_reduce(
211 $this->getVisibleFields($context),
212 static function (array $c, ProfileField $v): array {
213 if ($v->isCustom()) {
214 $c[$v->getIdentifier()] = $v;
215 }
216 return $c;
217 },
218 []
219 );
220 }
221
225 public function tempStorePicture(
226 \ilPropertyFormGUI $form
228 return $this->profile_fields_repository->getByClass(Fields\Standard\Avatar::class)
229 ->getDefinition()->tempStorePicture($form);
230 }
231
236 Context $context,
237 ?\ilObjUser $user,
238 array $fields_to_skip = []
239 ): array {
240 return array_filter(
241 array_reduce(
242 $this->getVisibleFields($context, $user, [], $fields_to_skip),
243 static function (array $c, ProfileField $v): array {
244 $c[$v->getSection()->value][] = $v;
245 return $c;
246 },
247 array_reduce(
248 AvailableProfileSections::cases(),
249 static function (array $c, AvailableProfileSections $v): array {
250 $c[$v->value] = [];
251 return $c;
252 },
253 []
254 )
255 )
256 );
257 }
258
259 private function addSectionFieldsToForm(
260 \ilPropertyFormGUI $form,
261 Context $context,
262 bool $do_require,
263 ?\ilObjUser $user,
264 array $fields
266 return array_reduce(
267 $fields,
268 function (\ilPropertyFormGUI $form, ProfileField $v) use ($context, $user, $do_require): \ilPropertyFormGUI {
269 $input = $v->getLegacyInput($this->lng, $context, $user);
270 $input->setDisabled(!$context->isFieldChangeable($v, $user));
271 $input->setRequired($do_require && $input->getRequired());
272 $form->addItem($input);
273 return $form;
274 },
275 $form
276 );
277 }
278}
getVisibleFields(Context $context, ?\ilObjUser $user=null, array $sections_to_skip=[], array $fields_to_skip=[])
getFields(array $sections_to_skip=[], array $fields_to_skip=[])
__construct(private readonly Language $lng, private readonly FieldsConfigurationRepository $profile_fields_repository, private readonly DataRepository $profile_data_repository)
getVisibleFieldsBySection(Context $context, ?\ilObjUser $user, array $fields_to_skip=[])
addFormValuesToUser(\ilPropertyFormGUI $form, Context $context, \ilObjUser $user, array $skip_fields=[])
addFieldsToForm(\ilPropertyFormGUI $form, Context $context, bool $do_require, ?\ilObjUser $user, array $fields_to_skip=[])
addSectionFieldsToForm(\ilPropertyFormGUI $form, Context $context, bool $do_require, ?\ilObjUser $user, array $fields)
userFieldVisibleToUser(string $setting_identifier)
return true
This class represents a section header in a property 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-...
$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...
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'))