ILIAS  release_8 Revision v8.24
class.ilObjUserFolder.php
Go to the documentation of this file.
1<?php
2
25{
26 public const ORG_OP_EDIT_USER_ACCOUNTS = 'edit_user_accounts';
27 public const FILE_TYPE_EXCEL = 'userfolder_export_excel_x86';
28 public const FILE_TYPE_CSV = 'userfolder_export_csv';
29 public const FILE_TYPE_XML = 'userfolder_export_xml';
30
31 public function __construct(
32 int $a_id,
33 bool $a_call_by_reference = true
34 ) {
35 $this->type = "usrf";
36 parent::__construct($a_id, $a_call_by_reference);
37 }
38
39
40 public function delete(): bool
41 {
42 return false;
43 }
44
45 public function getExportFilename(
46 string $a_mode = self::FILE_TYPE_EXCEL
47 ): string {
48 $filename = "";
49 $inst_id = IL_INST_ID;
50
51 $date = time();
52
53 switch ($a_mode) {
55 $filename = $date . "__" . $inst_id . "__xls_usrf";
56 break;
58 $filename = $date . "__" . $inst_id . "__csv_usrf.csv";
59 break;
61 $filename = $date . "__" . $inst_id . "__xml_usrf.xml";
62 break;
63 }
64 return $filename;
65 }
66
67 public function getExportDirectory(): string
68 {
69 $export_dir = ilFileUtils::getDataDir() . "/usrf_data/export";
70
71 return $export_dir;
72 }
73
78 public function getExportFiles(): array
79 {
80 $dir = $this->getExportDirectory();
81
82 // quit if export dir not available
83 if (!is_dir($dir) or
84 !is_writable($dir)) {
85 return array();
86 }
87
88 // open directory
89 $dir = dir($dir);
90
91 // initialize array
92 $file = array();
93
94 // get files and save the in the array
95 while ($entry = $dir->read()) {
96 if ($entry != "." and
97 $entry != ".." and
98 preg_match("/^[0-9]{10}_{2}[0-9]+_{2}([a-z0-9]{3})_usrf\.[a-z]{1,4}\$/", $entry, $matches)) {
99 $filearray["filename"] = $entry;
100 $filearray["filesize"] = filesize($this->getExportDirectory() . "/" . $entry);
101 $file[] = $filearray;
102 }
103 }
104
105 // close import directory
106 $dir->close();
107
108 // sort files
109 sort($file);
110
111 return $file;
112 }
113
114 protected function escapeXML(string $value): string
115 {
116 $value = str_replace(["&", "<", ">"], ["&amp;", "&lt;", "&gt;"], $value);
117 return $value;
118 }
119
120 protected function createXMLExport(
121 array $settings,
122 array $data,
123 string $filename
124 ): void {
125 global $DIC;
126
127 $rbacreview = $DIC['rbacreview'];
128 $ilDB = $DIC['ilDB'];
129 $log = $DIC['log'];
130
131 $file = fopen($filename, 'wb');
132
133 if (is_array($data)) {
134 $xmlWriter = new ilUserXMLWriter();
135 $xmlWriter->setObjects($data);
136 $xmlWriter->setSettings($settings);
137 $xmlWriter->setAttachRoles(true);
138
139 if ($xmlWriter->start()) {
140 fwrite($file, $xmlWriter->getXML());
141 }
142 }
143 }
144
145
149 protected function getUserDefinedExportFields(): array // Missing array type.
150 {
152
153 $udf_ex_fields = array();
154 foreach ($udf_obj->getDefinitions() as $definition) {
155 if ($definition["export"] != false) {
156 $udf_ex_fields[] = array("name" => $definition["field_name"],
157 "id" => $definition["field_id"]);
158 }
159 }
160
161 return $udf_ex_fields;
162 }
163
164 protected function createCSVExport(
165 array $settings,
166 array $data,
167 string $filename
168 ): void {
169
170 // header
171 $headerrow = array();
172 $udf_ex_fields = $this->getUserDefinedExportFields();
173 foreach ($settings as $value) { // standard fields
174 $headerrow[] = $this->lng->txt($value);
175 }
176 foreach ($udf_ex_fields as $f) { // custom fields
177 $headerrow[] = $f["name"];
178 }
179
180 $separator = ";";
181 $file = fopen($filename, 'wb');
182 $formattedrow = &ilCSVUtil::processCSVRow($headerrow, true, $separator);
183 fwrite($file, implode($separator, $formattedrow) . "\n");
184 foreach ($data as $row) {
185 $csvrow = array();
186 foreach ($settings as $header) { // standard fields
187 // multi-text
188 if (isset($row[$header]) && is_array($row[$header])) {
189 $row[$header] = implode(", ", $row[$header]);
190 }
191
192 $csvrow[] = $row[$header] ?? '';
193 }
194
195 // custom fields
196 reset($udf_ex_fields);
197 if (count($udf_ex_fields) > 0) {
198 $udf = new ilUserDefinedData($row["usr_id"]);
199 foreach ($udf_ex_fields as $f) { // custom fields
200 $csvrow[] = $udf->get("f_" . $f["id"]);
201 }
202 }
203
204 $formattedrow = &ilCSVUtil::processCSVRow($csvrow, true, $separator);
205 fwrite($file, implode($separator, $formattedrow) . "\n");
206 }
207 fclose($file);
208 }
209
210 protected function createExcelExport(
211 array $settings,
212 array $data,
213 string $filename
214 ): void {
215 $worksheet = new ilExcel();
216 $worksheet->addSheet($this->lng->txt("users"));
217
218 $row = 1;
219 $col = 0;
220
221 $udf_ex_fields = $this->getUserDefinedExportFields();
222
223 // title row
224 foreach ($settings as $value) { // standard fields
225 if ($value == 'ext_account') {
226 $value = 'user_ext_account';
227 }
228 $worksheet->setCell($row, $col, $this->lng->txt($value));
229 $col++;
230 }
231 foreach ($udf_ex_fields as $f) { // custom fields
232 $worksheet->setCell($row, $col, $f["name"]);
233 $col++;
234 }
235 $worksheet->setBold("A1:" . $worksheet->getColumnCoord($col - 1) . "1");
236
237 $this->lng->loadLanguageModule("meta");
238 foreach ($data as $index => $rowdata) {
239 $row++;
240 $col = 0;
241
242 // standard fields
243 foreach ($settings as $fieldname) {
244 $value = $rowdata[$fieldname] ?? "";
245 switch ($fieldname) {
246 case "language":
247 $worksheet->setCell($row, $col, $this->lng->txt("meta_l_" . $value));
248 break;
249 case "time_limit_from":
250 case "time_limit_until":
251 $value = $value
252 ? new ilDateTime($value, IL_CAL_UNIX)
253 : null;
254 $worksheet->setCell($row, $col, $value);
255 break;
256 case "last_login":
257 case "last_update":
258 case "create_date":
259 case "approve_date":
260 case "agree_date":
261 $value = $value
262 ? new ilDateTime($value, IL_CAL_DATETIME)
263 : null;
264 $worksheet->setCell($row, $col, $value);
265 break;
266
267 case "interests_general":
268 case "interests_help_offered":
269 case "interests_help_looking":
270 if (is_array($value) && count($value)) {
271 $value = implode(", ", $value);
272 } else {
273 $value = null;
274 }
275 // fallthrough
276
277 // no break
278 default:
279 $worksheet->setCell($row, $col, $value);
280 break;
281 }
282 $col++;
283 }
284
285 // custom fields
286 reset($udf_ex_fields);
287 if (count($udf_ex_fields) > 0) {
288 $udf = new ilUserDefinedData($rowdata["usr_id"]);
289 foreach ($udf_ex_fields as $f) { // custom fields
290 $worksheet->setCell($row, $col, $udf->get("f_" . $f["id"]));
291 $col++;
292 }
293 }
294 }
295
296 $worksheet->writeToFile($filename);
297 }
298
302 public static function getExportSettings(): array // Missing array type.
303 {
304 global $DIC;
305
306 $ilDB = $DIC['ilDB'];
307
308 $db_settings = array();
309
310 $up = new ilUserProfile();
311 $up->skipField("roles");
312 $profile_fields = $up->getStandardFields();
313
314 $query = "SELECT * FROM settings WHERE " .
315 $ilDB->like("keyword", "text", '%usr_settings_export_%');
316 $result = $ilDB->query($query);
317 while ($row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
318 if ($row["value"] == "1") {
319 if (preg_match("/usr_settings_export_(.*)/", $row["keyword"], $setting)) {
320 $db_settings[] = $setting[1];
321 }
322 }
323 }
324 $export_settings = array();
325 foreach ($profile_fields as $key => $value) {
326 if (in_array($key, $db_settings)) {
327 if (strcmp($key, "password") == 0) {
328 // we do not support password export with ILIAS >= 4.5.x
329 continue;
330 } else {
331 $export_settings[] = $key;
332 }
333 }
334 }
335 $export_settings[] = "usr_id";
336 $export_settings[] = "login";
337 $export_settings[] = "last_login";
338 $export_settings[] = "last_update";
339 $export_settings[] = "create_date";
340 $export_settings[] = "time_limit_owner";
341 $export_settings[] = "time_limit_unlimited";
342 $export_settings[] = "time_limit_from";
343 $export_settings[] = "time_limit_until";
344 $export_settings[] = "time_limit_message";
345 $export_settings[] = "active";
346 $export_settings[] = "approve_date";
347 $export_settings[] = "agree_date";
348 $export_settings[] = "client_ip";
349 $export_settings[] = "auth_mode";
350 $export_settings[] = "ext_account";
351 $export_settings[] = "feedhash";
352 return $export_settings;
353 }
354
358 public function buildExportFile(
359 string $a_mode = self::FILE_TYPE_EXCEL,
360 ?array $user_data_filter = null,
361 bool $use_temp_dir = false
362 ): string {
363 global $DIC;
364
365 $ilDB = $DIC['ilDB'];
366 $lng = $DIC['lng'];
367
368 if ($use_temp_dir) {
369 $expDir = ilFileUtils::ilTempnam();
370 $fullname = $expDir;
371 } else {
372 $expDir = $this->getExportDirectory();
373 // create export directory if needed
374 $this->createExportDirectory();
375 $fullname = $expDir . "/" . $this->getExportFilename($a_mode);
376 }
377
378 //get data
379 //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
380 $settings = self::getExportSettings();
381
382 // user languages
383 $query = "SELECT * FROM usr_pref WHERE keyword = " . $ilDB->quote('language', 'text');
384 $res = $ilDB->query($query);
385 $languages = array();
386 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
387 $languages[$row['usr_id']] = $row['value'];
388 }
389
390 // multi-text
391 $multi = array();
392 $set = $ilDB->query("SELECT * FROM usr_data_multi");
393 while ($row = $ilDB->fetchAssoc($set)) {
394 if (!is_array($user_data_filter) ||
395 in_array($row["usr_id"], $user_data_filter)) {
396 $multi[$row["usr_id"]][$row["field_id"]][] = $row["value"];
397 }
398 }
399
400 $data = array();
401 $query = "SELECT usr_data.* FROM usr_data " .
402 " ORDER BY usr_data.lastname, usr_data.firstname";
403 $result = $ilDB->query($query);
404 while ($row = $ilDB->fetchAssoc($result)) {
405 if (isset($languages[$row['usr_id']])) {
406 $row['language'] = $languages[$row['usr_id']];
407 } else {
408 $row['language'] = $lng->getDefaultLanguage();
409 }
410
411 if (isset($multi[$row["usr_id"]])) {
412 $row = array_merge($row, $multi[$row["usr_id"]]);
413 }
414
415 if (is_array($user_data_filter)) {
416 if (in_array($row["usr_id"], $user_data_filter)) {
417 $data[] = $row;
418 }
419 } else {
420 $data[] = $row;
421 }
422 }
423 //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
424
425 switch ($a_mode) {
426 case self::FILE_TYPE_EXCEL:
427 $this->createExcelExport($settings, $data, $fullname);
428 break;
429 case self::FILE_TYPE_CSV:
430 $this->createCSVExport($settings, $data, $fullname);
431 break;
432 case self::FILE_TYPE_XML:
433 $this->createXMLExport($settings, $data, $fullname);
434 break;
435 }
436 return $fullname;
437 }
438
439
443 protected function createExportDirectory(): void
444 {
445 if (!is_dir($this->getExportDirectory())) {
446 $usrf_data_dir = ilFileUtils::getDataDir() . "/usrf_data";
447 ilFileUtils::makeDir($usrf_data_dir);
448 if (!is_writable($usrf_data_dir)) {
449 $this->ilias->raiseError("Userfolder data directory (" . $usrf_data_dir
450 . ") not writeable.", $this->ilias->error_obj->MESSAGE);
451 }
452
453 // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
454 $export_dir = $usrf_data_dir . "/export";
455 ilFileUtils::makeDir($export_dir);
456 if (!is_dir($export_dir)) {
457 $this->ilias->raiseError("Creation of Userfolder Export Directory failed.", $this->ilias->error_obj->MESSAGE);
458 }
459 }
460 }
461
462
467 public static function getProfileFields(): array // Missing array type.
468 {
469 $up = new ilUserProfile();
470 $up->skipField("username");
471 $up->skipField("roles");
472 $up->skipGroup("preferences");
473 $fds = $up->getStandardFields();
474 $profile_fields = [];
475 foreach ($fds as $k => $f) {
476 $profile_fields[] = $k;
477 }
478
479 return $profile_fields;
480 }
481
482 public static function _writeNewAccountMail(
483 string $a_lang,
484 string $a_subject,
485 string $a_sal_g,
486 string $a_sal_f,
487 string $a_sal_m,
488 string $a_body
489 ): void {
490 global $DIC;
491
492 $ilDB = $DIC['ilDB'];
493
494 if (self::_lookupNewAccountMail($a_lang)) {
495 $values = array(
496 'subject' => array('text',$a_subject),
497 'body' => array('clob',$a_body),
498 'sal_g' => array('text',$a_sal_g),
499 'sal_f' => array('text',$a_sal_f),
500 'sal_m' => array('text',$a_sal_m)
501 );
502 $ilDB->update(
503 'mail_template',
504 $values,
505 array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
506 );
507 } else {
508 $values = array(
509 'subject' => array('text',$a_subject),
510 'body' => array('clob',$a_body),
511 'sal_g' => array('text',$a_sal_g),
512 'sal_f' => array('text',$a_sal_f),
513 'sal_m' => array('text',$a_sal_m),
514 'lang' => array('text',$a_lang),
515 'type' => array('text','nacc')
516 );
517 $ilDB->insert('mail_template', $values);
518 }
519 }
520
525 public static function _updateAccountMailAttachment(
526 string $a_lang,
527 string $a_tmp_name,
528 string $a_name
529 ): void {
530 global $DIC;
531
532 $ilDB = $DIC['ilDB'];
533
535 $fs->create();
536 $path = $fs->getAbsolutePath() . "/";
537
538 ilFileUtils::moveUploadedFile($a_tmp_name, $a_lang, $path . $a_lang);
539
540 $ilDB->update(
541 'mail_template',
542 array('att_file' => array('text', $a_name)),
543 array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
544 );
545 }
546
550 public static function _deleteAccountMailAttachment(
551 string $a_lang
552 ): void {
553 global $DIC;
554
555 $ilDB = $DIC['ilDB'];
556
558 $path = $fs->getAbsolutePath() . "/";
559
560 if (file_exists($path . $a_lang)) {
561 unlink($path . $a_lang);
562 }
563
564 $ilDB->update(
565 'mail_template',
566 array('att_file' => array('text', '')),
567 array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
568 );
569 }
570
575 public static function _lookupNewAccountMail(string $a_lang): array
576 {
577 global $DIC;
578
579 $ilDB = $DIC['ilDB'];
580
581 $set = $ilDB->query("SELECT * FROM mail_template " .
582 " WHERE type='nacc' AND lang = " . $ilDB->quote($a_lang, 'text'));
583
584 if ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
585 return $rec;
586 }
587
588 return [];
589 }
590
596 public static function _updateUserFolderAssignment(
597 int $a_old_id,
598 int $a_new_id
599 ): void {
600 global $DIC;
601
602 $ilDB = $DIC['ilDB'];
603
604 $query = "UPDATE usr_data SET time_limit_owner = " . $ilDB->quote($a_new_id, "integer") . " " .
605 "WHERE time_limit_owner = " . $ilDB->quote($a_old_id, "integer") . " ";
606 $ilDB->manipulate($query);
607 }
608}
$filename
Definition: buildRTE.php:78
const IL_CAL_UNIX
const IL_CAL_DATETIME
static & processCSVRow(array &$row, bool $quoteAll=false, string $separator=";", bool $outUTF8=false, bool $compatibleWithMSExcel=true)
Convertes an array for CSV usage.
@classDescription Date and time handling
addSheet(string $a_name, bool $a_activate=true)
Add sheet.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static getDataDir()
get data directory (outside webspace)
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
escapeXML(string $value)
createExportDirectory()
creates data directory for export files
static _writeNewAccountMail(string $a_lang, string $a_subject, string $a_sal_g, string $a_sal_f, string $a_sal_m, string $a_body)
__construct(int $a_id, bool $a_call_by_reference=true)
getUserDefinedExportFields()
Get all exportable user defined fields.
static _updateAccountMailAttachment(string $a_lang, string $a_tmp_name, string $a_name)
Update account mail attachment.
createCSVExport(array $settings, array $data, string $filename)
createXMLExport(array $settings, array $data, string $filename)
static getProfileFields()
Get profile fields.
static _updateUserFolderAssignment(int $a_old_id, int $a_new_id)
Update user folder assignment Typically called after deleting a category with local user accounts.
createExcelExport(array $settings, array $data, string $filename)
buildExportFile(string $a_mode=self::FILE_TYPE_EXCEL, ?array $user_data_filter=null, bool $use_temp_dir=false)
build xml export file
static _lookupNewAccountMail(string $a_lang)
static _deleteAccountMailAttachment(string $a_lang)
Delete account mail attachment.
getExportFilename(string $a_mode=self::FILE_TYPE_EXCEL)
getExportFiles()
Get a list of the already exported files in the export directory.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilUserProfile.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const USER_FOLDER_ID
Definition: constants.php:33
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: feed.php:28
$path
Definition: ltiservices.php:32
$res
Definition: ltiservices.php:69
$index
Definition: metadata.php:145
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
header include for all ilias files.
$query
$log
Definition: result.php:33
$lng