session_user_import.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. set_time_limit(0);
  9. $this_section = SECTION_PLATFORM_ADMIN;
  10. $session_id = isset($_GET['id_session']) ? intval($_GET['id_session']) : null;
  11. SessionManager::protectSession($session_id);
  12. $form_sent = 0;
  13. $tool_name = get_lang('Import users');
  14. //$interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('Administration'));
  15. $interbreadcrumb[] = ['url' => "session_list.php", "name" => get_lang('Session list')];
  16. $interbreadcrumb[] = [
  17. 'url' => "resume_session.php?id_session=".$session_id,
  18. "name" => get_lang('Session overview'),
  19. ];
  20. if (isset($_POST['formSent']) && $_POST['formSent']) {
  21. if (isset($_FILES['import_file']['tmp_name']) &&
  22. !empty($_FILES['import_file']['tmp_name'])
  23. ) {
  24. $form_sent = $_POST['formSent'];
  25. // CSV
  26. $users = Import::csvToArray($_FILES['import_file']['tmp_name']);
  27. $user_list = [];
  28. foreach ($users as $user_data) {
  29. $username = $user_data['username'];
  30. $user_id = UserManager::get_user_id_from_username($username);
  31. if ($user_id) {
  32. $user_list[] = $user_id;
  33. }
  34. }
  35. if (!empty($user_list)) {
  36. SessionManager::subscribeUsersToSession(
  37. $session_id,
  38. $user_list,
  39. null,
  40. false
  41. );
  42. foreach ($user_list as &$user_id) {
  43. $user_info = api_get_user_info($user_id);
  44. $user_id = $user_info['complete_name'];
  45. }
  46. $error_message = get_lang('Users added').' : '.implode(', ', $user_list);
  47. }
  48. } else {
  49. $error_message = get_lang('No file was sent');
  50. }
  51. }
  52. // Display the header.
  53. Display::display_header($tool_name);
  54. echo '<div class="actions">';
  55. echo '<a href="resume_session.php?id_session='.$session_id.'">'.
  56. Display::return_icon('back.png', get_lang('Back to').' '.get_lang('Administration'), '', ICON_SIZE_MEDIUM).
  57. '</a>';
  58. echo '</div>';
  59. if (!empty($error_message)) {
  60. echo Display::return_message($error_message, 'normal', false);
  61. }
  62. $form = new FormValidator(
  63. 'import_sessions',
  64. 'post',
  65. api_get_self().'?id_session='.$session_id,
  66. null,
  67. ['enctype' => 'multipart/form-data']
  68. );
  69. $form->addElement('hidden', 'formSent', 1);
  70. $form->addElement('file', 'import_file', get_lang('CSV file import location'));
  71. $form->addButtonImport(get_lang('Import'));
  72. $form->display();
  73. ?>
  74. <p><?php echo get_lang('The CSV file must look like this'); ?> :</p>
  75. <blockquote>
  76. <pre>
  77. username;
  78. admin;
  79. teacher;
  80. jmontoya;
  81. </pre>
  82. </blockquote>
  83. <?php
  84. Display::display_footer();