user_export.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. $this_section = SECTION_COURSES;
  5. api_protect_admin_script(true, true);
  6. $export = [];
  7. $export['file_type'] = isset($_REQUEST['file_type']) ? $_REQUEST['file_type'] : null;
  8. $export['course_code'] = isset($_REQUEST['course_code']) ? $_REQUEST['course_code'] : null;
  9. $export['course_session'] = isset($_REQUEST['course_session']) ? $_REQUEST['course_session'] : null;
  10. $export['addcsvheader'] = isset($_REQUEST['addcsvheader']) ? $_REQUEST['addcsvheader'] : null;
  11. $export['session'] = isset($_REQUEST['session']) ? $_REQUEST['session'] : null;
  12. // Database table definitions
  13. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  14. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  15. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  16. $session_course_user_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  17. $session_user_table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  18. $file_type = $export['file_type'];
  19. $course_code = Database::escape_string($export['course_code']);
  20. $courseInfo = api_get_course_info($course_code);
  21. $courseId = isset($courseInfo['real_id']) ? $courseInfo['real_id'] : 0;
  22. $courseSessionValue = explode(':', $export['course_session']);
  23. $courseSessionCode = '';
  24. $sessionId = 0;
  25. $courseSessionId = 0;
  26. $sessionInfo = [];
  27. if (!empty($export['session'])) {
  28. $sessionInfo = api_get_session_info($export['session']);
  29. $sessionId = isset($sessionInfo['id']) ? $sessionInfo['id'] : 0;
  30. }
  31. if (is_array($courseSessionValue) && isset($courseSessionValue[1])) {
  32. $courseSessionCode = $courseSessionValue[0];
  33. $sessionId = $courseSessionValue[1];
  34. $courseSessionInfo = api_get_course_info($courseSessionCode);
  35. $courseSessionId = $courseSessionInfo['real_id'];
  36. $sessionInfo = api_get_session_info($sessionId);
  37. }
  38. $sql = "SELECT
  39. u.user_id AS UserId,
  40. u.lastname AS LastName,
  41. u.firstname AS FirstName,
  42. u.email AS Email,
  43. u.username AS UserName,
  44. ".(($_configuration['password_encryption'] != 'none') ? " " : "u.password AS Password, ")."
  45. u.auth_source AS AuthSource,
  46. u.status AS Status,
  47. u.official_code AS OfficialCode,
  48. u.phone AS Phone,
  49. u.registration_date AS RegistrationDate";
  50. if (strlen($course_code) > 0) {
  51. $sql .= " FROM $user_table u, $course_user_table cu
  52. WHERE
  53. u.user_id = cu.user_id AND
  54. cu.c_id = $courseId AND
  55. cu.relation_type<>".COURSE_RELATION_TYPE_RRHH."
  56. ORDER BY lastname,firstname";
  57. $filename = 'export_users_'.$course_code.'_'.api_get_local_time();
  58. } elseif (strlen($courseSessionCode) > 0) {
  59. $sql .= " FROM $user_table u, $session_course_user_table scu
  60. WHERE
  61. u.user_id = scu.user_id AND
  62. scu.c_id = $courseSessionId AND
  63. scu.session_id = $sessionId
  64. ORDER BY lastname,firstname";
  65. $filename = 'export_users_'.$courseSessionCode.'_'.$sessionInfo['name'].'_'.api_get_local_time();
  66. } elseif ($sessionId > 0) {
  67. $sql .= " FROM $user_table u, $session_user_table su
  68. WHERE
  69. u.user_id = su.user_id AND
  70. su.session_id = $sessionId
  71. ORDER BY lastname,firstname";
  72. $filename = 'export_users_'.$sessionInfo['name'].'_'.api_get_local_time();
  73. } else {
  74. if (api_is_multiple_url_enabled()) {
  75. $tbl_user_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  76. $access_url_id = api_get_current_access_url_id();
  77. if ($access_url_id != -1) {
  78. $sql .= " FROM $user_table u
  79. INNER JOIN $tbl_user_rel_access_url as user_rel_url
  80. ON (u.user_id= user_rel_url.user_id)
  81. WHERE access_url_id = $access_url_id
  82. ORDER BY lastname,firstname";
  83. }
  84. } else {
  85. $sql .= " FROM $user_table u ORDER BY lastname,firstname";
  86. }
  87. $filename = 'export_users_'.api_get_local_time();
  88. }
  89. $data = [];
  90. $extra_fields = UserManager::get_extra_fields(0, 0, 5, 'ASC', false);
  91. if ($export['addcsvheader'] == '1' && $export['file_type'] == 'csv') {
  92. if ($_configuration['password_encryption'] != 'none') {
  93. $data[] = [
  94. 'UserId',
  95. 'LastName',
  96. 'FirstName',
  97. 'Email',
  98. 'UserName',
  99. 'AuthSource',
  100. 'Status',
  101. 'OfficialCode',
  102. 'PhoneNumber',
  103. 'RegistrationDate',
  104. ];
  105. } else {
  106. $data[] = [
  107. 'UserId',
  108. 'LastName',
  109. 'FirstName',
  110. 'Email',
  111. 'UserName',
  112. 'Password',
  113. 'AuthSource',
  114. 'Status',
  115. 'OfficialCode',
  116. 'PhoneNumber',
  117. 'RegistrationDate',
  118. ];
  119. }
  120. foreach ($extra_fields as $extra) {
  121. $data[0][] = $extra[1];
  122. }
  123. }
  124. $res = Database::query($sql);
  125. while ($user = Database::fetch_array($res, 'ASSOC')) {
  126. $student_data = UserManager:: get_extra_user_data(
  127. $user['UserId'],
  128. true,
  129. false
  130. );
  131. foreach ($student_data as $key => $value) {
  132. $key = substr($key, 6);
  133. if (is_array($value)) {
  134. $user[$key] = $value['extra_'.$key];
  135. } else {
  136. $user[$key] = $value;
  137. }
  138. }
  139. $data[] = $user;
  140. }
  141. switch ($file_type) {
  142. case 'xml':
  143. Export::arrayToXml($data, $filename, 'Contact', 'Contacts');
  144. exit;
  145. break;
  146. case 'csv':
  147. Export::arrayToCsv($data, $filename);
  148. exit;
  149. case 'xls':
  150. Export::arrayToXls($data, $filename);
  151. exit;
  152. break;
  153. }