user_export.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * @package chamilo.admin
  6. */
  7. $cidReset = true;
  8. $this_section = SECTION_PLATFORM_ADMIN;
  9. api_protect_admin_script();
  10. // Database table definitions
  11. $course_table = Database:: get_main_table(TABLE_MAIN_COURSE);
  12. $user_table = Database:: get_main_table(TABLE_MAIN_USER);
  13. $course_user_table = Database:: get_main_table(TABLE_MAIN_COURSE_USER);
  14. $tool_name = get_lang('ExportUserListXMLCSV');
  15. $interbreadcrumb[] = array('url' => Container::getRouter()->generate('administration') , "name" => get_lang('PlatformAdmin'));
  16. set_time_limit(0);
  17. $courses = array ();
  18. $courses[''] = '--';
  19. $sql = "SELECT code,visual_code,title FROM $course_table ORDER BY visual_code";
  20. if (api_is_multiple_url_enabled()) {
  21. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  22. $access_url_id = api_get_current_access_url_id();
  23. if ($access_url_id != -1){
  24. $sql = "SELECT code,visual_code,title
  25. FROM $course_table as c
  26. INNER JOIN $tbl_course_rel_access_url as course_rel_url
  27. ON (c.id = course_rel_url.c_id)
  28. WHERE access_url_id = $access_url_id
  29. ORDER BY visual_code";
  30. }
  31. }
  32. $result = Database::query($sql);
  33. while ($course = Database::fetch_object($result)) {
  34. $courses[$course->code] = $course->visual_code.' - '.$course->title;
  35. }
  36. $form = new FormValidator('export_users');
  37. $form->addElement('header', $tool_name);
  38. $form->addElement('radio', 'file_type', get_lang('OutputFileType'), 'XML','xml');
  39. $form->addElement('radio', 'file_type', null, 'CSV', 'csv');
  40. $form->addElement('radio', 'file_type', null, 'XLS', 'xls');
  41. $form->addElement('checkbox', 'addcsvheader', get_lang('AddCSVHeader'), get_lang('YesAddCSVHeader'),'1');
  42. $form->addElement('select', 'course_code', get_lang('OnlyUsersFromCourse'), $courses);
  43. $form->addButtonExport(get_lang('Export'));
  44. $form->setDefaults(array('file_type' => 'csv'));
  45. $passwordEncryption = api_get_configuration_value('password_encryption');
  46. if ($form->validate()) {
  47. $export = $form->exportValues();
  48. $file_type = $export['file_type'];
  49. $course_code = Database::escape_string($export['course_code']);
  50. $courseInfo = api_get_course_info($course_code);
  51. $courseId = $courseInfo['real_id'];
  52. $sql = "SELECT
  53. u.user_id AS UserId,
  54. u.lastname AS LastName,
  55. u.firstname AS FirstName,
  56. u.email AS Email,
  57. u.username AS UserName,
  58. ".(($passwordEncryption !='none')?" ":"u.password AS Password, ")."
  59. u.auth_source AS AuthSource,
  60. u.status AS Status,
  61. u.official_code AS OfficialCode,
  62. u.phone AS Phone";
  63. if (strlen($course_code) > 0) {
  64. $sql .= " FROM $user_table u, $course_user_table cu
  65. WHERE
  66. u.user_id = cu.user_id AND
  67. cu.c_id = $courseId AND
  68. cu.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  69. ORDER BY lastname,firstname";
  70. $filename = 'export_users_'.$course_code.'_'.api_get_local_time();
  71. } else {
  72. if (api_is_multiple_url_enabled()) {
  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
  77. INNER JOIN $tbl_user_rel_access_url as user_rel_url
  78. ON (u.user_id= user_rel_url.user_id)
  79. WHERE access_url_id = $access_url_id
  80. ORDER BY lastname,firstname";
  81. }
  82. } else {
  83. $sql .= " FROM $user_table u ORDER BY lastname,firstname";
  84. }
  85. $filename = 'export_users_'.api_get_local_time();
  86. }
  87. $data = array();
  88. $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC',false);
  89. if ($export['addcsvheader']=='1' AND $export['file_type']=='csv') {
  90. if ($passwordEncryption != 'none') {
  91. $data[] = array(
  92. 'UserId',
  93. 'LastName',
  94. 'FirstName',
  95. 'Email',
  96. 'UserName',
  97. 'AuthSource',
  98. 'Status',
  99. 'OfficialCode',
  100. 'PhoneNumber',
  101. );
  102. } else {
  103. $data[] = array(
  104. 'UserId',
  105. 'LastName',
  106. 'FirstName',
  107. 'Email',
  108. 'UserName',
  109. 'Password',
  110. 'AuthSource',
  111. 'Status',
  112. 'OfficialCode',
  113. 'PhoneNumber',
  114. );
  115. }
  116. foreach($extra_fields as $extra) {
  117. $data[0][]=$extra[1];
  118. }
  119. }
  120. $res = Database::query($sql);
  121. while($user = Database::fetch_array($res,'ASSOC')) {
  122. $student_data = UserManager:: get_extra_user_data(
  123. $user['UserId'],
  124. true,
  125. false
  126. );
  127. foreach($student_data as $key=>$value) {
  128. $key = substr($key, 6);
  129. if (is_array($value)) {
  130. $user[$key] = $value[$key];
  131. } else {
  132. $user[$key] = $value;
  133. }
  134. }
  135. $data[] = $user ;
  136. }
  137. switch ($file_type) {
  138. case 'xml':
  139. Export::arrayToXml($data, $filename, 'Contact', 'Contacts');
  140. exit;
  141. break;
  142. case 'csv':
  143. Export::arrayToCsv($data, $filename);
  144. exit;
  145. case 'xls':
  146. Export::arrayToXls($data, $filename);
  147. exit;
  148. break;
  149. }
  150. }
  151. Display :: display_header($tool_name);
  152. $form->display();
  153. Display :: display_footer();