00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00034 require_once "./classes/class.ilObject.php";
00035
00036 define('USER_FOLDER_ID',7);
00037
00038 class ilObjUserFolder extends ilObject
00039 {
00046 function ilObjUserFolder($a_id,$a_call_by_reference = true)
00047 {
00048 $this->type = "usrf";
00049 $this->ilObject($a_id,$a_call_by_reference);
00050 }
00051
00052
00059 function delete()
00060 {
00061
00062 return false;
00063
00064
00065 if (!parent::delete())
00066 {
00067 return false;
00068 }
00069
00070
00071
00072 return true;
00073 }
00074
00075
00076 function getExportFilename($a_mode = "userfolder_export_excel_x86")
00077 {
00078 $filename = "";
00079
00080
00081 $inst_id = IL_INST_ID;
00082
00083 $date = time();
00084
00085 switch($a_mode)
00086 {
00087 case "userfolder_export_excel_x86":
00088 $filename = $date."__".$inst_id."__xls_usrf.xls";
00089 break;
00090 case "userfolder_export_csv":
00091 $filename = $date."__".$inst_id."__csv_usrf.csv";
00092 break;
00093 case "userfolder_export_xml":
00094 $filename = $date."__".$inst_id."__xml_usrf.xml";
00095 break;
00096 }
00097 return $filename;
00098 }
00099
00100
00108 function getExportDirectory()
00109 {
00110 $export_dir = ilUtil::getDataDir()."/usrf_data/export";
00111
00112 return $export_dir;
00113 }
00114
00123 function getExportFiles()
00124 {
00125 $dir = $this->getExportDirectory();
00126
00127
00128 if (!@is_dir($dir) or
00129 !is_writeable($dir))
00130 {
00131 return array();
00132 }
00133
00134
00135 $dir = dir($dir);
00136
00137
00138 $file = array();
00139
00140
00141 while ($entry = $dir->read())
00142 {
00143 if ($entry != "." and
00144 $entry != ".." and
00145 preg_match("/^[0-9]{10}_{2}[0-9]+_{2}([a-z0-9]{3})_usrf\.[a-z]{1,3}\$/", $entry, $matches))
00146 {
00147 $filearray["filename"] = $entry;
00148 $filearray["filesize"] = filesize($this->getExportDirectory()."/".$entry);
00149 array_push($file, $filearray);
00150 }
00151 }
00152
00153
00154 $dir->close();
00155
00156
00157 sort ($file);
00158 reset ($file);
00159
00160 return $file;
00161 }
00162
00163 function escapeXML($value)
00164 {
00165 $value = str_replace("&", "&", $value);
00166 $value = str_replace("<", "<", $value);
00167 $value = str_replace(">", ">", $value);
00168 return $value;
00169 }
00170
00171 function createXMLExport(&$settings, &$data, $filename)
00172 {
00173 include_once './Services/User/classes/class.ilUserDefinedData.php';
00174 include_once './Services/User/classes/class.ilObjUser.php';
00175
00176 global $rbacreview;
00177 global $ilDB;
00178 global $log;
00179
00180 $file = fopen($filename, "w");
00181
00182 if (is_array($data))
00183 {
00184 include_once './Services/User/classes/class.ilUserXMLWriter.php';
00185
00186 $xmlWriter = new ilUserXMLWriter();
00187 $xmlWriter->setObjects($data);
00188 $xmlWriter->setSettings($settings);
00189 $xmlWriter->setAttachRoles (true);
00190
00191 if($xmlWriter->start())
00192 {
00193 fwrite($file, $xmlWriter->getXML());
00194 }
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 }
00467
00468 function createCSVExport(&$settings, &$data, $filename)
00469 {
00470 $headerrow = array();
00471 foreach ($settings as $value)
00472 {
00473 array_push($headerrow, $this->lng->txt($value));
00474 }
00475 $separator = ";";
00476 $file = fopen($filename, "w");
00477 $formattedrow =& ilUtil::processCSVRow($headerrow, TRUE, $separator);
00478 fwrite($file, join ($separator, $formattedrow) ."");
00479 foreach ($data as $row)
00480 {
00481 $csvrow = array();
00482 foreach ($settings as $header)
00483 {
00484 array_push($csvrow, $row[$header]);
00485 }
00486 $formattedrow =& ilUtil::processCSVRow($csvrow, TRUE, $separator);
00487 fwrite($file, join ($separator, $formattedrow) ."\n");
00488 }
00489 fclose($file);
00490 }
00491
00492 function createExcelExport(&$settings, &$data, $filename, $a_mode)
00493 {
00494 include_once "./classes/class.ilExcelUtils.php";
00495 include_once "./classes/class.ilExcelWriterAdapter.php";
00496 $adapter = new ilExcelWriterAdapter($filename, FALSE);
00497 $workbook = $adapter->getWorkbook();
00498
00499 $format_bold =& $workbook->addFormat();
00500 $format_bold->setBold();
00501 $format_percent =& $workbook->addFormat();
00502 $format_percent->setNumFormat("0.00%");
00503 $format_datetime =& $workbook->addFormat();
00504 $format_datetime->setNumFormat("DD/MM/YYYY hh:mm:ss");
00505 $format_title =& $workbook->addFormat();
00506 $format_title->setBold();
00507 $format_title->setColor('black');
00508 $format_title->setPattern(1);
00509 $format_title->setFgColor('silver');
00510 $worksheet =& $workbook->addWorksheet();
00511 $row = 0;
00512 $col = 0;
00513
00514 foreach ($settings as $value)
00515 {
00516 $worksheet->write($row, $col, ilExcelUtils::_convert_text($this->lng->txt($value), $a_mode), $format_title);
00517 $col++;
00518 }
00519
00520
00521 foreach ($data as $index => $rowdata)
00522 {
00523 $row++;
00524 $col = 0;
00525 foreach ($settings as $fieldname)
00526 {
00527
00528
00529 $value = $rowdata[$fieldname];
00530 switch ($fieldname)
00531 {
00532 case "language":
00533 $worksheet->write($row, $col, ilExcelUtils::_convert_text($this->lng->txt("lang_".$value), $a_mode));
00534 break;
00535 case "time_limit_from":
00536 case "time_limit_until":
00537 $date = strftime("%Y-%m-%d %H:%M:%S", $value);
00538 if (preg_match("/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/", $date, $matches))
00539 {
00540 $worksheet->write($row, $col, ilUtil::excelTime($matches[1],$matches[2],$matches[3],$matches[4],$matches[5],$matches[6]), $format_datetime);
00541 }
00542 break;
00543 case "last_login":
00544 case "last_update":
00545 case "create_date":
00546 case "approve_date":
00547 case "agree_date":
00548 if (preg_match("/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/", $value, $matches))
00549 {
00550 $worksheet->write($row, $col, ilUtil::excelTime($matches[1],$matches[2],$matches[3],$matches[4],$matches[5],$matches[6]), $format_datetime);
00551 }
00552 break;
00553 default:
00554 $worksheet->write($row, $col, ilExcelUtils::_convert_text($value, $a_mode));
00555 break;
00556 }
00557 $col++;
00558 }
00559 }
00560 $workbook->close();
00561 }
00562
00568 static function getExportSettings()
00569 {
00570 global $ilDB;
00571
00572 $db_settings = array();
00573 $profile_fields =& ilObjUserFolder::getProfileFields();
00574 $query = "SELECT * FROM `settings` WHERE keyword LIKE '%usr_settings_export_%' AND value = '1'";
00575 $result = $ilDB->query($query);
00576 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00577 {
00578 if (preg_match("/usr_settings_export_(.*)/", $row["keyword"], $setting))
00579 {
00580 array_push($db_settings, $setting[1]);
00581 }
00582 }
00583 $export_settings = array();
00584 foreach ($profile_fields as $key => $value)
00585 {
00586 if (in_array($value, $db_settings))
00587 {
00588 if (strcmp($value, "password") == 0)
00589 {
00590 array_push($export_settings, "passwd");
00591 }
00592 else
00593 {
00594 array_push($export_settings, $value);
00595 }
00596 }
00597 }
00598 array_push($export_settings, "login");
00599 array_push($export_settings, "last_login");
00600 array_push($export_settings, "last_update");
00601 array_push($export_settings, "create_date");
00602 array_push($export_settings, "i2passwd");
00603 array_push($export_settings, "time_limit_owner");
00604 array_push($export_settings, "time_limit_unlimited");
00605 array_push($export_settings, "time_limit_from");
00606 array_push($export_settings, "time_limit_until");
00607 array_push($export_settings, "time_limit_message");
00608 array_push($export_settings, "active");
00609 array_push($export_settings, "approve_date");
00610 array_push($export_settings, "agree_date");
00611 array_push($export_settings, "ilinc_id");
00612 array_push($export_settings, "ilinc_login");
00613 array_push($export_settings, "ilinc_passwd");
00614 array_push($export_settings, "client_ip");
00615 array_push($export_settings, "auth_mode");
00616 array_push($export_settings, "ext_account");
00617 return $export_settings;
00618 }
00619
00623 function buildExportFile($a_mode = "userfolder_export_excel_x86", $user_data_filter = FALSE)
00624 {
00625 global $ilBench;
00626 global $log;
00627 global $ilDB;
00628 global $ilias;
00629
00630
00631 $expDir = $this->getExportDirectory();
00632
00633
00634
00635
00636
00637
00638 $this->createExportDirectory();
00639
00640
00641
00642 $settings =& $this->getExportSettings();
00643 $data = array();
00644 $query = "SELECT usr_data.*, usr_pref.value AS language FROM usr_data, usr_pref WHERE usr_pref.usr_id = usr_data.usr_id AND usr_pref.keyword = 'language' ORDER BY usr_data.lastname, usr_data.firstname";
00645 $result = $ilDB->query($query);
00646 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00647 {
00648 if (is_array($user_data_filter))
00649 {
00650 if (in_array($row["usr_id"], $user_data_filter)) array_push($data, $row);
00651 }
00652 else
00653 {
00654 array_push($data, $row);
00655 }
00656 }
00657
00658
00659 $fullname = $expDir."/".$this->getExportFilename($a_mode);
00660 switch ($a_mode)
00661 {
00662 case "userfolder_export_excel_x86":
00663 $this->createExcelExport($settings, $data, $fullname, "latin1");
00664 break;
00665 case "userfolder_export_csv":
00666 $this->createCSVExport($settings, $data, $fullname);
00667 break;
00668 case "userfolder_export_xml":
00669 $this->createXMLExport($settings, $data, $fullname);
00670 break;
00671 }
00672
00673
00674 return $fullname;
00675 }
00676
00677
00683 function createExportDirectory()
00684 {
00685 if (!@is_dir($this->getExportDirectory()))
00686 {
00687 $usrf_data_dir = ilUtil::getDataDir()."/usrf_data";
00688 ilUtil::makeDir($usrf_data_dir);
00689 if(!is_writable($usrf_data_dir))
00690 {
00691 $this->ilias->raiseError("Userfolder data directory (".$usrf_data_dir
00692 .") not writeable.",$this->ilias->error_obj->MESSAGE);
00693 }
00694
00695
00696 $export_dir = $usrf_data_dir."/export";
00697 ilUtil::makeDir($export_dir);
00698 if(!@is_dir($export_dir))
00699 {
00700 $this->ilias->raiseError("Creation of Userfolder Export Directory failed.",$this->ilias->error_obj->MESSAGE);
00701 }
00702 }
00703 }
00704
00710 static function &getProfileFields()
00711 {
00712 $profile_fields = array(
00713 "gender",
00714 "firstname",
00715 "lastname",
00716 "title",
00717 "password",
00718 "institution",
00719 "department",
00720 "street",
00721 "zipcode",
00722 "city",
00723 "country",
00724 "phone_office",
00725 "phone_home",
00726 "phone_mobile",
00727 "fax",
00728 "email",
00729 "hobby",
00730 "referral_comment",
00731 "matriculation",
00732 "upload",
00733 "language",
00734 "skin_style",
00735 "hits_per_page",
00736 "show_users_online",
00737 "instant_messengers",
00738 "hide_own_online_status"
00739 );
00740 return $profile_fields;
00741 }
00742
00743 function _writeNewAccountMail($a_lang, $a_subject, $a_sal_g, $a_sal_f, $a_sal_m, $a_body)
00744 {
00745 global $ilDB;
00746
00747 $ilDB->query("REPLACE INTO usr_new_account_mail ".
00748 "(lang, subject, body, sal_g, sal_f, sal_m) VALUES ".
00749 "(".$ilDB->quote($a_lang).",".$ilDB->quote($a_subject).",".$ilDB->quote($a_body).
00750 ",".$ilDB->quote($a_sal_g).",".$ilDB->quote($a_sal_f).",".$ilDB->quote($a_sal_m).")");
00751 }
00752
00753 function _lookupNewAccountMail($a_lang)
00754 {
00755 global $ilDB;
00756
00757 $set = $ilDB->query("SELECT * FROM usr_new_account_mail ".
00758 " WHERE lang = ".$ilDB->quote($a_lang));
00759
00760 if ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
00761 {
00762 return $rec;
00763 }
00764 return array();
00765 }
00766
00778 public static function _updateUserFolderAssignment($a_old_id,$a_new_id)
00779 {
00780 global $ilDB;
00781
00782 $query = "UPDATE usr_data SET time_limit_owner = ".$ilDB->quote($a_new_id)." ".
00783 "WHERE time_limit_owner = ".$ilDB->quote($a_old_id)." ";
00784 $ilDB->query($query);
00785
00786 return true;
00787 }
00788
00789
00790 }
00791 ?>