user_import.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php // $Id: user_import.php 14792 2008-04-08 20:57:53Z yannoo $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos SPRL
  6. Copyright (c) 2008 Julio Montoya Armas <gugli100@gmail.com>
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. ==============================================================================
  19. * This tool allows platform admins to add users by uploading a CSV or XML file
  20. * This code is inherited from admin/user_import.php
  21. * Created on 26 julio 2008 by Julio Montoya gugli100@gmail.com
  22. ==============================================================================
  23. */
  24. /*
  25. ==============================================================================
  26. Main script
  27. ==============================================================================
  28. */
  29. $language_file = array ('admin', 'registration', 'index', 'trad4all', 'tracking');
  30. $cidReset = true;
  31. require '../inc/global.inc.php';
  32. require_once 'myspace.lib.php';
  33. $this_section = SECTION_PLATFORM_ADMIN; // TODO: Platform admin section?
  34. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  35. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  36. require_once api_get_path(LIBRARY_PATH).'classmanager.lib.php';
  37. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  38. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  39. $tool_name = get_lang('ImportUserListXMLCSV');
  40. api_block_anonymous_users();
  41. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('MySpace'));
  42. $id_session = '';
  43. if (isset($_GET['id_session']) && $_GET['id_session'] != '') {
  44. $id_session = intval($_GET['id_session']);
  45. $interbreadcrumb[] = array ('url' => 'session.php', 'name' => get_lang('Sessions'));
  46. $interbreadcrumb[] = array ('url' => 'course.php?id_session='.$id_session.'', 'name' => get_lang('Course'));
  47. }
  48. // Set this option to true to enforce strict purification for usenames.
  49. $purification_option_for_usernames = false;
  50. /*
  51. // Checking whether the current coach is the admin coach.
  52. if (!api_is_coach()) {
  53. api_not_allowed(true);
  54. }
  55. */
  56. // Checking whether the current coach is the admin coach.
  57. if (api_get_setting('add_users_by_coach') == 'true') {
  58. if (!api_is_platform_admin()) {
  59. if (isset($_REQUEST['id_session'])) {
  60. $id_session = intval($_REQUEST['id_session']);
  61. $sql = 'SELECT id_coach FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).' WHERE id='.$id_session;
  62. $rs = Database::query($sql);
  63. if (Database::result($rs, 0, 0) != $_user['user_id']) {
  64. api_not_allowed(true);
  65. }
  66. } else {
  67. api_not_allowed(true);
  68. }
  69. }
  70. } else {
  71. api_not_allowed(true);
  72. }
  73. set_time_limit(0);
  74. if ($_POST['formSent'] && $_FILES['import_file']['size'] !== 0) {
  75. $file_type = $_POST['file_type'];
  76. $id_session = intval($_POST['id_session']);
  77. if ($file_type == 'csv') {
  78. $users = MySpace::parse_csv_data($_FILES['import_file']['tmp_name']);
  79. } else {
  80. $users = MySpace::parse_xml_data($_FILES['import_file']['tmp_name']);
  81. }
  82. if (count($users) > 0) {
  83. $results = MySpace::validate_data($users);
  84. $errors = $results['errors'];
  85. $users = $results['users'];
  86. if (count($errors) == 0) {
  87. if (!empty($id_session)) {
  88. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  89. // Selecting all the courses from the session id requested.
  90. $sql = "SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'";
  91. $result = Database::query($sql);
  92. $course_list = array();
  93. while ($row = Database::fetch_array($result)) {
  94. $course_list[] = $row['course_code'];
  95. }
  96. $errors = MySpace::get_user_creator($users, $course_list, $id_session);
  97. $users = MySpace::check_all_usernames($users, $course_list, $id_session);
  98. if (count($errors) == 0) {
  99. MySpace::save_data($users, $course_list, $id_session);
  100. }
  101. } else {
  102. header('Location: course.php?id_session='.$id_session.'&action=error_message&message='.urlencode(get_lang('NoSessionId')));
  103. }
  104. }
  105. } else {
  106. header('Location: course.php?id_session='.$id_session.'&action=error_message&message='.urlencode(get_lang('NoUsersRead')));
  107. }
  108. }
  109. Display :: display_header($tool_name);
  110. if ($_FILES['import_file']['size'] == 0 && $_POST) {
  111. Display::display_error_message(get_lang('ThisFieldIsRequired'));
  112. }
  113. if (count($errors) != 0) {
  114. $error_message = '<ul>';
  115. foreach ($errors as $index => $error_user) {
  116. $error_message .= '<li><strong>'.$error_user['error'].'</strong>: ';
  117. $error_message .= api_get_person_name($error_user['FirstName'], $error_user['LastName']);
  118. $error_message .= '</li>';
  119. }
  120. $error_message .= '</ul>';
  121. Display :: display_error_message($error_message, false);
  122. }
  123. $form = new FormValidator('user_import');
  124. $form->addElement('hidden', 'formSent');
  125. $form->addElement('hidden', 'id_session',$id_session);
  126. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  127. $form->addRule('import_file', get_lang('ThisFieldIsRequired'), 'required');
  128. $allowed_file_types = array ('xml', 'csv');
  129. $form->addRule('import_file', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  130. $form->addElement('radio', 'file_type', get_lang('FileType'), 'XML (<a href="exemple.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)', 'xml');
  131. $form->addElement('radio', 'file_type', null, 'CSV (<a href="exemple.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', 'csv');
  132. $form->addElement('radio', 'sendMail', get_lang('SendMailToUsers'), get_lang('Yes'), 1);
  133. $form->addElement('radio', 'sendMail', null, get_lang('No'), 0);
  134. $form->addElement('submit', 'submit', get_lang('Ok'));
  135. $defaults['formSent'] = 1;
  136. $defaults['sendMail'] = 0;
  137. $defaults['file_type'] = 'xml';
  138. $form->setDefaults($defaults);
  139. $form->display();
  140. /*
  141. <?php echo implode('/',$defined_auth_sources); ?>
  142. &lt;AuthSource&gt;<?php echo implode('/',$defined_auth_sources); ?>&lt;/AuthSource&gt;
  143. */
  144. ?>
  145. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  146. <blockquote>
  147. <pre>
  148. <b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;OfficialCode;PhoneNumber;
  149. <b>Montoya</b>;<b>Julio</b>;<b>info@localhost</b>;jmontoya;123456789;code1;3141516
  150. <b>Doewing</b>;<b>Johny</b>;<b>info@localhost</b>;jdoewing;123456789;code2;3141516
  151. </pre>
  152. </blockquote>
  153. <p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  154. <blockquote>
  155. <pre>
  156. &lt;?xml version=&quot;1.0&quot; encoding=&quot;<?php echo api_refine_encoding_id(api_get_system_encoding()); ?>&quot;?&gt;
  157. &lt;Contacts&gt;
  158. &lt;Contact&gt;
  159. <b>&lt;LastName&gt;Montoya&lt;/LastName&gt;</b>
  160. <b>&lt;FirstName&gt;Julio&lt;/FirstName&gt;</b>
  161. <b>&lt;Email&gt;info@localhost&lt;/Email&gt;</b>
  162. &lt;UserName&gt;jmontoya&lt;/UserName&gt;
  163. &lt;Password&gt;123456&lt;/Password&gt;
  164. &lt;OfficialCode&gt;code1&lt;/OfficialCode&gt;
  165. &lt;PhoneNumber&gt;3141516&lt;/PhoneNumber&gt;
  166. &lt;/Contact&gt;
  167. &lt;/Contacts&gt;
  168. </pre>
  169. </blockquote>
  170. <?php
  171. /*
  172. ==============================================================================
  173. FOOTER
  174. ==============================================================================
  175. */
  176. Display :: display_footer();