changes for editing users by csv import -refs #6839

1.9.x
unknown 10 years ago
parent c57aac4c04
commit 8f5ae4b7b0
  1. 1
      main/admin/index.php
  2. 312
      main/admin/user_import.php

@ -74,6 +74,7 @@ if (api_is_platform_admin()) {
array('url'=>'user_add.php', 'label' => get_lang('AddUsers')),
array('url'=>'user_export.php', 'label' => get_lang('ExportUserListXMLCSV')),
array('url'=>'user_import.php', 'label' => get_lang('ImportUserListXMLCSV')),
array('url'=>'user_import.php?upd=1', 'label' => get_lang('EditUserListXMLCSV')),
);
if (api_get_setting('allow_social_tool') == 'true') {

@ -39,31 +39,56 @@ function validate_data($users)
foreach ($users as $user) {
foreach ($mandatory_fields as $field) {
if (empty($user[$field])) {
$user['error'] = get_lang($field.'Mandatory');
$errors[] = $user;
if (isset($user[$field])) {
if (empty($user[$field])) {
$user['error'] = get_lang($field.'Mandatory');
$errors[] = $user;
}
}
}
// 2. Check username, first, check whether it is empty.
if (!UserManager::is_username_empty($user['UserName'])) {
// 2.1. Check whether username is too long.
if (UserManager::is_username_too_long($user['UserName'])) {
$user['error'] = get_lang('UserNameTooLong');
$errors[] = $user;
}
// 2.2. Check whether the username was used twice in import file.
if (isset($usernames[$user['UserName']])) {
$user['error'] = get_lang('UserNameUsedTwice');
$errors[] = $user;
}
$usernames[$user['UserName']] = 1;
// 2.3. Check whether username is allready occupied.
if (!UserManager::is_username_available($user['UserName'])) {
$user['error'] = get_lang('UserNameNotAvailable');
$errors[] = $user;
}
}
if (isset($_POST['upd'])) {
if (isset($user['NewUserName'])) {
if (!UserManager::is_username_empty($user['NewUserName'])) {
// 2.1. Check whether username is too long.
if (UserManager::is_username_too_long($user['NewUserName'])) {
$user['error'] = get_lang('UserNameTooLong');
$errors[] = $user;
}
// 2.2. Check whether the username was used twice in import file.
if (isset($usernames[$user['NewUserName']])) {
$user['error'] = get_lang('UserNameUsedTwice');
$errors[] = $user;
}
$usernames[$user['UserName']] = 1;
// 2.3. Check whether username is allready occupied.
if (!UserManager::is_username_available($user['NewUserName']) && $user['NewUserName'] != $user['UserName']) {
$user['error'] = get_lang('UserNameNotAvailable');
$errors[] = $user;
}
}
}
} else {
if (!UserManager::is_username_empty($user['UserName'])) {
// 2.1. Check whether username is too long.
if (UserManager::is_username_too_long($user['UserName'])) {
$user['error'] = get_lang('UserNameTooLong');
$errors[] = $user;
}
// 2.2. Check whether the username was used twice in import file.
if (isset($usernames[$user['UserName']])) {
$user['error'] = get_lang('UserNameUsedTwice');
$errors[] = $user;
}
$usernames[$user['UserName']] = 1;
// 2.3. Check whether username is allready occupied.
if (!UserManager::is_username_available($user['UserName'])) {
$user['error'] = get_lang('UserNameNotAvailable1');
$errors[] = $user;
}
}
}
// 3. Check status.
if (isset($user['Status']) && !api_status_exists($user['Status'])) {
$user['error'] = get_lang('WrongStatus');
@ -126,6 +151,118 @@ function complete_missing_data($user)
return $user;
}
/**
* Update users from the imported data
* @param array $users List of users
* @return void
* @uses global variable $inserted_in_course, which returns the list of courses the user was inserted in
*/
function updateUsers($users)
{
global $insertedIn_course;
// Not all scripts declare the $inserted_in_course array (although they should).
if (!isset($inserted_in_course)) {
$inserted_in_course = array();
}
require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
$usergroup = new UserGroup();
$send_mail = $_POST['sendMail'] ? true : false;
if (is_array($users)) {
foreach ($users as $user) {
$user = complete_missing_data($user);
$user['Status'] = api_status_key($user['Status']);
$userName = $user['UserName'];
$userInfo = api_get_user_info_from_username($userName);
$user_id = $userInfo['user_id'];
if ($user_id == 0) {
return false;
}
$firstName = isset($user['FirstName']) ? $user['FirstName'] : $userInfo['firstname'];
$lastName = isset($user['LastName']) ? $user['LastName'] : $userInfo['lastname'];
$userName = isset($user['NewUserName']) ? $user['NewUserName'] : $userInfo['username'];
$password = isset($user['Password']) ? $user['Password'] : $userInfo['password'];
$authSource = isset($user['AuthSource']) ? $user['AuthSource'] : $userInfo['auth_source'];
$email = isset($user['Email']) ? $user['Email'] : $userInfo['email'];
$status = isset($user['Status']) ? $user['Status'] : $userInfo['status'];
$officialCode = isset($user['OfficialCode']) ? $user['OfficialCode'] : $userInfo['official_code'];
$phone = isset($user['PhoneNumber']) ? $user['PhoneNumber'] : $userInfo['phone'];
$pictureUrl = isset($user['PictureUri']) ? $user['PictureUri'] : $userInfo['picture_url'];
$expirationDate = isset($user['ExpirationDate']) ? $user['ExpirationDate'] : $userInfo['expiration_date'];
$active = isset($user['Active']) ? $user['Active'] : $userInfo['active'];
$creatorId = $userInfo['creator_id'];
$hrDeptId = $userInfo['hr_dept_id'];
$language = isset($user['Language']) ? $user['Language'] : $userInfo['language'];
$sendEmail = isset($user['SendEmail']) ? $user['SendEmail'] : $userInfo['language'];
$userUpdated = UserManager :: update_user(
$user_id,
$firstName,
$lastName,
$userName,
$password,
$authSource,
$email,
$status,
$officialCode,
$phone,
$pictureUrl,
$expirationDate,
$active,
$creatorId,
$hrDeptId,
null,
$language,
'',
'',
''
);
if (!is_array($user['Courses']) && !empty($user['Courses'])) {
$user['Courses'] = array($user['Courses']);
}
if (is_array($user['Courses'])) {
foreach ($user['Courses'] as $course) {
if (CourseManager::course_exists($course)) {
CourseManager::subscribe_user($user_id, $course, $user['Status']);
$course_info = CourseManager::get_course_information($course);
$inserted_in_course[$course] = $course_info['title'];
}
if (CourseManager :: course_exists($course, true)) {
// Also subscribe to virtual courses through check on visual code.
$list = CourseManager :: get_courses_info_from_visual_code($course);
foreach ($list as $vcourse) {
if ($vcourse['code'] == $course) {
// Ignore, this has already been inserted.
} else {
CourseManager :: subscribe_user($user_id, $vcourse['code'], $user['Status']);
$inserted_in_course[$vcourse['code']] = $vcourse['title'];
}
}
}
}
}
if (!empty($user['ClassId'])) {
$classId = explode('|', trim($user['ClassId']));
foreach ($classId as $id) {
$usergroup->subscribe_users_to_usergroup($id, array($user_id), false);
}
}
// Saving extra fields.
global $extra_fields;
// We are sure that the extra field exists.
foreach ($extra_fields as $extras) {
if (isset($user[$extras[1]])) {
$key = $extras[1];
$value = $user[$extras[1]];
UserManager::update_extra_field_value($user_id, $key, $value);
}
}
}
}
}
/**
* Save the imported data
* @param array $users List of users
@ -323,7 +460,11 @@ $user_id_error = array();
$error_message = '';
if (isset($_POST['formSent']) && $_POST['formSent'] AND $_FILES['import_file']['size'] !== 0) {
$file_type = $_POST['file_type'];
if (!isset($_POST['file_type'])) {
$file_type = 'csv';
} else {
$file_type = $_POST['file_type'];
}
Security::clear_token();
$tok = Security::get_token();
$allowed_file_mimetype = array('csv', 'xml');
@ -342,6 +483,7 @@ if (isset($_POST['formSent']) && $_POST['formSent'] AND $_FILES['import_file']['
$errors = validate_data($users);
$error_kind_file = false;
} else {
$error_kind_file = true;
}
} else {
@ -367,7 +509,11 @@ if (isset($_POST['formSent']) && $_POST['formSent'] AND $_FILES['import_file']['
$inserted_in_course = array();
if (strcmp($file_type, 'csv') === 0) {
save_data($users_to_insert);
if (!isset($_POST['upd'])) {
save_data($users_to_insert);
} else {
updateUsers($users_to_insert);
}
} elseif (strcmp($file_type, 'xml') === 0) {
save_data($users_to_insert);
} else {
@ -398,7 +544,7 @@ if (isset($_POST['formSent']) && $_POST['formSent'] AND $_FILES['import_file']['
$warning_message = 'session_message';
if ($error_kind_file) {
$error_message = get_lang('YouMustImportAFileAccordingToSelectedOption');
$error_message = get_lang('YouMustImportAFileAccordingToSelectedOption1');
} else {
header('Location: '.api_get_path(WEB_CODE_PATH).'admin/user_list.php?action=show_message&warn='.urlencode($warning_message).'&message='.urlencode($see_message_import).'&sec_token='.$tok);
exit;
@ -415,28 +561,33 @@ $form = new FormValidator('user_import','post','user_import.php');
$form->addElement('header', '', $tool_name);
$form->addElement('hidden', 'formSent');
$form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
if (isset($_GET['upd'])) {
$form->addElement('hidden', 'upd');
}
$group = array();
$group[] = $form->createElement(
'radio',
'file_type',
'',
'CSV (<a href="example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)',
'csv'
);
$group[] = $form->createElement(
'radio',
'file_type',
null,
'XML (<a href="example.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)',
'xml'
);
$form->addGroup($group, '', get_lang('FileType'), '<br/>');
$group = array();
$group[] = $form->createElement('radio', 'sendMail', '', get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'sendMail', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('SendMailToUsers'), '<br/>');
if (!isset($_GET['upd'])) {
$group[] = $form->createElement(
'radio',
'file_type',
'',
'CSV (<a href="example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)',
'csv'
);
$group[] = $form->createElement(
'radio',
'file_type',
null,
'XML (<a href="example.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)',
'xml'
);
$form->addGroup($group, '', get_lang('FileType'), '<br/>');
$group = array();
$group[] = $form->createElement('radio', 'sendMail', '', get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'sendMail', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('SendMailToUsers'), '<br/>');
}
$form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"');
$defaults['formSent'] = 1;
$defaults['sendMail'] = 0;
@ -464,32 +615,51 @@ if ($count_fields > 0) {
?>
<p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
<?php
if (isset($_GET['upd'])) {?>
<blockquote>
<pre>
<b>UserName</b>;LastName;FirstName;Email;NewUserName;Password;AuthSource;OfficialCode;PhoneNumber;Status;ExpirationDate;Active;Language;Courses;ClassId;
xxx;xxx;xxx;xxx;xxx;xxx;xxx;xxx;xxx;user/teacher/drh;0000-00-00 00:00:00;0/1;xxx;<span style="color:red;"><?php if (count($list_reponse) > 0) echo implode(';', $list_reponse).';'; ?></span>xxx1|xxx2|xxx3;1;<br />
</pre>
</blockquote>
<?php
} else {
?>
<blockquote>
<pre>
<b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;AuthSource;OfficialCode;PhoneNumber;Status;<span style="color:red;"><?php if (count($list) > 0) echo implode(';', $list).';'; ?></span>Courses;ClassId;
<b>xxx</b>;<b>xxx</b>;<b>xxx</b>;xxx;xxx;<?php echo implode('/', $defined_auth_sources); ?>;xxx;xxx;user/teacher/drh;<span style="color:red;"><?php if (count($list_reponse) > 0) echo implode(';', $list_reponse).';'; ?></span>xxx1|xxx2|xxx3;1;<br />
</pre>
</blockquote>
<?php
}
?>
<p><?php
if (!isset($_GET['upd'])) {
echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
<blockquote>
<pre>
<b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;AuthSource;OfficialCode;PhoneNumber;Status;<span style="color:red;"><?php if (count($list) > 0) echo implode(';', $list).';'; ?></span>Courses;ClassId;
<b>xxx</b>;<b>xxx</b>;<b>xxx</b>;xxx;xxx;<?php echo implode('/', $defined_auth_sources); ?>;xxx;xxx;user/teacher/drh;<span style="color:red;"><?php if (count($list_reponse) > 0) echo implode(';', $list_reponse).';'; ?></span>xxx1|xxx2|xxx3;1;<br />
</pre>
</blockquote>
<p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
<blockquote>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;<?php echo api_refine_encoding_id(api_get_system_encoding()); ?>&quot;?&gt;
&lt;Contacts&gt;
&lt;Contact&gt;
<b>&lt;LastName&gt;xxx&lt;/LastName&gt;</b>
<b>&lt;FirstName&gt;xxx&lt;/FirstName&gt;</b>
&lt;UserName&gt;xxx&lt;/UserName&gt;
&lt;Password&gt;xxx&lt;/Password&gt;
&lt;AuthSource&gt;<?php echo implode('/', $defined_auth_sources); ?>&lt;/AuthSource&gt;
<b>&lt;Email&gt;xxx&lt;/Email&gt;</b>
&lt;OfficialCode&gt;xxx&lt;/OfficialCode&gt;
&lt;PhoneNumber&gt;xxx&lt;/PhoneNumber&gt;
&lt;Status&gt;user/teacher/drh<?php if ($result_xml != '') { echo '<br /><span style="color:red;">', $result_xml; echo '</span>'; } ?>&lt;/Status&gt;
&lt;Courses&gt;xxx1|xxx2|xxx3&lt;/Courses&gt;
&lt;ClassId&gt;1&lt;/ClassId&gt;
&lt;/Contact&gt;
&lt;/Contacts&gt;
</pre>
</blockquote>
<pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;<?php echo api_refine_encoding_id(api_get_system_encoding()); ?>&quot;?&gt;
&lt;Contacts&gt;
&lt;Contact&gt;
<b>&lt;LastName&gt;xxx&lt;/LastName&gt;</b>
<b>&lt;FirstName&gt;xxx&lt;/FirstName&gt;</b>
&lt;UserName&gt;xxx&lt;/UserName&gt;
&lt;Password&gt;xxx&lt;/Password&gt;
&lt;AuthSource&gt;<?php echo implode('/', $defined_auth_sources); ?>&lt;/AuthSource&gt;
<b>&lt;Email&gt;xxx&lt;/Email&gt;</b>
&lt;OfficialCode&gt;xxx&lt;/OfficialCode&gt;
&lt;PhoneNumber&gt;xxx&lt;/PhoneNumber&gt;
&lt;Status&gt;user/teacher/drh<?php if ($result_xml != '') { echo '<br /><span style="color:red;">', $result_xml; echo '</span>'; } ?>&lt;/Status&gt;
&lt;Courses&gt;xxx1|xxx2|xxx3&lt;/Courses&gt;
&lt;ClassId&gt;1&lt;/ClassId&gt;
&lt;/Contact&gt;
&lt;/Contacts&gt;
</pre>
</blockquote>
<?php
}
Display :: display_footer();

Loading…
Cancel
Save