session_export.php 10 KB

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