user_export.php 4.7 KB

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