ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 $groups_to_skip = [],
46 array $fields_to_skip = []
47 ): array {
48 return array_reduce(
49 $this->user_fields,
50 function (array $c, ProfileField $v) use ($groups_to_skip, $fields_to_skip): array {
51 if (!in_array($v->getSection(), $groups_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 $groups_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(), $groups_to_skip)
73 && !in_array(get_class($v->getDefinition()), $fields_to_skip)
74 && $context->isFieldVisibleInType($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 addFieldsToForm(
85 \ilPropertyFormGUI $form,
86 Context $context,
87 bool $do_require,
88 ?\ilObjUser $user,
89 array $fields_to_skip = []
91 return array_reduce(
92 $this->getVisibleFieldsBySection($context, $user, $fields_to_skip),
93 function (\ilPropertyFormGUI $c, array $v) use ($context, $user, $do_require): \ilPropertyFormGUI {
94 $section_header = new \ilFormSectionHeaderGUI();
95 $section_header->setTitle($this->lng->txt($v[0]->getSection()->value));
96 $c->addItem($section_header);
97 return $this->addSectionFieldsToForm($c, $context, $do_require, $user, $v);
98 },
99 $form
100 );
101 }
102
103 public function addFormValuesToUser(
104 \ilPropertyFormGUI $form,
105 Context $context,
106 \ilObjUser $user
107 ): \ilObjUser {
108 return array_reduce(
109 $this->getVisibleFields($context, $user),
110 static function (\ilObjUser $c, ProfileField $v) use ($form, $context, $user): \ilObjUser {
111 if ($form->getItemByPostVar($v->getIdentifier())->getDisabled()) {
112 return $c;
113 }
114 return $v->addValueToUserObject(
115 $c,
116 $context,
117 $form->getInput($v->getIdentifier()),
118 $form
119 );
120 },
121 $user
122 );
123 }
124
125 public function getDataFor(int $usr_id): Data
126 {
127 return $this->profile_data_repository->getSingle($usr_id);
128 }
129
130 public function getDataForMultiple(
131 array $usr_ids
132 ): \Generator {
133 return $this->profile_data_repository->getMultiple($usr_ids);
134 }
135
136 public function isProfileIncomplete(\ilObjUser $user): bool
137 {
138 foreach ($this->user_fields as $field) {
139 if (!$field->isVisibleToUser()) {
140 continue;
141 }
142
143 if ($field->isRequired() && empty($field->retrieveValueFromUser($user))) {
144 return true;
145 }
146 }
147
148 return false;
149 }
150
151 public function userFieldVisibleToUser(
152 string $setting_identifier
153 ): bool {
154 $field = $this->profile_fields_repository->getByIdentifier($setting_identifier);
155 if ($field === null) {
156 return false;
157 }
158
159 return $field->isVisibleToUser();
160 }
161
162 public function userFieldEditableByUser(string $setting): bool
163 {
164 $field = $this->profile_fields_repository->getByIdentifier($setting);
165 if ($field === null) {
166 return false;
167 }
168 return $field->isVisibleToUser() && $field->isChangeableByUser();
169 }
170
171 public function getIgnorableRequiredFields(): array // Missing array type.
172 {
173 return array_reduce(
174 $this->user_fields,
175 static function (array $c, ProfileField $v): array {
176 if ($v->getIdentifier() === 'username'
177 || $v->getIdentifier() === 'password'
178 || $v->isRequired()
179 || !$v->isChangeableByUser()) {
180 return $c;
181 }
182 $c[] = $v;
183 return $c;
184 },
185 []
186 );
187 }
188
193 public function getAllUserDefinedFields(): array
194 {
195 return array_reduce(
196 $this->user_fields,
197 function (array $c, ProfileField $v): array {
198 if ($v->isCustom()) {
199 $c[$v->getIdentifier()] = $v;
200 }
201 return $c;
202 },
203 []
204 );
205 }
206
212 Context $context
213 ): array {
214 return array_reduce(
215 $this->getVisibleFields($context),
216 function (array $c, ProfileField $v): array {
217 if ($v->isCustom()) {
218 $c[$v->getIdentifier()] = $v;
219 }
220 return $c;
221 },
222 []
223 );
224 }
225
229 public function tempStorePicture(
230 \ilPropertyFormGUI $form
232 return $this->profile_fields_repository->getByClass(Fields\Standard\Avatar::class)
233 ->getDefinition()->tempStorePicture($form);
234 }
235
237 Context $context,
238 ?\ilObjUser $user,
239 array $fields_to_skip = []
240 ): array {
241 return array_filter(
242 array_reduce(
243 $this->getVisibleFields($context, $user, [], $fields_to_skip),
244 function (array $c, ProfileField $v): array {
245 $c[$v->getSection()->value][] = $v;
246 return $c;
247 },
248 array_reduce(
249 AvailableProfileSections::cases(),
250 static function (array $c, AvailableProfileSections $v): array {
251 $c[$v->value] = [];
252 return $c;
253 },
254 []
255 )
256 )
257 );
258 }
259
260 private function addSectionFieldsToForm(
261 \ilPropertyFormGUI $form,
262 Context $context,
263 bool $do_require,
264 ?\ilObjUser $user,
265 array $fields
267 return array_reduce(
268 $fields,
269 function (\ilPropertyFormGUI $form, ProfileField $v) use ($context, $user, $do_require): \ilPropertyFormGUI {
270 $input = $v->getLegacyInput($this->lng, $context, $user);
271 $input->setDisabled(!$context->isFieldChangeableInType($v, $user));
272 $input->setRequired($do_require && $v->isRequired());
273 $form->addItem($input);
274 return $form;
275 },
276 $form
277 );
278 }
279}
getFields(array $groups_to_skip=[], array $fields_to_skip=[])
getVisibleFields(Context $context, ?\ilObjUser $user=null, array $groups_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)
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