session_export.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. $sql = "SELECT id,name,id_coach,username,date_start,date_end FROM $tbl_session INNER JOIN $tbl_user
  56. ON $tbl_user.user_id = $tbl_session.id_coach ORDER BY id";
  57. global $_configuration;
  58. if ($_configuration['multiple_access_urls']==true) {
  59. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  60. $access_url_id = api_get_current_access_url_id();
  61. if ($access_url_id != -1){
  62. $sql = "SELECT id, name,id_coach,username,date_start,date_end FROM $tbl_session s INNER JOIN $tbl_session_rel_access_url as session_rel_url
  63. ON (s.id= session_rel_url.session_id) INNER JOIN $tbl_user u ON (u.user_id = s.id_coach)
  64. WHERE access_url_id = $access_url_id
  65. ORDER BY id";
  66. }
  67. }
  68. $result=api_sql_query($sql,__FILE__,__LINE__);
  69. }
  70. else
  71. {
  72. $sql = "SELECT id,name,username,date_start,date_end
  73. FROM $tbl_session
  74. INNER JOIN $tbl_user
  75. ON $tbl_user.user_id = $tbl_session.id_coach
  76. WHERE id='$session_id'";
  77. $result = api_sql_query($sql,__FILE__,__LINE__);
  78. }
  79. if(Database::num_rows($result))
  80. {
  81. if(!file_exists($archivePath))
  82. {
  83. mkpath($archivePath);
  84. }
  85. if(!file_exists($archivePath.'index.html'))
  86. {
  87. $fp=fopen($archivePath.'index.html','w');
  88. fputs($fp,'<html><head></head><body></body></html>');
  89. fclose($fp);
  90. }
  91. $archiveFile='export_sessions_'.$session_id.'_'.date('Y-m-d_H-i-s').'.'.$file_type;
  92. while( file_exists($archivePath.$archiveFile))
  93. {
  94. $archiveFile='export_users_'.$session_id.'_'.date('Y-m-d_H-i-s').'_'.uniqid('').'.'.$file_type;
  95. }
  96. $fp=fopen($archivePath.$archiveFile,'w');
  97. if($file_type == 'csv')
  98. {
  99. $cvs = true;
  100. fputs($fp,"SessionName;Coach;DateStart;DateEnd;Users;Courses;\n");
  101. }
  102. else
  103. {
  104. $cvs = false;
  105. fputs($fp,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<Sessions>\n");
  106. }
  107. while($row=Database::fetch_array($result))
  108. {
  109. $add = '';
  110. $row['name'] = str_replace(';',',',$row['name']);
  111. $row['username'] = str_replace(';',',',$row['username']);
  112. $row['date_start'] = str_replace(';',',',$row['date_start']);
  113. $row['date_end'] = str_replace(';',',',$row['date_end']);
  114. if($cvs){
  115. $add.= $row['name'].';'.$row['username'].';'.$row['date_start'].';'.$row['date_end'].';';
  116. }
  117. else {
  118. $add = "\t<Session>\n"
  119. ."\t\t<SessionName>$row[name]</SessionName>\n"
  120. ."\t\t<Coach>$row[username]</Coach>\n"
  121. ."\t\t<DateStart>$row[date_start]</DateStart>\n"
  122. ."\t\t<DateEnd>$row[date_end]</DateEnd>\n";
  123. }
  124. //users
  125. $sql = "SELECT DISTINCT $tbl_user.username FROM $tbl_user
  126. INNER JOIN $tbl_session_user
  127. ON $tbl_user.user_id = $tbl_session_user.id_user
  128. AND $tbl_session_user.id_session = '".$row['id']."'";
  129. $rsUsers = api_sql_query($sql,__FILE__,__LINE__);
  130. $users = '';
  131. while($rowUsers = Database::fetch_array($rsUsers)){
  132. if($cvs){
  133. $users .= str_replace(';',',',$rowUsers['username']).'|';
  134. }
  135. else {
  136. $users .= "\t\t<User>$rowUsers[username]</User>\n";
  137. }
  138. }
  139. if(!empty($users) && $cvs)
  140. $users = api_substr($users , 0, api_strlen($users)-1);
  141. if($cvs)
  142. $users .= ';';
  143. $add .= $users;
  144. //courses
  145. $sql = "SELECT DISTINCT $tbl_course.code, $tbl_user.username FROM $tbl_course
  146. INNER JOIN $tbl_session_course
  147. ON $tbl_course.code = $tbl_session_course.course_code
  148. AND $tbl_session_course.id_session = '".$row['id']."'
  149. LEFT JOIN $tbl_user
  150. ON $tbl_user.user_id = $tbl_session_course.id_coach";
  151. $rsCourses = api_sql_query($sql,__FILE__,__LINE__);
  152. $courses = '';
  153. while($rowCourses = Database::fetch_array($rsCourses)){
  154. if($cvs){
  155. $courses .= str_replace(';',',',$rowCourses['code']);
  156. $courses .= '['.str_replace(';',',',$rowCourses['username']).'][';
  157. }
  158. else {
  159. $courses .= "\t\t<Course>\n";
  160. $courses .= "\t\t\t<CourseCode>$rowCourses[code]</CourseCode>\n";
  161. $courses .= "\t\t\t<Coach>$rowCourses[username]</Coach>\n";
  162. }
  163. // rel user courses
  164. $sql = "SELECT DISTINCT username
  165. FROM $tbl_user
  166. INNER JOIN $tbl_session_course_user
  167. ON $tbl_session_course_user.id_user = $tbl_user.user_id
  168. AND $tbl_session_course_user.course_code='".$rowCourses['code']."'
  169. AND id_session='".$row['id']."'";
  170. $rsUsersCourse = api_sql_query($sql,__FILE__,__LINE__);
  171. while($rowUsersCourse = Database::fetch_array($rsUsersCourse)){
  172. if($cvs){
  173. $userscourse .= str_replace(';',',',$rowUsersCourse['username']).',';
  174. }
  175. else {
  176. $courses .= "\t\t\t<User>$rowUsersCourse[username]</User>\n";
  177. }
  178. }
  179. if($cvs){
  180. if(!empty($userscourse))
  181. $userscourse = api_substr($userscourse , 0, api_strlen($userscourse)-1);
  182. $courses .= $userscourse.']|';
  183. }
  184. else {
  185. $courses .= "\t\t</Course>\n";
  186. }
  187. }
  188. if(!empty($courses) && $cvs)
  189. $courses = api_substr($courses , 0, api_strlen($courses)-1);
  190. $add .= $courses;
  191. if($cvs) {
  192. $breakline = api_is_windows_os()?"\r\n":"\n";
  193. $add .= ";$breakline";
  194. } else {
  195. $add .= "\t</Session>\n";
  196. }
  197. fputs($fp, $add);
  198. }
  199. if(!$cvs)
  200. fputs($fp,"</Sessions>\n");
  201. fclose($fp);
  202. $errorMsg=get_lang('UserListHasBeenExported').'<br/><a href="'.$archiveURL.$archiveFile.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  203. }
  204. }
  205. // display the header
  206. Display::display_header($tool_name);
  207. // display the tool title
  208. // api_display_tool_title($tool_name);
  209. //select of sessions
  210. $sql = "SELECT id, name FROM $tbl_session ORDER BY name";
  211. global $_configuration;
  212. if ($_configuration['multiple_access_urls']==true) {
  213. $tbl_session_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  214. $access_url_id = api_get_current_access_url_id();
  215. if ($access_url_id != -1){
  216. $sql = "SELECT id, name FROM $tbl_session s INNER JOIN $tbl_session_rel_access_url as session_rel_url
  217. ON (s.id= session_rel_url.session_id)
  218. WHERE access_url_id = $access_url_id
  219. ORDER BY name";
  220. }
  221. }
  222. $result=api_sql_query($sql,__FILE__,__LINE__);
  223. $Sessions=api_store_result($result);
  224. ?>
  225. <?php
  226. if(!empty($errorMsg))
  227. {
  228. Display::display_normal_message($errorMsg, false); //main API
  229. }
  230. ?>
  231. <form method="post" action="<?php echo api_get_self(); ?>" style="margin:0px;">
  232. <input type="hidden" name="formSent" value="1">
  233. <div class="row"><div class="form_header"><?php echo $tool_name; ?></div></div>
  234. <table border="0" cellpadding="5" cellspacing="0">
  235. <tr>
  236. <td nowrap="nowrap" valign="top"><?php echo get_lang('OutputFileType'); ?> :</td>
  237. <td>
  238. <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>
  239. <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>
  240. </td>
  241. </tr>
  242. <tr>
  243. <td><?php echo get_lang('WhichSessionToExport'); ?> :</td>
  244. <td><select name="session_id">
  245. <option value=""><?php echo get_lang('AllSessions') ?></option>
  246. <?php
  247. foreach($Sessions as $enreg)
  248. {
  249. ?>
  250. <option value="<?php echo $enreg['id']; ?>" <?php if($session_id == $enreg['id']) echo 'selected="selected"'; ?>><?php echo $enreg['name']; ?></option>
  251. <?php
  252. }
  253. unset($Courses);
  254. ?>
  255. </select></td>
  256. </tr>
  257. <tr>
  258. <td>&nbsp;</td>
  259. <td>
  260. <button class="save" type="submit" name="name" value="<?php echo get_lang('ExportSession') ?>"><?php echo get_lang('ExportSession') ?></button>
  261. </td>
  262. </tr>
  263. </table>
  264. </form>
  265. <?php
  266. /*
  267. ==============================================================================
  268. FOOTER
  269. ==============================================================================
  270. */
  271. Display::display_footer();
  272. ?>