user_import.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. $tool_name = get_lang('ImportUsersToACourse');
  10. $interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
  11. $interbreadcrumb[] = array ("url" => "#", "name" => get_lang("ImportUsersToACourse"));
  12. $form = new FormValidator('user_import','post','user_import.php');
  13. $form->addElement('header', '', $tool_name);
  14. $form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
  15. $form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
  16. $course_code = api_get_course_id();
  17. if (api_get_session_id()) {
  18. api_not_allowed();
  19. }
  20. if (empty($course_code)) {
  21. api_not_allowed();
  22. }
  23. $message = '';
  24. $user_to_show = array();
  25. $type = '';
  26. if ($form->validate()) {
  27. if (isset($_FILES['import_file']['size']) && $_FILES['import_file']['size'] !== 0) {
  28. $users = Import::csv_to_array($_FILES['import_file']['tmp_name']);
  29. $invalid_users = array();
  30. $clean_users = array();
  31. $users_in_file = array();
  32. if (!empty($users)) {
  33. foreach ($users as $user) {
  34. $user_info = api_get_user_info($user['id']);
  35. if (!empty($user_info)) {
  36. $clean_users[$user['id']] = $user_info;
  37. $users_in_file[$user['id']] = $user;
  38. } else {
  39. $invalid_users[] = $user['id'];
  40. }
  41. }
  42. if (empty($invalid_users)) {
  43. $type = 'confirmation';
  44. $message = get_lang('ListOfUsersSubscribedToCourse');
  45. foreach ($users as $user) {
  46. $result = CourseManager :: subscribe_user($user['id'], $course_code, STUDENT);
  47. //just to make sure
  48. if (CourseManager :: is_user_subscribed_in_course($user['id'], $course_code)) {
  49. $user_to_show[]= $clean_users[$user['id']]['complete_name'];
  50. }
  51. }
  52. } else {
  53. $message = get_lang('CheckUsersWithId');
  54. $type = 'warning';
  55. foreach ($invalid_users as $invalid_user) {
  56. $user_to_show[]= $invalid_user;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. Display::display_header();
  63. if (!empty($message)) {
  64. if (!empty($user_to_show)) {
  65. if ($type == 'confirmation') {
  66. Display::display_confirmation_message($message.': <br />'.implode(', ', $user_to_show), false);
  67. } else {
  68. Display::display_warning_message($message.': '.implode(', ', $user_to_show));
  69. }
  70. } else {
  71. Display::display_error_message(get_lang('ErrorsWhenImportingFile'));
  72. }
  73. }
  74. $form->display();
  75. Display::display_footer();