user_import.php 6.3 KB

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