ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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{
26 function ilObjUserFolder($a_id,$a_call_by_reference = true)
27 {
28 $this->type = "usrf";
29 $this->ilObject($a_id,$a_call_by_reference);
30 }
31
32
39 function delete()
40 {
41 // DISABLED
42 return false;
43
44 // always call parent delete function first!!
45 if (!parent::delete())
46 {
47 return false;
48 }
49 // put here userfolder specific stuff
50
51 // always call parent delete function at the end!!
52 return true;
53 }
54
55
56 function getExportFilename($a_mode = "userfolder_export_excel_x86")
57 {
58 $filename = "";
59 //$settings = $this->ilias->getAllSettings();
60 //$this->inst_id = $settings["inst_id"];
61 $inst_id = IL_INST_ID;
62
63 $date = time();
64
65 switch($a_mode)
66 {
67 case "userfolder_export_excel_x86":
68 $filename = $date."__".$inst_id."__xls_usrf.xls";
69 break;
70 case "userfolder_export_csv":
71 $filename = $date."__".$inst_id."__csv_usrf.csv";
72 break;
73 case "userfolder_export_xml":
74 $filename = $date."__".$inst_id."__xml_usrf.xml";
75 break;
76 }
77 return $filename;
78 }
79
80
89 {
90 $export_dir = ilUtil::getDataDir()."/usrf_data/export";
91
92 return $export_dir;
93 }
94
103 function getExportFiles()
104 {
105 $dir = $this->getExportDirectory();
106
107 // quit if export dir not available
108 if (!@is_dir($dir) or
109 !is_writeable($dir))
110 {
111 return array();
112 }
113
114 // open directory
115 $dir = dir($dir);
116
117 // initialize array
118 $file = array();
119
120 // get files and save the in the array
121 while ($entry = $dir->read())
122 {
123 if ($entry != "." and
124 $entry != ".." and
125 preg_match("/^[0-9]{10}_{2}[0-9]+_{2}([a-z0-9]{3})_usrf\.[a-z]{1,3}\$/", $entry, $matches))
126 {
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 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 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 $rbacreview;
157 global $ilDB;
158 global $log;
159
160 $file = fopen($filename, "w");
161
162 if (is_array($data))
163 {
164 include_once './Services/User/classes/class.ilUserXMLWriter.php';
165
166 $xmlWriter = new ilUserXMLWriter();
167 $xmlWriter->setObjects($data);
168 $xmlWriter->setSettings($settings);
169 $xmlWriter->setAttachRoles (true);
170
171 if($xmlWriter->start())
172 {
173 fwrite($file, $xmlWriter->getXML());
174 }
175 }
176 }
177
178
183 {
184 include_once './Services/User/classes/class.ilUserDefinedFields.php';
186
187 $udf_ex_fields = array();
188 foreach($udf_obj->getDefinitions() as $definition)
189 {
190 if ($definition["export"] != FALSE)
191 {
192 $udf_ex_fields[] = array("name" => $definition["field_name"],
193 "id" => $definition["field_id"]);
194 }
195 }
196
197 return $udf_ex_fields;
198 }
199
200 function createCSVExport(&$settings, &$data, $filename)
201 {
202
203 // header
204 $headerrow = array();
205 $udf_ex_fields = $this->getUserDefinedExportFields();
206 foreach ($settings as $value) // standard fields
207 {
208 array_push($headerrow, $this->lng->txt($value));
209 }
210 foreach ($udf_ex_fields as $f) // custom fields
211 {
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 {
221 $csvrow = array();
222 foreach ($settings as $header) // standard fields
223 {
224 // multi-text
225 if(is_array($row[$header]))
226 {
227 $row[$header] = implode(", ", $row[$header]);
228 }
229
230 array_push($csvrow, $row[$header]);
231 }
232
233 // custom fields
234 reset($udf_ex_fields);
235 if (count($udf_ex_fields) > 0)
236 {
237 include_once("./Services/User/classes/class.ilUserDefinedData.php");
238 $udf = new ilUserDefinedData($row["usr_id"]);
239 foreach ($udf_ex_fields as $f) // custom fields
240 {
241 array_push($csvrow, $udf->get("f_".$f["id"]));
242 }
243 }
244
245 $formattedrow =& ilUtil::processCSVRow($csvrow, TRUE, $separator);
246 fwrite($file, join ($separator, $formattedrow) ."\n");
247 }
248 fclose($file);
249 }
250
251 function createExcelExport(&$settings, &$data, $filename, $a_mode)
252 {
253 include_once "./Services/Excel/classes/class.ilExcelUtils.php";
254 include_once "./Services/Excel/classes/class.ilExcelWriterAdapter.php";
255 $adapter = new ilExcelWriterAdapter($filename, FALSE);
256 $workbook = $adapter->getWorkbook();
257 // Creating a worksheet
258 $format_bold =& $workbook->addFormat();
259 $format_bold->setBold();
260 $format_percent =& $workbook->addFormat();
261 $format_percent->setNumFormat("0.00%");
262 $format_datetime =& $workbook->addFormat();
263 $format_datetime->setNumFormat("DD/MM/YYYY hh:mm:ss");
264 $format_title =& $workbook->addFormat();
265 $format_title->setBold();
266 $format_title->setColor('black');
267 $format_title->setPattern(1);
268 $format_title->setFgColor('silver');
269 $worksheet =& $workbook->addWorksheet();
270 $row = 0;
271 $col = 0;
272
273 $udf_ex_fields = $this->getUserDefinedExportFields();
274
275 // title row
276 foreach ($settings as $value) // standard fields
277 {
278 if($value == 'ext_account')
279 {
280 $value = 'user_ext_account';
281 }
282 $worksheet->write($row, $col, ilExcelUtils::_convert_text($this->lng->txt($value), $a_mode), $format_title);
283 $col++;
284 }
285 foreach ($udf_ex_fields as $f) // custom fields
286 {
287 $worksheet->write($row, $col, ilExcelUtils::_convert_text($f["name"], $a_mode), $format_title);
288 $col++;
289 }
290
291 $this->lng->loadLanguageModule("meta");
292 foreach ($data as $index => $rowdata)
293 {
294 $row++;
295 $col = 0;
296
297 // standard fields
298 foreach ($settings as $fieldname)
299 {
300 $value = $rowdata[$fieldname];
301 switch ($fieldname)
302 {
303 case "language":
304 $worksheet->write($row, $col, ilExcelUtils::_convert_text($this->lng->txt("meta_l_".$value), $a_mode));
305 break;
306 case "time_limit_from":
307 case "time_limit_until":
308 $date = strftime("%Y-%m-%d %H:%M:%S", $value);
309 if (preg_match("/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/", $date, $matches))
310 {
311 $worksheet->write($row, $col, ilUtil::excelTime($matches[1],$matches[2],$matches[3],$matches[4],$matches[5],$matches[6]), $format_datetime);
312 }
313 break;
314 case "last_login":
315 case "last_update":
316 case "create_date":
317 case "approve_date":
318 case "agree_date":
319 if (preg_match("/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/", $value, $matches))
320 {
321 $worksheet->write($row, $col, ilUtil::excelTime($matches[1],$matches[2],$matches[3],$matches[4],$matches[5],$matches[6]), $format_datetime);
322 }
323 break;
324
325 case "interests_general":
326 case "interests_help_offered":
327 case "interests_help_looking":
328 if(is_array($value) && sizeof($value))
329 {
330 $value = implode(", ", $value);
331 }
332 else
333 {
334 $value = null;
335 }
336 // fallthrough
337
338 default:
339 $worksheet->write($row, $col, ilExcelUtils::_convert_text($value, $a_mode));
340 break;
341 }
342 $col++;
343 }
344
345 // custom fields
346 reset($udf_ex_fields);
347 if (count($udf_ex_fields) > 0)
348 {
349 include_once("./Services/User/classes/class.ilUserDefinedData.php");
350 $udf = new ilUserDefinedData($rowdata["usr_id"]);
351 foreach ($udf_ex_fields as $f) // custom fields
352 {
353 $worksheet->write($row, $col, ilExcelUtils::_convert_text($udf->get("f_".$f["id"]), $a_mode));
354 $col++;
355 }
356 }
357 }
358 $workbook->close();
359 }
360
366 static function getExportSettings()
367 {
368 global $ilDB;
369
370 $db_settings = array();
371
372 include_once("./Services/User/classes/class.ilUserProfile.php");
373 $up = new ilUserProfile();
374 $up->skipField("roles");
375 $profile_fields = $up->getStandardFields();
376
377 /*$profile_fields =& ilObjUserFolder::getProfileFields();
378 $profile_fields[] = "preferences";*/
379
380 $query = "SELECT * FROM settings WHERE ".
381 $ilDB->like("keyword", "text", '%usr_settings_export_%');
382 $result = $ilDB->query($query);
383 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
384 {
385 if ($row["value"] == "1")
386 {
387 if (preg_match("/usr_settings_export_(.*)/", $row["keyword"], $setting))
388 {
389 array_push($db_settings, $setting[1]);
390 }
391 }
392 }
393 $export_settings = array();
394 foreach ($profile_fields as $key => $value)
395 {
396 if (in_array($key, $db_settings))
397 {
398 if (strcmp($key, "password") == 0)
399 {
400 // we do not support password export with ILIAS >= 4.5.x
401 continue;
402 }
403 else
404 {
405 array_push($export_settings, $key);
406 }
407 }
408 }
409 array_push($export_settings, "usr_id");
410 array_push($export_settings, "login");
411 array_push($export_settings, "last_login");
412 array_push($export_settings, "last_update");
413 array_push($export_settings, "create_date");
414 array_push($export_settings, "time_limit_owner");
415 array_push($export_settings, "time_limit_unlimited");
416 array_push($export_settings, "time_limit_from");
417 array_push($export_settings, "time_limit_until");
418 array_push($export_settings, "time_limit_message");
419 array_push($export_settings, "active");
420 array_push($export_settings, "approve_date");
421 array_push($export_settings, "agree_date");
422 array_push($export_settings, "client_ip");
423 array_push($export_settings, "auth_mode");
424 array_push($export_settings, "ext_account");
425 array_push($export_settings, "feedhash");
426 return $export_settings;
427 }
428
432 function buildExportFile($a_mode = "userfolder_export_excel_x86", $user_data_filter = FALSE)
433 {
434 global $ilBench;
435 global $log;
436 global $ilDB;
437 global $ilias;
438 global $lng;
439
440 //get Log File
441 $expDir = $this->getExportDirectory();
442 //$expLog = &$log;
443 //$expLog->delete();
444 //$expLog->setLogFormat("");
445 //$expLog->write(date("[y-m-d H:i:s] ")."Start export of user data");
446
447 // create export directory if needed
448 $this->createExportDirectory();
449
450 //get data
451 //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
452 $settings =& $this->getExportSettings();
453
454 // user languages
455 $query = "SELECT * FROM usr_pref WHERE keyword = ".$ilDB->quote('language','text');
456 $res = $ilDB->query($query);
457 $languages = array();
458 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
459 {
460 $languages[$row['usr_id']] = $row['value'];
461 }
462
463 // multi-text
464 $multi = array();
465 $set = $ilDB->query("SELECT * FROM usr_data_multi");
466 while($row = $ilDB->fetchAssoc($set))
467 {
468 if(!is_array($user_data_filter) ||
469 in_array($row["usr_id"], $user_data_filter))
470 {
471 $multi[$row["usr_id"]][$row["field_id"]][] = $row["value"];
472 }
473 }
474
475 $data = array();
476 $query = "SELECT usr_data.* FROM usr_data ".
477 " ORDER BY usr_data.lastname, usr_data.firstname";
478 $result = $ilDB->query($query);
479 while ($row = $ilDB->fetchAssoc($result))
480 {
481 if(isset($languages[$row['usr_id']]))
482 {
483 $row['language'] = $languages[$row['usr_id']];
484 }
485 else
486 {
487 $row['language'] = $lng->getDefaultLanguage();
488 }
489
490 if(isset($multi[$row["usr_id"]]))
491 {
492 $row = array_merge($row, $multi[$row["usr_id"]]);
493 }
494
495 if (is_array($user_data_filter))
496 {
497 if (in_array($row["usr_id"], $user_data_filter)) array_push($data, $row);
498 }
499 else
500 {
501 array_push($data, $row);
502 }
503 }
504 //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
505
506 $fullname = $expDir."/".$this->getExportFilename($a_mode);
507 switch ($a_mode)
508 {
509 case "userfolder_export_excel_x86":
510 $this->createExcelExport($settings, $data, $fullname, "latin1");
511 break;
512 case "userfolder_export_csv":
513 $this->createCSVExport($settings, $data, $fullname);
514 break;
515 case "userfolder_export_xml":
516 $this->createXMLExport($settings, $data, $fullname);
517 break;
518 }
519 //$expLog->write(date("[y-m-d H:i:s] ")."Finished export of user data");
520
521 return $fullname;
522 }
523
524
531 {
532 if (!@is_dir($this->getExportDirectory()))
533 {
534 $usrf_data_dir = ilUtil::getDataDir()."/usrf_data";
535 ilUtil::makeDir($usrf_data_dir);
536 if(!is_writable($usrf_data_dir))
537 {
538 $this->ilias->raiseError("Userfolder data directory (".$usrf_data_dir
539 .") not writeable.",$this->ilias->error_obj->MESSAGE);
540 }
541
542 // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
543 $export_dir = $usrf_data_dir."/export";
544 ilUtil::makeDir($export_dir);
545 if(!@is_dir($export_dir))
546 {
547 $this->ilias->raiseError("Creation of Userfolder Export Directory failed.",$this->ilias->error_obj->MESSAGE);
548 }
549 }
550 }
551
552
558 static function &getProfileFields()
559 {
560 include_once("./Services/User/classes/class.ilUserProfile.php");
561 $up = new ilUserProfile();
562 $up->skipField("username");
563 $up->skipField("roles");
564 $up->skipGroup("preferences");
565 $fds = $up->getStandardFields();
566 foreach ($fds as $k => $f)
567 {
568 $profile_fields[] = $k;
569 }
570
571 return $profile_fields;
572 }
573
574 function _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
575 {
576 global $ilDB;
577
578 if(self::_lookupNewAccountMail($a_lang))
579 {
580 $values = array(
581 'subject' => array('text',$a_subject),
582 'body' => array('clob',$a_body),
583 'sal_g' => array('text',$a_sal_g),
584 'sal_f' => array('text',$a_sal_f),
585 'sal_m' => array('text',$a_sal_m)
586 );
587 $ilDB->update('mail_template',
588 $values,
589 array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
590 );
591 }
592 else
593 {
594 $values = array(
595 'subject' => array('text',$a_subject),
596 'body' => array('clob',$a_body),
597 'sal_g' => array('text',$a_sal_g),
598 'sal_f' => array('text',$a_sal_f),
599 'sal_m' => array('text',$a_sal_m),
600 'lang' => array('text',$a_lang),
601 'type' => array('text','nacc')
602 );
603 $ilDB->insert('mail_template',$values);
604 }
605 }
606
607 function _updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
608 {
609 global $ilDB;
610
611 include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
612 $fs = new ilFSStorageUserFolder($this->getId());
613 $fs->create();
614 $path = $fs->getAbsolutePath()."/";
615
616 ilUtil::moveUploadedFile($a_tmp_name, $a_lang, $path.$a_lang);
617
618 $ilDB->update('mail_template',
619 array('att_file' => array('text', $a_name)),
620 array('lang' => array('text',$a_lang), 'type' => array('text','nacc')));
621 }
622
624 {
625 global $ilDB;
626
627 include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
628 $fs = new ilFSStorageUserFolder($this->getId());
629 $path = $fs->getAbsolutePath()."/";
630
631 @unlink($path.$a_lang);
632
633 $ilDB->update('mail_template',
634 array('att_file' => array('text', '')),
635 array('lang' => array('text',$a_lang), 'type' => array('text','nacc')));
636 }
637
638 function _lookupNewAccountMail($a_lang)
639 {
640 global $ilDB;
641
642 $set = $ilDB->query("SELECT * FROM mail_template ".
643 " WHERE type='nacc' AND lang = ".$ilDB->quote($a_lang,'text'));
644
645 if ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
646 {
647 return $rec;
648 }
649 return array();
650 }
651
663 public static function _updateUserFolderAssignment($a_old_id,$a_new_id)
664 {
665 global $ilDB;
666
667 $query = "UPDATE usr_data SET time_limit_owner = ".$ilDB->quote($a_new_id, "integer")." ".
668 "WHERE time_limit_owner = ".$ilDB->quote($a_old_id, "integer")." ";
669 $ilDB->manipulate($query);
670
671 return true;
672 }
673
674
675} // END class.ilObjUserFolder
676?>
$result
print $file
$filename
Definition: buildRTE.php:89
const DB_FETCHMODE_ASSOC
Definition: class.ilDB.php:10
_convert_text($a_text, $a_target="has been removed")
Class ilExcelWriterAdapter.
createExportDirectory()
creates data directory for export files (data_dir/usrf_data/export, depending on data directory that ...
_deleteAccountMailAttachment($a_lang)
_updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
getUserDefinedExportFields()
Get all exportable user defined fields.
_writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
getExportFilename($a_mode="userfolder_export_excel_x86")
ilObjUserFolder($a_id, $a_call_by_reference=true)
Constructor @access public.
createExcelExport(&$settings, &$data, $filename, $a_mode)
createCSVExport(&$settings, &$data, $filename)
buildExportFile($a_mode="userfolder_export_excel_x86", $user_data_filter=FALSE)
build xml export file
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)
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.
ilObject($a_id=0, $a_reference=true)
Constructor @access public.
getId()
get object id @access public
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 excelTime($year="", $month="", $day="", $hour="", $minute="", $second="")
Calculates a Microsoft Excel date/time value.
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 ...
$header
$data
global $ilBench
Definition: ilias.php:18
$separator
redirection script todo: (a better solution should control the processing via a xml file)
$path
Definition: index.php:22
global $ilDB