session_export.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php // $Id: session_export.php,v 1.1 2006/04/20 09:58:01 elixir_inter Exp $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 University of Ghent (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Olivier Brouckaert
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * @package dokeos.admin
  22. ==============================================================================
  23. */
  24. $langFile='admin';
  25. $cidReset=true;
  26. include('../inc/global.inc.php');
  27. api_protect_admin_script();
  28. include(api_get_library_path().'/fileManage.lib.php');
  29. $session_id=$_GET['session_id'];
  30. $formSent=0;
  31. $errorMsg='';
  32. $tbl_user=Database::get_main_table(MAIN_USER_TABLE);
  33. $tbl_course = Database::get_main_table(MAIN_COURSE_TABLE);
  34. $tbl_course_user = Database::get_main_table(MAIN_COURSE_USER_TABLE);
  35. $tbl_session = Database::get_main_table(MAIN_SESSION_TABLE);
  36. $tbl_session_user = Database::get_main_table(MAIN_SESSION_USER_TABLE);
  37. $tbl_session_course = Database::get_main_table(MAIN_SESSION_COURSE_TABLE);
  38. $tbl_session_course_user = Database::get_main_table(MAIN_SESSION_COURSE_USER_TABLE);
  39. $archivePath=api_get_path(SYS_PATH).$archiveDirName.'/';
  40. $archiveURL=api_get_path(WEB_CODE_PATH).'course_info/download.php?archive=';
  41. $tool_name=get_lang('ExportSessionListXMLCSV');
  42. $interbreadcrumb[]=array("url" => "index.php","name" => get_lang('AdministrationTools'));
  43. set_time_limit(0);
  44. if($_POST['formSent'] )
  45. {
  46. $formSent=$_POST['formSent'];
  47. $file_type=($_POST['file_type'] == 'csv')?'csv':'xml';
  48. $session_id=$_POST['session_id'];
  49. if(empty($session_id))
  50. {
  51. $result=api_sql_query("SELECT id,name,id_coach,date_start,date_end FROM $tbl_session ORDER BY id",__FILE__,__LINE__);
  52. }
  53. else
  54. {
  55. $sql = "SELECT id,name,username,date_start,date_end
  56. FROM $tbl_session
  57. INNER JOIN $tbl_user
  58. ON $tbl_user.user_id = $tbl_session.id_coach
  59. WHERE id='$session_id'";
  60. $result = api_sql_query($sql,__FILE__,__LINE__);
  61. }
  62. if(mysql_num_rows($result))
  63. {
  64. if(!file_exists($archivePath))
  65. {
  66. mkpath($archivePath);
  67. }
  68. if(!file_exists($archivePath.'index.html'))
  69. {
  70. $fp=fopen($archivePath.'index.html','w');
  71. fputs($fp,'<html><head></head><body></body></html>');
  72. fclose($fp);
  73. }
  74. $archiveFile='export_sessions_'.$session_id.'_'.date('Y-m-d_H-i-s').'.'.$file_type;
  75. while( file_exists($archivePath.$archiveFile))
  76. {
  77. $archiveFile='export_users_'.$session_id.'_'.date('Y-m-d_H-i-s').'_'.uniqid('').'.'.$file_type;
  78. }
  79. $fp=fopen($archivePath.$archiveFile,'w');
  80. if($file_type == 'csv')
  81. {
  82. $cvs = true;
  83. fputs($fp,"SessionName;Coach;DateStart;DateEnd;Users;Courses;\n");
  84. }
  85. else
  86. {
  87. $cvs = false;
  88. fputs($fp,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<Sessions>\n");
  89. }
  90. while($row=mysql_fetch_array($result))
  91. {
  92. $add = '';
  93. $row['name'] = str_replace(';',',',$row['name']);
  94. $row['username'] = str_replace(';',',',$row['username']);
  95. $row['date_start'] = str_replace(';',',',$row['date_start']);
  96. $row['date_end'] = str_replace(';',',',$row['date_end']);
  97. if($cvs){
  98. $add.= $row['name'].';'.$row['username'].';'.$row['date_start'].';'.$row['date_end'].';';
  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. }
  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
  111. AND $tbl_session_user.id_session = '".$row['id']."'";
  112. $rsUsers = api_sql_query($sql,__FILE__,__LINE__);
  113. $users = '';
  114. while($rowUsers = mysql_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 = substr($users , 0, strlen($users)-1);
  124. if($cvs)
  125. $users .= ';';
  126. $add .= $users;
  127. //courses
  128. $sql = "SELECT DISTINCT $tbl_course.code, $tbl_user.username FROM $tbl_course
  129. INNER JOIN $tbl_session_course
  130. ON $tbl_course.code = $tbl_session_course.course_code
  131. AND $tbl_session_course.id_session = '".$row['id']."'
  132. LEFT JOIN $tbl_user
  133. ON $tbl_user.user_id = $tbl_session_course.id_coach";
  134. $rsCourses = api_sql_query($sql,__FILE__,__LINE__);
  135. $courses = '';
  136. while($rowCourses = mysql_fetch_array($rsCourses)){
  137. if($cvs){
  138. $courses .= str_replace(';',',',$rowCourses['code']);
  139. $courses .= '['.str_replace(';',',',$rowCourses['username']).'][';
  140. }
  141. else {
  142. $courses .= "\t\t<Course>\n";
  143. $courses .= "\t\t\t<CourseCode>$rowCourses[code]</CourseCode>\n";
  144. $courses .= "\t\t\t<Coach>$rowCourses[username]</Coach>\n";
  145. }
  146. // rel user courses
  147. $sql = "SELECT DISTINCT username
  148. FROM $tbl_user
  149. INNER JOIN $tbl_session_course_user
  150. ON $tbl_session_course_user.id_user = $tbl_user.user_id
  151. AND $tbl_session_course_user.course_code='".$rowCourses['code']."'
  152. AND id_session='".$row['id']."'";
  153. $rsUsersCourse = api_sql_query($sql,__FILE__,__LINE__);
  154. while($rowUsersCourse = mysql_fetch_array($rsUsersCourse)){
  155. if($cvs){
  156. $userscourse .= str_replace(';',',',$rowUsersCourse['username']).',';
  157. }
  158. else {
  159. $courses .= "\t\t\t<User>$rowUsersCourse[username]</User>\n";
  160. }
  161. }
  162. if($cvs){
  163. if(!empty($userscourse))
  164. $userscourse = substr($userscourse , 0, strlen($userscourse)-1);
  165. $courses .= $userscourse.']|';
  166. }
  167. else {
  168. $courses .= "\t\t</Course>\n";
  169. }
  170. }
  171. if(!empty($courses) && $cvs)
  172. $courses = substr($courses , 0, strlen($courses)-1);
  173. $add .= $courses;
  174. if($cvs)
  175. $add .= ';';
  176. else
  177. $add .= "\t</Session>\n";
  178. fputs($fp, $add);
  179. }
  180. if(!$cvs)
  181. fputs($fp,"</Sessions>\n");
  182. fclose($fp);
  183. $errorMsg=get_lang('UserListHasBeenExported').'<br/><a href="'.$archiveURL.$archiveFile.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  184. }
  185. }
  186. Display::display_header($tool_name);
  187. api_display_tool_title($tool_name);
  188. //select of sessions
  189. $sql = "SELECT id, name FROM $tbl_session ORDER BY name";
  190. $result=api_sql_query($sql,__FILE__,__LINE__);
  191. $Sessions=api_store_result($result);
  192. ?>
  193. <?php
  194. if(!empty($errorMsg))
  195. {
  196. Display::display_normal_message($errorMsg); //main API
  197. }
  198. ?>
  199. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="margin:0px;">
  200. <input type="hidden" name="formSent" value="1">
  201. <table border="0" cellpadding="5" cellspacing="0">
  202. <tr>
  203. <td nowrap="nowrap" valign="top"><?php echo get_lang('OutputFileType'); ?> :</td>
  204. <td>
  205. <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>
  206. <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>
  207. </td>
  208. </tr>
  209. <tr>
  210. <td><?php echo get_lang('WhichSessionToExport'); ?> :</td>
  211. <td><select name="session_id">
  212. <option value=""><?php echo get_lang('AllSessions') ?></option>
  213. <?php
  214. foreach($Sessions as $enreg)
  215. {
  216. ?>
  217. <option value="<?php echo $enreg['id']; ?>" <?php if($session_id == $enreg['id']) echo 'selected="selected"'; ?>><?php echo $enreg['name']; ?></option>
  218. <?php
  219. }
  220. unset($Courses);
  221. ?>
  222. </select></td>
  223. </tr>
  224. <tr>
  225. <td>&nbsp;</td>
  226. <td><input type="submit" value="<?php echo get_lang('Ok'); ?>"></td>
  227. </tr>
  228. </table>
  229. </form>
  230. <?php
  231. /*
  232. ==============================================================================
  233. FOOTER
  234. ==============================================================================
  235. */
  236. Display::display_footer();
  237. ?>