user_export.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. /**
  7. * Code
  8. */
  9. // name of the language file that needs to be included
  10. $language_file = 'admin';
  11. $cidReset = true;
  12. require_once '../inc/global.inc.php';
  13. $this_section = SECTION_PLATFORM_ADMIN;
  14. api_protect_admin_script();
  15. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  16. require_once api_get_path(LIBRARY_PATH).'export.lib.inc.php';
  17. // Database table definitions
  18. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  19. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  20. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  21. $tool_name = get_lang('ExportUserListXMLCSV');
  22. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  23. set_time_limit(0);
  24. $courses = array ();
  25. $courses[''] = '--';
  26. $sql = "SELECT code,visual_code,title FROM $course_table ORDER BY visual_code";
  27. global $_configuration;
  28. if ($_configuration['multiple_access_urls']) {
  29. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  30. $access_url_id = api_get_current_access_url_id();
  31. if ($access_url_id != -1){
  32. $sql = "SELECT code,visual_code,title FROM $course_table as c INNER JOIN $tbl_course_rel_access_url as course_rel_url
  33. ON (c.code = course_rel_url.course_code)
  34. WHERE access_url_id = $access_url_id
  35. ORDER BY visual_code";
  36. }
  37. }
  38. $result = Database::query($sql);
  39. while ($course = Database::fetch_object($result))
  40. {
  41. $courses[$course->code] = $course->visual_code.' - '.$course->title;
  42. }
  43. $form = new FormValidator('export_users');
  44. $form->addElement('header', '', $tool_name);
  45. $form->addElement('radio', 'file_type', get_lang('OutputFileType'), 'XML','xml');
  46. $form->addElement('radio', 'file_type', null, 'CSV','csv');
  47. $form->addElement('checkbox', 'addcsvheader', get_lang('AddCSVHeader'), get_lang('YesAddCSVHeader'),'1');
  48. $form->addElement('select', 'course_code', get_lang('OnlyUsersFromCourse'), $courses);
  49. $form->addElement('style_submit_button', 'submit',get_lang('Export'),'class="save"');
  50. $form->setDefaults(array('file_type'=>'csv'));
  51. if ($form->validate()) {
  52. global $_configuration;
  53. $export = $form->exportValues();
  54. $file_type = $export['file_type'];
  55. $course_code = Database::escape_string($export['course_code']);
  56. $sql = "SELECT u.user_id AS UserId,
  57. u.lastname AS LastName,
  58. u.firstname AS FirstName,
  59. u.email AS Email,
  60. u.username AS UserName,
  61. ".(($_configuration['password_encryption']!='none')?" ":"u.password AS Password, ")."
  62. u.auth_source AS AuthSource,
  63. u.status AS Status,
  64. u.official_code AS OfficialCode,
  65. u.phone AS Phone";
  66. if (strlen($course_code) > 0) {
  67. $sql .= " FROM $user_table u, $course_user_table cu WHERE u.user_id = cu.user_id AND course_code = '$course_code' AND cu.relation_type<>".COURSE_RELATION_TYPE_RRHH." ORDER BY lastname,firstname";
  68. $filename = 'export_users_'.$course_code.'_'.date('Y-m-d_H-i-s');
  69. } else {
  70. global $_configuration;
  71. if ($_configuration['multiple_access_urls']) {
  72. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  73. $access_url_id = api_get_current_access_url_id();
  74. if ($access_url_id != -1) {
  75. $sql.= " FROM $user_table u INNER JOIN $tbl_user_rel_access_url as user_rel_url
  76. ON (u.user_id= user_rel_url.user_id)
  77. WHERE access_url_id = $access_url_id
  78. ORDER BY lastname,firstname";
  79. }
  80. } else {
  81. $sql .= " FROM $user_table u ORDER BY lastname,firstname";
  82. }
  83. $filename = 'export_users_'.date('Y-m-d_H-i-s');
  84. }
  85. $data = array();
  86. $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC',false);
  87. if ($export['addcsvheader']=='1' AND $export['file_type']=='csv') {
  88. if($_configuration['password_encryption']!='none') {
  89. $data[] = array('UserId', 'LastName', 'FirstName', 'Email', 'UserName', 'AuthSource', 'Status', 'OfficialCode', 'PhoneNumber');
  90. } else {
  91. $data[] = array('UserId', 'LastName', 'FirstName', 'Email', 'UserName','Password', 'AuthSource', 'Status', 'OfficialCode', 'PhoneNumber');
  92. }
  93. foreach($extra_fields as $extra) {
  94. $data[0][]=$extra[1];
  95. }
  96. }
  97. $res = Database::query($sql);
  98. while($user = Database::fetch_array($res,'ASSOC')) {
  99. $student_data= UserManager :: get_extra_user_data($user['UserId'],true,false);
  100. foreach($student_data as $key=>$value) {
  101. $key= substr($key,6);
  102. if (is_array($value))
  103. $user[$key]= $value[$key];
  104. else {
  105. $user[$key]= $value;
  106. }
  107. }
  108. $data[] = $user ;
  109. }
  110. switch($file_type) {
  111. case 'xml':
  112. Export::export_table_xml($data,$filename,'Contact','Contacts');
  113. exit;
  114. break;
  115. case 'csv':
  116. Export::export_table_csv($data,$filename);
  117. exit;
  118. break;
  119. }
  120. }
  121. Display :: display_header($tool_name);
  122. $form->display();
  123. Display :: display_footer();