ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilUserXMLWriter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use ILIAS\User\BuildExportFieldArray;
25use ILIAS\User\Settings\DataRepository as UserSettingsDataRepository;
29
40{
41 use BuildExportFieldArray;
42
43 private readonly ILIAS $ilias;
44 private readonly ilDBInterface $db;
45 private readonly Language $lng;
46 private readonly Profile $user_profile;
48 private readonly UserSettingsDataRepository $user_settings_data_repo;
49 private array $users; // Missing array type.
50 private int $user_id = 0;
51 private bool $attach_roles = false;
52 private bool $attach_preferences = false;
53
58 private array $fields_to_export = [];
59
60 public function __construct()
61 {
63 global $DIC;
64
65 $this->ilias = $DIC['ilias'];
66 $this->db = $DIC['ilDB'];
67 $this->lng = $DIC['lng'];
68 $this->user_id = $DIC['ilUser']->getId();
69
70 $local_dic = LocalDIC::dic();
71 $this->user_profile = $local_dic[Profile::class];
72 $this->user_settings = LocalDIC::dic()[Settings::class];
73 $this->user_settings_data_repo = $local_dic[UserSettingsDataRepository::class];
74
75
76 $this->attach_roles = false;
77
79 }
80
81 public function setAttachRoles(bool $value): void
82 {
83 $this->attach_roles = $value;
84 }
85
86 public function setObjects(array $users): void // Missing array type.
87 {
88 $this->users = $users;
89 }
90
91
92 public function start(): bool
93 {
94 if (!is_array($this->users)) {
95 return false;
96 }
97
98 $this->__buildHeader();
99
100 $this->addUDFsToXML();
101
102 foreach ($this->users as $user) {
103 $this->__handleUser($user);
104 }
105
106 $this->__buildFooter();
107
108 return true;
109 }
110
111 public function getXML(): string
112 {
113 return $this->xmlDumpMem(false);
114 }
115
116
117 public function __buildHeader(): void
118 {
119 $this->xmlSetDtdDef('<!DOCTYPE Users PUBLIC "-//ILIAS//DTD UserImport//EN" "' . ILIAS_HTTP_PATH . '/components/ILIAS/Export/xml/ilias_user_5_1.dtd">');
120 $this->xmlSetGenCmt('User of ilias system');
121 $this->xmlHeader();
122
123 $this->xmlStartTag('Users');
124 }
125
126 public function __buildFooter(): void
127 {
128 $this->xmlEndTag('Users');
129 }
130
131 public function __handleUser(array $row): void // Missing array type.
132 {
133 if ($this->fields_to_export === []) {
134 $this->setFieldsToExport(
135 array_merge(
136 $this->getExportFieldArray(
137 $this->lng,
138 $this->user_profile,
139 $this->user_settings
140 ),
141 ['time_limit_owner']
142 )
143 );
144 }
145
146 $settings = $this->user_settings_data_repo->getFor($row['usr_id']);
147
148 if ($row['language'] === null
149 || $row['language'] === '') {
150 $row['language'] = $this->lng->getDefaultLanguage();
151 }
152
153 $attrs = [
154 'Id' => 'il_' . IL_INST_ID . '_usr_' . $row['usr_id'],
155 'Language' => $row['language'],
156 'Action' => 'Update'
157 ];
158
159 $this->xmlStartTag('User', $attrs);
160
161 $this->xmlElement('Login', null, $row['login']);
162
163 if ($this->attach_roles == true) {
164 $query = sprintf(
165 'SELECT object_data.title, object_data.description, rbac_fa.* ' .
166 'FROM object_data, rbac_ua, rbac_fa WHERE rbac_ua.usr_id = %s ' .
167 'AND rbac_ua.rol_id = rbac_fa.rol_id AND object_data.obj_id = rbac_fa.rol_id',
168 $this->db->quote($row['usr_id'], 'integer')
169 );
170 $rbacresult = $this->db->query($query);
171
172 while ($rbacrow = $rbacresult->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
173 if ($rbacrow['assign'] != 'y') {
174 continue;
175 }
176
177 $type = '';
178
179 if ($rbacrow['parent'] == ROLE_FOLDER_ID) {
180 $type = 'Global';
181 } else {
182 $type = 'Local';
183 }
184 if ($type !== '') {
185 $this->xmlElement(
186 'Role',
187 ['Id' => 'il_' . IL_INST_ID . '_role_' . $rbacrow['rol_id'], 'Type' => $type],
188 $rbacrow['title']
189 );
190 }
191 }
192 }
193
194 $this->__addElement('Firstname', $row['firstname']);
195 $this->__addElement('Lastname', $row['lastname']);
196 $this->__addElement('Title', $row['title']);
197
198 if ($this->canExport('PersonalPicture', 'upload')) {
199 $imageData = $this->getPictureValue($row['usr_id']);
200 if ($imageData) {
201 $value = array_shift($imageData); //$imageData['value'];
202 $this->__addElement('PersonalPicture', $value, $imageData, 'upload');
203 }
204 }
205
206
207 $this->__addElement('Gender', $row['gender']);
208 $this->__addElement('Email', $row['email']);
209 $this->__addElement('SecondEmail', $row['second_email'], null, 'second_email');
210 $this->__addElement('Birthday', $row['birthday']);
211 $this->__addElement('Institution', $row['institution']);
212 $this->__addElement('Street', $row['street']);
213 $this->__addElement('City', $row['city']);
214 $this->__addElement('PostalCode', $row['zipcode'], null, 'zipcode');
215 $this->__addElement('Country', $row['country']);
216 $this->__addElement('PhoneOffice', $row['phone_office'], null, 'phone_office');
217 $this->__addElement('PhoneHome', $row['phone_home'], null, 'phone_home');
218 $this->__addElement('PhoneMobile', $row['phone_mobile'], null, 'phone_mobile');
219 $this->__addElement('Fax', $row['fax']);
220 $this->__addElement('Hobby', $row['hobby']);
221
222 $this->__addElementMulti('GeneralInterest', $row['interests_general'] ?? [], null, 'interests_general');
223 $this->__addElementMulti('OfferingHelp', $row['interests_help_offered'] ?? [], null, 'interests_help_offered');
224 $this->__addElementMulti('LookingForHelp', $row['interests_help_looking'] ?? [], null, 'interests_help_looking');
225
226 $this->__addElement('Department', $row['department']);
227 $this->__addElement('Comment', $row['referral_comment'], null, 'referral_comment');
228 $this->__addElement('Matriculation', $row['matriculation']);
229 $this->__addElement('Active', $row['active'] ? 'true' : 'false');
230 $this->__addElement('ClientIP', $row['client_ip'], null, 'client_ip');
231 $this->__addElement('TimeLimitOwner', (string) $row['time_limit_owner'], null, 'time_limit_owner');
232 $this->__addElement('TimeLimitUnlimited', (string) $row['time_limit_unlimited'], null, 'time_limit_unlimited');
233 $this->__addElement('TimeLimitFrom', (string) $row['time_limit_from'], null, 'time_limit_from');
234 $this->__addElement('TimeLimitUntil', (string) $row['time_limit_until'], null, 'time_limit_until');
235 $this->__addElement('ApproveDate', $row['approve_date'], null, 'approve_date');
236 $this->__addElement('AgreeDate', $row['agree_date'], null, 'agree_date');
237
238 if ($row['auth_mode'] !== null
239 && $row['auth_mode'] !== '') {
240 $this->__addElement('AuthMode', null, ['type' => $row['auth_mode']], 'auth_mode', true);
241 }
242
243 if ($row['ext_account'] !== null
244 && $row['ext_account'] !== '') {
245 $this->__addElement('ExternalAccount', $row['ext_account'], null, 'ext_account', true);
246 }
247
248 if (isset($settings['skin'])
249 && isset($settings['style'])
250 && $this->canExport('Look', 'skin_style')) {
251 $this->__addElement(
252 'Look',
253 null,
254 [
255 'Skin' => $settings['skin'], 'Style' => $settings['style']
256 ],
257 'skin_style',
258 true
259 );
260 }
261
262
263 $this->__addElement('LastUpdate', $row['last_update'], null, 'last_update');
264 $this->__addElement('LastLogin', $row['last_login'], null, 'last_login');
265
266 $udf_data = $this->addUDFsToXML();
267
268 $this->__addElement('AccountInfo', $row['ext_account'], ['Type' => 'external']);
269
270 $this->__addElement('GMapsInfo', null, [
271 'longitude' => $row['longitude'],
272 'latitude' => $row['latitude'],
273 'zoom' => $row['loc_zoom']]);
274
275 $this->__addElement('Feedhash', $row['feed_hash']);
276
277 if ($this->attach_preferences || $this->canExport('prefs', 'preferences')) {
278 $this->__handlePreferences($settings, $row);
279 }
280
281 $this->xmlEndTag('User');
282 }
283
284
285 private function __handlePreferences(array $prefs, array $row): void // Missing array type.
286 {
287 //todo nadia: test mail_address_option
288 $mailOptions = new ilMailOptions($row['usr_id']);
289 $prefs['mail_incoming_type'] = $mailOptions->getIncomingType();
290 $prefs['mail_address_option'] = $mailOptions->getEmailAddressMode();
291 $prefs['mail_signature'] = $mailOptions->getSignature();
292 if ($prefs !== []) {
293 $this->xmlStartTag('Prefs');
294 foreach ($prefs as $key => $value) {
295 if (self::isPrefExportable($key)) {
296 $this->xmlElement('Pref', ['key' => $key], $value);
297 }
298 }
299 $this->xmlEndTag('Prefs');
300 }
301 }
302
303 public function __addElementMulti(
304 string $tagname,
305 array $value,
306 ?array $attrs = null,
307 ?string $settingsname = null,
308 bool $requiredTag = false
309 ): void {
310 foreach ($value as $item) {
311 $this->__addElement($tagname, $item, $attrs, $settingsname, $requiredTag);
312 }
313 }
314
315 public function __addElement(
316 string $tagname,
317 ?string $value,
318 ?array $attrs = null,
319 ?string $settingsname = null,
320 bool $requiredTag = false
321 ): void {
322 if ($this->canExport($tagname, $settingsname)
323 && ($value !== null
324 || $requiredTag
325 || is_array($attrs) && count($attrs) > 0)) {
326 $this->xmlElement($tagname, $attrs, (string) $value);
327 }
328 }
329
330 private function canExport(
331 string $tagname,
332 ?string $settingsname = null
333 ): bool {
334 return $this->fields_to_export === []
335 || in_array(strtolower($tagname), $this->fields_to_export) !== false
336 || in_array($settingsname, $this->fields_to_export) !== false;
337 }
338
339 public function setFieldsToExport(array $fields_to_export): void // Missing array type.
340 {
341 $this->fields_to_export = $fields_to_export;
342 }
343
347 public function setAttachPreferences(bool $attach_preferences): void
348 {
349 $this->attach_preferences = $attach_preferences;
350 }
351
356 private function getPictureValue(int $usr_id): ?array
357 {
358 $avatar_resolver = new ilUserAvatarResolver($usr_id);
359 $avatar_resolver->setForcePicture(true);
360 if (!$avatar_resolver->hasProfilePicture()) {
361 return null;
362 }
363
364 [$image_data, $image_type] = $avatar_resolver->getUserPictureForVCard();
365
366 if ($image_data === null || $image_type === null) {
367 return null;
368 }
369
370 return [
371 'value' => base64_encode($image_data),
372 'encoding' => 'Base64',
373 'imagetype' => $image_type
374 ];
375 }
376
380 private function addUDFsToXML(): void
381 {
382 foreach ($this->user_profile->getVisibleFields(Context::Export) as $field) {
383 if (!$field->isCustom()) {
384 continue;
385 }
386 $this->xmlElement(
387 'UserDefinedField',
388 [
389 'Id' => $field->getIdentifier(),
390 'Name' => $field->getLabel($this->lng)
391 ],
392 (string) ($this->user_data['f_' . $field->getIdentifier()] ?? '')
393 );
394 }
395 }
396
400 public static function getExportablePreferences(): array // Missing array type.
401 {
402 return [
403 'public_city',
404 'public_country',
405 'public_department',
406 'public_email',
407 'public_second_email',
408 'public_fax',
409 'public_hobby',
410 'public_institution',
411 'public_matriculation',
412 'public_phone',
413 'public_phone_home',
414 'public_phone_mobile',
415 'public_phone_office',
416 'public_profile',
417 'public_street',
418 'public_avatar',
419 'public_zip',
420 'send_info_mails',
421 /*'show_users_online',*/
422 'hide_own_online_status',
423 'bs_allow_to_contact_me',
424 'chat_osc_accept_msg',
425 'chat_broadcast_typing',
426 'user_tz',
427 'weekstart',
428 'mail_incoming_type',
429 'mail_signature',
430 'mail_linebreak',
431 'public_interests_general',
432 'public_interests_help_offered',
433 'public_interests_help_looking'
434 ];
435 }
436
440 public static function isPrefExportable(string $key): bool
441 {
442 return in_array($key, self::getExportablePreferences());
443 }
444}
Class ilUserAvatarResolver.
XML writer class Class to simplify manual writing of xml documents.
setAttachPreferences(bool $attach_preferences)
if set to true, all preferences of a user will be set
getPictureValue(int $usr_id)
return array with base-encoded picture data as key value, encoding type as encoding,...
static isPrefExportable(string $key)
returns wether a key from db is exportable or not
array $fields_to_export
fields to be exported
canExport(string $tagname, ?string $settingsname=null)
readonly ilDBInterface $db
readonly SettingsImplementation $user_settings
readonly Language $lng
addUDFsToXML()
add user defined field data to xml (using usr dtd)
__handlePreferences(array $prefs, array $row)
static getExportablePreferences()
return exportable preference keys as found in db
readonly Profile $user_profile
readonly UserSettingsDataRepository $user_settings_data_repo
setObjects(array $users)
setFieldsToExport(array $fields_to_export)
__addElementMulti(string $tagname, array $value, ?array $attrs=null, ?string $settingsname=null, bool $requiredTag=false)
setAttachRoles(bool $value)
__addElement(string $tagname, ?string $value, ?array $attrs=null, ?string $settingsname=null, bool $requiredTag=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlSetGenCmt(string $genCmt)
Sets generated comment.
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
__construct(string $version="1.0", string $outEnc="utf-8", string $inEnc="utf-8")
xmlHeader()
Writes xml header.
xmlEndTag(string $tag)
Writes an endtag.
xmlSetDtdDef(string $dtdDef)
Sets dtd definition.
xmlDumpMem(bool $format=true)
Returns xml document from memory.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
const IL_INST_ID
Definition: constants.php:40
const ROLE_FOLDER_ID
Definition: constants.php:34
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilDBInterface.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
Class ilObjForumAdministration.
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26