session_export.php 11 KB

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