ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilUserXMLWriter.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
35{
36 private ILIAS $ilias;
38 private Language $lng;
40 private array $users; // Missing array type.
41 private int $user_id = 0;
42 private bool $attach_roles = false;
43 private bool $attach_preferences = false;
44
49 private array $settings = [];
50
51 public function __construct()
52 {
54 global $DIC;
55
56 $this->ilias = $DIC['ilias'];
57 $this->db = $DIC['ilDB'];
58 $this->lng = $DIC['lng'];
59 $this->user_id = $DIC['ilUser']->getId();
60
61 $this->attach_roles = false;
62
64 }
65
66 public function setAttachRoles(bool $value): void
67 {
68 $this->attach_roles = $value;
69 }
70
71 public function setObjects(array $users): void // Missing array type.
72 {
73 $this->users = $users;
74 }
75
76
77 public function start(): bool
78 {
79 if (!is_array($this->users)) {
80 return false;
81 }
82
83 $this->__buildHeader();
84
85 $this->addUDFsToXML();
86
87 foreach ($this->users as $user) {
88 $this->__handleUser($user);
89 }
90
91 $this->__buildFooter();
92
93 return true;
94 }
95
96 public function getXML(): string
97 {
98 return $this->xmlDumpMem(false);
99 }
100
101
102 public function __buildHeader(): void
103 {
104 $this->xmlSetDtdDef('<!DOCTYPE Users PUBLIC "-//ILIAS//DTD UserImport//EN" "' . ILIAS_HTTP_PATH . '/components/ILIAS/Export/xml/ilias_user_5_1.dtd">');
105 $this->xmlSetGenCmt('User of ilias system');
106 $this->xmlHeader();
107
108 $this->xmlStartTag('Users');
109 }
110
111 public function __buildFooter(): void
112 {
113 $this->xmlEndTag('Users');
114 }
115
116 public function __handleUser(array $row): void // Missing array type.
117 {
118 if ($this->settings === []) {
120 }
121
122 $prefs = ilObjUser::_getPreferences($row['usr_id']);
123
124 if ($row['language'] === null
125 || $row['language'] === '') {
126 $row['language'] = $this->lng->getDefaultLanguage();
127 }
128
129 $attrs = [
130 'Id' => 'il_' . IL_INST_ID . '_usr_' . $row['usr_id'],
131 'Language' => $row['language'],
132 'Action' => 'Update'
133 ];
134
135 $this->xmlStartTag('User', $attrs);
136
137 $this->xmlElement('Login', null, $row['login']);
138
139 if ($this->attach_roles == true) {
140 $query = sprintf(
141 'SELECT object_data.title, object_data.description, rbac_fa.* ' .
142 'FROM object_data, rbac_ua, rbac_fa WHERE rbac_ua.usr_id = %s ' .
143 'AND rbac_ua.rol_id = rbac_fa.rol_id AND object_data.obj_id = rbac_fa.rol_id',
144 $this->db->quote($row['usr_id'], 'integer')
145 );
146 $rbacresult = $this->db->query($query);
147
148 while ($rbacrow = $rbacresult->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
149 if ($rbacrow['assign'] != 'y') {
150 continue;
151 }
152
153 $type = '';
154
155 if ($rbacrow['parent'] == ROLE_FOLDER_ID) {
156 $type = 'Global';
157 } else {
158 $type = 'Local';
159 }
160 if ($type !== '') {
161 $this->xmlElement(
162 'Role',
163 ['Id' => 'il_' . IL_INST_ID . '_role_' . $rbacrow['rol_id'], 'Type' => $type],
164 $rbacrow['title']
165 );
166 }
167 }
168 }
169
170 $this->__addElement('Firstname', $row['firstname']);
171 $this->__addElement('Lastname', $row['lastname']);
172 $this->__addElement('Title', $row['title']);
173
174 if ($this->canExport('PersonalPicture', 'upload')) {
175 $imageData = $this->getPictureValue($row['usr_id']);
176 if ($imageData) {
177 $value = array_shift($imageData); //$imageData['value'];
178 $this->__addElement('PersonalPicture', $value, $imageData, 'upload');
179 }
180 }
181
182
183 $this->__addElement('Gender', $row['gender']);
184 $this->__addElement('Email', $row['email']);
185 $this->__addElement('SecondEmail', $row['second_email'], null, 'second_email');
186 $this->__addElement('Birthday', $row['birthday']);
187 $this->__addElement('Institution', $row['institution']);
188 $this->__addElement('Street', $row['street']);
189 $this->__addElement('City', $row['city']);
190 $this->__addElement('PostalCode', $row['zipcode'], null, 'zipcode');
191 $this->__addElement('Country', $row['country']);
192 $this->__addElement('SelCountry', $row['sel_country'], null, 'sel_country');
193 $this->__addElement('PhoneOffice', $row['phone_office'], null, 'phone_office');
194 $this->__addElement('PhoneHome', $row['phone_home'], null, 'phone_home');
195 $this->__addElement('PhoneMobile', $row['phone_mobile'], null, 'phone_mobile');
196 $this->__addElement('Fax', $row['fax']);
197 $this->__addElement('Hobby', $row['hobby']);
198
199 $this->__addElementMulti('GeneralInterest', $row['interests_general'] ?? [], null, 'interests_general');
200 $this->__addElementMulti('OfferingHelp', $row['interests_help_offered'] ?? [], null, 'interests_help_offered');
201 $this->__addElementMulti('LookingForHelp', $row['interests_help_looking'] ?? [], null, 'interests_help_looking');
202
203 $this->__addElement('Department', $row['department']);
204 $this->__addElement('Comment', $row['referral_comment'], null, 'referral_comment');
205 $this->__addElement('Matriculation', $row['matriculation']);
206 $this->__addElement('Active', $row['active'] ? 'true' : 'false');
207 $this->__addElement('ClientIP', $row['client_ip'], null, 'client_ip');
208 $this->__addElement('TimeLimitOwner', $row['time_limit_owner'], null, 'time_limit_owner');
209 $this->__addElement('TimeLimitUnlimited', $row['time_limit_unlimited'], null, 'time_limit_unlimited');
210 $this->__addElement('TimeLimitFrom', $row['time_limit_from'], null, 'time_limit_from');
211 $this->__addElement('TimeLimitUntil', $row['time_limit_until'], null, 'time_limit_until');
212 $this->__addElement('TimeLimitMessage', $row['time_limit_message'], null, 'time_limit_message');
213 $this->__addElement('ApproveDate', $row['approve_date'], null, 'approve_date');
214 $this->__addElement('AgreeDate', $row['agree_date'], null, 'agree_date');
215
216 if ($row['auth_mode'] !== null
217 && $row['auth_mode'] !== '') {
218 $this->__addElement('AuthMode', null, ['type' => $row['auth_mode']], 'auth_mode', true);
219 }
220
221 if ($row['ext_account'] !== null
222 && $row['ext_account'] !== '') {
223 $this->__addElement('ExternalAccount', $row['ext_account'], null, 'ext_account', true);
224 }
225
226 if (isset($prefs['skin'])
227 && isset($prefs['style'])
228 && $this->canExport('Look', 'skin_style')) {
229 $this->__addElement(
230 'Look',
231 null,
232 [
233 'Skin' => $prefs['skin'], 'Style' => $prefs['style']
234 ],
235 'skin_style',
236 true
237 );
238 }
239
240
241 $this->__addElement('LastUpdate', $row['last_update'], null, 'last_update');
242 $this->__addElement('LastLogin', $row['last_login'], null, 'last_login');
243
244 $udf_data = $this->addUDFsToXML();
245
246 $this->__addElement('AccountInfo', $row['ext_account'], ['Type' => 'external']);
247
248 $this->__addElement('GMapsInfo', null, [
249 'longitude' => $row['longitude'],
250 'latitude' => $row['latitude'],
251 'zoom' => $row['loc_zoom']]);
252
253 $this->__addElement('Feedhash', $row['feed_hash']);
254
255 if ($this->attach_preferences || $this->canExport('prefs', 'preferences')) {
256 $this->__handlePreferences($prefs, $row);
257 }
258
259 $this->xmlEndTag('User');
260 }
261
262
263 private function __handlePreferences(array $prefs, array $row): void // Missing array type.
264 {
265 //todo nadia: test mail_address_option
266 $mailOptions = new ilMailOptions($row['usr_id']);
267 $prefs['mail_incoming_type'] = $mailOptions->getIncomingType();
268 $prefs['mail_address_option'] = $mailOptions->getEmailAddressMode();
269 $prefs['mail_signature'] = $mailOptions->getSignature();
270 if ($prefs !== []) {
271 $this->xmlStartTag('Prefs');
272 foreach ($prefs as $key => $value) {
273 if (self::isPrefExportable($key)) {
274 $this->xmlElement('Pref', ['key' => $key], $value);
275 }
276 }
277 $this->xmlEndTag('Prefs');
278 }
279 }
280
281 public function __addElementMulti(
282 string $tagname,
283 array $value,
284 ?array $attrs = null,
285 ?string $settingsname = null,
286 bool $requiredTag = false
287 ): void {
288 foreach ($value as $item) {
289 $this->__addElement($tagname, $item, $attrs, $settingsname, $requiredTag);
290 }
291 }
292
293 public function __addElement(
294 string $tagname,
295 ?string $value,
296 ?array $attrs = null,
297 ?string $settingsname = null,
298 bool $requiredTag = false
299 ): void {
300 if ($this->canExport($tagname, $settingsname)
301 && ($value !== null
302 || $requiredTag
303 || is_array($attrs) && count($attrs) > 0)) {
304 $this->xmlElement($tagname, $attrs, (string) $value);
305 }
306 }
307
308 private function canExport(
309 string $tagname,
310 ?string $settingsname = null
311 ): bool {
312 return $this->settings === []
313 || in_array(strtolower($tagname), $this->settings) !== false
314 || in_array($settingsname, $this->settings) !== false;
315 }
316
317 public function setSettings(array $settings): void // Missing array type.
318 {
319 $this->settings = $settings;
320 }
321
325 public function setAttachPreferences(bool $attach_preferences): void
326 {
327 $this->attach_preferences = $attach_preferences;
328 }
329
334 private function getPictureValue(int $usr_id): ?array
335 {
336 $avatar_resolver = new ilUserAvatarResolver($usr_id);
337 $avatar_resolver->setForcePicture(true);
338 if (!$avatar_resolver->hasProfilePicture()) {
339 return null;
340 }
341
342 [$image_data, $image_type] = $avatar_resolver->getUserPictureForVCard();
343
344 if ($image_data === null || $image_type === null) {
345 return null;
346 }
347
348 return [
349 'value' => base64_encode($image_data),
350 'encoding' => 'Base64',
351 'imagetype' => $image_type
352 ];
353 }
354
358 private function addUDFsToXML(): void
359 {
360 foreach ($this->user_profile->getVisibleFields(Context::Export) as $field) {
361 $this->xmlElement(
362 'UserDefinedField',
363 [
364 'Id' => $field->getIdentifier(),
365 'Name' => $field->getName($this->lng)
366 ],
367 (string) ($this->user_data['f_' . $field->getIdentifier()] ?? '')
368 );
369 }
370 }
371
375 public static function getExportablePreferences(): array // Missing array type.
376 {
377 return [
378 'public_city',
379 'public_country',
380 'public_department',
381 'public_email',
382 'public_second_email',
383 'public_fax',
384 'public_hobby',
385 'public_institution',
386 'public_matriculation',
387 'public_phone',
388 'public_phone_home',
389 'public_phone_mobile',
390 'public_phone_office',
391 'public_profile',
392 'public_street',
393 'public_upload',
394 'public_zip',
395 'send_info_mails',
396 /*'show_users_online',*/
397 'hide_own_online_status',
398 'bs_allow_to_contact_me',
399 'chat_osc_accept_msg',
400 'chat_broadcast_typing',
401 'user_tz',
402 'weekstart',
403 'mail_incoming_type',
404 'mail_signature',
405 'mail_linebreak',
406 'public_interests_general',
407 'public_interests_help_offered',
408 'public_interests_help_looking'
409 ];
410 }
411
415 public static function isPrefExportable(string $key): bool
416 {
417 return in_array($key, self::getExportablePreferences());
418 }
419}
static _getPreferences(int $user_id)
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
canExport(string $tagname, ?string $settingsname=null)
array $settings
fields to be exported
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
setSettings(array $settings)
setObjects(array $users)
__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