user_import.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once '../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. $tool_name = get_lang('ImportUsersToACourse');
  13. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  14. $interbreadcrumb[] = array ("url" => "#", "name" => get_lang("ImportUsersToACourse"));
  15. $form = new FormValidator('user_import', 'post', 'user_import.php');
  16. $form->addElement('header', $tool_name);
  17. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  18. $form->addElement('checkbox', 'unsubscribe_users', null, get_lang('UnsubscribeUsersAlreadyAddedInCourse'));
  19. $form->addButtonImport(get_lang('Import'));
  20. $course_code = api_get_course_id();
  21. if (empty($course_code)) {
  22. api_not_allowed(true);
  23. }
  24. $session_id = api_get_session_id();
  25. $message = '';
  26. $user_to_show = array();
  27. $type = '';
  28. if ($form->validate()) {
  29. if (isset($_FILES['import_file']['size']) && $_FILES['import_file']['size'] !== 0) {
  30. $unsubscribe_users = isset($_POST['unsubscribe_users']) ? true : false;
  31. //@todo : csvToArray deprecated
  32. $users = Import::csvToArray($_FILES['import_file']['tmp_name']);
  33. $invalid_users = array();
  34. $clean_users = array();
  35. if (!empty($users)) {
  36. $empty_line = 0;
  37. foreach ($users as $user_data) {
  38. $user_id = null;
  39. $user_data = array_change_key_case($user_data, CASE_LOWER);
  40. // Checking "username" field
  41. if (isset($user_data['username']) && !empty($user_data['username'])) {
  42. $user_id = UserManager::get_user_id_from_username($user_data['username']);
  43. }
  44. // Checking "id" field
  45. if (isset($user_data['id']) && !empty($user_data['id'])) {
  46. $user_id = $user_data['id'];
  47. }
  48. if (UserManager::is_user_id_valid($user_id)) {
  49. $clean_users[] = $user_id;
  50. } else {
  51. $invalid_users[] = $user_data;
  52. }
  53. }
  54. if (empty($invalid_users)) {
  55. $type = 'confirmation';
  56. $message = get_lang('ListOfUsersSubscribedToCourse');
  57. if ($unsubscribe_users) {
  58. $current_user_list = CourseManager::get_user_list_from_course_code($course_code, $session_id);
  59. if (!empty($current_user_list)) {
  60. $user_ids = array();
  61. foreach ($current_user_list as $user) {
  62. if (!CourseManager::is_course_teacher($user['user_id'], $course_code)) {
  63. $user_ids[]= $user['user_id'];
  64. }
  65. }
  66. CourseManager::unsubscribe_user($user_ids, $course_code, $session_id);
  67. }
  68. }
  69. foreach ($clean_users as $userId) {
  70. $userInfo = api_get_user_info($userId);
  71. CourseManager::subscribe_user($userId, $course_code, STUDENT, $session_id);
  72. if (empty($session_id)) {
  73. //just to make sure
  74. if (CourseManager :: is_user_subscribed_in_course($userId, $course_code)) {
  75. $user_to_show[]= $userInfo['complete_name'];
  76. }
  77. } else {
  78. //just to make sure
  79. if (CourseManager::is_user_subscribed_in_course($userId, $course_code, true, $session_id)) {
  80. $user_to_show[]= $userInfo['complete_name'];
  81. }
  82. }
  83. }
  84. } else {
  85. $message = get_lang('CheckUsersWithId');
  86. $type = 'warning';
  87. foreach ($invalid_users as $invalid_user) {
  88. $user_to_show[]= $invalid_user;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. Display::display_header();
  95. if (!empty($message)) {
  96. if (!empty($user_to_show)) {
  97. $userMessage = null;
  98. foreach ($user_to_show as $user) {
  99. if (!is_array($user)) {
  100. $user = array($user);
  101. }
  102. $user = array_filter($user);
  103. $userMessage .= implode(', ', $user)."<br />";
  104. }
  105. if ($type == 'confirmation') {
  106. Display::display_confirmation_message($message.': <br />'.$userMessage, false);
  107. } else {
  108. Display::display_warning_message($message.': <br />'.$userMessage, false);
  109. }
  110. } else {
  111. $empty_line_msg = ($empty_line == 0) ? get_lang('ErrorsWhenImportingFile'): get_lang('ErrorsWhenImportingFile').': '.get_lang('EmptyHeaderLine');
  112. Display::display_error_message($empty_line_msg);
  113. }
  114. }
  115. $form->display();
  116. echo get_lang('CSVMustLookLike');
  117. echo '<blockquote><pre>
  118. username;
  119. jdoe;
  120. jmontoya;
  121. </pre>
  122. </blockquote>';
  123. echo get_lang('Or');
  124. echo '<blockquote><pre>
  125. id;
  126. 23;
  127. 1337;
  128. </pre>
  129. </blockquote>';
  130. Display::display_footer();