session_export.php 9.3 KB

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