session_export.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. // setting the section (for the tabs)
  9. $this_section = SECTION_PLATFORM_ADMIN;
  10. api_protect_admin_script(true);
  11. $session_id = isset($_GET['session_id']) ? intval($_GET['session_id']) : 0;
  12. $formSent = 0;
  13. $errorMsg = '';
  14. // Database Table Definitions
  15. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  16. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  17. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  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 = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  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_path=&archive=';
  24. $tool_name = get_lang('ExportSessionListXMLCSV');
  25. $interbreadcrumb[] = ['url' => 'session_list.php', 'name' => get_lang('SessionList')];
  26. set_time_limit(0);
  27. if (isset($_POST['formSent'])) {
  28. $formSent = $_POST['formSent'];
  29. $file_type = isset($_POST['file_type']) ? $_POST['file_type'] : 'csv';
  30. $session_id = $_POST['session_id'];
  31. if (empty($session_id)) {
  32. $sql = "SELECT
  33. s.id,
  34. name,
  35. id_coach,
  36. username,
  37. access_start_date,
  38. access_end_date,
  39. visibility,
  40. session_category_id
  41. FROM $tbl_session s
  42. INNER JOIN $tbl_user
  43. ON $tbl_user.user_id = s.id_coach
  44. ORDER BY id";
  45. if (api_is_multiple_url_enabled()) {
  46. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  47. $access_url_id = api_get_current_access_url_id();
  48. if ($access_url_id != -1) {
  49. $sql = "SELECT s.id, name,id_coach,username,access_start_date,access_end_date,visibility,session_category_id
  50. FROM $tbl_session s
  51. INNER JOIN $tbl_session_rel_access_url as session_rel_url
  52. ON (s.id= session_rel_url.session_id)
  53. INNER JOIN $tbl_user u ON (u.user_id = s.id_coach)
  54. WHERE access_url_id = $access_url_id
  55. ORDER BY id";
  56. }
  57. }
  58. $result = Database::query($sql);
  59. } else {
  60. $sql = "SELECT s.id,name,username,access_start_date,access_end_date,visibility,session_category_id
  61. FROM $tbl_session s
  62. INNER JOIN $tbl_user
  63. ON $tbl_user.user_id = s.id_coach
  64. WHERE s.id='$session_id'";
  65. $result = Database::query($sql);
  66. }
  67. if (Database::num_rows($result)) {
  68. $sessionListToExport = [];
  69. if (in_array($file_type, ['csv', 'xls'])) {
  70. $archiveFile = 'export_sessions_'.$session_id.'_'.api_get_local_time();
  71. $cvs = true;
  72. $sessionListToExport[] = [
  73. 'SessionName',
  74. 'Coach',
  75. 'DateStart',
  76. 'DateEnd',
  77. 'Visibility',
  78. 'SessionCategory',
  79. 'Users',
  80. 'Courses',
  81. ];
  82. } else {
  83. if (!file_exists($archivePath)) {
  84. mkdir($archivePath, api_get_permissions_for_new_directories(), true);
  85. }
  86. if (!file_exists($archivePath.'index.html')) {
  87. $fp = fopen($archivePath.'index.html', 'w');
  88. fputs($fp, '<html><head></head><body></body></html>');
  89. fclose($fp);
  90. }
  91. $archiveFile = 'export_sessions_'.$session_id.'_'.api_get_local_time().'.'.$file_type;
  92. while (file_exists($archivePath.$archiveFile)) {
  93. $archiveFile = 'export_users_'.$session_id.'_'.api_get_local_time().'_'.uniqid('').'.'.$file_type;
  94. }
  95. $cvs = false;
  96. $fp = fopen($archivePath.$archiveFile, 'w');
  97. fputs($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Sessions>\n");
  98. }
  99. while ($row = Database::fetch_array($result)) {
  100. $row['name'] = str_replace(';', ',', $row['name']);
  101. $row['username'] = str_replace(';', ',', $row['username']);
  102. $row['access_start_date'] = str_replace(';', ',', $row['access_start_date']);
  103. $row['access_end_date'] = str_replace(';', ',', $row['access_end_date']);
  104. $row['visibility'] = str_replace(';', ',', $row['visibility']);
  105. $row['session_category'] = str_replace(';', ',', $row['session_category_id']);
  106. // users
  107. $sql = "SELECT DISTINCT $tbl_user.username
  108. FROM $tbl_user
  109. INNER JOIN $tbl_session_user
  110. ON
  111. $tbl_user.user_id = $tbl_session_user.user_id AND
  112. $tbl_session_user.relation_type<>".SESSION_RELATION_TYPE_RRHH." AND
  113. $tbl_session_user.session_id = '".$row['id']."'";
  114. $rsUsers = Database::query($sql);
  115. $users = '';
  116. while ($rowUsers = Database::fetch_array($rsUsers)) {
  117. if ($cvs) {
  118. $users .= str_replace(';', ',', $rowUsers['username']).'|';
  119. } else {
  120. $users .= "\t\t<User>$rowUsers[username]</User>\n";
  121. }
  122. }
  123. if (!empty($users) && $cvs) {
  124. $users = api_substr($users, 0, api_strlen($users) - 1);
  125. }
  126. // Courses
  127. $sql = "SELECT DISTINCT c.code, sc.id, c_id
  128. FROM $tbl_course c
  129. INNER JOIN $tbl_session_course_user sc
  130. ON c.id = sc.c_id AND sc.session_id = '".$row['id']."'";
  131. $rsCourses = Database::query($sql);
  132. $courses = '';
  133. while ($rowCourses = Database::fetch_array($rsCourses)) {
  134. // get coachs from a course
  135. $sql = "SELECT u.username
  136. FROM $tbl_session_course_user scu
  137. INNER JOIN $tbl_user u
  138. ON u.user_id = scu.user_id
  139. WHERE
  140. scu.c_id = '{$rowCourses['c_id']}' AND
  141. scu.session_id = '".$row['id']."' AND
  142. scu.status = 2 ";
  143. $rs_coachs = Database::query($sql);
  144. $coachs = [];
  145. while ($row_coachs = Database::fetch_array($rs_coachs)) {
  146. $coachs[] = $row_coachs['username'];
  147. }
  148. $coachs = implode(",", $coachs);
  149. if ($cvs) {
  150. $courses .= str_replace(';', ',', $rowCourses['code']);
  151. $courses .= '['.str_replace(';', ',', $coachs).'][';
  152. } else {
  153. $courses .= "\t\t<Course>\n";
  154. $courses .= "\t\t\t<CourseCode>$rowCourses[code]</CourseCode>\n";
  155. $courses .= "\t\t\t<Coach>$coachs</Coach>\n";
  156. }
  157. // rel user courses
  158. $sql = "SELECT DISTINCT u.username
  159. FROM $tbl_session_course_user scu
  160. INNER JOIN $tbl_session_user su
  161. ON
  162. scu.user_id = su.user_id AND
  163. scu.session_id = su.session_id AND
  164. su.relation_type<>".SESSION_RELATION_TYPE_RRHH."
  165. INNER JOIN $tbl_user u
  166. ON
  167. scu.user_id = u.user_id AND
  168. scu.c_id='".$rowCourses['c_id']."' AND
  169. scu.session_id='".$row['id']."'";
  170. $rsUsersCourse = Database::query($sql);
  171. $userscourse = '';
  172. while ($rowUsersCourse = Database::fetch_array($rsUsersCourse)) {
  173. if ($cvs) {
  174. $userscourse .= str_replace(';', ',', $rowUsersCourse['username']).',';
  175. } else {
  176. $courses .= "\t\t\t<User>$rowUsersCourse[username]</User>\n";
  177. }
  178. }
  179. if ($cvs) {
  180. if (!empty($userscourse)) {
  181. $userscourse = api_substr(
  182. $userscourse,
  183. 0,
  184. api_strlen($userscourse) - 1
  185. );
  186. }
  187. $courses .= $userscourse.']|';
  188. } else {
  189. $courses .= "\t\t</Course>\n";
  190. }
  191. }
  192. if (!empty($courses) && $cvs) {
  193. $courses = api_substr($courses, 0, api_strlen($courses) - 1);
  194. }
  195. $add = $courses;
  196. if (in_array($file_type, ['csv', 'xls'])) {
  197. $sessionListToExport[] = [
  198. $row['name'],
  199. $row['username'],
  200. $row['access_start_date'],
  201. $row['access_end_date'],
  202. $row['visibility'],
  203. $row['session_category'],
  204. $users,
  205. $courses,
  206. ];
  207. } else {
  208. $add = "\t<Session>\n"
  209. ."\t\t<SessionName>$row[name]</SessionName>\n"
  210. ."\t\t<Coach>$row[username]</Coach>\n"
  211. ."\t\t<DateStart>$row[access_start_date]</DateStart>\n"
  212. ."\t\t<DateEnd>$row[access_end_date]</DateEnd>\n"
  213. ."\t\t<Visibility>$row[visibility]</Visibility>\n"
  214. ."\t\t<SessionCategory>$row[session_category]</SessionCategory>\n";
  215. }
  216. if (!$cvs) {
  217. $add .= "\t</Session>\n";
  218. fputs($fp, $add);
  219. }
  220. }
  221. switch ($file_type) {
  222. case 'xml':
  223. fputs($fp, "</Sessions>\n");
  224. fclose($fp);
  225. $errorMsg = get_lang('UserListHasBeenExported').'<br/>
  226. <a class="btn btn-default" href="'.$archiveURL.$archiveFile.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  227. break;
  228. case 'csv':
  229. Export::arrayToCsv($sessionListToExport, $archiveFile);
  230. exit;
  231. case 'xls':
  232. Export::arrayToXls($sessionListToExport, $archiveFile);
  233. exit;
  234. break;
  235. }
  236. }
  237. }
  238. // display the header
  239. Display::display_header($tool_name);
  240. //select of sessions
  241. $sql = "SELECT id, name FROM $tbl_session ORDER BY name";
  242. if (api_is_multiple_url_enabled()) {
  243. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  244. $access_url_id = api_get_current_access_url_id();
  245. if ($access_url_id != -1) {
  246. $sql = "SELECT s.id, name FROM $tbl_session s
  247. INNER JOIN $tbl_session_rel_access_url as session_rel_url
  248. ON (s.id = session_rel_url.session_id)
  249. WHERE access_url_id = $access_url_id
  250. ORDER BY name";
  251. }
  252. }
  253. $result = Database::query($sql);
  254. $Sessions = Database::store_result($result);
  255. echo '<div class="actions">';
  256. echo '<a href="../session/session_list.php">'.
  257. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('SessionList'), '', ICON_SIZE_MEDIUM).'</a>';
  258. echo '</div>';
  259. if (!empty($errorMsg)) {
  260. echo Display::return_message($errorMsg, 'normal', false); //main API
  261. }
  262. $form = new FormValidator('session_export', 'post', api_get_self());
  263. $form->addElement('hidden', 'formSent', 1);
  264. $form->addElement('radio', 'file_type', get_lang('OutputFileType'), 'CSV', 'csv', null);
  265. $form->addElement('radio', 'file_type', '', 'XLS', 'xls', null);
  266. $form->addElement('radio', 'file_type', null, 'XML', 'xml', null, ['id' => 'file_type_xml']);
  267. $options = [];
  268. $options['0'] = get_lang('AllSessions');
  269. foreach ($Sessions as $enreg) {
  270. $options[$enreg['id']] = $enreg['name'];
  271. }
  272. $form->addElement('select', 'session_id', get_lang('WhichSessionToExport'), $options);
  273. $form->addButtonExport(get_lang('ExportSession'));
  274. $defaults = [];
  275. $defaults['file_type'] = 'csv';
  276. $form->setDefaults($defaults);
  277. $form->display();
  278. unset($courses);
  279. Display::display_footer();