session_export.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. // name of the language file that needs to be included
  7. $language_file = 'admin';
  8. $cidReset = true;
  9. //require_once '../inc/global.inc.php';
  10. // setting the section (for the tabs)
  11. $this_section = SECTION_PLATFORM_ADMIN;
  12. api_protect_admin_script(true);
  13. $session_id = intval($_GET['session_id']);
  14. $formSent = 0;
  15. $errorMsg = '';
  16. // Database Table Definitions
  17. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  18. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  19. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  20. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  21. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  22. $archivePath = api_get_path(SYS_ARCHIVE_PATH);
  23. $archiveURL = api_get_path(WEB_CODE_PATH) . 'course_info/download.php?archive=';
  24. $tool_name = get_lang('ExportSessionListXMLCSV');
  25. global $_configuration;
  26. $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('Sessions'));
  27. set_time_limit(0);
  28. if ($_POST['formSent']) {
  29. $formSent = $_POST['formSent'];
  30. $file_type = ($_POST['file_type'] == 'csv') ? 'csv' : 'xml';
  31. $session_id = $_POST['session_id'];
  32. if (empty($session_id)) {
  33. $sql = "SELECT id,name,id_coach,username,access_start_date,access_end_date,visibility,session_category_id
  34. FROM $tbl_session INNER JOIN $tbl_user
  35. ON $tbl_user.user_id = $tbl_session.id_coach ORDER BY id";
  36. if ($_configuration['multiple_access_urls']) {
  37. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  38. $access_url_id = api_get_current_access_url_id();
  39. if ($access_url_id != -1) {
  40. $sql = "SELECT id, name,id_coach,username,access_start_date,access_end_date,visibility,session_category_id
  41. FROM $tbl_session s INNER JOIN $tbl_session_rel_access_url as session_rel_url
  42. ON (s.id= session_rel_url.session_id) INNER JOIN $tbl_user u ON (u.user_id = s.id_coach)
  43. WHERE access_url_id = $access_url_id
  44. ORDER BY id";
  45. }
  46. }
  47. $result = Database::query($sql);
  48. } else {
  49. $sql = "SELECT id,name,username,access_start_date,access_end_date,visibility,session_category_id
  50. FROM $tbl_session
  51. INNER JOIN $tbl_user
  52. ON $tbl_user.user_id = $tbl_session.id_coach
  53. WHERE id='$session_id'";
  54. $result = Database::query($sql);
  55. }
  56. if (Database::num_rows($result)) {
  57. if (!file_exists($archivePath)) {
  58. mkdir($archivePath, api_get_permissions_for_new_directories(), true);
  59. }
  60. if (!file_exists($archivePath . 'index.html')) {
  61. $fp = fopen($archivePath . 'index.html', 'w');
  62. fputs($fp, '<html><head></head><body></body></html>');
  63. fclose($fp);
  64. }
  65. $archiveFile = 'export_sessions_' . $session_id . '_' . date('Y-m-d_H-i-s') . '.' . $file_type;
  66. while (file_exists($archivePath . $archiveFile)) {
  67. $archiveFile = 'export_users_' . $session_id . '_' . date('Y-m-d_H-i-s') . '_' . uniqid('') . '.' . $file_type;
  68. }
  69. $fp = fopen($archivePath . $archiveFile, 'w');
  70. if ($file_type == 'csv') {
  71. $cvs = true;
  72. fputs($fp, "SessionName;Coach;DateStart;DateEnd;Visibility;SessionCategory;Users;Courses;\n");
  73. } else {
  74. $cvs = false;
  75. fputs($fp, "<?xml version=\"1.0\" encoding=\"" . api_get_system_encoding() . "\"?>\n<Sessions>\n");
  76. }
  77. while ($row = Database::fetch_array($result)) {
  78. $add = '';
  79. $row['name'] = str_replace(';', ',', $row['name']);
  80. $row['username'] = str_replace(';', ',', $row['username']);
  81. $row['access_start_date'] = str_replace(';', ',', api_get_local_time($row['access_start_date'], null, null, true));
  82. $row['access_end_date'] = str_replace(';', ',', api_get_local_time($row['access_end_date'], null, null, true));
  83. $row['visibility'] = str_replace(';', ',', $row['visibility']);
  84. $row['session_category'] = str_replace(';', ',', $row['session_category_id']);
  85. if ($cvs) {
  86. $add.= $row['name'] . ';' . $row['username'] . ';' . $row['access_start_date'] . ';' . $row['access_end_date'] . ';' . $row['visibility'] . ';' . $row['session_category'] . ';';
  87. } else {
  88. $add = "\t<Session>\n"
  89. . "\t\t<SessionName>$row[name]</SessionName>\n"
  90. . "\t\t<Coach>$row[username]</Coach>\n"
  91. . "\t\t<DateStart>$row[access_start_date]</DateStart>\n"
  92. . "\t\t<DateEnd>$row[access_end_date]</DateEnd>\n"
  93. . "\t\t<Visibility>$row[visibility]</Visibility>\n"
  94. . "\t\t<SessionCategory>$row[session_category]</SessionCategory>\n";
  95. }
  96. //users
  97. $sql = "SELECT DISTINCT $tbl_user.username FROM $tbl_user
  98. INNER JOIN $tbl_session_user
  99. ON $tbl_user.user_id = $tbl_session_user.id_user AND $tbl_session_user.relation_type<>" . SESSION_RELATION_TYPE_RRHH . "
  100. AND $tbl_session_user.id_session = '" . $row['id'] . "'";
  101. $rsUsers = Database::query($sql);
  102. $users = '';
  103. while ($rowUsers = Database::fetch_array($rsUsers)) {
  104. if ($cvs) {
  105. $users .= str_replace(';', ',', $rowUsers['username']) . '|';
  106. } else {
  107. $users .= "\t\t<User>$rowUsers[username]</User>\n";
  108. }
  109. }
  110. if (!empty($users) && $cvs)
  111. $users = api_substr($users, 0, api_strlen($users) - 1);
  112. if ($cvs)
  113. $users .= ';';
  114. $add .= $users;
  115. //courses
  116. $sql = "SELECT DISTINCT $tbl_course.id, $tbl_course.code
  117. FROM $tbl_course
  118. INNER JOIN $tbl_session_course_user
  119. ON $tbl_course.id = $tbl_session_course_user.c_id
  120. AND $tbl_session_course_user.id_session = '" . $row['id'] . "'";
  121. $rsCourses = Database::query($sql);
  122. $courses = '';
  123. while ($rowCourses = Database::fetch_array($rsCourses)) {
  124. // Get coachs from a course
  125. $sql = "SELECT u.username
  126. FROM $tbl_session_course_user scu
  127. INNER JOIN $tbl_user u ON u.user_id = scu.id_user
  128. WHERE scu.c_id = '{$rowCourses['id']}' AND
  129. scu.id_session = '" . $row['id'] . "' AND
  130. scu.status = 2 ";
  131. $rs_coachs = Database::query($sql);
  132. $coachs = array();
  133. while ($row_coachs = Database::fetch_array($rs_coachs)) {
  134. $coachs[] = $row_coachs['username'];
  135. }
  136. $coachs = implode(",", $coachs);
  137. if ($cvs) {
  138. $courses .= str_replace(';', ',', $rowCourses['code']);
  139. $courses .= '[' . str_replace(';', ',', $coachs) . '][';
  140. } else {
  141. $courses .= "\t\t<Course>\n";
  142. $courses .= "\t\t\t<CourseCode>$rowCourses[code]</CourseCode>\n";
  143. $courses .= "\t\t\t<Coach>$coachs</Coach>\n";
  144. }
  145. // rel user courses
  146. $sql = "SELECT DISTINCT u.username
  147. FROM $tbl_session_course_user scu
  148. INNER JOIN $tbl_session_user su ON scu.id_user = su.id_user AND scu.id_session = su.id_session AND su.relation_type<>" . SESSION_RELATION_TYPE_RRHH . "
  149. INNER JOIN $tbl_user u
  150. ON scu.id_user = u.user_id
  151. AND scu.c_id='" . $rowCourses['id'] . "'
  152. AND scu.id_session='" . $row['id'] . "'";
  153. $rsUsersCourse = Database::query($sql);
  154. $userscourse = '';
  155. while ($rowUsersCourse = Database::fetch_array($rsUsersCourse)) {
  156. if ($cvs) {
  157. $userscourse .= str_replace(';', ',', $rowUsersCourse['username']) . ',';
  158. } else {
  159. $courses .= "\t\t\t<User>$rowUsersCourse[username]</User>\n";
  160. }
  161. }
  162. if ($cvs) {
  163. if (!empty($userscourse))
  164. $userscourse = api_substr($userscourse, 0, api_strlen($userscourse) - 1);
  165. $courses .= $userscourse . ']|';
  166. }
  167. else {
  168. $courses .= "\t\t</Course>\n";
  169. }
  170. }
  171. if (!empty($courses) && $cvs)
  172. $courses = api_substr($courses, 0, api_strlen($courses) - 1);
  173. $add .= $courses;
  174. if ($cvs) {
  175. $breakline = api_is_windows_os() ? "\r\n" : "\n";
  176. $add .= ";$breakline";
  177. } else {
  178. $add .= "\t</Session>\n";
  179. }
  180. fputs($fp, $add);
  181. }
  182. if (!$cvs)
  183. fputs($fp, "</Sessions>\n");
  184. fclose($fp);
  185. $errorMsg = get_lang('UserListHasBeenExported') . '<br/><a class="btn" href="' . $archiveURL . $archiveFile . '">' . get_lang('ClickHereToDownloadTheFile') . '</a>';
  186. }
  187. }
  188. // display the header
  189. Display::display_header($tool_name);
  190. //select of sessions
  191. $sql = "SELECT id, name FROM $tbl_session ORDER BY name";
  192. if (api_is_multiple_url_enabled()) {
  193. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  194. $access_url_id = api_get_current_access_url_id();
  195. if ($access_url_id != -1) {
  196. $sql = "SELECT id, name FROM $tbl_session s INNER JOIN $tbl_session_rel_access_url as session_rel_url
  197. ON (s.id= session_rel_url.session_id)
  198. WHERE access_url_id = $access_url_id
  199. ORDER BY name";
  200. }
  201. }
  202. $result = Database::query($sql);
  203. $Sessions = Database::store_result($result);
  204. echo '<div class="actions">';
  205. echo '<a href="../admin/index.php">' . Display::return_icon('back.png', get_lang('BackTo') . ' ' . get_lang('PlatformAdmin'), '', ICON_SIZE_MEDIUM) . '</a>';
  206. echo '</div>';
  207. if (!empty($errorMsg)) {
  208. Display::display_normal_message($errorMsg, false); //main API
  209. }
  210. $form = new FormValidator('session_export', 'post', api_get_self());
  211. $form->addElement('hidden', 'formSent', 1);
  212. $form->addElement('radio', 'file_type', get_lang('OutputFileType'), 'CSV', 'csv', null, array('id' => 'file_type_csv'));
  213. $form->addElement('radio', 'file_type', null, 'XML', 'xml', null, array('id' => 'file_type_xml'));
  214. $options = array();
  215. $options['0'] = get_lang('AllSessions');
  216. foreach ($Sessions as $enreg) {
  217. $options[$enreg['id']] = $enreg['name'];
  218. }
  219. $form->addElement('select', 'session_id', get_lang('WhichSessionToExport'), $options);
  220. $form->addElement('button', 'submit', get_lang('ExportSession'));
  221. $defaults = array();
  222. $defaults['file_type'] = 'csv';
  223. $form->setDefaults($defaults);
  224. $form->display();
  225. unset($Courses);
  226. Display::display_footer();