ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
14 require_once "./Services/Object/classes/class.ilObject.php";
15 
16 define('USER_FOLDER_ID',7);
17 
19 {
26  function __construct($a_id,$a_call_by_reference = true)
27  {
28  $this->type = "usrf";
29  parent::__construct($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";
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 
88  function getExportDirectory()
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,4}\$/", $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';
185  $udf_obj =& ilUserDefinedFields::_getInstance();
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)
252  {
253  include_once "./Services/Excel/classes/class.ilExcel.php";
254  $worksheet = new ilExcel();
255  $worksheet->addSheet($this->lng->txt("users"));
256 
257  $row = 1;
258  $col = 0;
259 
260  $udf_ex_fields = $this->getUserDefinedExportFields();
261 
262  // title row
263  foreach ($settings as $value) // standard fields
264  {
265  if($value == 'ext_account')
266  {
267  $value = 'user_ext_account';
268  }
269  $worksheet->setCell($row, $col, $this->lng->txt($value));
270  $col++;
271  }
272  foreach ($udf_ex_fields as $f) // custom fields
273  {
274  $worksheet->setCell($row, $col, $f["name"]);
275  $col++;
276  }
277  $worksheet->setBold("A1:".$worksheet->getColumnCoord($col-1)."1");
278 
279  $this->lng->loadLanguageModule("meta");
280  foreach ($data as $index => $rowdata)
281  {
282  $row++;
283  $col = 0;
284 
285  // standard fields
286  foreach ($settings as $fieldname)
287  {
288  $value = $rowdata[$fieldname];
289  switch ($fieldname)
290  {
291  case "language":
292  $worksheet->setCell($row, $col, $this->lng->txt("meta_l_".$value));
293  break;
294  case "time_limit_from":
295  case "time_limit_until":
296  $value = $value
297  ? new ilDateTime($value, IL_CAL_UNIX)
298  : null;
299  $worksheet->setCell($row, $col, $value);
300  break;
301  case "last_login":
302  case "last_update":
303  case "create_date":
304  case "approve_date":
305  case "agree_date":
306  $value = $value
307  ? new ilDateTime($value, IL_CAL_DATETIME)
308  : null;
309  $worksheet->setCell($row, $col, $value);
310  break;
311 
312  case "interests_general":
313  case "interests_help_offered":
314  case "interests_help_looking":
315  if(is_array($value) && sizeof($value))
316  {
317  $value = implode(", ", $value);
318  }
319  else
320  {
321  $value = null;
322  }
323  // fallthrough
324 
325  default:
326  $worksheet->setCell($row, $col, $value);
327  break;
328  }
329  $col++;
330  }
331 
332  // custom fields
333  reset($udf_ex_fields);
334  if (count($udf_ex_fields) > 0)
335  {
336  include_once("./Services/User/classes/class.ilUserDefinedData.php");
337  $udf = new ilUserDefinedData($rowdata["usr_id"]);
338  foreach ($udf_ex_fields as $f) // custom fields
339  {
340  $worksheet->setCell($row, $col, $udf->get("f_".$f["id"]));
341  $col++;
342  }
343  }
344  }
345 
346  $worksheet->writeToFile($filename);
347  }
348 
354  static function getExportSettings()
355  {
356  global $ilDB;
357 
358  $db_settings = array();
359 
360  include_once("./Services/User/classes/class.ilUserProfile.php");
361  $up = new ilUserProfile();
362  $up->skipField("roles");
363  $profile_fields = $up->getStandardFields();
364 
365  /*$profile_fields =& ilObjUserFolder::getProfileFields();
366  $profile_fields[] = "preferences";*/
367 
368  $query = "SELECT * FROM settings WHERE ".
369  $ilDB->like("keyword", "text", '%usr_settings_export_%');
370  $result = $ilDB->query($query);
371  while ($row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC))
372  {
373  if ($row["value"] == "1")
374  {
375  if (preg_match("/usr_settings_export_(.*)/", $row["keyword"], $setting))
376  {
377  array_push($db_settings, $setting[1]);
378  }
379  }
380  }
381  $export_settings = array();
382  foreach ($profile_fields as $key => $value)
383  {
384  if (in_array($key, $db_settings))
385  {
386  if (strcmp($key, "password") == 0)
387  {
388  // we do not support password export with ILIAS >= 4.5.x
389  continue;
390  }
391  else
392  {
393  array_push($export_settings, $key);
394  }
395  }
396  }
397  array_push($export_settings, "usr_id");
398  array_push($export_settings, "login");
399  array_push($export_settings, "last_login");
400  array_push($export_settings, "last_update");
401  array_push($export_settings, "create_date");
402  array_push($export_settings, "time_limit_owner");
403  array_push($export_settings, "time_limit_unlimited");
404  array_push($export_settings, "time_limit_from");
405  array_push($export_settings, "time_limit_until");
406  array_push($export_settings, "time_limit_message");
407  array_push($export_settings, "active");
408  array_push($export_settings, "approve_date");
409  array_push($export_settings, "agree_date");
410  array_push($export_settings, "client_ip");
411  array_push($export_settings, "auth_mode");
412  array_push($export_settings, "ext_account");
413  array_push($export_settings, "feedhash");
414  return $export_settings;
415  }
416 
420  function buildExportFile($a_mode = "userfolder_export_excel_x86", $user_data_filter = FALSE)
421  {
422  global $ilBench;
423  global $log;
424  global $ilDB;
425  global $ilias;
426  global $lng;
427 
428  //get Log File
429  $expDir = $this->getExportDirectory();
430  //$expLog = &$log;
431  //$expLog->delete();
432  //$expLog->setLogFormat("");
433  //$expLog->write(date("[y-m-d H:i:s] ")."Start export of user data");
434 
435  // create export directory if needed
436  $this->createExportDirectory();
437 
438  //get data
439  //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
440  $settings =& $this->getExportSettings();
441 
442  // user languages
443  $query = "SELECT * FROM usr_pref WHERE keyword = ".$ilDB->quote('language','text');
444  $res = $ilDB->query($query);
445  $languages = array();
446  while($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC))
447  {
448  $languages[$row['usr_id']] = $row['value'];
449  }
450 
451  // multi-text
452  $multi = array();
453  $set = $ilDB->query("SELECT * FROM usr_data_multi");
454  while($row = $ilDB->fetchAssoc($set))
455  {
456  if(!is_array($user_data_filter) ||
457  in_array($row["usr_id"], $user_data_filter))
458  {
459  $multi[$row["usr_id"]][$row["field_id"]][] = $row["value"];
460  }
461  }
462 
463  $data = array();
464  $query = "SELECT usr_data.* FROM usr_data ".
465  " ORDER BY usr_data.lastname, usr_data.firstname";
466  $result = $ilDB->query($query);
467  while ($row = $ilDB->fetchAssoc($result))
468  {
469  if(isset($languages[$row['usr_id']]))
470  {
471  $row['language'] = $languages[$row['usr_id']];
472  }
473  else
474  {
475  $row['language'] = $lng->getDefaultLanguage();
476  }
477 
478  if(isset($multi[$row["usr_id"]]))
479  {
480  $row = array_merge($row, $multi[$row["usr_id"]]);
481  }
482 
483  if (is_array($user_data_filter))
484  {
485  if (in_array($row["usr_id"], $user_data_filter)) array_push($data, $row);
486  }
487  else
488  {
489  array_push($data, $row);
490  }
491  }
492  //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
493 
494  $fullname = $expDir."/".$this->getExportFilename($a_mode);
495  switch ($a_mode)
496  {
497  case "userfolder_export_excel_x86":
498  $this->createExcelExport($settings, $data, $fullname);
499  break;
500  case "userfolder_export_csv":
501  $this->createCSVExport($settings, $data, $fullname);
502  break;
503  case "userfolder_export_xml":
504  $this->createXMLExport($settings, $data, $fullname);
505  break;
506  }
507  //$expLog->write(date("[y-m-d H:i:s] ")."Finished export of user data");
508 
509  return $fullname;
510  }
511 
512 
519  {
520  if (!@is_dir($this->getExportDirectory()))
521  {
522  $usrf_data_dir = ilUtil::getDataDir()."/usrf_data";
523  ilUtil::makeDir($usrf_data_dir);
524  if(!is_writable($usrf_data_dir))
525  {
526  $this->ilias->raiseError("Userfolder data directory (".$usrf_data_dir
527  .") not writeable.",$this->ilias->error_obj->MESSAGE);
528  }
529 
530  // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
531  $export_dir = $usrf_data_dir."/export";
532  ilUtil::makeDir($export_dir);
533  if(!@is_dir($export_dir))
534  {
535  $this->ilias->raiseError("Creation of Userfolder Export Directory failed.",$this->ilias->error_obj->MESSAGE);
536  }
537  }
538  }
539 
540 
546  static function &getProfileFields()
547  {
548  include_once("./Services/User/classes/class.ilUserProfile.php");
549  $up = new ilUserProfile();
550  $up->skipField("username");
551  $up->skipField("roles");
552  $up->skipGroup("preferences");
553  $fds = $up->getStandardFields();
554  foreach ($fds as $k => $f)
555  {
556  $profile_fields[] = $k;
557  }
558 
559  return $profile_fields;
560  }
561 
562  static function _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
563  {
564  global $ilDB;
565 
566  if(self::_lookupNewAccountMail($a_lang))
567  {
568  $values = array(
569  'subject' => array('text',$a_subject),
570  'body' => array('clob',$a_body),
571  'sal_g' => array('text',$a_sal_g),
572  'sal_f' => array('text',$a_sal_f),
573  'sal_m' => array('text',$a_sal_m)
574  );
575  $ilDB->update('mail_template',
576  $values,
577  array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
578  );
579  }
580  else
581  {
582  $values = array(
583  'subject' => array('text',$a_subject),
584  'body' => array('clob',$a_body),
585  'sal_g' => array('text',$a_sal_g),
586  'sal_f' => array('text',$a_sal_f),
587  'sal_m' => array('text',$a_sal_m),
588  'lang' => array('text',$a_lang),
589  'type' => array('text','nacc')
590  );
591  $ilDB->insert('mail_template',$values);
592  }
593  }
594 
602  public static function _updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
603  {
604  global $ilDB;
605 
606  include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
608  $fs->create();
609  $path = $fs->getAbsolutePath()."/";
610 
611  ilUtil::moveUploadedFile($a_tmp_name, $a_lang, $path.$a_lang);
612 
613  $ilDB->update('mail_template',
614  array('att_file' => array('text', $a_name)),
615  array('lang' => array('text',$a_lang), 'type' => array('text','nacc')));
616  }
617 
622  public static function _deleteAccountMailAttachment($a_lang)
623  {
624  global $ilDB;
625 
626  include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
628  $path = $fs->getAbsolutePath()."/";
629 
630  unlink($path.$a_lang);
631 
632  $ilDB->update('mail_template',
633  array('att_file' => array('text', '')),
634  array('lang' => array('text',$a_lang), 'type' => array('text','nacc')));
635  }
636 
637  static function _lookupNewAccountMail($a_lang)
638  {
639  global $ilDB;
640 
641  $set = $ilDB->query("SELECT * FROM mail_template ".
642  " WHERE type='nacc' AND lang = ".$ilDB->quote($a_lang,'text'));
643 
644  if ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC))
645  {
646  return $rec;
647  }
648  return array();
649  }
650 
662  public static function _updateUserFolderAssignment($a_old_id,$a_new_id)
663  {
664  global $ilDB;
665 
666  $query = "UPDATE usr_data SET time_limit_owner = ".$ilDB->quote($a_new_id, "integer")." ".
667  "WHERE time_limit_owner = ".$ilDB->quote($a_old_id, "integer")." ";
668  $ilDB->manipulate($query);
669 
670  return true;
671  }
672 
673 
674 } // END class.ilObjUserFolder
675 ?>
static _updateUserFolderAssignment($a_old_id, $a_new_id)
Update user folder assignment Typically called after deleting a category with local user accounts...
getExportFilename($a_mode="userfolder_export_excel_x86")
$path
Definition: aliased.php:25
$worksheet
const IL_CAL_DATETIME
static _getInstance()
Get instance.
Class ilUserDefinedData.
$result
getExportFiles()
Get a list of the already exported files in the export directory.
Class ilObject Basic functions for all objects.
getExportDirectory()
Get the location of the export directory for the user accounts.
static _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
Class ilUserProfile.
createXMLExport(&$settings, &$data, $filename)
const IL_CAL_UNIX
createExportDirectory()
creates data directory for export files (data_dir/usrf_data/export, depending on data directory that ...
static _deleteAccountMailAttachment($a_lang)
Delete account mail attachment.
static getExportSettings()
getExport Settings
static _lookupNewAccountMail($a_lang)
$header
XML writer class.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
buildExportFile($a_mode="userfolder_export_excel_x86", $user_data_filter=FALSE)
build xml export file
Date and time handling
redirection script todo: (a better solution should control the processing via a xml file) ...
static _updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
Update account mail attachment.
static & processCSVRow(&$row, $quoteAll=FALSE, $separator=";", $outUTF8=FALSE, $compatibleWithMSExcel=TRUE)
Convertes an array for CSV usage.
Create styles array
The data for the language used.
__construct($a_id, $a_call_by_reference=true)
Constructor public.
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
static & getProfileFields()
Get profile fields (DEPRECATED, use ilUserProfile() instead)
static getDataDir()
get data directory (outside webspace)
global $ilBench
Definition: ilias.php:18
global $ilDB
getUserDefinedExportFields()
Get all exportable user defined fields.
const USER_FOLDER_ID
Class ilObjUserFolder.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
$languages
Definition: cssgen2.php:34
createExcelExport(&$settings, &$data, $filename)
createCSVExport(&$settings, &$data, $filename)