ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilUserDataSet.php
Go to the documentation of this file.
1 <?php
2 
22 class ilUserDataSet extends ilDataSet
23 {
26  protected array $temp_picture_dirs = []; // Missing array type.
27  public array $multi = []; // Missing array type.
28  protected array $users; // Missing array type.
29 
30  public function __construct()
31  {
33  $this->user_profile = new ilUserProfile();
34  }
35 
36  public function initByExporter(ilXmlExporter $xml_exporter): void
37  {
38  parent::initByExporter($xml_exporter);
40  $config = $this->export->getExportConfigs()->getElementByClassName('ilUserExportConfig');
41  $this->export_config = $config;
42  }
43 
44  public function getSupportedVersions(): array // Missing array type.
45  {
46  return ["4.3.0", "4.5.0", "5.1.0", "5.2.0", "5.3.0"];
47  }
48 
49  protected function getXmlNamespace(string $a_entity, string $a_schema_version): string
50  {
51  return "https://www.ilias.de/xml/Services/User/" . $a_entity;
52  }
53 
54  protected function getTypes(string $a_entity, string $a_version): array // Missing array type.
55  {
56  // user profile type
57  if ($a_entity == "usr_profile") {
58  switch ($a_version) {
59  case "4.3.0":
60  case "4.5.0":
61  case "5.1.0":
62  case "5.2.0":
63  case "5.3.0":
64  return [
65  "Id" => "integer",
66  "Username" => "text",
67  "Firstname" => "text",
68  "Lastname" => "text",
69  "Title" => "text",
70  "Birthday" => "text",
71  "Gender" => "text",
72  "Institution" => "text",
73  "Department" => "text",
74  "Street" => "text",
75  "Zipcode" => "text",
76  "City" => "text",
77  "Country" => "text",
78  "SelCountry" => "text",
79  "PhoneOffice" => "text",
80  "PhoneHome" => "text",
81  "PhoneMobile" => "text",
82  "Fax" => "text",
83  "Email" => "text",
84  "SecondEmail" => "text",
85  "Hobby" => "text",
86  "ReferralComment" => "text",
87  "Matriculation" => "text",
88  "Latitude" => "text",
89  "Longitude" => "text",
90  "LocZoom" => "text",
91  "Picture" => "directory"
92  ];
93  }
94  }
95 
96  if ($a_entity == "usr_setting") {
97  switch ($a_version) {
98  case "4.3.0":
99  case "4.5.0":
100  case "5.1.0":
101  case "5.2.0":
102  case "5.3.0":
103  return [
104  "UserId" => "integer",
105  "Keyword" => "text",
106  "Value" => "text"
107  ];
108  }
109  }
110 
111  if ($this->export_config->getExportType() == "personal_data") {
112  switch ($a_version) {
113  case "4.3.0":
114  case "4.5.0":
115  case "5.1.0":
116  case "5.2.0":
117  case "5.3.0":
118  return [
119  "Id" => "integer"
120  ];
121  }
122  }
123 
124  if ($a_entity == "usr_multi") {
125  switch ($a_version) {
126  case "4.5.0":
127  case "5.1.0":
128  case "5.2.0":
129  case "5.3.0":
130  return [
131  "UserId" => "integer",
132  "FieldId" => "text",
133  "Value" => "text"
134  ];
135  }
136  }
137  return [];
138  }
139 
140  public function getXmlRecord(string $a_entity, string $a_version, array $a_set): array // Missing array type.
141  {
142  global $DIC;
143 
144  if ($a_entity == "usr_profile") {
145  $tmp_dir = ilFileUtils::ilTempnam();
146  ilFileUtils::makeDir($tmp_dir);
147 
149  $a_set["Id"],
150  "small",
151  true,
152  true
153  );
154 
155  if ($im != "") {
156  ilObjUser::copyProfilePicturesToDirectory($a_set["Id"], $tmp_dir);
157  }
158 
159  $this->temp_picture_dirs[$a_set["Id"]] = $tmp_dir;
160 
161  $a_set["Picture"] = $tmp_dir;
162  }
163 
164  return $a_set;
165  }
166 
167  public function afterXmlRecordWriting(string $a_entity, string $a_version, array $a_set): void // Missing array type.
168  {
169  if ($a_entity == "usr_profile") {
170  // cleanup temp dirs for pictures
171  $tmp_dir = $this->temp_picture_dirs[$a_set["Id"]];
172  if ($tmp_dir != "" && is_dir($tmp_dir)) {
173  ilFileUtils::delDir($tmp_dir);
174  }
175  }
176  }
177 
178  public function readData(string $a_entity, string $a_version, array $a_ids): void // Missing array type.
179  {
180  global $DIC;
181 
182  $ilDB = $DIC['ilDB'];
183 
184  if (!is_array($a_ids)) {
185  $a_ids = [$a_ids];
186  }
187 
188  if ($this->export_config->getExportType() == "personal_data") {
189  switch ($a_version) {
190  case "4.3.0":
191  case "4.5.0":
192  case "5.1.0":
193  case "5.2.0":
194  case "5.3.0":
195  $this->data = [];
196  foreach ($a_ids as $id) {
197  $this->data[] = ["Id" => $id];
198  }
199  break;
200  }
201  }
202 
203  if ($a_entity == "usr_profile") {
204  switch ($a_version) {
205  case "4.3.0":
206  case "4.5.0":
207  case "5.1.0":
208  $this->getDirectDataFromQuery("SELECT usr_id id, login username, firstname, lastname, " .
209  " title, birthday, gender, institution, department, street, city, zipcode, country, sel_country, " .
210  " phone_office, phone_home, phone_mobile, fax, email, hobby, referral_comment, matriculation, " .
211  " delicious, latitude, longitude, loc_zoom" .
212  " FROM usr_data u " .
213  "WHERE " .
214  $ilDB->in("u.usr_id", $a_ids, false, "integer"));
215  break;
216 
217  case "5.2.0":
218  $this->getDirectDataFromQuery("SELECT usr_id id, login username, firstname, lastname, " .
219  " title, birthday, gender, institution, department, street, city, zipcode, country, sel_country, " .
220  " phone_office, phone_home, phone_mobile, fax, email, hobby, referral_comment, matriculation, " .
221  " latitude, longitude, loc_zoom" .
222  " FROM usr_data u " .
223  "WHERE " .
224  $ilDB->in("u.usr_id", $a_ids, false, "integer"));
225  break;
226  case "5.3.0":
227  $this->getDirectDataFromQuery("SELECT usr_id id, login username, firstname, lastname, " .
228  " title, birthday, gender, institution, department, street, city, zipcode, country, sel_country, " .
229  " phone_office, phone_home, phone_mobile, fax, email, second_email, hobby, referral_comment, matriculation, " .
230  " latitude, longitude, loc_zoom" .
231  " FROM usr_data u " .
232  "WHERE " .
233  $ilDB->in("u.usr_id", $a_ids, false, "integer"));
234  break;
235  }
236  }
237 
238  if ($a_entity == "usr_setting") {
239  switch ($a_version) {
240  case "4.3.0":
241  case "4.5.0":
242  case "5.1.0":
243  case "5.2.0":
244  case "5.3.0":
245  // for all user ids get data from usr_pref and mail options, create records user_id/name/value
246  $prefs = ["date_format", "day_end", "day_start", "bs_allow_to_contact_me", "chat_osc_accept_msg", "hide_own_online_status", "language",
247  "public_birthday", "puplic_city", "public_country", "public_delicious", "public_department", "public_email", "public_second_email",
248  "public_fax", "public_gender", "public_hobby", "public_im_aim", "public_im_icq", "public_im_jabber",
249  "public_im_msn", "public_im_skype", "public_im_voip", "public_im_yahoo", "public_institution", "public_location",
250  "public_matriculation", "public_phone_home", "public_phone_mobile", "public_phone_office",
251  "public_profile", "public_sel_country", "public_street", "public_title", "public_upload", "public_zipcode",
252  "screen_reader_optimization", "show_users_online",
253  "store_last_visited", "time_format", "user_tz", "weekstart",
254  "session_reminder_lead_time", "usr_starting_point",
255  "chat_broadcast_typing"];
256 
257  if (version_compare($a_version, '5.2.0', '>=')) {
258  unset(
259  $prefs['public_im_aim'], $prefs['public_im_icq'], $prefs['public_im_jabber'],
260  $prefs['public_im_msn'], $prefs['public_im_skype'], $prefs['public_im_voip'],
261  $prefs['public_im_yahoo'], $prefs['public_delicious']
262  );
263  }
264 
265  $this->data = [];
266  $set = $ilDB->query("SELECT * FROM usr_pref " .
267  " WHERE " . $ilDB->in("keyword", $prefs, false, "text") .
268  " AND " . $ilDB->in("usr_id", $a_ids, false, "integer"));
269  while ($rec = $ilDB->fetchAssoc($set)) {
270  $this->data[] = ["UserId" => $rec["usr_id"], "Keyword" => $rec["keyword"], "Value" => $rec["value"]];
271  }
272  break;
273  }
274  }
275 
276  if ($a_entity == "usr_multi") {
277  switch ($a_version) {
278  case "4.5.0":
279  case "5.1.0":
280  case "5.2.0":
281  case "5.3.0":
282  $this->data = [];
283  $set = $ilDB->query("SELECT * FROM usr_data_multi" .
284  " WHERE " . $ilDB->in("usr_id", $a_ids, false, "integer"));
285  while ($rec = $ilDB->fetchAssoc($set)) {
286  $this->data[] = ["UserId" => $rec["usr_id"], "FieldId" => $rec["field_id"], "Value" => $rec["value"]];
287  }
288  break;
289  }
290  }
291  }
292 
293  public function importRecord(
294  string $a_entity,
295  array $a_types,
296  array $a_rec,
297  ilImportMapping $a_mapping,
298  string $a_schema_version
299  ): void {
300  global $DIC;
301 
302  $ilSetting = $DIC['ilSetting'];
303  $ilUser = $DIC['ilUser'];
304 
305  switch ($a_entity) {
306  case "usr":
307  // only users themselves import their profiles!
308  // thus we can map the import id of the dataset to the current user
309  $a_mapping->addMapping("components/ILIAS/User", "usr", $a_rec["Id"], $ilUser->getId());
310  break;
311 
312  case "usr_profile":
313  $usr_id = $a_mapping->getMapping("components/ILIAS/User", "usr", $a_rec["Id"]);
314  if ($usr_id > 0 && ilObject::_lookupType($usr_id) == "usr") {
315  if (!isset($this->users[$usr_id])) {
316  $this->users[$usr_id] = new ilObjUser($usr_id);
317  }
318  $user = $this->users[$usr_id];
319  $prof = new ilUserProfile();
320  $prof->skipField("username");
321  $prof->skipField("password");
322  $prof->skipField("roles");
323  $prof->skipGroup("settings");
324  $fields = $prof->getStandardFields();
325  foreach ($fields as $k => $f) {
326  $up_k = $this->convertToLeadingUpper($k);
327  // only change fields, when it is possible in profile
328  if ($this->user_profile->userSettingVisible($k) &&
329  !$ilSetting->get("usr_settings_disable_" . $k) &&
330  ($f["method"] ?? "") != "" && isset($a_rec[$up_k])) {
331  $set_method = "set" . substr($f["method"], 3);
332  $user->{$set_method}(ilUtil::secureString($a_rec[$up_k]));
333  }
334  }
335 
336  $user->setLatitude($a_rec["Latitude"] ?? null);
337  $user->setLongitude($a_rec["Longitude"] ?? null);
338  $zoom = isset($a_rec["LocZoom"]) ? (int) $a_rec["LocZoom"] : null;
339  $user->setLocationZoom($zoom);
340 
341  $user->update();
342 
343  // personal picture
344  $pic_dir = $this->getImportDirectory() . "/" . str_replace("..", "", $a_rec["Picture"]);
345  if ($pic_dir != "" && is_dir($pic_dir)) {
346  $upload_file = $pic_dir . "/usr_" . $a_rec["Id"] . ".jpg";
347  if (!is_file($upload_file)) {
348  $upload_file = $pic_dir . "/upload_" . $a_rec["Id"] . "pic";
349  }
350  if (is_file($upload_file)) {
351  ilObjUser::_uploadPersonalPicture($upload_file, $user->getId());
352  }
353  }
354  }
355  break;
356 
357  case "usr_setting":
358  $usr_id = $a_mapping->getMapping("components/ILIAS/User", "usr", $a_rec["UserId"]);
359  if ($usr_id > 0 && ilObject::_lookupType($usr_id) == "usr") {
360  if (!isset($this->users[$usr_id])) {
361  $this->users[$usr_id] = new ilObjUser($usr_id);
362  }
363  $user = $this->users[$usr_id];
364  $user->writePref($a_rec["Keyword"], ilUtil::secureString($a_rec["Value"]));
365  }
366  break;
367 
368  case "usr_multi":
369  $usr_id = $a_mapping->getMapping("components/ILIAS/User", "usr", $a_rec["UserId"]);
370  if ($usr_id > 0 && ilObject::_lookupType($usr_id) == "usr") {
371  $this->multi[$usr_id][$a_rec["FieldId"]][] = ilUtil::secureString($a_rec["Value"]);
372  }
373  break;
374  }
375  }
376 }
static copyProfilePicturesToDirectory(int $a_user_id, string $a_dir)
convertToLeadingUpper(string $a_str)
Make xyz_abc a XyzAbc string.
initByExporter(ilXmlExporter $xml_exporter)
afterXmlRecordWriting(string $a_entity, string $a_version, array $a_set)
static secureString(string $a_str, bool $a_strip_html=true, string $a_allow="")
Class ilUserProfile.
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
static _uploadPersonalPicture(string $tmp_file, int $obj_id)
Create a personal picture image file from a temporary image file.
readData(string $a_entity, string $a_version, array $a_ids)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getTypes(string $a_entity, string $a_version)
getXmlRecord(string $a_entity, string $a_version, array $a_set)
ilUserExportConfig $export_config
getMapping(string $a_comp, string $a_entity, string $a_old_id)
ilUserProfile $user_profile
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
global $DIC
Definition: shib_login.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getDirectDataFromQuery(string $a_query, bool $a_convert_to_leading_upper=true, bool $a_set=true)
Get data from query.This is a standard procedure, all db field names are directly mapped to abstract ...
static _getPersonalPicturePath(int $a_usr_id, string $a_size='small', bool $a_force_pic=false, bool $a_prevent_no_photo_image=false, bool $html_export=false)
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
global $ilSetting
Definition: privfeed.php:31
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(Container $dic, ilPlugin $plugin)
getXmlNamespace(string $a_entity, string $a_schema_version)
static _lookupType(int $id, bool $reference=false)
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...