user_import.php 6.8 KB

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