[svn r18648] Minor - Little code styling changes

skala
Yannick Warnier 16 years ago
parent c75c478e41
commit 5fe47ae163
  1. 106
      main/admin/user_import.php

@ -1,4 +1,4 @@
<?php // $Id: user_import.php 18645 2009-02-24 02:00:35Z yannoo $ <?php // $Id: user_import.php 18648 2009-02-24 05:35:43Z yannoo $
/* For licensing terms, see /dokeos_license.txt */ /* For licensing terms, see /dokeos_license.txt */
/** /**
============================================================================== ==============================================================================
@ -10,70 +10,56 @@
/** /**
* validate the imported data * validate the imported data
*/ */
function validate_data($users) function validate_data($users) {
{
global $defined_auth_sources; global $defined_auth_sources;
$errors = array (); $errors = array ();
$usernames = array (); $usernames = array ();
foreach ($users as $index => $user) foreach ($users as $index => $user) {
{
//1. check if mandatory fields are set //1. check if mandatory fields are set
$mandatory_fields = array ('LastName', 'FirstName'); $mandatory_fields = array ('LastName', 'FirstName');
if (api_get_setting('registration', 'email') == 'true') if (api_get_setting('registration', 'email') == 'true') {
{
$mandatory_fields[] = 'Email'; $mandatory_fields[] = 'Email';
} }
foreach ($mandatory_fields as $key => $field) foreach ($mandatory_fields as $key => $field) {
{ if (!isset ($user[$field]) || strlen($user[$field]) == 0) {
if (!isset ($user[$field]) || strlen($user[$field]) == 0)
{
$user['error'] = get_lang($field.'Mandatory'); $user['error'] = get_lang($field.'Mandatory');
$errors[] = $user; $errors[] = $user;
} }
} }
//2. check username //2. check username
if (isset ($user['UserName']) && strlen($user['UserName']) != 0) if (isset ($user['UserName']) && strlen($user['UserName']) != 0) {
{
//2.1. check if no username was used twice in import file //2.1. check if no username was used twice in import file
if (isset ($usernames[$user['UserName']])) if (isset ($usernames[$user['UserName']])) {
{
$user['error'] = get_lang('UserNameUsedTwice'); $user['error'] = get_lang('UserNameUsedTwice');
$errors[] = $user; $errors[] = $user;
} }
$usernames[$user['UserName']] = 1; $usernames[$user['UserName']] = 1;
//2.2. check if username isn't allready in use in database //2.2. check if username isn't allready in use in database
if (!UserManager :: is_username_available($user['UserName'])) if (!UserManager :: is_username_available($user['UserName'])) {
{
$user['error'] = get_lang('UserNameNotAvailable'); $user['error'] = get_lang('UserNameNotAvailable');
$errors[] = $user; $errors[] = $user;
} }
//2.3. check if username isn't longer than the 20 allowed characters //2.3. check if username isn't longer than the 20 allowed characters
if (strlen($user['UserName']) > 20) if (strlen($user['UserName']) > 20) {
{
$user['error'] = get_lang('UserNameTooLong'); $user['error'] = get_lang('UserNameTooLong');
$errors[] = $user; $errors[] = $user;
} }
} }
//3. check status //3. check status
if (isset ($user['Status']) && !api_status_exists($user['Status'])) if (isset ($user['Status']) && !api_status_exists($user['Status'])) {
{
$user['error'] = get_lang('WrongStatus'); $user['error'] = get_lang('WrongStatus');
$errors[] = $user; $errors[] = $user;
} }
//4. Check classname //4. Check classname
if (isset ($user['ClassName']) && strlen($user['ClassName']) != 0) if (isset ($user['ClassName']) && strlen($user['ClassName']) != 0) {
{ if (!ClassManager :: class_name_exists($user['ClassName'])) {
if (!ClassManager :: class_name_exists($user['ClassName']))
{
$user['error'] = get_lang('ClassNameNotAvailable'); $user['error'] = get_lang('ClassNameNotAvailable');
$errors[] = $user; $errors[] = $user;
} }
} }
//5. Check authentication source //5. Check authentication source
if (isset ($user['AuthSource']) && strlen($user['AuthSource']) != 0) if (isset ($user['AuthSource']) && strlen($user['AuthSource']) != 0) {
{ if (!in_array($user['AuthSource'], $defined_auth_sources)) {
if (!in_array($user['AuthSource'], $defined_auth_sources))
{
$user['error'] = get_lang('AuthSourceNotAvailable'); $user['error'] = get_lang('AuthSourceNotAvailable');
$errors[] = $user; $errors[] = $user;
} }
@ -85,18 +71,14 @@ function validate_data($users)
* Add missing user-information (which isn't required, like password, username * Add missing user-information (which isn't required, like password, username
* etc) * etc)
*/ */
function complete_missing_data($user) function complete_missing_data($user) {
{
//1. Create a username if necessary //1. Create a username if necessary
if (!isset ($user['UserName']) || strlen($user['UserName']) == 0) if (!isset ($user['UserName']) || strlen($user['UserName']) == 0) {
{
$username = strtolower(ereg_replace('[^a-zA-Z]', '', substr($user['FirstName'], 0, 3).' '.substr($user['LastName'], 0, 4))); $username = strtolower(ereg_replace('[^a-zA-Z]', '', substr($user['FirstName'], 0, 3).' '.substr($user['LastName'], 0, 4)));
if (!UserManager :: is_username_available($username)) if (!UserManager :: is_username_available($username)) {
{
$i = 0; $i = 0;
$temp_username = $username.$i; $temp_username = $username.$i;
while (!UserManager :: is_username_available($temp_username)) while (!UserManager :: is_username_available($temp_username)) {
{
$temp_username = $username.++$i; $temp_username = $username.++$i;
} }
$username = $temp_username; $username = $temp_username;
@ -104,18 +86,15 @@ function complete_missing_data($user)
$user['UserName'] = $username; $user['UserName'] = $username;
} }
//2. generate a password if necessary //2. generate a password if necessary
if (!isset ($user['Password']) || strlen($user['Password']) == 0) if (!isset ($user['Password']) || strlen($user['Password']) == 0) {
{
$user['Password'] = api_generate_password(); $user['Password'] = api_generate_password();
} }
//3. set status if not allready set //3. set status if not allready set
if (!isset ($user['Status']) || strlen($user['Status']) == 0) if (!isset ($user['Status']) || strlen($user['Status']) == 0) {
{
$user['Status'] = 'user'; $user['Status'] = 'user';
} }
//4. set authsource if not allready set //4. set authsource if not allready set
if (!isset ($user['AuthSource']) || strlen($user['AuthSource']) == 0) if (!isset ($user['AuthSource']) || strlen($user['AuthSource']) == 0) {
{
$user['AuthSource'] = PLATFORM_AUTH_SOURCE; $user['AuthSource'] = PLATFORM_AUTH_SOURCE;
} }
return $user; return $user;
@ -211,12 +190,10 @@ function parse_csv_data($file) {
/** /**
* XML-parser: handle start of element * XML-parser: handle start of element
*/ */
function element_start($parser, $data) function element_start($parser, $data) {
{
global $user; global $user;
global $current_tag; global $current_tag;
switch ($data) switch ($data) {
{
case 'Contact' : case 'Contact' :
$user = array (); $user = array ();
break; break;
@ -227,8 +204,7 @@ function element_start($parser, $data)
/** /**
* XML-parser: handle end of element * XML-parser: handle end of element
*/ */
function element_end($parser, $data) function element_end($parser, $data) {
{
global $user; global $user;
global $users; global $users;
global $current_value; global $current_value;
@ -253,8 +229,7 @@ function element_end($parser, $data)
/** /**
* XML-parser: handle character data * XML-parser: handle character data
*/ */
function character_data($parser, $data) function character_data($parser, $data) {
{
global $current_value; global $current_value;
$current_value = $data; $current_value = $data;
} }
@ -263,8 +238,7 @@ function character_data($parser, $data)
* @param string $file Path to the XML-file * @param string $file Path to the XML-file
* @return array All userinformation read from the file * @return array All userinformation read from the file
*/ */
function parse_xml_data($file) function parse_xml_data($file) {
{
global $current_tag; global $current_tag;
global $current_value; global $current_value;
global $user; global $user;
@ -293,8 +267,7 @@ require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php'
$formSent = 0; $formSent = 0;
$errorMsg = ''; $errorMsg = '';
$defined_auth_sources[] = PLATFORM_AUTH_SOURCE; $defined_auth_sources[] = PLATFORM_AUTH_SOURCE;
if (is_array($extAuthSource)) if (is_array($extAuthSource)) {
{
$defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource)); $defined_auth_sources = array_merge($defined_auth_sources, array_keys($extAuthSource));
} }
@ -305,20 +278,15 @@ $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAd
set_time_limit(0); set_time_limit(0);
$extra_fields = Usermanager::get_extra_fields(0, 0, 5, 'ASC',false); $extra_fields = Usermanager::get_extra_fields(0, 0, 5, 'ASC',false);
if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) {
{
$file_type = $_POST['file_type']; $file_type = $_POST['file_type'];
if ($file_type == 'csv') if ($file_type == 'csv') {
{
$users = parse_csv_data($_FILES['import_file']['tmp_name']); $users = parse_csv_data($_FILES['import_file']['tmp_name']);
} } else {
else
{
$users = parse_xml_data($_FILES['import_file']['tmp_name']); $users = parse_xml_data($_FILES['import_file']['tmp_name']);
} }
$errors = validate_data($users); $errors = validate_data($users);
if (count($errors) == 0) if (count($errors) == 0) {
{
$inserted_in_course = array(); $inserted_in_course = array();
save_data($users); save_data($users);
$msg2 = ''; $msg2 = '';
@ -338,16 +306,13 @@ if ($_POST['formSent'] AND $_FILES['import_file']['size'] !== 0)
Display :: display_header($tool_name); Display :: display_header($tool_name);
//api_display_tool_title($tool_name); //api_display_tool_title($tool_name);
if($_FILES['import_file']['size'] == 0 AND $_POST) if($_FILES['import_file']['size'] == 0 AND $_POST) {
{
Display::display_error_message(get_lang('ThisFieldIsRequired')); Display::display_error_message(get_lang('ThisFieldIsRequired'));
} }
if (count($errors) != 0) if (count($errors) != 0) {
{
$error_message = '<ul>'; $error_message = '<ul>';
foreach ($errors as $index => $error_user) foreach ($errors as $index => $error_user) {
{
$error_message .= '<li><b>'.$error_user['error'].'</b>: '; $error_message .= '<li><b>'.$error_user['error'].'</b>: ';
$error_message .= $error_user['UserName'].'&nbsp;('.$error_user['FirstName'].' '.$error_user['LastName'].')'; $error_message .= $error_user['UserName'].'&nbsp;('.$error_user['FirstName'].' '.$error_user['LastName'].')';
$error_message .= '</li>'; $error_message .= '</li>';
@ -429,4 +394,3 @@ if ($count_fields > 0) {
============================================================================== ==============================================================================
*/ */
Display :: display_footer(); Display :: display_footer();
?>
Loading…
Cancel
Save