ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  public 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  public function delete()
40  {
41  // DISABLED
42  return false;
43 
44  // always call parent delete function first!!
45  if (!parent::delete()) {
46  return false;
47  }
48  // put here userfolder specific stuff
49 
50  // always call parent delete function at the end!!
51  return true;
52  }
53 
54 
55  public function getExportFilename($a_mode = "userfolder_export_excel_x86")
56  {
57  $filename = "";
58  //$settings = $this->ilias->getAllSettings();
59  //$this->inst_id = $settings["inst_id"];
60  $inst_id = IL_INST_ID;
61 
62  $date = time();
63 
64  switch ($a_mode) {
65  case "userfolder_export_excel_x86":
66  $filename = $date . "__" . $inst_id . "__xls_usrf";
67  break;
68  case "userfolder_export_csv":
69  $filename = $date . "__" . $inst_id . "__csv_usrf.csv";
70  break;
71  case "userfolder_export_xml":
72  $filename = $date . "__" . $inst_id . "__xml_usrf.xml";
73  break;
74  }
75  return $filename;
76  }
77 
78 
86  public function getExportDirectory()
87  {
88  $export_dir = ilUtil::getDataDir() . "/usrf_data/export";
89 
90  return $export_dir;
91  }
92 
101  public function getExportFiles()
102  {
103  $dir = $this->getExportDirectory();
104 
105  // quit if export dir not available
106  if (!@is_dir($dir) or
107  !is_writeable($dir)) {
108  return array();
109  }
110 
111  // open directory
112  $dir = dir($dir);
113 
114  // initialize array
115  $file = array();
116 
117  // get files and save the in the array
118  while ($entry = $dir->read()) {
119  if ($entry != "." and
120  $entry != ".." and
121  preg_match("/^[0-9]{10}_{2}[0-9]+_{2}([a-z0-9]{3})_usrf\.[a-z]{1,4}\$/", $entry, $matches)) {
122  $filearray["filename"] = $entry;
123  $filearray["filesize"] = filesize($this->getExportDirectory() . "/" . $entry);
124  array_push($file, $filearray);
125  }
126  }
127 
128  // close import directory
129  $dir->close();
130 
131  // sort files
132  sort($file);
133  reset($file);
134 
135  return $file;
136  }
137 
138  public function escapeXML($value)
139  {
140  $value = str_replace("&", "&amp;", $value);
141  $value = str_replace("<", "&lt;", $value);
142  $value = str_replace(">", "&gt;", $value);
143  return $value;
144  }
145 
146  public function createXMLExport(&$settings, &$data, $filename)
147  {
148  include_once './Services/User/classes/class.ilUserDefinedData.php';
149  include_once './Services/User/classes/class.ilObjUser.php';
150 
151  global $rbacreview;
152  global $ilDB;
153  global $log;
154 
155  $file = fopen($filename, "w");
156 
157  if (is_array($data)) {
158  include_once './Services/User/classes/class.ilUserXMLWriter.php';
159 
160  $xmlWriter = new ilUserXMLWriter();
161  $xmlWriter->setObjects($data);
162  $xmlWriter->setSettings($settings);
163  $xmlWriter->setAttachRoles(true);
164 
165  if ($xmlWriter->start()) {
166  fwrite($file, $xmlWriter->getXML());
167  }
168  }
169  }
170 
171 
175  public function getUserDefinedExportFields()
176  {
177  include_once './Services/User/classes/class.ilUserDefinedFields.php';
179 
180  $udf_ex_fields = array();
181  foreach ($udf_obj->getDefinitions() as $definition) {
182  if ($definition["export"] != false) {
183  $udf_ex_fields[] = array("name" => $definition["field_name"],
184  "id" => $definition["field_id"]);
185  }
186  }
187 
188  return $udf_ex_fields;
189  }
190 
191  public function createCSVExport(&$settings, &$data, $filename)
192  {
193 
194  // header
195  $headerrow = array();
196  $udf_ex_fields = $this->getUserDefinedExportFields();
197  foreach ($settings as $value) { // standard fields
198  array_push($headerrow, $this->lng->txt($value));
199  }
200  foreach ($udf_ex_fields as $f) { // custom fields
201  array_push($headerrow, $f["name"]);
202  }
203 
204  $separator = ";";
205  $file = fopen($filename, "w");
206  $formattedrow =&ilUtil::processCSVRow($headerrow, true, $separator);
207  fwrite($file, join($separator, $formattedrow) . "\n");
208  foreach ($data as $row) {
209  $csvrow = array();
210  foreach ($settings as $header) { // standard fields
211  // multi-text
212  if (is_array($row[$header])) {
213  $row[$header] = implode(", ", $row[$header]);
214  }
215 
216  array_push($csvrow, $row[$header]);
217  }
218 
219  // custom fields
220  reset($udf_ex_fields);
221  if (count($udf_ex_fields) > 0) {
222  include_once("./Services/User/classes/class.ilUserDefinedData.php");
223  $udf = new ilUserDefinedData($row["usr_id"]);
224  foreach ($udf_ex_fields as $f) { // custom fields
225  array_push($csvrow, $udf->get("f_" . $f["id"]));
226  }
227  }
228 
229  $formattedrow =&ilUtil::processCSVRow($csvrow, true, $separator);
230  fwrite($file, join($separator, $formattedrow) . "\n");
231  }
232  fclose($file);
233  }
234 
235  public function createExcelExport(&$settings, &$data, $filename)
236  {
237  include_once "./Services/Excel/classes/class.ilExcel.php";
238  $worksheet = new ilExcel();
239  $worksheet->addSheet($this->lng->txt("users"));
240 
241  $row = 1;
242  $col = 0;
243 
244  $udf_ex_fields = $this->getUserDefinedExportFields();
245 
246  // title row
247  foreach ($settings as $value) { // standard fields
248  if ($value == 'ext_account') {
249  $value = 'user_ext_account';
250  }
251  $worksheet->setCell($row, $col, $this->lng->txt($value));
252  $col++;
253  }
254  foreach ($udf_ex_fields as $f) { // custom fields
255  $worksheet->setCell($row, $col, $f["name"]);
256  $col++;
257  }
258  $worksheet->setBold("A1:" . $worksheet->getColumnCoord($col-1) . "1");
259 
260  $this->lng->loadLanguageModule("meta");
261  foreach ($data as $index => $rowdata) {
262  $row++;
263  $col = 0;
264 
265  // standard fields
266  foreach ($settings as $fieldname) {
267  $value = $rowdata[$fieldname];
268  switch ($fieldname) {
269  case "language":
270  $worksheet->setCell($row, $col, $this->lng->txt("meta_l_" . $value));
271  break;
272  case "time_limit_from":
273  case "time_limit_until":
274  $value = $value
275  ? new ilDateTime($value, IL_CAL_UNIX)
276  : null;
277  $worksheet->setCell($row, $col, $value);
278  break;
279  case "last_login":
280  case "last_update":
281  case "create_date":
282  case "approve_date":
283  case "agree_date":
284  $value = $value
285  ? new ilDateTime($value, IL_CAL_DATETIME)
286  : null;
287  $worksheet->setCell($row, $col, $value);
288  break;
289 
290  case "interests_general":
291  case "interests_help_offered":
292  case "interests_help_looking":
293  if (is_array($value) && sizeof($value)) {
294  $value = implode(", ", $value);
295  } else {
296  $value = null;
297  }
298  // fallthrough
299 
300  // no break
301  default:
302  $worksheet->setCell($row, $col, $value);
303  break;
304  }
305  $col++;
306  }
307 
308  // custom fields
309  reset($udf_ex_fields);
310  if (count($udf_ex_fields) > 0) {
311  include_once("./Services/User/classes/class.ilUserDefinedData.php");
312  $udf = new ilUserDefinedData($rowdata["usr_id"]);
313  foreach ($udf_ex_fields as $f) { // custom fields
314  $worksheet->setCell($row, $col, $udf->get("f_" . $f["id"]));
315  $col++;
316  }
317  }
318  }
319 
320  $worksheet->writeToFile($filename);
321  }
322 
328  public static function getExportSettings()
329  {
330  global $ilDB;
331 
332  $db_settings = array();
333 
334  include_once("./Services/User/classes/class.ilUserProfile.php");
335  $up = new ilUserProfile();
336  $up->skipField("roles");
337  $profile_fields = $up->getStandardFields();
338 
339  /*$profile_fields =& ilObjUserFolder::getProfileFields();
340  $profile_fields[] = "preferences";*/
341 
342  $query = "SELECT * FROM settings WHERE " .
343  $ilDB->like("keyword", "text", '%usr_settings_export_%');
344  $result = $ilDB->query($query);
345  while ($row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
346  if ($row["value"] == "1") {
347  if (preg_match("/usr_settings_export_(.*)/", $row["keyword"], $setting)) {
348  array_push($db_settings, $setting[1]);
349  }
350  }
351  }
352  $export_settings = array();
353  foreach ($profile_fields as $key => $value) {
354  if (in_array($key, $db_settings)) {
355  if (strcmp($key, "password") == 0) {
356  // we do not support password export with ILIAS >= 4.5.x
357  continue;
358  } else {
359  array_push($export_settings, $key);
360  }
361  }
362  }
363  array_push($export_settings, "usr_id");
364  array_push($export_settings, "login");
365  array_push($export_settings, "last_login");
366  array_push($export_settings, "last_update");
367  array_push($export_settings, "create_date");
368  array_push($export_settings, "time_limit_owner");
369  array_push($export_settings, "time_limit_unlimited");
370  array_push($export_settings, "time_limit_from");
371  array_push($export_settings, "time_limit_until");
372  array_push($export_settings, "time_limit_message");
373  array_push($export_settings, "active");
374  array_push($export_settings, "approve_date");
375  array_push($export_settings, "agree_date");
376  array_push($export_settings, "client_ip");
377  array_push($export_settings, "auth_mode");
378  array_push($export_settings, "ext_account");
379  array_push($export_settings, "feedhash");
380  return $export_settings;
381  }
382 
386  public function buildExportFile($a_mode = "userfolder_export_excel_x86", $user_data_filter = false)
387  {
388  global $ilBench;
389  global $log;
390  global $ilDB;
391  global $ilias;
392  global $lng;
393 
394  //get Log File
395  $expDir = $this->getExportDirectory();
396  //$expLog = &$log;
397  //$expLog->delete();
398  //$expLog->setLogFormat("");
399  //$expLog->write(date("[y-m-d H:i:s] ")."Start export of user data");
400 
401  // create export directory if needed
402  $this->createExportDirectory();
403 
404  //get data
405  //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
406  $settings =&$this->getExportSettings();
407 
408  // user languages
409  $query = "SELECT * FROM usr_pref WHERE keyword = " . $ilDB->quote('language', 'text');
410  $res = $ilDB->query($query);
411  $languages = array();
412  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
413  $languages[$row['usr_id']] = $row['value'];
414  }
415 
416  // multi-text
417  $multi = array();
418  $set = $ilDB->query("SELECT * FROM usr_data_multi");
419  while ($row = $ilDB->fetchAssoc($set)) {
420  if (!is_array($user_data_filter) ||
421  in_array($row["usr_id"], $user_data_filter)) {
422  $multi[$row["usr_id"]][$row["field_id"]][] = $row["value"];
423  }
424  }
425 
426  $data = array();
427  $query = "SELECT usr_data.* FROM usr_data " .
428  " ORDER BY usr_data.lastname, usr_data.firstname";
429  $result = $ilDB->query($query);
430  while ($row = $ilDB->fetchAssoc($result)) {
431  if (isset($languages[$row['usr_id']])) {
432  $row['language'] = $languages[$row['usr_id']];
433  } else {
434  $row['language'] = $lng->getDefaultLanguage();
435  }
436 
437  if (isset($multi[$row["usr_id"]])) {
438  $row = array_merge($row, $multi[$row["usr_id"]]);
439  }
440 
441  if (is_array($user_data_filter)) {
442  if (in_array($row["usr_id"], $user_data_filter)) {
443  array_push($data, $row);
444  }
445  } else {
446  array_push($data, $row);
447  }
448  }
449  //$expLog->write(date("[y-m-d H:i:s] ")."User data export: build an array of all user data entries");
450 
451  $fullname = $expDir . "/" . $this->getExportFilename($a_mode);
452  switch ($a_mode) {
453  case "userfolder_export_excel_x86":
454  $this->createExcelExport($settings, $data, $fullname);
455  break;
456  case "userfolder_export_csv":
457  $this->createCSVExport($settings, $data, $fullname);
458  break;
459  case "userfolder_export_xml":
460  $this->createXMLExport($settings, $data, $fullname);
461  break;
462  }
463  //$expLog->write(date("[y-m-d H:i:s] ")."Finished export of user data");
464 
465  return $fullname;
466  }
467 
468 
474  public function createExportDirectory()
475  {
476  if (!@is_dir($this->getExportDirectory())) {
477  $usrf_data_dir = ilUtil::getDataDir() . "/usrf_data";
478  ilUtil::makeDir($usrf_data_dir);
479  if (!is_writable($usrf_data_dir)) {
480  $this->ilias->raiseError("Userfolder data directory (" . $usrf_data_dir
481  . ") not writeable.", $this->ilias->error_obj->MESSAGE);
482  }
483 
484  // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
485  $export_dir = $usrf_data_dir . "/export";
486  ilUtil::makeDir($export_dir);
487  if (!@is_dir($export_dir)) {
488  $this->ilias->raiseError("Creation of Userfolder Export Directory failed.", $this->ilias->error_obj->MESSAGE);
489  }
490  }
491  }
492 
493 
499  public static function &getProfileFields()
500  {
501  include_once("./Services/User/classes/class.ilUserProfile.php");
502  $up = new ilUserProfile();
503  $up->skipField("username");
504  $up->skipField("roles");
505  $up->skipGroup("preferences");
506  $fds = $up->getStandardFields();
507  foreach ($fds as $k => $f) {
508  $profile_fields[] = $k;
509  }
510 
511  return $profile_fields;
512  }
513 
514  public static function _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
515  {
516  global $ilDB;
517 
518  if (self::_lookupNewAccountMail($a_lang)) {
519  $values = array(
520  'subject' => array('text',$a_subject),
521  'body' => array('clob',$a_body),
522  'sal_g' => array('text',$a_sal_g),
523  'sal_f' => array('text',$a_sal_f),
524  'sal_m' => array('text',$a_sal_m)
525  );
526  $ilDB->update(
527  'mail_template',
528  $values,
529  array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
530  );
531  } else {
532  $values = array(
533  'subject' => array('text',$a_subject),
534  'body' => array('clob',$a_body),
535  'sal_g' => array('text',$a_sal_g),
536  'sal_f' => array('text',$a_sal_f),
537  'sal_m' => array('text',$a_sal_m),
538  'lang' => array('text',$a_lang),
539  'type' => array('text','nacc')
540  );
541  $ilDB->insert('mail_template', $values);
542  }
543  }
544 
552  public static function _updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
553  {
554  global $ilDB;
555 
556  include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
558  $fs->create();
559  $path = $fs->getAbsolutePath() . "/";
560 
561  ilUtil::moveUploadedFile($a_tmp_name, $a_lang, $path . $a_lang);
562 
563  $ilDB->update(
564  'mail_template',
565  array('att_file' => array('text', $a_name)),
566  array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
567  );
568  }
569 
574  public static function _deleteAccountMailAttachment($a_lang)
575  {
576  global $ilDB;
577 
578  include_once "Services/User/classes/class.ilFSStorageUserFolder.php";
580  $path = $fs->getAbsolutePath() . "/";
581 
582  if (file_exists($path . $a_lang)) {
583  unlink($path . $a_lang);
584  }
585 
586  $ilDB->update(
587  'mail_template',
588  array('att_file' => array('text', '')),
589  array('lang' => array('text',$a_lang), 'type' => array('text','nacc'))
590  );
591  }
592 
593  public static function _lookupNewAccountMail($a_lang)
594  {
595  global $ilDB;
596 
597  $set = $ilDB->query("SELECT * FROM mail_template " .
598  " WHERE type='nacc' AND lang = " . $ilDB->quote($a_lang, 'text'));
599 
600  if ($rec = $set->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
601  return $rec;
602  }
603  return array();
604  }
605 
617  public static function _updateUserFolderAssignment($a_old_id, $a_new_id)
618  {
619  global $ilDB;
620 
621  $query = "UPDATE usr_data SET time_limit_owner = " . $ilDB->quote($a_new_id, "integer") . " " .
622  "WHERE time_limit_owner = " . $ilDB->quote($a_old_id, "integer") . " ";
623  $ilDB->manipulate($query);
624 
625  return true;
626  }
627 } // END class.ilObjUserFolder
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")
$worksheet
const IL_CAL_DATETIME
static _getInstance()
Get instance.
Class ilUserDefinedData.
$result
buildExportFile($a_mode="userfolder_export_excel_x86", $user_data_filter=false)
build xml export file
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.
static _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
Class ilUserProfile.
createXMLExport(&$settings, &$data, $filename)
$index
Definition: metadata.php:60
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 & processCSVRow(&$row, $quoteAll=false, $separator=";", $outUTF8=false, $compatibleWithMSExcel=true)
Convertes an array for CSV usage.
foreach($_POST as $key=> $value) $res
static _lookupNewAccountMail($a_lang)
XML writer class.
Date and time handling
redirection script todo: (a better solution should control the processing via a xml file) ...
$query
static _updateAccountMailAttachment($a_lang, $a_tmp_name, $a_name)
Update account mail attachment.
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
createExcelExport(&$settings, &$data, $filename)
$key
Definition: croninfo.php:18
createCSVExport(&$settings, &$data, $filename)