session_export.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 Ghent University (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. // name of the language file that needs to be included
  25. $language_file='admin';
  26. $cidReset=true;
  27. include('../inc/global.inc.php');
  28. // setting the section (for the tabs)
  29. $this_section=SECTION_PLATFORM_ADMIN;
  30. api_protect_admin_script(true);
  31. include(api_get_path(LIBRARY_PATH).'/fileManage.lib.php');
  32. $session_id=$_GET['session_id'];
  33. $formSent=0;
  34. $errorMsg='';
  35. // Database Table Definitions
  36. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  37. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  38. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  39. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  40. $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  41. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  42. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  43. $archivePath=api_get_path(SYS_PATH).$archiveDirName.'/';
  44. $archiveURL=api_get_path(WEB_CODE_PATH).'course_info/download.php?archive=';
  45. $tool_name=get_lang('ExportSessionListXMLCSV');
  46. $interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  47. set_time_limit(0);
  48. if($_POST['formSent'] )
  49. {
  50. $formSent=$_POST['formSent'];
  51. $file_type=($_POST['file_type'] == 'csv')?'csv':'xml';
  52. $session_id=$_POST['session_id'];
  53. if(empty($session_id))
  54. {
  55. $result=api_sql_query("SELECT id,name,id_coach,date_start,date_end FROM $tbl_session ORDER BY id",__FILE__,__LINE__);
  56. }
  57. else
  58. {
  59. $sql = "SELECT id,name,username,date_start,date_end
  60. FROM $tbl_session
  61. INNER JOIN $tbl_user
  62. ON $tbl_user.user_id = $tbl_session.id_coach
  63. WHERE id='$session_id'";
  64. $result = api_sql_query($sql,__FILE__,__LINE__);
  65. }
  66. if(mysql_num_rows($result))
  67. {
  68. if(!file_exists($archivePath))
  69. {
  70. mkpath($archivePath);
  71. }
  72. if(!file_exists($archivePath.'index.html'))
  73. {
  74. $fp=fopen($archivePath.'index.html','w');
  75. fputs($fp,'<html><head></head><body></body></html>');
  76. fclose($fp);
  77. }
  78. $archiveFile='export_sessions_'.$session_id.'_'.date('Y-m-d_H-i-s').'.'.$file_type;
  79. while( file_exists($archivePath.$archiveFile))
  80. {
  81. $archiveFile='export_users_'.$session_id.'_'.date('Y-m-d_H-i-s').'_'.uniqid('').'.'.$file_type;
  82. }
  83. $fp=fopen($archivePath.$archiveFile,'w');
  84. if($file_type == 'csv')
  85. {
  86. $cvs = true;
  87. fputs($fp,"SessionName;Coach;DateStart;DateEnd;Users;Courses;\n");
  88. }
  89. else
  90. {
  91. $cvs = false;
  92. fputs($fp,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<Sessions>\n");
  93. }
  94. while($row=mysql_fetch_array($result))
  95. {
  96. $add = '';
  97. $row['name'] = str_replace(';',',',$row['name']);
  98. $row['username'] = str_replace(';',',',$row['username']);
  99. $row['date_start'] = str_replace(';',',',$row['date_start']);
  100. $row['date_end'] = str_replace(';',',',$row['date_end']);
  101. if($cvs){
  102. $add.= $row['name'].';'.$row['username'].';'.$row['date_start'].';'.$row['date_end'].';';
  103. }
  104. else {
  105. $add = "\t<Session>\n"
  106. ."\t\t<SessionName>$row[name]</SessionName>\n"
  107. ."\t\t<Coach>$row[username]</Coach>\n"
  108. ."\t\t<DateStart>$row[date_start]</DateStart>\n"
  109. ."\t\t<DateEnd>$row[date_end]</DateEnd>\n";
  110. }
  111. //users
  112. $sql = "SELECT DISTINCT $tbl_user.username FROM $tbl_user
  113. INNER JOIN $tbl_session_user
  114. ON $tbl_user.user_id = $tbl_session_user.id_user
  115. AND $tbl_session_user.id_session = '".$row['id']."'";
  116. $rsUsers = api_sql_query($sql,__FILE__,__LINE__);
  117. $users = '';
  118. while($rowUsers = mysql_fetch_array($rsUsers)){
  119. if($cvs){
  120. $users .= str_replace(';',',',$rowUsers['username']).'|';
  121. }
  122. else {
  123. $users .= "\t\t<User>$rowUsers[username]</User>\n";
  124. }
  125. }
  126. if(!empty($users) && $cvs)
  127. $users = substr($users , 0, strlen($users)-1);
  128. if($cvs)
  129. $users .= ';';
  130. $add .= $users;
  131. //courses
  132. $sql = "SELECT DISTINCT $tbl_course.code, $tbl_user.username FROM $tbl_course
  133. INNER JOIN $tbl_session_course
  134. ON $tbl_course.code = $tbl_session_course.course_code
  135. AND $tbl_session_course.id_session = '".$row['id']."'
  136. LEFT JOIN $tbl_user
  137. ON $tbl_user.user_id = $tbl_session_course.id_coach";
  138. $rsCourses = api_sql_query($sql,__FILE__,__LINE__);
  139. $courses = '';
  140. while($rowCourses = mysql_fetch_array($rsCourses)){
  141. if($cvs){
  142. $courses .= str_replace(';',',',$rowCourses['code']);
  143. $courses .= '['.str_replace(';',',',$rowCourses['username']).'][';
  144. }
  145. else {
  146. $courses .= "\t\t<Course>\n";
  147. $courses .= "\t\t\t<CourseCode>$rowCourses[code]</CourseCode>\n";
  148. $courses .= "\t\t\t<Coach>$rowCourses[username]</Coach>\n";
  149. }
  150. // rel user courses
  151. $sql = "SELECT DISTINCT username
  152. FROM $tbl_user
  153. INNER JOIN $tbl_session_course_user
  154. ON $tbl_session_course_user.id_user = $tbl_user.user_id
  155. AND $tbl_session_course_user.course_code='".$rowCourses['code']."'
  156. AND id_session='".$row['id']."'";
  157. $rsUsersCourse = api_sql_query($sql,__FILE__,__LINE__);
  158. while($rowUsersCourse = mysql_fetch_array($rsUsersCourse)){
  159. if($cvs){
  160. $userscourse .= str_replace(';',',',$rowUsersCourse['username']).',';
  161. }
  162. else {
  163. $courses .= "\t\t\t<User>$rowUsersCourse[username]</User>\n";
  164. }
  165. }
  166. if($cvs){
  167. if(!empty($userscourse))
  168. $userscourse = substr($userscourse , 0, strlen($userscourse)-1);
  169. $courses .= $userscourse.']|';
  170. }
  171. else {
  172. $courses .= "\t\t</Course>\n";
  173. }
  174. }
  175. if(!empty($courses) && $cvs)
  176. $courses = substr($courses , 0, strlen($courses)-1);
  177. $add .= $courses;
  178. if($cvs)
  179. $add .= ';';
  180. else
  181. $add .= "\t</Session>\n";
  182. fputs($fp, $add);
  183. }
  184. if(!$cvs)
  185. fputs($fp,"</Sessions>\n");
  186. fclose($fp);
  187. $errorMsg=get_lang('UserListHasBeenExported').'<br/><a href="'.$archiveURL.$archiveFile.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  188. }
  189. }
  190. Display::display_header($tool_name);
  191. api_display_tool_title($tool_name);
  192. //select of sessions
  193. $sql = "SELECT id, name FROM $tbl_session ORDER BY name";
  194. $result=api_sql_query($sql,__FILE__,__LINE__);
  195. $Sessions=api_store_result($result);
  196. ?>
  197. <?php
  198. if(!empty($errorMsg))
  199. {
  200. Display::display_normal_message($errorMsg, false); //main API
  201. }
  202. ?>
  203. <form method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;">
  204. <input type="hidden" name="formSent" value="1">
  205. <table border="0" cellpadding="5" cellspacing="0">
  206. <tr>
  207. <td nowrap="nowrap" valign="top"><?php echo get_lang('OutputFileType'); ?> :</td>
  208. <td>
  209. <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>
  210. <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>
  211. </td>
  212. </tr>
  213. <tr>
  214. <td><?php echo get_lang('WhichSessionToExport'); ?> :</td>
  215. <td><select name="session_id">
  216. <option value=""><?php echo get_lang('AllSessions') ?></option>
  217. <?php
  218. foreach($Sessions as $enreg)
  219. {
  220. ?>
  221. <option value="<?php echo $enreg['id']; ?>" <?php if($session_id == $enreg['id']) echo 'selected="selected"'; ?>><?php echo $enreg['name']; ?></option>
  222. <?php
  223. }
  224. unset($Courses);
  225. ?>
  226. </select></td>
  227. </tr>
  228. <tr>
  229. <td>&nbsp;</td>
  230. <td><input type="submit" value="<?php echo get_lang('Ok'); ?>"></td>
  231. </tr>
  232. </table>
  233. </form>
  234. <?php
  235. /*
  236. ==============================================================================
  237. FOOTER
  238. ==============================================================================
  239. */
  240. Display::display_footer();
  241. ?>