user_import.php 6.7 KB

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