ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
UserProfileMigrations.php
Go to the documentation of this file.
1<?php
2
19namespace ILIAS\User\Setup;
20
26use ILIAS\Data\UUID\Factory as UUIDFactory;
27
29{
30 private const string MIGRATION_COMPLETED = 'usr_profile_migration_completed';
31 private \ilDBInterface $db;
32 private \ilSetting $settings;
34
35 public function getLabel(): string
36 {
37 return 'Clean-Up and Consolidate User Profile Fields';
38 }
39
41 {
42 return 1;
43 }
44
45 public function getPreconditions(Environment $environment): array
46 {
47 return [
48 new \ilDatabaseInitializedObjective(),
49 new \ilSettingsFactoryExistsObjective(),
50 new \ilDatabaseUpdatedObjective()
51 ];
52 }
53
54 public function prepare(Environment $environment): void
55 {
56 $this->db = $environment->getResource(Environment::RESOURCE_DATABASE);
57 $this->settings = $environment->getResource(Environment::RESOURCE_SETTINGS_FACTORY)->settingsFor();
58 $this->admin_interaction = $environment->getResource(Environment::RESOURCE_ADMIN_INTERACTION);
59 }
60
61 public function step(Environment $environment): void
62 {
65 $this->settings->set(self::MIGRATION_COMPLETED, '1');
66 }
67
68 public function getRemainingAmountOfSteps(): int
69 {
70 return $this->settings->get(self::MIGRATION_COMPLETED) === '1' ? 0 : 1;
71 }
72
73 private function migrateCustomFieldAccess(): void
74 {
75 if (!$this->db->tableColumnExists('udf_definition', 'prg_export')) {
76 return;
77 }
78
79 $custom_fields_query = $this->db->query('SELECT * FROM udf_definition');
80
81 while (($row = $this->db->fetchObject($custom_fields_query))) {
82 $this->insertConfig(
83 $row->field_id,
84 $row->registration_visible,
85 $row->visible,
86 $row->visible_lua,
87 $row->course_export,
88 $row->group_export,
89 $row->prg_export,
90 $row->changeable,
91 $row->changeable_lua,
92 $row->required,
93 $row->export,
94 $row->searchable,
95 $row->certificate
96 );
97 }
98 $this->db->dropTableColumn(
99 'udf_definition',
100 'visible'
101 );
102 $this->db->dropTableColumn(
103 'udf_definition',
104 'changeable'
105 );
106 $this->db->dropTableColumn(
107 'udf_definition',
108 'required'
109 );
110 $this->db->dropTableColumn(
111 'udf_definition',
112 'searchable'
113 );
114 $this->db->dropTableColumn(
115 'udf_definition',
116 'export'
117 );
118 $this->db->dropTableColumn(
119 'udf_definition',
120 'course_export'
121 );
122 $this->db->dropTableColumn(
123 'udf_definition',
124 'registration_visible'
125 );
126 $this->db->dropTableColumn(
127 'udf_definition',
128 'visible_lua'
129 );
130 $this->db->dropTableColumn(
131 'udf_definition',
132 'changeable_lua'
133 );
134 $this->db->dropTableColumn(
135 'udf_definition',
136 'group_export'
137 );
138 $this->db->dropTableColumn(
139 'udf_definition',
140 'certificate'
141 );
142 $this->db->dropTableColumn(
143 'udf_definition',
144 'prg_export'
145 );
146 }
147
148 private function migrateStandardFieldConfig(): void
149 {
150 $field_ids = [
151 'username',
152 'firstname',
153 'lastname',
154 'title',
155 'birthday',
156 'gender',
157 'avatar',
158 'roles',
159 'org_units',
160 'interests_general' ,
161 'interests_help_offered',
162 'interests_help_looking',
163 'institution',
164 'department',
165 'street',
166 'zipcode',
167 'city',
168 'country',
169 'phone_office',
170 'phone_home',
171 'phone_mobile',
172 'fax',
173 'email',
174 'second_email',
175 'hobby',
176 'referral_comment',
177 'matriculation',
178 'awrn_user_show',
179 'allow_contact_request',
180 'incoming_mail',
181 'language',
182 'skin_style',
183 'session_reminder'
184 ];
185
186 $property_attributes = [
187 'usr_settings_visib_reg',
188 'usr_settings_hide',
189 'usr_settings_visib_lua',
190 'usr_settings_course_export',
191 'usr_settings_group_export',
192 'usr_settings_prg_export',
193 'usr_settings_disable',
194 'usr_settings_changeable_lua',
195 'require',
196 'usr_settings_export',
197 'search_enabled',
198 'certificate',
199 ];
200
201 $this->updateCountryField($property_attributes);
202 foreach ($field_ids as $field_id) {
203 $this->insertConfig(
204 $field_id,
205 ...$this->fetchConfigValuesFromSettings($field_id, $property_attributes)
206 );
207
208 foreach ($property_attributes as $attribute) {
209 $this->settings->delete("{$attribute}_{$field_id}");
210 }
211 }
212 }
213
214 private function updateCountryField(array $property_attributes): void
215 {
216 $message = 'ILIAS up to now knows two types of country information: One selectable by a dropdown '
217 . 'the other one as a text field. The latter one will be removed. Would you like us to move '
218 . 'the current information in the text field to a custom field with the name "Country"? If you '
219 . 'choose to not move the information it will simply be deleted.';
220
221 if ($this->admin_interaction->confirmOrDeny($message)) {
222 $uuid = (new UUIDFactory())->uuid4AsString();
223 $this->db->insert(
224 'udf_definition',
225 [
226 'field_id' => [
228 $uuid
229 ],
230 'field_name' => [
232 'Country'
233 ],
234 'field_type' => [
236 Text::class
237 ],
238 'section' => [
240 AvailableSections::ContactData->value
241 ]
242 ]
243 );
244 $this->insertConfig(
245 $uuid,
246 ...$this->fetchConfigValuesFromSettings('old_country', $property_attributes)
247 );
248 $query = $this->db->query(
249 'SELECT usr_id, old_country FROM usr_data WHERE old_country IS NOT NULL AND NOT old_country = ""'
250 );
251
252 $insert = [];
253 while (($row = $this->db->fetchObject($query))) {
254 $insert[] = "('{$row->usr_id}', '{$uuid}', {$this->db->quote($row->old_country, \ilDBConstants::T_TEXT)})";
255 }
256
257 if ($insert === []) {
258 return;
259 }
260
261 $this->db->manipulate(
262 'INSERT INTO usr_profile_data (usr_id, field_id, value) VALUES ' . implode(',', $insert)
263 );
264 }
265
266 foreach ($property_attributes as $attribute) {
267 $this->settings->delete("{$attribute}_old_country");
268 }
269 $this->db->dropTableColumn('usr_data', 'old_country');
270 }
271
273 string $field_id,
274 array $attributes
275 ): array {
276 return array_map(
277 function (string $v) use ($field_id): bool {
278 if ($field_id === 'username' && $v === 'usr_settings_disable') {
279 return $this->settings->get('allow_change_loginname', '0') === '1';
280 }
281 $value = $this->retrievePropertyAttributeValue($field_id, $v);
282 if (in_array($v, ['usr_settings_hide', 'usr_settings_disable'])) {
283 return !$value;
284 }
285 return $value;
286 },
287 $attributes
288 );
289 }
290
291 private function retrievePropertyAttributeValue(string $field_id, string $attribute): bool
292 {
293 return $this->settings->get("{$attribute}_{$field_id}", '0') === '1';
294 }
295
296 private function insertConfig(
297 string $field_id,
298 int $visible_in_registration,
299 int $visible_to_user,
300 int $visible_in_lua,
301 int $visible_in_crss,
302 int $visible_in_grps,
303 int $visible_in_prgs,
304 int $changeable_by_user,
305 int $changeable_in_lua,
306 int $required,
307 int $export,
308 int $searchable,
309 int $available_in_certs
310 ): void {
311 if ($this->db->fetchAll(
312 $this->db->query(
313 "SELECT count(field_id) as cnt FROM usr_field_config WHERE field_id='{$field_id}'"
314 ),
315 \ilDBConstants::FETCHMODE_OBJECT
316 )[0]->cnt !== 0) {
317 return;
318 }
319 $this->db->insert(
320 'usr_field_config',
321 [
322 'field_id' => [
324 $field_id
325 ],
326 'visible_in_registration' => [
328 $visible_in_registration
329 ],
330 'visible_to_user' => [
332 $visible_to_user
333 ],
334 'visible_in_lua' => [
336 $visible_in_lua
337 ],
338 'visible_in_crss' => [
340 $visible_in_crss
341 ],
342 'visible_in_grps' => [
344 $visible_in_grps
345 ],
346 'visible_in_prgs' => [
348 $visible_in_prgs
349 ],
350 'changeable_by_user' => [
352 $changeable_by_user
353 ],
354 'changeable_in_lua' => [
356 $changeable_in_lua
357 ],
358 'required' => [
360 $required
361 ],
362 'export' => [
364 $export
365 ],
366 'searchable' => [
368 $searchable
369 ],
370 'available_in_certs' => [
372 $available_in_certs
373 ]
374 ]
375 );
376 }
377}
getPreconditions(Environment $environment)
Objectives the migration depend on.
getDefaultAmountOfStepsPerRun()
Tell the default amount of steps to be executed for one run of the migration.
updateCountryField(array $property_attributes)
retrievePropertyAttributeValue(string $field_id, string $attribute)
step(Environment $environment)
Run one step of the migration.
prepare(Environment $environment)
Prepare the migration by means of some environment.
insertConfig(string $field_id, int $visible_in_registration, int $visible_to_user, int $visible_in_lua, int $visible_in_crss, int $visible_in_grps, int $visible_in_prgs, int $changeable_by_user, int $changeable_in_lua, int $required, int $export, int $searchable, int $available_in_certs)
fetchConfigValuesFromSettings(string $field_id, array $attributes)
getRemainingAmountOfSteps()
Count up how many "things" need to be migrated.
Class ilDBConstants.
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This defines ways in which objectives may interact with admins during the setup.
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
A migration is a potentially long lasting operation that can be broken into discrete steps.
Definition: Migration.php:29
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Agent.php:21
if(!file_exists('../ilias.ini.php'))
$message
Definition: xapiexit.php:31