user_import.php 5.3 KB

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