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