Reading the Username, username, Id or id rows when importing users to a course see BT#5848

1.9.x
Julio Montoya 12 years ago
parent 33fe03110e
commit a4b86fca87
  1. 97
      main/user/user_import.php

@ -22,7 +22,7 @@ $tool_name = get_lang('ImportUsersToACourse');
$interbreadcrumb[] = array ("url" => "user.php", "name" => get_lang("Users"));
$interbreadcrumb[] = array ("url" => "#", "name" => get_lang("ImportUsersToACourse"));
$form = new FormValidator('user_import','post','user_import.php');
$form = new FormValidator('user_import', 'post', 'user_import.php');
$form->addElement('header', $tool_name);
$form->addElement('file', 'import_file', get_lang('ImportCSVFileLocation'));
@ -36,7 +36,6 @@ if (empty($course_code)) {
api_not_allowed(true);
}
$session_id = api_get_session_id();
$message = '';
@ -45,44 +44,41 @@ $type = '';
if ($form->validate()) {
if (isset($_FILES['import_file']['size']) && $_FILES['import_file']['size'] !== 0) {
$unsubscribe_users = isset($_POST['unsubscribe_users']) ? true : false;
$users = Import::csv_to_array($_FILES['import_file']['tmp_name']);
$invalid_users = array();
$clean_users = array();
if (!empty($users)) {
if (!empty($users)) {
$empty_line = 0;
foreach ($users as $user_data) {
$username = "";
if (array_key_exists("username", $user_data)) {
$username = $user_data['username'];
}
else if (array_key_exists("Username", $user_data)) {
$username = $user_data['Username'];
$user_id = null;
$user_data = array_change_key_case($user_data, CASE_LOWER);
// Checking "username" field
if (isset($user_data['username']) && !empty($user_data['username'])) {
$user_id = UserManager::get_user_id_from_username($user_data['username']);
}
else if (array_key_exists(get_lang("Username"), $user_data)) {
$username = $user_data[get_lang("Username")];
// Checking "id" field
if (isset($user_data['id']) && !empty($user_data['id'])) {
$user_id = $user_data['id'];
}
$user_id = UserManager::get_user_id_from_username($username);
$user_info = api_get_user_info($user_id);
if ($user_id && !empty($user_info)) {
$clean_users[$user_id] = $user_info;
if (UserManager::is_user_id_valid($user_id)) {
$clean_users[] = $user_id;
} else {
if ($username == '') {
$empty_line = 1;
} else {
$invalid_users[] = $user_id;
}
$invalid_users[] = $user_data;
}
}
if (empty($invalid_users)) {
$type = 'confirmation';
$message = get_lang('ListOfUsersSubscribedToCourse');
if ($unsubscribe_users) {
$current_user_list = CourseManager::get_user_list_from_course_code($course_code, $session_id);
if (!empty($current_user_list)) {
@ -91,30 +87,31 @@ if ($form->validate()) {
$user_ids[]= $user['user_id'];
}
CourseManager::unsubscribe_user($user_ids, $course_code, $session_id);
}
}
foreach ($clean_users as $user_info) {
$user_id = $user_info['user_id'];
CourseManager :: subscribe_user($user_id, $course_code, STUDENT, $session_id);
}
}
foreach ($clean_users as $userId) {
$userInfo = api_get_user_info($userId);
CourseManager::subscribe_user($userId, $course_code, STUDENT, $session_id);
if (empty($session_id)) {
//just to make sure
if (CourseManager :: is_user_subscribed_in_course($user_id, $course_code)) {
$user_to_show[]= $user_info['complete_name'];
if (CourseManager :: is_user_subscribed_in_course($userId, $course_code)) {
$user_to_show[]= $userInfo['complete_name'];
}
} else {
//just to make sure
if (CourseManager :: is_user_subscribed_in_course($user_id, $course_code, true, $session_id)) {
$user_to_show[]= $user_info['complete_name'];
if (CourseManager::is_user_subscribed_in_course($userId, $course_code, true, $session_id)) {
$user_to_show[]= $userInfo['complete_name'];
}
}
}
}
} else {
$message = get_lang('CheckUsersWithId');
$type = 'warning';
foreach ($invalid_users as $invalid_user) {
$type = 'warning';
foreach ($invalid_users as $invalid_user) {
$user_to_show[]= $invalid_user;
}
}
}
}
}
}
}
@ -123,18 +120,26 @@ Display::display_header();
if (!empty($message)) {
if (!empty($user_to_show)) {
$userMessage = null;
foreach ($user_to_show as $user) {
if (!is_array($user)) {
$user = array($user);
}
$user = array_filter($user);
$userMessage .= implode(', ', $user)."<br />";
}
if ($type == 'confirmation') {
Display::display_confirmation_message($message.': <br />'.implode(', ', $user_to_show), false);
Display::display_confirmation_message($message.': <br />'.$userMessage, false);
} else {
Display::display_warning_message($message.': '.implode(', ', $user_to_show));
Display::display_warning_message($message.': <br />'.$userMessage, false);
}
} else {
$empty_line_msg = ($empty_line == 0) ? get_lang('ErrorsWhenImportingFile'): get_lang('ErrorsWhenImportingFile').': '. get_lang('EmptyHeaderLine');
$empty_line_msg = ($empty_line == 0) ? get_lang('ErrorsWhenImportingFile'): get_lang('ErrorsWhenImportingFile').': '.get_lang('EmptyHeaderLine');
Display::display_error_message($empty_line_msg);
}
}
$form->display();
echo get_lang('CSVMustLookLike');
@ -142,7 +147,7 @@ echo '<blockquote><pre>
username;
jdoe;
jmontoya;
</pre>
</pre>
</blockquote>';
Display::display_footer();
Display::display_footer();

Loading…
Cancel
Save