user_import.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once __DIR__.'/../inc/global.inc.php';
  4. $this_section = SECTION_COURSES;
  5. // notice for unauthorized people.
  6. api_protect_course_script(true);
  7. if (api_get_setting('allow_user_course_subscription_by_course_admin') == 'false') {
  8. if (!api_is_platform_admin()) {
  9. api_not_allowed(true);
  10. }
  11. }
  12. // Make sure we know if we're importing students or teachers into the course
  13. $userType = STUDENT;
  14. if (!empty($_REQUEST['type']) && $_REQUEST['type'] == COURSEMANAGER) {
  15. $userType = COURSEMANAGER;
  16. }
  17. $tool_name = get_lang('Import users list');
  18. $interbreadcrumb[] = ["url" => "user.php", "name" => get_lang("Users")];
  19. $interbreadcrumb[] = ["url" => "#", "name" => get_lang("Import users list")];
  20. $form = new FormValidator('user_import', 'post', 'user_import.php');
  21. $form->addElement('header', $tool_name);
  22. $form->addElement('file', 'import_file', get_lang('CSV file import location'));
  23. $form->addElement('checkbox', 'unsubscribe_users', null, get_lang('Unsubscribe users already added'));
  24. $form->addElement('hidden', 'type', $userType);
  25. $form->addButtonImport(get_lang('Import'));
  26. $course_code = api_get_course_id();
  27. if (empty($course_code)) {
  28. api_not_allowed(true);
  29. }
  30. $session_id = api_get_session_id();
  31. $message = '';
  32. $user_to_show = [];
  33. $type = '';
  34. if ($form->validate()) {
  35. if (isset($_FILES['import_file']['size']) && $_FILES['import_file']['size'] !== 0) {
  36. $unsubscribe_users = isset($_POST['unsubscribe_users']) ? true : false;
  37. //@todo : csvToArray deprecated
  38. $users = Import::csvToArray($_FILES['import_file']['tmp_name']);
  39. $invalid_users = [];
  40. $clean_users = [];
  41. if (!empty($users)) {
  42. $empty_line = 0;
  43. foreach ($users as $user_data) {
  44. $user_id = null;
  45. $user_data = array_change_key_case($user_data, CASE_LOWER);
  46. // Checking "username" field
  47. if (isset($user_data['username']) && !empty($user_data['username'])) {
  48. $user_id = UserManager::get_user_id_from_username($user_data['username']);
  49. }
  50. // Checking "id" field
  51. if (isset($user_data['id']) && !empty($user_data['id'])) {
  52. $user_id = $user_data['id'];
  53. }
  54. if (UserManager::is_user_id_valid($user_id)) {
  55. $clean_users[] = $user_id;
  56. } else {
  57. $invalid_users[] = $user_data;
  58. }
  59. }
  60. if (empty($invalid_users)) {
  61. $type = 'confirmation';
  62. $message = get_lang('List of users subscribed to course');
  63. if ($unsubscribe_users) {
  64. $current_user_list = CourseManager::get_user_list_from_course_code(
  65. $course_code,
  66. $session_id,
  67. null,
  68. null,
  69. $userType
  70. );
  71. if (!empty($current_user_list)) {
  72. $user_ids = [];
  73. foreach ($current_user_list as $user) {
  74. if ($userType == COURSEMANAGER) {
  75. if (CourseManager::is_course_teacher($user['user_id'], $course_code)) {
  76. $user_ids[] = $user['user_id'];
  77. }
  78. } else {
  79. if (!CourseManager::is_course_teacher($user['user_id'], $course_code)) {
  80. $user_ids[] = $user['user_id'];
  81. }
  82. }
  83. }
  84. CourseManager::unsubscribe_user($user_ids, $course_code, $session_id);
  85. }
  86. }
  87. foreach ($clean_users as $userId) {
  88. $userInfo = api_get_user_info($userId);
  89. CourseManager::subscribeUser($userId, $course_code, $userType, $session_id);
  90. if (empty($session_id)) {
  91. //just to make sure
  92. if (CourseManager::is_user_subscribed_in_course($userId, $course_code)) {
  93. $user_to_show[] = $userInfo['complete_name'];
  94. }
  95. } else {
  96. //just to make sure
  97. if (CourseManager::is_user_subscribed_in_course($userId, $course_code, true, $session_id)) {
  98. $user_to_show[] = $userInfo['complete_name'];
  99. }
  100. }
  101. }
  102. } else {
  103. $message = get_lang('Use user\'s IDs from the file to subscribe them');
  104. $type = 'warning';
  105. foreach ($invalid_users as $invalid_user) {
  106. $user_to_show[] = $invalid_user;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. Display::display_header();
  113. if (!empty($message)) {
  114. if (!empty($user_to_show)) {
  115. $userMessage = null;
  116. foreach ($user_to_show as $user) {
  117. if (!is_array($user)) {
  118. $user = [$user];
  119. }
  120. $user = array_filter($user);
  121. $userMessage .= implode(', ', $user)."<br />";
  122. }
  123. if ($type == 'confirmation') {
  124. echo Display::return_message($message.': <br />'.$userMessage, 'confirm', false);
  125. } else {
  126. echo Display::return_message($message.': <br />'.$userMessage, 'warning', false);
  127. }
  128. } else {
  129. $empty_line_msg = ($empty_line == 0) ? get_lang('Errors when importing file') : get_lang('Errors when importing file').': '.get_lang('There are empty lines in the header of selected file');
  130. echo Display::return_message($empty_line_msg, 'error');
  131. }
  132. }
  133. $form->display();
  134. echo get_lang('The CSV file must look like this');
  135. echo '<blockquote><pre>
  136. username
  137. jdoe
  138. jmontoya
  139. </pre>
  140. </blockquote>';
  141. echo get_lang('or');
  142. echo '<blockquote><pre>
  143. id
  144. 23
  145. 1337
  146. </pre>
  147. </blockquote>';
  148. Display::display_footer();