ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 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 
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,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';
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, $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 
427  return $export_settings;
428  }
429 
433  function buildExportFile($a_mode = "userfolder_export_excel_x86", $user_data_filter = FALSE)
434  {
435  global $ilBench;
436  global $log;
437  global $ilDB;
438  global $ilias;
439  global $lng;
440 
441  //get Log File
442  $expDir = $this->getExportDirectory();
443  //$expLog = &$log;
444  //$expLog->delete();
445  //$expLog->setLogFormat("");
446  //$expLog->write(date("[y-m-d H:i:s] ")."Start export of user data");
447 
448  // create export directory if needed
449  $this->createExportDirectory();
450 
451  //get data
452  //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
453  $settings =& $this->getExportSettings();
454 
455  // user languages
456  $query = "SELECT * FROM usr_pref WHERE keyword = ".$ilDB->quote('language','text');
457  $res = $ilDB->query($query);
458  $languages = array();
459  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
460  {
461  $languages[$row['usr_id']] = $row['value'];
462  }
463 
464  // multi-text
465  $multi = array();
466  $set = $ilDB->query("SELECT * FROM usr_data_multi");
467  while($row = $ilDB->fetchAssoc($set))
468  {
469  if(!is_array($user_data_filter) ||
470  in_array($row["usr_id"], $user_data_filter))
471  {
472  $multi[$row["usr_id"]][$row["field_id"]][] = $row["value"];
473  }
474  }
475 
476  $data = array();
477  $query = "SELECT usr_data.* FROM usr_data ".
478  " ORDER BY usr_data.lastname, usr_data.firstname";
479  $result = $ilDB->query($query);
480  while ($row = $ilDB->fetchAssoc($result))
481  {
482  if(isset($languages[$row['usr_id']]))
483  {
484  $row['language'] = $languages[$row['usr_id']];
485  }
486  else
487  {
488  $row['language'] = $lng->getDefaultLanguage();
489  }
490 
491  if(isset($multi[$row["usr_id"]]))
492  {
493  $row = array_merge($row, $multi[$row["usr_id"]]);
494  }
495 
496  if (is_array($user_data_filter))
497  {
498  if (in_array($row["usr_id"], $user_data_filter)) array_push($data, $row);
499  }
500  else
501  {
502  array_push($data, $row);
503  }
504  }
505  //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
506 
507  $fullname = $expDir."/".$this->getExportFilename($a_mode);
508  switch ($a_mode)
509  {
510  case "userfolder_export_excel_x86":
511  $this->createExcelExport($settings, $data, $fullname, "latin1");
512  break;
513  case "userfolder_export_csv":
514  $this->createCSVExport($settings, $data, $fullname);
515  break;
516  case "userfolder_export_xml":
517  $this->createXMLExport($settings, $data, $fullname);
518  break;
519  }
520  //$expLog->write(date("[y-m-d H:i:s] ")."Finished export of user data");
521 
522  return $fullname;
523  }
524 
525 
532  {
533  if (!@is_dir($this->getExportDirectory()))
534  {
535  $usrf_data_dir = ilUtil::getDataDir()."/usrf_data";
536  ilUtil::makeDir($usrf_data_dir);
537  if(!is_writable($usrf_data_dir))
538  {
539  $this->ilias->raiseError("Userfolder data directory (".$usrf_data_dir
540  .") not writeable.",$this->ilias->error_obj->MESSAGE);
541  }
542 
543  // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
544  $export_dir = $usrf_data_dir."/export";
545  ilUtil::makeDir($export_dir);
546  if(!@is_dir($export_dir))
547  {
548  $this->ilias->raiseError("Creation of Userfolder Export Directory failed.",$this->ilias->error_obj->MESSAGE);
549  }
550  }
551  }
552 
553 
559  static function &getProfileFields()
560  {
561  include_once("./Services/User/classes/class.ilUserProfile.php");
562  $up = new ilUserProfile();
563  $up->skipField("username");
564  $up->skipField("roles");
565  $up->skipGroup("preferences");
566  $fds = $up->getStandardFields();
567  foreach ($fds as $k => $f)
568  {
569  $profile_fields[] = $k;
570  }
571 
572  return $profile_fields;
573  }
574 
575  function _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
576  {
577  global $ilDB;
578 
579  if(self::_lookupNewAccountMail($a_lang))
580  {
581  $values = array(
582  'subject' => array('text',$a_subject),
583  'body' => array('clob',$a_body),
584  'sal_g' => array('text',$a_sal_g),
585  'sal_f' => array('text',$a_sal_f),
586  'sal_m' => array('text',$a_sal_m)
587  );
588  $ilDB->update('mail_template',
589  $values,
590  array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
591  );
592  }
593  else
594  {
595  $values = array(
596  'subject' => array('text',$a_subject),
597  'body' => array('clob',$a_body),
598  'sal_g' => array('text',$a_sal_g),
599  'sal_f' => array('text',$a_sal_f),
600  'sal_m' => array('text',$a_sal_m),
601  'lang' => array('text',$a_lang),
602  'type' => array('text','nacc')
603  );
604  $ilDB->insert('mail_template',$values);
605  }
606  }
607 
608  function _updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
609  {
610  global $ilDB;
611 
612  include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
613  $fs = new ilFSStorageUserFolder($this->getId());
614  $fs->create();
615  $path = $fs->getAbsolutePath()."/";
616 
617  move_uploaded_file($a_tmp_name, $path.$a_lang);
618 
619  $ilDB->update('mail_template',
620  array('att_file' => array('text', $a_name)),
621  array('lang' => array('text',$a_lang), 'type' => array('text','nacc')));
622  }
623 
625  {
626  global $ilDB;
627 
628  include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
629  $fs = new ilFSStorageUserFolder($this->getId());
630  $path = $fs->getAbsolutePath()."/";
631 
632  @unlink($path.$a_lang);
633 
634  $ilDB->update('mail_template',
635  array('att_file' => array('text', '')),
636  array('lang' => array('text',$a_lang), 'type' => array('text','nacc')));
637  }
638 
639  function _lookupNewAccountMail($a_lang)
640  {
641  global $ilDB;
642 
643  $set = $ilDB->query("SELECT * FROM mail_template ".
644  " WHERE type='nacc' AND lang = ".$ilDB->quote($a_lang,'text'));
645 
646  if ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
647  {
648  return $rec;
649  }
650  return array();
651  }
652 
664  public static function _updateUserFolderAssignment($a_old_id,$a_new_id)
665  {
666  global $ilDB;
667 
668  $query = "UPDATE usr_data SET time_limit_owner = ".$ilDB->quote($a_new_id, "integer")." ".
669  "WHERE time_limit_owner = ".$ilDB->quote($a_old_id, "integer")." ";
670  $ilDB->manipulate($query);
671 
672  return true;
673  }
674 
675 
676 } // END class.ilObjUserFolder
677 ?>