session_export.php 9.2 KB

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