ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjUserFolder.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
14require_once "./Services/Object/classes/class.ilObject.php";
15
16define('USER_FOLDER_ID', 7);
17
19{
20 public const ORG_OP_EDIT_USER_ACCOUNTS = 'edit_user_accounts';
21
22 public const FILE_TYPE_EXCEL = 'userfolder_export_excel_x86';
23 public const FILE_TYPE_CSV = 'userfolder_export_csv';
24 public const FILE_TYPE_XML = 'userfolder_export_xml';
31 public function __construct($a_id, $a_call_by_reference = true)
32 {
33 $this->type = "usrf";
34 parent::__construct($a_id, $a_call_by_reference);
35 }
36
37
44 public function delete()
45 {
46 // DISABLED
47 return false;
48
49 // always call parent delete function first!!
50 if (!parent::delete()) {
51 return false;
52 }
53 // put here userfolder specific stuff
54
55 // always call parent delete function at the end!!
56 return true;
57 }
58
59
60 public function getExportFilename($a_mode = self::FILE_TYPE_EXCEL)
61 {
62 $filename = "";
63 //$settings = $this->ilias->getAllSettings();
64 //$this->inst_id = $settings["inst_id"];
65 $inst_id = IL_INST_ID;
66
67 $date = time();
68
69 switch ($a_mode) {
71 $filename = $date . "__" . $inst_id . "__xls_usrf";
72 break;
74 $filename = $date . "__" . $inst_id . "__csv_usrf.csv";
75 break;
77 $filename = $date . "__" . $inst_id . "__xml_usrf.xml";
78 break;
79 }
80 return $filename;
81 }
82
83
91 public function getExportDirectory()
92 {
93 $export_dir = ilUtil::getDataDir() . "/usrf_data/export";
94
95 return $export_dir;
96 }
97
106 public function getExportFiles()
107 {
108 $dir = $this->getExportDirectory();
109
110 // quit if export dir not available
111 if (!@is_dir($dir) or
112 !is_writeable($dir)) {
113 return array();
114 }
115
116 // open directory
117 $dir = dir($dir);
118
119 // initialize array
120 $file = array();
121
122 // get files and save the in the array
123 while ($entry = $dir->read()) {
124 if ($entry != "." and
125 $entry != ".." and
126 preg_match("/^[0-9]{10}_{2}[0-9]+_{2}([a-z0-9]{3})_usrf\.[a-z]{1,4}\$/", $entry, $matches)) {
127 $filearray["filename"] = $entry;
128 $filearray["filesize"] = filesize($this->getExportDirectory() . "/" . $entry);
129 array_push($file, $filearray);
130 }
131 }
132
133 // close import directory
134 $dir->close();
135
136 // sort files
137 sort($file);
138 reset($file);
139
140 return $file;
141 }
142
143 protected function escapeXML($value)
144 {
145 $value = str_replace("&", "&amp;", $value);
146 $value = str_replace("<", "&lt;", $value);
147 $value = str_replace(">", "&gt;", $value);
148 return $value;
149 }
150
151 protected function createXMLExport(&$settings, &$data, $filename)
152 {
153 include_once './Services/User/classes/class.ilUserDefinedData.php';
154 include_once './Services/User/classes/class.ilObjUser.php';
155
156 global $DIC;
157
158 $rbacreview = $DIC['rbacreview'];
159 global $DIC;
160
161 $ilDB = $DIC['ilDB'];
162 global $DIC;
163
164 $log = $DIC['log'];
165
166 $file = fopen($filename, "w");
167
168 if (is_array($data)) {
169 include_once './Services/User/classes/class.ilUserXMLWriter.php';
170
171 $xmlWriter = new ilUserXMLWriter();
172 $xmlWriter->setObjects($data);
173 $xmlWriter->setSettings($settings);
174 $xmlWriter->setAttachRoles(true);
175
176 if ($xmlWriter->start()) {
177 fwrite($file, $xmlWriter->getXML());
178 }
179 }
180 }
181
182
186 protected function getUserDefinedExportFields()
187 {
188 include_once './Services/User/classes/class.ilUserDefinedFields.php';
190
191 $udf_ex_fields = array();
192 foreach ($udf_obj->getDefinitions() as $definition) {
193 if ($definition["export"] != false) {
194 $udf_ex_fields[] = array("name" => $definition["field_name"],
195 "id" => $definition["field_id"]);
196 }
197 }
198
199 return $udf_ex_fields;
200 }
201
202 protected function createCSVExport(&$settings, &$data, $filename)
203 {
204
205 // header
206 $headerrow = array();
207 $udf_ex_fields = $this->getUserDefinedExportFields();
208 foreach ($settings as $value) { // standard fields
209 array_push($headerrow, $this->lng->txt($value));
210 }
211 foreach ($udf_ex_fields as $f) { // custom fields
212 array_push($headerrow, $f["name"]);
213 }
214
215 $separator = ";";
216 $file = fopen($filename, "w");
217 $formattedrow = &ilUtil::processCSVRow($headerrow, true, $separator);
218 fwrite($file, join($separator, $formattedrow) . "\n");
219 foreach ($data as $row) {
220 $csvrow = array();
221 foreach ($settings as $header) { // standard fields
222 // multi-text
223 if (is_array($row[$header])) {
224 $row[$header] = implode(", ", $row[$header]);
225 }
226
227 array_push($csvrow, $row[$header]);
228 }
229
230 // custom fields
231 reset($udf_ex_fields);
232 if (count($udf_ex_fields) > 0) {
233 include_once("./Services/User/classes/class.ilUserDefinedData.php");
234 $udf = new ilUserDefinedData($row["usr_id"]);
235 foreach ($udf_ex_fields as $f) { // custom fields
236 array_push($csvrow, $udf->get("f_" . $f["id"]));
237 }
238 }
239
240 $formattedrow = &ilUtil::processCSVRow($csvrow, true, $separator);
241 fwrite($file, join($separator, $formattedrow) . "\n");
242 }
243 fclose($file);
244 }
245
246 protected function createExcelExport(&$settings, &$data, $filename)
247 {
248 include_once "./Services/Excel/classes/class.ilExcel.php";
249 $worksheet = new ilExcel();
250 $worksheet->addSheet($this->lng->txt("users"));
251
252 $row = 1;
253 $col = 0;
254
255 $udf_ex_fields = $this->getUserDefinedExportFields();
256
257 // title row
258 foreach ($settings as $value) { // standard fields
259 if ($value == 'ext_account') {
260 $value = 'user_ext_account';
261 }
262 $worksheet->setCell($row, $col, $this->lng->txt($value));
263 $col++;
264 }
265 foreach ($udf_ex_fields as $f) { // custom fields
266 $worksheet->setCell($row, $col, $f["name"]);
267 $col++;
268 }
269 $worksheet->setBold("A1:" . $worksheet->getColumnCoord($col - 1) . "1");
270
271 $this->lng->loadLanguageModule("meta");
272 foreach ($data as $index => $rowdata) {
273 $row++;
274 $col = 0;
275
276 // standard fields
277 foreach ($settings as $fieldname) {
278 $value = $rowdata[$fieldname];
279 switch ($fieldname) {
280 case "language":
281 $worksheet->setCell($row, $col, $this->lng->txt("meta_l_" . $value));
282 break;
283 case "time_limit_from":
284 case "time_limit_until":
285 $value = $value
286 ? new ilDateTime($value, IL_CAL_UNIX)
287 : null;
288 $worksheet->setCell($row, $col, $value);
289 break;
290 case "last_login":
291 case "last_update":
292 case "create_date":
293 case "approve_date":
294 case "agree_date":
295 $value = $value
296 ? new ilDateTime($value, IL_CAL_DATETIME)
297 : null;
298 $worksheet->setCell($row, $col, $value);
299 break;
300
301 case "interests_general":
302 case "interests_help_offered":
303 case "interests_help_looking":
304 if (is_array($value) && sizeof($value)) {
305 $value = implode(", ", $value);
306 } else {
307 $value = null;
308 }
309 // fallthrough
310
311 // no break
312 default:
313 $worksheet->setCell($row, $col, $value);
314 break;
315 }
316 $col++;
317 }
318
319 // custom fields
320 reset($udf_ex_fields);
321 if (count($udf_ex_fields) > 0) {
322 include_once("./Services/User/classes/class.ilUserDefinedData.php");
323 $udf = new ilUserDefinedData($rowdata["usr_id"]);
324 foreach ($udf_ex_fields as $f) { // custom fields
325 $worksheet->setCell($row, $col, $udf->get("f_" . $f["id"]));
326 $col++;
327 }
328 }
329 }
330
331 $worksheet->writeToFile($filename);
332 }
333
339 public static function getExportSettings()
340 {
341 global $DIC;
342
343 $ilDB = $DIC['ilDB'];
344
345 $db_settings = array();
346
347 include_once("./Services/User/classes/class.ilUserProfile.php");
348 $up = new ilUserProfile();
349 $up->skipField("roles");
350 $profile_fields = $up->getStandardFields();
351
352 /*$profile_fields =& ilObjUserFolder::getProfileFields();
353 $profile_fields[] = "preferences";*/
354
355 $query = "SELECT * FROM settings WHERE " .
356 $ilDB->like("keyword", "text", '%usr_settings_export_%');
357 $result = $ilDB->query($query);
358 while ($row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
359 if ($row["value"] == "1") {
360 if (preg_match("/usr_settings_export_(.*)/", $row["keyword"], $setting)) {
361 array_push($db_settings, $setting[1]);
362 }
363 }
364 }
365 $export_settings = array();
366 foreach ($profile_fields as $key => $value) {
367 if (in_array($key, $db_settings)) {
368 if (strcmp($key, "password") == 0) {
369 // we do not support password export with ILIAS >= 4.5.x
370 continue;
371 } else {
372 array_push($export_settings, $key);
373 }
374 }
375 }
376 array_push($export_settings, "usr_id");
377 array_push($export_settings, "login");
378 array_push($export_settings, "last_login");
379 array_push($export_settings, "last_update");
380 array_push($export_settings, "create_date");
381 array_push($export_settings, "time_limit_owner");
382 array_push($export_settings, "time_limit_unlimited");
383 array_push($export_settings, "time_limit_from");
384 array_push($export_settings, "time_limit_until");
385 array_push($export_settings, "time_limit_message");
386 array_push($export_settings, "active");
387 array_push($export_settings, "approve_date");
388 array_push($export_settings, "agree_date");
389 array_push($export_settings, "client_ip");
390 array_push($export_settings, "auth_mode");
391 array_push($export_settings, "ext_account");
392 array_push($export_settings, "feedhash");
393 return $export_settings;
394 }
395
404 public function buildExportFile($a_mode = self::FILE_TYPE_EXCEL, $user_data_filter = false, $use_temp_dir = false)
405 {
406 global $DIC;
407
408 $ilBench = $DIC['ilBench'];
409 global $DIC;
410
411 $log = $DIC['log'];
412 global $DIC;
413
414 $ilDB = $DIC['ilDB'];
415 global $DIC;
416
417 $ilias = $DIC['ilias'];
418 global $DIC;
419
420 $lng = $DIC['lng'];
421
422 if ($use_temp_dir) {
423 $expDir = ilUtil::ilTempnam();
424 $fullname = $expDir;
425 } else {
426 $expDir = $this->getExportDirectory();
427 // create export directory if needed
428 $this->createExportDirectory();
429 $fullname = $expDir . "/" . $this->getExportFilename($a_mode);
430 }
431
432 //get data
433 //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
434 $settings = &$this->getExportSettings();
435
436 // user languages
437 $query = "SELECT * FROM usr_pref WHERE keyword = " . $ilDB->quote('language', 'text');
438 $res = $ilDB->query($query);
439 $languages = array();
440 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
441 $languages[$row['usr_id']] = $row['value'];
442 }
443
444 // multi-text
445 $multi = array();
446 $set = $ilDB->query("SELECT * FROM usr_data_multi");
447 while ($row = $ilDB->fetchAssoc($set)) {
448 if (!is_array($user_data_filter) ||
449 in_array($row["usr_id"], $user_data_filter)) {
450 $multi[$row["usr_id"]][$row["field_id"]][] = $row["value"];
451 }
452 }
453
454 $data = array();
455 $query = "SELECT usr_data.* FROM usr_data " .
456 " ORDER BY usr_data.lastname, usr_data.firstname";
457 $result = $ilDB->query($query);
458 while ($row = $ilDB->fetchAssoc($result)) {
459 if (isset($languages[$row['usr_id']])) {
460 $row['language'] = $languages[$row['usr_id']];
461 } else {
462 $row['language'] = $lng->getDefaultLanguage();
463 }
464
465 if (isset($multi[$row["usr_id"]])) {
466 $row = array_merge($row, $multi[$row["usr_id"]]);
467 }
468
469 if (is_array($user_data_filter)) {
470 if (in_array($row["usr_id"], $user_data_filter)) {
471 array_push($data, $row);
472 }
473 } else {
474 array_push($data, $row);
475 }
476 }
477 //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
478
479 switch ($a_mode) {
481 $this->createExcelExport($settings, $data, $fullname);
482 break;
484 $this->createCSVExport($settings, $data, $fullname);
485 break;
487 $this->createXMLExport($settings, $data, $fullname);
488 break;
489 }
490 //$expLog->write(date("[y-m-d H:i:s] ")."Finished export of user data");
491
492 return $fullname;
493 }
494
495
501 protected function createExportDirectory()
502 {
503 if (!@is_dir($this->getExportDirectory())) {
504 $usrf_data_dir = ilUtil::getDataDir() . "/usrf_data";
505 ilUtil::makeDir($usrf_data_dir);
506 if (!is_writable($usrf_data_dir)) {
507 $this->ilias->raiseError("Userfolder data directory (" . $usrf_data_dir
508 . ") not writeable.", $this->ilias->error_obj->MESSAGE);
509 }
510
511 // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
512 $export_dir = $usrf_data_dir . "/export";
513 ilUtil::makeDir($export_dir);
514 if (!@is_dir($export_dir)) {
515 $this->ilias->raiseError("Creation of Userfolder Export Directory failed.", $this->ilias->error_obj->MESSAGE);
516 }
517 }
518 }
519
520
526 public static function &getProfileFields()
527 {
528 include_once("./Services/User/classes/class.ilUserProfile.php");
529 $up = new ilUserProfile();
530 $up->skipField("username");
531 $up->skipField("roles");
532 $up->skipGroup("preferences");
533 $fds = $up->getStandardFields();
534 foreach ($fds as $k => $f) {
535 $profile_fields[] = $k;
536 }
537
538 return $profile_fields;
539 }
540
541 public static function _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
542 {
543 global $DIC;
544
545 $ilDB = $DIC['ilDB'];
546
547 if (self::_lookupNewAccountMail($a_lang)) {
548 $values = array(
549 'subject' => array('text',$a_subject),
550 'body' => array('clob',$a_body),
551 'sal_g' => array('text',$a_sal_g),
552 'sal_f' => array('text',$a_sal_f),
553 'sal_m' => array('text',$a_sal_m)
554 );
555 $ilDB->update(
556 'mail_template',
557 $values,
558 array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
559 );
560 } else {
561 $values = array(
562 'subject' => array('text',$a_subject),
563 'body' => array('clob',$a_body),
564 'sal_g' => array('text',$a_sal_g),
565 'sal_f' => array('text',$a_sal_f),
566 'sal_m' => array('text',$a_sal_m),
567 'lang' => array('text',$a_lang),
568 'type' => array('text','nacc')
569 );
570 $ilDB->insert('mail_template', $values);
571 }
572 }
573
581 public static function _updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
582 {
583 global $DIC;
584
585 $ilDB = $DIC['ilDB'];
586
587 include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
589 $fs->create();
590 $path = $fs->getAbsolutePath() . "/";
591
592 ilUtil::moveUploadedFile($a_tmp_name, $a_lang, $path . $a_lang);
593
594 $ilDB->update(
595 'mail_template',
596 array('att_file' => array('text', $a_name)),
597 array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
598 );
599 }
600
605 public static function _deleteAccountMailAttachment($a_lang)
606 {
607 global $DIC;
608
609 $ilDB = $DIC['ilDB'];
610
611 include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
613 $path = $fs->getAbsolutePath() . "/";
614
615 if (file_exists($path . $a_lang)) {
616 unlink($path . $a_lang);
617 }
618
619 $ilDB->update(
620 'mail_template',
621 array('att_file' => array('text', '')),
622 array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
623 );
624 }
625
630 public static function _lookupNewAccountMail($a_lang)
631 {
632 global $DIC;
633
634 $ilDB = $DIC['ilDB'];
635
636 $set = $ilDB->query("SELECT * FROM mail_template " .
637 " WHERE type='nacc' AND lang = " . $ilDB->quote($a_lang, 'text'));
638
639 if ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
640 return $rec;
641 }
642 return array();
643 }
644
657 public static function _updateUserFolderAssignment($a_old_id, $a_new_id)
658 {
659 global $DIC;
660
661 $ilDB = $DIC['ilDB'];
662
663 $query = "UPDATE usr_data SET time_limit_owner = " . $ilDB->quote($a_new_id, "integer") . " " .
664 "WHERE time_limit_owner = " . $ilDB->quote($a_old_id, "integer") . " ";
665 $ilDB->manipulate($query);
666
667 return true;
668 }
669} // END class.ilObjUserFolder
$result
$filename
Definition: buildRTE.php:89
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
const IL_CAL_DATETIME
const USER_FOLDER_ID
Class ilObjUserFolder.
@classDescription Date and time handling
static _deleteAccountMailAttachment($a_lang)
Delete account mail attachment.
createExportDirectory()
creates data directory for export files (data_dir/usrf_data/export, depending on data directory that ...
buildExportFile($a_mode=self::FILE_TYPE_EXCEL, $user_data_filter=false, $use_temp_dir=false)
build xml export file
getExportFilename($a_mode=self::FILE_TYPE_EXCEL)
getUserDefinedExportFields()
Get all exportable user defined fields.
createExcelExport(&$settings, &$data, $filename)
__construct($a_id, $a_call_by_reference=true)
Constructor @access public.
static _updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
Update account mail attachment.
createCSVExport(&$settings, &$data, $filename)
createXMLExport(&$settings, &$data, $filename)
static _updateUserFolderAssignment($a_old_id, $a_new_id)
Update user folder assignment Typically called after deleting a category with local user accounts.
static getExportSettings()
getExport Settings
static & getProfileFields()
Get profile fields (DEPRECATED, use ilUserProfile() instead)
static _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
static _lookupNewAccountMail($a_lang)
getExportFiles()
Get a list of the already exported files in the export directory.
getExportDirectory()
Get the location of the export directory for the user accounts.
Class ilObject Basic functions for all objects.
Class ilUserDefinedData.
static _getInstance()
Get instance.
Class ilUserProfile.
XML writer class.
static getDataDir()
get data directory (outside webspace)
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
static & processCSVRow(&$row, $quoteAll=false, $separator=";", $outUTF8=false, $compatibleWithMSExcel=true)
Convertes an array for CSV usage.
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
const IL_INST_ID
Definition: constants.php:38
global $DIC
Definition: goto.php:24
global $ilBench
Definition: ilias.php:21
$index
Definition: metadata.php:128
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
redirection script todo: (a better solution should control the processing via a xml file)
$query
foreach($_POST as $key=> $value) $res
global $ilDB
$data
Definition: storeScorm.php:23