session_export.php 9.8 KB

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