user_import.php 6.4 KB

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