session_export.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. ////require_once '../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=';
  24. $tool_name = get_lang('ExportSessionListXMLCSV');
  25. $interbreadcrumb[] = array('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 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
  131. AND sc.session_id = '".$row['id']."'";
  132. $rsCourses = Database::query($sql);
  133. $courses = '';
  134. while ($rowCourses = Database::fetch_array($rsCourses)) {
  135. // get coachs from a course
  136. $sql = "SELECT u.username
  137. FROM $tbl_session_course_user scu
  138. INNER JOIN $tbl_user u
  139. ON u.user_id = scu.user_id
  140. WHERE
  141. scu.c_id = '{$rowCourses['c_id']}' AND
  142. scu.session_id = '".$row['id']."' AND
  143. scu.status = 2 ";
  144. $rs_coachs = Database::query($sql);
  145. $coachs = array();
  146. while ($row_coachs = Database::fetch_array($rs_coachs)) {
  147. $coachs[] = $row_coachs['username'];
  148. }
  149. $coachs = implode(",", $coachs);
  150. if ($cvs) {
  151. $courses .= str_replace(';',',',$rowCourses['code']);
  152. $courses .= '['.str_replace(';',',',$coachs).'][';
  153. } else {
  154. $courses .= "\t\t<Course>\n";
  155. $courses .= "\t\t\t<CourseCode>$rowCourses[code]</CourseCode>\n";
  156. $courses .= "\t\t\t<Coach>$coachs</Coach>\n";
  157. }
  158. // rel user courses
  159. $sql = "SELECT DISTINCT u.username
  160. FROM $tbl_session_course_user scu
  161. INNER JOIN $tbl_session_user su
  162. ON
  163. scu.user_id = su.user_id AND
  164. scu.session_id = su.session_id AND
  165. su.relation_type<>".SESSION_RELATION_TYPE_RRHH."
  166. INNER JOIN $tbl_user u
  167. ON
  168. scu.user_id = u.user_id AND
  169. scu.c_id='".$rowCourses['c_id']."' AND
  170. scu.session_id='".$row['id']."'";
  171. $rsUsersCourse = Database::query($sql);
  172. $userscourse = '';
  173. while ($rowUsersCourse = Database::fetch_array($rsUsersCourse)){
  174. if ($cvs) {
  175. $userscourse .= str_replace(';',',',$rowUsersCourse['username']).',';
  176. } else {
  177. $courses .= "\t\t\t<User>$rowUsersCourse[username]</User>\n";
  178. }
  179. }
  180. if ($cvs) {
  181. if (!empty($userscourse)) {
  182. $userscourse = api_substr(
  183. $userscourse,
  184. 0,
  185. api_strlen($userscourse) - 1
  186. );
  187. }
  188. $courses .= $userscourse.']|';
  189. } else {
  190. $courses .= "\t\t</Course>\n";
  191. }
  192. }
  193. if (!empty($courses) && $cvs) {
  194. $courses = api_substr($courses, 0, api_strlen($courses) - 1);
  195. }
  196. $add .= $courses;
  197. if (in_array($file_type, ['csv', 'xls'])) {
  198. $sessionListToExport[] = [
  199. $row['name'],
  200. $row['username'],
  201. $row['access_start_date'],
  202. $row['access_end_date'],
  203. $row['visibility'],
  204. $row['session_category'],
  205. $users,
  206. $courses
  207. ];
  208. } else {
  209. $add = "\t<Session>\n"
  210. ."\t\t<SessionName>$row[name]</SessionName>\n"
  211. ."\t\t<Coach>$row[username]</Coach>\n"
  212. ."\t\t<DateStart>$row[access_start_date]</DateStart>\n"
  213. ."\t\t<DateEnd>$row[access_end_date]</DateEnd>\n"
  214. ."\t\t<Visibility>$row[visibility]</Visibility>\n"
  215. ."\t\t<SessionCategory>$row[session_category]</SessionCategory>\n";
  216. }
  217. if (!$cvs) {
  218. $add .= "\t</Session>\n";
  219. fputs($fp, $add);
  220. }
  221. }
  222. switch ($file_type) {
  223. case 'xml':
  224. fputs($fp, "</Sessions>\n");
  225. fclose($fp);
  226. $errorMsg = get_lang('UserListHasBeenExported').'<br/>
  227. <a class="btn btn-default" href="'.$archiveURL.$archiveFile.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  228. break;
  229. case 'csv':
  230. Export::arrayToCsv($sessionListToExport, $archiveFile);
  231. exit;
  232. case 'xls':
  233. Export::arrayToXls($sessionListToExport, $archiveFile);
  234. exit;
  235. break;
  236. }
  237. }
  238. }
  239. // display the header
  240. Display::display_header($tool_name);
  241. //select of sessions
  242. $sql = "SELECT id, name FROM $tbl_session ORDER BY name";
  243. if (api_is_multiple_url_enabled()) {
  244. $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  245. $access_url_id = api_get_current_access_url_id();
  246. if ($access_url_id != -1){
  247. $sql = "SELECT s.id, name FROM $tbl_session s
  248. INNER JOIN $tbl_session_rel_access_url as session_rel_url
  249. ON (s.id = session_rel_url.session_id)
  250. WHERE access_url_id = $access_url_id
  251. ORDER BY name";
  252. }
  253. }
  254. $result = Database::query($sql);
  255. $Sessions = Database::store_result($result);
  256. echo '<div class="actions">';
  257. echo '<a href="../session/session_list.php">'.
  258. Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('SessionList'),'',ICON_SIZE_MEDIUM).'</a>';
  259. echo '</div>';
  260. if (!empty($errorMsg)) {
  261. Display::display_normal_message($errorMsg, false); //main API
  262. }
  263. $form = new FormValidator('session_export', 'post', api_get_self());
  264. $form->addElement('hidden', 'formSent', 1);
  265. $form->addElement('radio', 'file_type', get_lang('OutputFileType'), 'CSV' , 'csv', null);
  266. $form->addElement('radio', 'file_type', '', 'XLS' , 'xls', null);
  267. $form->addElement('radio', 'file_type', null, 'XML', 'xml', null, array('id' => 'file_type_xml'));
  268. $options = array();
  269. $options['0'] = get_lang('AllSessions');
  270. foreach ($Sessions as $enreg) {
  271. $options[$enreg['id']] = $enreg['name'];
  272. }
  273. $form->addElement('select', 'session_id', get_lang('WhichSessionToExport'), $options);
  274. $form->addButtonExport(get_lang('ExportSession'));
  275. $defaults = array();
  276. $defaults['file_type'] = 'csv';
  277. $form->setDefaults($defaults);
  278. $form->display();
  279. unset($Courses);
  280. Display::display_footer();