user_export.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // $Id: user_export.php 19597 2009-04-07 14:38:36Z pcool $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) Olivier Brouckaert
  10. Copyright (c) Bart Mollet, Hogeschool Gent
  11. For a full list of contributors, see "credits.txt".
  12. The full license can be read in "license.txt".
  13. This program is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU General Public License
  15. as published by the Free Software Foundation; either version 2
  16. of the License, or (at your option) any later version.
  17. See the GNU General Public License for more details.
  18. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  19. ==============================================================================
  20. */
  21. /**
  22. ==============================================================================
  23. * @package dokeos.admin
  24. ==============================================================================
  25. */
  26. // name of the language file that needs to be included
  27. $language_file = 'admin';
  28. $cidReset = true;
  29. include ('../inc/global.inc.php');
  30. $this_section = SECTION_PLATFORM_ADMIN;
  31. api_protect_admin_script();
  32. include (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  33. include (api_get_path(LIBRARY_PATH).'export.lib.inc.php');
  34. include (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  35. // Database table definitions
  36. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  37. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  38. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  39. $tool_name = get_lang('ExportUserListXMLCSV');
  40. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  41. set_time_limit(0);
  42. $courses = array ();
  43. $courses[''] = '--';
  44. $sql = "SELECT code,visual_code,title FROM $course_table ORDER BY visual_code";
  45. global $_configuration;
  46. if ($_configuration['multiple_access_urls']==true) {
  47. $tbl_course_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  48. $access_url_id = api_get_current_access_url_id();
  49. if ($access_url_id != -1){
  50. $sql = "SELECT code,visual_code,title FROM $course_table as c INNER JOIN $tbl_course_rel_access_url as course_rel_url
  51. ON (c.code = course_rel_url.course_code)
  52. WHERE access_url_id = $access_url_id
  53. ORDER BY visual_code";
  54. }
  55. }
  56. $result = Database::query($sql);
  57. while ($course = Database::fetch_object($result))
  58. {
  59. $courses[$course->code] = $course->visual_code.' - '.$course->title;
  60. }
  61. $form = new FormValidator('export_users');
  62. $form->addElement('header', '', $tool_name);
  63. $form->addElement('radio', 'file_type', get_lang('OutputFileType'), 'XML','xml');
  64. $form->addElement('radio', 'file_type', null, 'CSV','csv');
  65. $form->addElement('checkbox', 'addcsvheader', get_lang('AddCSVHeader'), get_lang('YesAddCSVHeader'),'1');
  66. $form->addElement('select', 'course_code', get_lang('OnlyUsersFromCourse'), $courses);
  67. $form->addElement('style_submit_button', 'submit',get_lang('Ok'),'class="save"');
  68. $form->setDefaults(array('file_type'=>'csv'));
  69. if ($form->validate())
  70. {
  71. global $userPasswordCrypted;
  72. $export = $form->exportValues();
  73. $file_type = $export['file_type'];
  74. $course_code = $export['course_code'];
  75. $sql = "SELECT u.user_id AS UserId,
  76. u.lastname AS LastName,
  77. u.firstname AS FirstName,
  78. u.email AS Email,
  79. u.username AS UserName,
  80. ".(($userPasswordCrypted!='none')?" ":"u.password AS Password, ")."
  81. u.auth_source AS AuthSource,
  82. u.status AS Status,
  83. u.official_code AS OfficialCode,
  84. u.phone AS Phone";
  85. if (strlen($course_code) > 0) {
  86. $sql .= " FROM $user_table u, $course_user_table cu WHERE u.user_id = cu.user_id AND course_code = '$course_code' AND cu.relation_type<>".COURSE_RELATION_TYPE_RRHH." ORDER BY lastname,firstname";
  87. $filename = 'export_users_'.$course_code.'_'.date('Y-m-d_H-i-s');
  88. } else {
  89. global $_configuration;
  90. if ($_configuration['multiple_access_urls']==true) {
  91. $tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  92. $access_url_id = api_get_current_access_url_id();
  93. if ($access_url_id != -1) {
  94. $sql.= " FROM $user_table u INNER JOIN $tbl_user_rel_access_url as user_rel_url
  95. ON (u.user_id= user_rel_url.user_id)
  96. WHERE access_url_id = $access_url_id
  97. ORDER BY lastname,firstname";
  98. }
  99. } else {
  100. $sql .= " FROM $user_table u ORDER BY lastname,firstname";
  101. }
  102. $filename = 'export_users_'.date('Y-m-d_H-i-s');
  103. }
  104. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  105. $data = array();
  106. $extra_fields = Usermanager::get_extra_fields(0, 0, 5, 'ASC',false);
  107. if ($export['addcsvheader']=='1' AND $export['file_type']=='csv')
  108. {
  109. if($userPasswordCrypted!='none') {
  110. $data[] = array('UserId', 'LastName', 'FirstName', 'Email', 'UserName', 'AuthSource', 'Status', 'OfficialCode', 'Phone');
  111. } else {
  112. $data[] = array('UserId', 'LastName', 'FirstName', 'Email', 'UserName','Password', 'AuthSource', 'Status', 'OfficialCode', 'Phone');
  113. }
  114. foreach($extra_fields as $extra) {
  115. $data[0][]=$extra[1];
  116. }
  117. }
  118. $res = Database::query($sql);
  119. while($user = Database::fetch_array($res,'ASSOC')) {
  120. $student_data= UserManager :: get_extra_user_data($user['UserId'],true,false);
  121. foreach($student_data as $key=>$value) {
  122. $key= substr($key,6);
  123. if (is_array($value))
  124. $user[$key]= $value[$key];
  125. else {
  126. $user[$key]= $value;
  127. }
  128. }
  129. $data[] = $user ;
  130. }
  131. switch($file_type) {
  132. case 'xml':
  133. Export::export_table_xml($data,$filename,'Contact','Contacts');
  134. exit;
  135. break;
  136. case 'csv':
  137. Export::export_table_csv($data,$filename);
  138. exit;
  139. break;
  140. }
  141. }
  142. Display :: display_header($tool_name);
  143. //api_display_tool_title($tool_name);
  144. $form->display();
  145. /*
  146. ==============================================================================
  147. FOOTER
  148. ==============================================================================
  149. */
  150. Display :: display_footer();
  151. ?>