ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilUserXMLWriter.php
Go to the documentation of this file.
1 <?php
2 
29 {
30  private ILIAS $ilias;
31  private ilDBInterface $db;
32  private ilLanguage $lng;
33  private array $users; // Missing array type.
34  private int $user_id = 0;
35  private bool $attach_roles = false;
36  private bool $attach_preferences = false;
37 
42  private array $settings = [];
43 
44  public function __construct()
45  {
47  global $DIC;
48 
49  $this->ilias = $DIC['ilias'];
50  $this->db = $DIC['ilDB'];
51  $this->lng = $DIC['lng'];
52  $this->user_id = $DIC['ilUser']->getId();
53 
54  $this->attach_roles = false;
55 
57  }
58 
59  public function setAttachRoles(bool $value): void
60  {
61  $this->attach_roles = $value;
62  }
63 
64  public function setObjects(array $users): void // Missing array type.
65  {
66  $this->users = $users;
67  }
68 
69 
70  public function start(): bool
71  {
72  if (!is_array($this->users)) {
73  return false;
74  }
75 
76  $this->__buildHeader();
77 
79  $udf_data->addToXML($this);
80 
81  foreach ($this->users as $user) {
82  $this->__handleUser($user);
83  }
84 
85  $this->__buildFooter();
86 
87  return true;
88  }
89 
90  public function getXML(): string
91  {
92  return $this->xmlDumpMem(false);
93  }
94 
95 
96  public function __buildHeader(): void
97  {
98  $this->xmlSetDtdDef('<!DOCTYPE Users PUBLIC "-//ILIAS//DTD UserImport//EN" "' . ILIAS_HTTP_PATH . '/xml/ilias_user_5_1.dtd">');
99  $this->xmlSetGenCmt('User of ilias system');
100  $this->xmlHeader();
101 
102  $this->xmlStartTag('Users');
103  }
104 
105  public function __buildFooter(): void
106  {
107  $this->xmlEndTag('Users');
108  }
109 
110  public function __handleUser(array $row): void // Missing array type.
111  {
112  if ($this->settings === []) {
114  }
115 
116  $prefs = ilObjUser::_getPreferences($row['usr_id']);
117 
118  if ($row['language'] === null
119  || $row['language'] === '') {
120  $row['language'] = $this->lng->getDefaultLanguage();
121  }
122 
123  $attrs = [
124  'Id' => 'il_' . IL_INST_ID . '_usr_' . $row['usr_id'],
125  'Language' => $row['language'],
126  'Action' => 'Update'
127  ];
128 
129  $this->xmlStartTag('User', $attrs);
130 
131  $this->xmlElement('Login', null, $row['login']);
132 
133  if ($this->attach_roles == true) {
134  $query = sprintf(
135  'SELECT object_data.title, object_data.description, rbac_fa.* ' .
136  'FROM object_data, rbac_ua, rbac_fa WHERE rbac_ua.usr_id = %s ' .
137  'AND rbac_ua.rol_id = rbac_fa.rol_id AND object_data.obj_id = rbac_fa.rol_id',
138  $this->db->quote($row['usr_id'], 'integer')
139  );
140  $rbacresult = $this->db->query($query);
141 
142  while ($rbacrow = $rbacresult->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
143  if ($rbacrow['assign'] != 'y') {
144  continue;
145  }
146 
147  $type = '';
148 
149  if ($rbacrow['parent'] == ROLE_FOLDER_ID) {
150  $type = 'Global';
151  } else {
152  $type = 'Local';
153  }
154  if ($type !== '') {
155  $this->xmlElement(
156  'Role',
157  ['Id' => 'il_' . IL_INST_ID . '_role_' . $rbacrow['rol_id'], 'Type' => $type],
158  $rbacrow['title']
159  );
160  }
161  }
162  }
163 
164  $this->__addElement('Firstname', $row['firstname']);
165  $this->__addElement('Lastname', $row['lastname']);
166  $this->__addElement('Title', $row['title']);
167 
168  if ($this->canExport('PersonalPicture', 'upload')) {
169  $imageData = $this->getPictureValue($row['usr_id']);
170  if ($imageData) {
171  $value = array_shift($imageData); //$imageData['value'];
172  $this->__addElement('PersonalPicture', $value, $imageData, 'upload');
173  }
174  }
175 
176 
177  $this->__addElement('Gender', $row['gender']);
178  $this->__addElement('Email', $row['email']);
179  $this->__addElement('SecondEmail', $row['second_email'], null, 'second_email');
180  $this->__addElement('Birthday', $row['birthday']);
181  $this->__addElement('Institution', $row['institution']);
182  $this->__addElement('Street', $row['street']);
183  $this->__addElement('City', $row['city']);
184  $this->__addElement('PostalCode', $row['zipcode'], null, 'zipcode');
185  $this->__addElement('Country', $row['country']);
186  $this->__addElement('SelCountry', $row['sel_country'], null, 'sel_country');
187  $this->__addElement('PhoneOffice', $row['phone_office'], null, 'phone_office');
188  $this->__addElement('PhoneHome', $row['phone_home'], null, 'phone_home');
189  $this->__addElement('PhoneMobile', $row['phone_mobile'], null, 'phone_mobile');
190  $this->__addElement('Fax', $row['fax']);
191  $this->__addElement('Hobby', $row['hobby']);
192 
193  $this->__addElementMulti('GeneralInterest', $row['interests_general'] ?? [], null, 'interests_general');
194  $this->__addElementMulti('OfferingHelp', $row['interests_help_offered'] ?? [], null, 'interests_help_offered');
195  $this->__addElementMulti('LookingForHelp', $row['interests_help_looking'] ?? [], null, 'interests_help_looking');
196 
197  $this->__addElement('Department', $row['department']);
198  $this->__addElement('Comment', $row['referral_comment'], null, 'referral_comment');
199  $this->__addElement('Matriculation', $row['matriculation']);
200  $this->__addElement('Active', $row['active'] ? 'true' : 'false');
201  $this->__addElement('ClientIP', $row['client_ip'], null, 'client_ip');
202  $this->__addElement('TimeLimitOwner', $row['time_limit_owner'], null, 'time_limit_owner');
203  $this->__addElement('TimeLimitUnlimited', $row['time_limit_unlimited'], null, 'time_limit_unlimited');
204  $this->__addElement('TimeLimitFrom', $row['time_limit_from'], null, 'time_limit_from');
205  $this->__addElement('TimeLimitUntil', $row['time_limit_until'], null, 'time_limit_until');
206  $this->__addElement('TimeLimitMessage', $row['time_limit_message'], null, 'time_limit_message');
207  $this->__addElement('ApproveDate', $row['approve_date'], null, 'approve_date');
208  $this->__addElement('AgreeDate', $row['agree_date'], null, 'agree_date');
209 
210  if ($row['auth_mode'] !== null
211  && $row['auth_mode'] !== '') {
212  $this->__addElement('AuthMode', null, ['type' => $row['auth_mode']], 'auth_mode', true);
213  }
214 
215  if ($row['ext_account'] !== null
216  && $row['ext_account'] !== '') {
217  $this->__addElement('ExternalAccount', $row['ext_account'], null, 'ext_account', true);
218  }
219 
220  if (isset($prefs['skin'])
221  && isset($prefs['style'])
222  && $this->canExport('Look', 'skin_style')) {
223  $this->__addElement(
224  'Look',
225  null,
226  [
227  'Skin' => $prefs['skin'], 'Style' => $prefs['style']
228  ],
229  'skin_style',
230  true
231  );
232  }
233 
234 
235  $this->__addElement('LastUpdate', $row['last_update'], null, 'last_update');
236  $this->__addElement('LastLogin', $row['last_login'], null, 'last_login');
237 
238  $udf_data = new ilUserDefinedData($row['usr_id']);
239  $udf_data->addToXML($this);
240 
241  $this->__addElement('AccountInfo', $row['ext_account'], ['Type' => 'external']);
242 
243  $this->__addElement('GMapsInfo', null, [
244  'longitude' => $row['longitude'],
245  'latitude' => $row['latitude'],
246  'zoom' => $row['loc_zoom']]);
247 
248  $this->__addElement('Feedhash', $row['feed_hash']);
249 
250  if ($this->attach_preferences || $this->canExport('prefs', 'preferences')) {
251  $this->__handlePreferences($prefs, $row);
252  }
253 
254  $this->xmlEndTag('User');
255  }
256 
257 
258  private function __handlePreferences(array $prefs, array $row): void // Missing array type.
259  {
260  //todo nadia: test mail_address_option
261  $mailOptions = new ilMailOptions($row['usr_id']);
262  $prefs['mail_incoming_type'] = $mailOptions->getIncomingType();
263  $prefs['mail_address_option'] = $mailOptions->getEmailAddressMode();
264  $prefs['mail_signature'] = $mailOptions->getSignature();
265  if ($prefs !== []) {
266  $this->xmlStartTag('Prefs');
267  foreach ($prefs as $key => $value) {
268  if (self::isPrefExportable($key)) {
269  $this->xmlElement('Pref', ['key' => $key], $value);
270  }
271  }
272  $this->xmlEndTag('Prefs');
273  }
274  }
275 
276  public function __addElementMulti(
277  string $tagname,
278  array $value,
279  ?array $attrs = null,
280  ?string $settingsname = null,
281  bool $requiredTag = false
282  ): void {
283  foreach ($value as $item) {
284  $this->__addElement($tagname, $item, $attrs, $settingsname, $requiredTag);
285  }
286  }
287 
288  public function __addElement(
289  string $tagname,
290  ?string $value,
291  array $attrs = null,
292  ?string $settingsname = null,
293  bool $requiredTag = false
294  ): void {
295  if ($this->canExport($tagname, $settingsname)
296  && ($value !== null
297  || $requiredTag
298  || is_array($attrs) && count($attrs) > 0)) {
299  $this->xmlElement($tagname, $attrs, (string) $value);
300  }
301  }
302 
303  private function canExport(
304  string $tagname,
305  ?string $settingsname = null
306  ): bool {
307  return $this->settings === []
308  || in_array(strtolower($tagname), $this->settings) !== false
309  || in_array($settingsname, $this->settings) !== false;
310  }
311 
312  public function setSettings(array $settings): void // Missing array type.
313  {
314  $this->settings = $settings;
315  }
316 
321  private function getPictureValue(int $usr_id): ?array
322  {
323  $avatar_resolver = new ilUserAvatarResolver($usr_id);
324  $avatar_resolver->setForcePicture(true);
325  if (!$avatar_resolver->hasProfilePicture()) {
326  return null;
327  }
328 
329  [$image_data, $image_type] = $avatar_resolver->getUserPictureForVCard();
330 
331  if ($image_data === null || $image_type === null) {
332  return null;
333  }
334 
335  return [
336  'value' => base64_encode($image_data),
337  'encoding' => 'Base64',
338  'imagetype' => $image_type
339  ];
340  }
341 
342 
346  public function setAttachPreferences(bool $attach_preferences): void
347  {
348  $this->attach_preferences = $attach_preferences;
349  }
350 
354  public static function getExportablePreferences(): array // Missing array type.
355  {
356  return [
357  'hits_per_page',
358  'public_city',
359  'public_country',
360  'public_department',
361  'public_email',
362  'public_second_email',
363  'public_fax',
364  'public_hobby',
365  'public_institution',
366  'public_matriculation',
367  'public_phone',
368  'public_phone_home',
369  'public_phone_mobile',
370  'public_phone_office',
371  'public_profile',
372  'public_street',
373  'public_upload',
374  'public_zip',
375  'send_info_mails',
376  /*'show_users_online',*/
377  'hide_own_online_status',
378  'bs_allow_to_contact_me',
379  'chat_osc_accept_msg',
380  'chat_broadcast_typing',
381  'user_tz',
382  'weekstart',
383  'mail_incoming_type',
384  'mail_signature',
385  'mail_linebreak',
386  'public_interests_general',
387  'public_interests_help_offered',
388  'public_interests_help_looking'
389  ];
390  }
391 
395  public static function isPrefExportable(string $key): bool
396  {
397  return in_array($key, self::getExportablePreferences());
398  }
399 }
const IL_INST_ID
Definition: constants.php:40
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__handlePreferences(array $prefs, array $row)
Class ChatMainBarProvider .
static _getPreferences(int $user_id)
get preferences for user
getPictureValue(int $usr_id)
return array with base-encoded picture data as key value, encoding type as encoding, and image type as key type.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlSetGenCmt(string $genCmt)
Sets generated comment.
canExport(string $tagname, ?string $settingsname=null)
setAttachPreferences(bool $attach_preferences)
if set to true, all preferences of a user will be set
setObjects(array $users)
xmlEndTag(string $tag)
Writes an endtag.
global $DIC
Definition: feed.php:28
xmlSetDtdDef(string $dtdDef)
Sets dtd definition.
__construct(VocabulariesInterface $vocabularies)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193
array $settings
fields to be exported
__addElementMulti(string $tagname, array $value, ?array $attrs=null, ?string $settingsname=null, bool $requiredTag=false)
header include for all ilias files.
xmlHeader()
Writes xml header.
__addElement(string $tagname, ?string $value, array $attrs=null, ?string $settingsname=null, bool $requiredTag=false)
Class ilUserAvatarResolver.
const ROLE_FOLDER_ID
Definition: constants.php:34
setSettings(array $settings)
static isPrefExportable(string $key)
returns wether a key from db is exportable or not
setAttachRoles(bool $value)
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
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")
static getExportablePreferences()
return exportable preference keys as found in db
xmlDumpMem(bool $format=true)
Returns xml document from memory.