user_import.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $language_file = array('registration', 'admin', 'userInfo');
  4. //require_once '../inc/global.inc.php';
  5. $this_section = SECTION_COURSES;
  6. // notice for unauthorized people.
  7. api_protect_course_script(true);
  8. if (api_get_setting('allow_user_course_subscription_by_course_admin') == 'false') {
  9. if (!api_is_platform_admin()) {
  10. api_not_allowed(true);
  11. }
  12. }
  13. $tool_name = get_lang('ImportUsersToACourse');
  14. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  15. $interbreadcrumb[] = array ("url" => "#", "name" => get_lang("ImportUsersToACourse"));
  16. $form = new FormValidator('user_import','post','user_import.php');
  17. $form->addElement('header', $tool_name);
  18. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  19. $form->addElement('checkbox', 'unsubscribe_users', null, get_lang('UnsubscribeUsersAlreadyAddedInCourse'));
  20. $form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  21. $course_code = api_get_course_id();
  22. if (empty($course_code)) {
  23. api_not_allowed(true);
  24. }
  25. $session_id = api_get_session_id();
  26. $message = '';
  27. $user_to_show = array();
  28. $type = '';
  29. if ($form->validate()) {
  30. if (isset($_FILES['import_file']['size']) && $_FILES['import_file']['size'] !== 0) {
  31. $unsubscribe_users = isset($_POST['unsubscribe_users']) ? true : false;
  32. $users = Import::csv_to_array($_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. $user_ids[]= $user['user_id'];
  63. }
  64. CourseManager::unsubscribe_user($user_ids, $course_code, $session_id);
  65. }
  66. }
  67. foreach ($clean_users as $userId) {
  68. $userInfo = api_get_user_info($userId);
  69. CourseManager::subscribe_user($userId, $course_code, STUDENT, $session_id);
  70. if (empty($session_id)) {
  71. //just to make sure
  72. if (CourseManager :: is_user_subscribed_in_course($userId, $course_code)) {
  73. $user_to_show[]= $userInfo['complete_name'];
  74. }
  75. } else {
  76. //just to make sure
  77. if (CourseManager::is_user_subscribed_in_course($userId, $course_code, true, $session_id)) {
  78. $user_to_show[]= $userInfo['complete_name'];
  79. }
  80. }
  81. }
  82. } else {
  83. $message = get_lang('CheckUsersWithId');
  84. $type = 'warning';
  85. foreach ($invalid_users as $invalid_user) {
  86. $user_to_show[]= $invalid_user;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. Display::display_header();
  93. if (!empty($message)) {
  94. if (!empty($user_to_show)) {
  95. $userMessage = null;
  96. foreach ($user_to_show as $user) {
  97. if (!is_array($user)) {
  98. $user = array($user);
  99. }
  100. $user = array_filter($user);
  101. $userMessage .= implode(', ', $user)."<br />";
  102. }
  103. if ($type == 'confirmation') {
  104. Display::display_confirmation_message($message.': <br />'.$userMessage, false);
  105. } else {
  106. Display::display_warning_message($message.': <br />'.$userMessage, false);
  107. }
  108. } else {
  109. $empty_line_msg = ($empty_line == 0) ? get_lang('ErrorsWhenImportingFile'): get_lang('ErrorsWhenImportingFile').': '.get_lang('EmptyHeaderLine');
  110. Display::display_error_message($empty_line_msg);
  111. }
  112. }
  113. $form->display();
  114. echo get_lang('CSVMustLookLike');
  115. echo '<blockquote><pre>
  116. username;
  117. jdoe;
  118. jmontoya;
  119. </pre>
  120. </blockquote>';
  121. echo get_lang('Or');
  122. echo '<blockquote><pre>
  123. id;
  124. 23;
  125. 1337;
  126. </pre>
  127. </blockquote>';
  128. Display::display_footer();