session_export.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. $interbreadcrumb[] = array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  28. set_time_limit(0);
  29. if ($_POST['formSent'] )
  30. {
  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. {
  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. global $_configuration;
  39. if ($_configuration['multiple_access_urls']) {
  40. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  41. $access_url_id = api_get_current_access_url_id();
  42. if ($access_url_id != -1){
  43. $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
  44. ON (s.id= session_rel_url.session_id) INNER JOIN $tbl_user u ON (u.user_id = s.id_coach)
  45. WHERE access_url_id = $access_url_id
  46. ORDER BY id";
  47. }
  48. }
  49. $result=Database::query($sql);
  50. }
  51. else
  52. {
  53. $sql = "SELECT id,name,username,date_start,date_end,visibility,session_category_id
  54. FROM $tbl_session
  55. INNER JOIN $tbl_user
  56. ON $tbl_user.user_id = $tbl_session.id_coach
  57. WHERE id='$session_id'";
  58. $result = Database::query($sql);
  59. }
  60. if(Database::num_rows($result))
  61. {
  62. if(!file_exists($archivePath))
  63. {
  64. mkdir($archivePath, api_get_permissions_for_new_directories(), true);
  65. }
  66. if(!file_exists($archivePath.'index.html'))
  67. {
  68. $fp=fopen($archivePath.'index.html','w');
  69. fputs($fp,'<html><head></head><body></body></html>');
  70. fclose($fp);
  71. }
  72. $archiveFile='export_sessions_'.$session_id.'_'.date('Y-m-d_H-i-s').'.'.$file_type;
  73. while( file_exists($archivePath.$archiveFile))
  74. {
  75. $archiveFile='export_users_'.$session_id.'_'.date('Y-m-d_H-i-s').'_'.uniqid('').'.'.$file_type;
  76. }
  77. $fp=fopen($archivePath.$archiveFile,'w');
  78. if($file_type == 'csv')
  79. {
  80. $cvs = true;
  81. fputs($fp,"SessionName;Coach;DateStart;DateEnd;Visibility;SessionCategory;Users;Courses;\n");
  82. }
  83. else
  84. {
  85. $cvs = false;
  86. fputs($fp, "<?xml version=\"1.0\" encoding=\"".api_get_system_encoding()."\"?>\n<Sessions>\n");
  87. }
  88. while($row=Database::fetch_array($result))
  89. {
  90. $add = '';
  91. $row['name'] = str_replace(';',',',$row['name']);
  92. $row['username'] = str_replace(';',',',$row['username']);
  93. $row['date_start'] = str_replace(';',',',$row['date_start']);
  94. $row['date_end'] = str_replace(';',',',$row['date_end']);
  95. $row['visibility'] = str_replace(';',',',$row['visibility']);
  96. $row['session_category'] = str_replace(';',',',$row['session_category_id']);
  97. if($cvs){
  98. $add.= $row['name'].';'.$row['username'].';'.$row['date_start'].';'.$row['date_end'].';'.$row['visibility'].';'.$row['session_category'].';';
  99. }
  100. else {
  101. $add = "\t<Session>\n"
  102. ."\t\t<SessionName>$row[name]</SessionName>\n"
  103. ."\t\t<Coach>$row[username]</Coach>\n"
  104. ."\t\t<DateStart>$row[date_start]</DateStart>\n"
  105. ."\t\t<DateEnd>$row[date_end]</DateEnd>\n"
  106. ."\t\t<Visibility>$row[visibility]</Visibility>\n"
  107. ."\t\t<SessionCategory>$row[session_category]</SessionCategory>\n";
  108. }
  109. //users
  110. $sql = "SELECT DISTINCT $tbl_user.username FROM $tbl_user
  111. INNER JOIN $tbl_session_user
  112. ON $tbl_user.user_id = $tbl_session_user.id_user AND $tbl_session_user.relation_type<>".SESSION_RELATION_TYPE_RRHH."
  113. AND $tbl_session_user.id_session = '".$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. }
  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. if($cvs)
  127. $users .= ';';
  128. $add .= $users;
  129. //courses
  130. $sql = "SELECT DISTINCT $tbl_course.code
  131. FROM $tbl_course
  132. INNER JOIN $tbl_session_course_user
  133. ON $tbl_course.code = $tbl_session_course_user.course_code
  134. AND $tbl_session_course_user.id_session = '".$row['id']."'";
  135. $rsCourses = Database::query($sql);
  136. $courses = '';
  137. while($rowCourses = Database::fetch_array($rsCourses)){
  138. // get coachs from a course
  139. $sql = "SELECT u.username
  140. FROM $tbl_session_course_user scu
  141. INNER JOIN $tbl_user u ON u.user_id = scu.id_user
  142. WHERE scu.course_code = '{$rowCourses['code']}'
  143. AND scu.id_session = '".$row['id']."' AND 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. }
  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 ON scu.id_user = su.id_user AND scu.id_session = su.id_session AND su.relation_type<>".SESSION_RELATION_TYPE_RRHH."
  163. INNER JOIN $tbl_user u
  164. ON scu.id_user = u.user_id
  165. AND scu.course_code='".$rowCourses['code']."'
  166. AND scu.id_session='".$row['id']."'";
  167. $rsUsersCourse = Database::query($sql);
  168. $userscourse = '';
  169. while($rowUsersCourse = Database::fetch_array($rsUsersCourse)){
  170. if($cvs){
  171. $userscourse .= str_replace(';',',',$rowUsersCourse['username']).',';
  172. }
  173. else {
  174. $courses .= "\t\t\t<User>$rowUsersCourse[username]</User>\n";
  175. }
  176. }
  177. if($cvs){
  178. if(!empty($userscourse))
  179. $userscourse = api_substr($userscourse , 0, api_strlen($userscourse)-1);
  180. $courses .= $userscourse.']|';
  181. }
  182. else {
  183. $courses .= "\t\t</Course>\n";
  184. }
  185. }
  186. if(!empty($courses) && $cvs)
  187. $courses = api_substr($courses , 0, api_strlen($courses)-1);
  188. $add .= $courses;
  189. if($cvs) {
  190. $breakline = api_is_windows_os()?"\r\n":"\n";
  191. $add .= ";$breakline";
  192. } else {
  193. $add .= "\t</Session>\n";
  194. }
  195. fputs($fp, $add);
  196. }
  197. if(!$cvs)
  198. fputs($fp,"</Sessions>\n");
  199. fclose($fp);
  200. $errorMsg=get_lang('UserListHasBeenExported').'<br/><a href="'.$archiveURL.$archiveFile.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  201. }
  202. }
  203. // display the header
  204. Display::display_header($tool_name);
  205. // display the tool title
  206. // api_display_tool_title($tool_name);
  207. //select of sessions
  208. $sql = "SELECT id, name FROM $tbl_session ORDER BY name";
  209. global $_configuration;
  210. if ($_configuration['multiple_access_urls']) {
  211. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  212. $access_url_id = api_get_current_access_url_id();
  213. if ($access_url_id != -1){
  214. $sql = "SELECT id, name FROM $tbl_session s INNER JOIN $tbl_session_rel_access_url as session_rel_url
  215. ON (s.id= session_rel_url.session_id)
  216. WHERE access_url_id = $access_url_id
  217. ORDER BY name";
  218. }
  219. }
  220. $result=Database::query($sql);
  221. $Sessions=Database::store_result($result);
  222. ?>
  223. <?php
  224. if(!empty($errorMsg))
  225. {
  226. Display::display_normal_message($errorMsg, false); //main API
  227. }
  228. ?>
  229. <form method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;">
  230. <input type="hidden" name="formSent" value="1">
  231. <div class="row"><div class="form_header"><?php echo $tool_name; ?></div></div>
  232. <table border="0" cellpadding="5" cellspacing="0">
  233. <tr>
  234. <td nowrap="nowrap" valign="top"><?php echo get_lang('OutputFileType'); ?> :</td>
  235. <td>
  236. <input class="checkbox" type="radio" name="file_type" id="file_type_xml" value="xml" <?php if($formSent && $file_type == 'xml') echo 'checked="checked"'; ?>> <label for="file_type_xml">XML</label><br>
  237. <input class="checkbox" type="radio" name="file_type" id="file_type_csv" value="csv" <?php if(!$formSent || $file_type == 'csv') echo 'checked="checked"'; ?>> <label for="file_type_csv">CSV</label><br>
  238. </td>
  239. </tr>
  240. <tr>
  241. <td><?php echo get_lang('WhichSessionToExport'); ?> :</td>
  242. <td><select name="session_id">
  243. <option value=""><?php echo get_lang('AllSessions') ?></option>
  244. <?php
  245. foreach($Sessions as $enreg)
  246. {
  247. ?>
  248. <option value="<?php echo $enreg['id']; ?>" <?php if($session_id == $enreg['id']) echo 'selected="selected"'; ?>><?php echo $enreg['name']; ?></option>
  249. <?php
  250. }
  251. unset($Courses);
  252. ?>
  253. </select></td>
  254. </tr>
  255. <tr>
  256. <td>&nbsp;</td>
  257. <td>
  258. <button class="save" type="submit" name="name" value="<?php echo get_lang('ExportSession') ?>"><?php echo get_lang('ExportSession') ?></button>
  259. </td>
  260. </tr>
  261. </table>
  262. </form>
  263. <?php
  264. /*
  265. ==============================================================================
  266. FOOTER
  267. ==============================================================================
  268. */
  269. Display::display_footer();
  270. ?>