user_export.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once __DIR__.'/../inc/global.inc.php';
  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. $session_course_user_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  15. $tool_name = get_lang('Export users list');
  16. $interbreadcrumb[] = ["url" => 'index.php', "name" => get_lang('Administration')];
  17. set_time_limit(0);
  18. $coursesSessions = [];
  19. $coursesSessions[''] = '--';
  20. $allCoursesFromSessions = SessionManager::getAllCoursesFromAllSessions();
  21. $coursesSessions = array_merge($coursesSessions, $allCoursesFromSessions);
  22. $courses = [];
  23. $courses[''] = '--';
  24. $sql = "SELECT code,visual_code,title FROM $course_table ORDER BY visual_code";
  25. global $_configuration;
  26. if (api_is_multiple_url_enabled()) {
  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
  31. FROM $course_table as c
  32. INNER JOIN $tbl_course_rel_access_url as course_rel_url
  33. ON (c.id = course_rel_url.c_id)
  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. $courses[$course->code] = $course->visual_code.' - '.$course->title;
  41. }
  42. $form = new FormValidator('export_users');
  43. $form->addElement('header', $tool_name);
  44. $form->addElement('radio', 'file_type', get_lang('Output file type'), 'XML', 'xml');
  45. $form->addElement('radio', 'file_type', null, 'CSV', 'csv');
  46. $form->addElement('radio', 'file_type', null, 'XLS', 'xls');
  47. $form->addElement('checkbox', 'addcsvheader', get_lang('Include header line?'), get_lang('YesInclude header line?'), '1');
  48. $form->addElement('select', 'course_code', get_lang('Only users from the course'), $courses);
  49. $form->addElement('select', 'course_session', get_lang('Only users from the courseSession'), $coursesSessions);
  50. $form->addButtonExport(get_lang('Export'));
  51. $form->setDefaults(['file_type' => 'csv']);
  52. if ($form->validate()) {
  53. $export = $form->exportValues();
  54. $file_type = $export['file_type'];
  55. $course_code = Database::escape_string($export['course_code']);
  56. $courseInfo = api_get_course_info($course_code);
  57. $courseId = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : 0;
  58. $courseSessionValue = explode(':', $export['course_session']);
  59. $courseSessionCode = '';
  60. $sessionId = 0;
  61. $courseSessionId = 0;
  62. $sessionInfo = [];
  63. if (is_array($courseSessionValue) && isset($courseSessionValue[1])) {
  64. $courseSessionCode = $courseSessionValue[0];
  65. $sessionId = $courseSessionValue[1];
  66. $courseSessionInfo = api_get_course_info($courseSessionCode);
  67. $courseSessionId = $courseSessionInfo['real_id'];
  68. $sessionInfo = api_get_session_info($sessionId);
  69. }
  70. $sql = "SELECT
  71. u.user_id AS UserId,
  72. u.lastname AS LastName,
  73. u.firstname AS FirstName,
  74. u.email AS Email,
  75. u.username AS UserName,
  76. ".(($_configuration['password_encryption'] != 'none') ? " " : "u.password AS Password, ")."
  77. u.auth_source AS AuthSource,
  78. u.status AS Status,
  79. u.official_code AS OfficialCode,
  80. u.phone AS Phone,
  81. u.registration_date AS RegistrationDate";
  82. if (strlen($course_code) > 0) {
  83. $sql .= " FROM $user_table u, $course_user_table cu
  84. WHERE
  85. u.user_id = cu.user_id AND
  86. cu.c_id = $courseId AND
  87. cu.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  88. ORDER BY lastname,firstname";
  89. $filename = 'export_users_'.$course_code.'_'.api_get_local_time();
  90. } elseif (strlen($courseSessionCode) > 0) {
  91. $sql .= " FROM $user_table u, $session_course_user_table scu
  92. WHERE
  93. u.user_id = scu.user_id AND
  94. scu.c_id = $courseSessionId AND
  95. scu.session_id = $sessionId
  96. ORDER BY lastname,firstname";
  97. $filename = 'export_users_'.$courseSessionCode.'_'.$sessionInfo['name'].'_'.api_get_local_time();
  98. } else {
  99. if (api_is_multiple_url_enabled()) {
  100. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  101. $access_url_id = api_get_current_access_url_id();
  102. if ($access_url_id != -1) {
  103. $sql .= " FROM $user_table u
  104. INNER JOIN $tbl_user_rel_access_url as user_rel_url
  105. ON (u.user_id= user_rel_url.user_id)
  106. WHERE access_url_id = $access_url_id
  107. ORDER BY lastname,firstname";
  108. }
  109. } else {
  110. $sql .= " FROM $user_table u ORDER BY lastname,firstname";
  111. }
  112. $filename = 'export_users_'.api_get_local_time();
  113. }
  114. $data = [];
  115. $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', false);
  116. if (!empty($export['addcsvheader'])) {
  117. if ($export['addcsvheader'] == '1' && ($export['file_type'] == 'csv' || $export['file_type'] == 'xls')) {
  118. if ($_configuration['password_encryption'] != 'none') {
  119. $data[] = [
  120. 'UserId',
  121. 'LastName',
  122. 'FirstName',
  123. 'Email',
  124. 'UserName',
  125. 'AuthSource',
  126. 'Status',
  127. 'OfficialCode',
  128. 'PhoneNumber',
  129. 'RegistrationDate',
  130. ];
  131. } else {
  132. $data[] = [
  133. 'UserId',
  134. 'LastName',
  135. 'FirstName',
  136. 'Email',
  137. 'UserName',
  138. 'Password',
  139. 'AuthSource',
  140. 'Status',
  141. 'OfficialCode',
  142. 'PhoneNumber',
  143. 'RegistrationDate',
  144. ];
  145. }
  146. foreach ($extra_fields as $extra) {
  147. $data[0][] = $extra[1];
  148. }
  149. }
  150. }
  151. $res = Database::query($sql);
  152. while ($user = Database::fetch_array($res, 'ASSOC')) {
  153. $student_data = UserManager:: get_extra_user_data(
  154. $user['UserId'],
  155. true,
  156. false
  157. );
  158. foreach ($student_data as $key => $value) {
  159. $key = substr($key, 6);
  160. if (is_array($value)) {
  161. $user[$key] = $value['extra_'.$key];
  162. } else {
  163. $user[$key] = $value;
  164. }
  165. }
  166. $data[] = $user;
  167. }
  168. switch ($file_type) {
  169. case 'xml':
  170. Export::arrayToXml($data, $filename, 'Contact', 'Contacts');
  171. exit;
  172. break;
  173. case 'csv':
  174. Export::arrayToCsv($data, $filename);
  175. exit;
  176. case 'xls':
  177. Export::arrayToXls($data, $filename);
  178. exit;
  179. break;
  180. }
  181. }
  182. Display :: display_header($tool_name);
  183. $form->display();
  184. Display :: display_footer();