diff --git a/main/admin/class_user_import.php b/main/admin/class_user_import.php index df1897a4ed..75809b3e13 100644 --- a/main/admin/class_user_import.php +++ b/main/admin/class_user_import.php @@ -55,9 +55,9 @@ function validate_data($user_classes) { //2.1.1 check if code exists in DB $class_table = Database :: get_main_table(TABLE_MAIN_CLASS); - $sql = "SELECT * FROM $class_table WHERE name = '".mysql_real_escape_string($user_class['ClassName'])."'"; + $sql = "SELECT * FROM $class_table WHERE name = '".Database::escape_string($user_class['ClassName'])."'"; $res = api_sql_query($sql, __FILE__, __LINE__); - if (mysql_num_rows($res) == 0) + if (Database::num_rows($res) == 0) { $user_class['error'] = get_lang('CodeDoesNotExists'); $errors[] = $user_class; diff --git a/main/admin/user_import.php b/main/admin/user_import.php index 1a4a260fef..80732487d5 100644 --- a/main/admin/user_import.php +++ b/main/admin/user_import.php @@ -1,4 +1,4 @@ - 'index.php', "name" => get_lang('PlatformAd set_time_limit(0); $extra_fields = Usermanager::get_extra_fields(0, 0, 5, 'ASC',false); - +$user_id_error=array(); if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) { $file_type = $_POST['file_type']; - if ($file_type == 'csv') { + if (strcmp($file_type,'csv')===0 && strcmp($_FILES['import_file']['type'],'text/'.$file_type.'')===0) { $users = parse_csv_data($_FILES['import_file']['tmp_name']); - } else { + $errors = validate_data($users); + $error_kind_file=false; + } elseif (strcmp($file_type,'xml')===0 && strcmp($_FILES['import_file']['type'],'text/'.$file_type.'')===0) { $users = parse_xml_data($_FILES['import_file']['tmp_name']); + $errors = validate_data($users); + $error_kind_file=false; + } else { + $error_kind_file=true; + } + + //list user id whith error + if (is_array($errors) && is_array($users)) { + foreach ($errors as $my_errors) { + $user_id_error[]=$my_errors['UserId']; + } + + foreach ($users as $my_user) { + if (!in_array($my_user['UserId'],$user_id_error)) { + $users_to_insert[]=$my_user; + } + } + } + if ( count($users_to_insert)>0 && $error_kind_file===false ) { + $errors=array(); + $users=$users_to_insert; + $see_message_import=get_lang('FileImportedJustUsersThatAreNotRegistered'); + } else { + $see_message_import=get_lang('FileImported'); } - $errors = validate_data($users); - if (count($errors) == 0) { + + if ( count($errors) == 0 && $error_kind_file===false ) { $inserted_in_course = array(); save_data($users); $msg2 = ''; @@ -299,7 +325,7 @@ if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) { } Security::clear_token(); $tok = Security::get_token(); - header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')).'&warn='.urlencode($msg2).'&sec_token='.$tok); + header('Location: user_list.php?action=show_message&message='.urlencode($see_message_import).'&warn='.urlencode($msg2).'&sec_token='.$tok); exit (); } } @@ -320,6 +346,9 @@ if (count($errors) != 0) { $error_message .= ''; Display :: display_error_message($error_message, false); } +if ($error_kind_file===true) { + Display :: display_error_message(get_lang('YouMustImportAFileAccordingToSelectedOption')); +} $form = new FormValidator('user_import'); $form->addElement('hidden', 'formSent'); $form->addElement('file', 'import_file', get_lang('ImportFileLocation')); diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index 725806f2ca..6052cb9595 100644 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -1,4 +1,4 @@ -