user_import.php 5.4 KB

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