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