|
|
|
@ -39,13 +39,37 @@ function validate_data($users) |
|
|
|
|
|
|
|
|
|
foreach ($users as $user) { |
|
|
|
|
foreach ($mandatory_fields as $field) { |
|
|
|
|
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 (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'])) { |
|
|
|
@ -60,10 +84,11 @@ function validate_data($users) |
|
|
|
|
$usernames[$user['UserName']] = 1; |
|
|
|
|
// 2.3. Check whether username is allready occupied. |
|
|
|
|
if (!UserManager::is_username_available($user['UserName'])) { |
|
|
|
|
$user['error'] = get_lang('UserNameNotAvailable'); |
|
|
|
|
$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) { |
|
|
|
|
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) { |
|
|
|
|
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,7 +561,11 @@ $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(); |
|
|
|
|
if (!isset($_GET['upd'])) { |
|
|
|
|
$group[] = $form->createElement( |
|
|
|
|
'radio', |
|
|
|
|
'file_type', |
|
|
|
@ -423,6 +573,7 @@ $group[] = $form->createElement( |
|
|
|
|
'CSV (<a href="example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', |
|
|
|
|
'csv' |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$group[] = $form->createElement( |
|
|
|
|
'radio', |
|
|
|
|
'file_type', |
|
|
|
@ -436,7 +587,7 @@ $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,13 +615,30 @@ 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> |
|
|
|
|
<p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
|
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
?> |
|
|
|
|
<p><?php |
|
|
|
|
if (!isset($_GET['upd'])) { |
|
|
|
|
echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p> |
|
|
|
|
<blockquote> |
|
|
|
|
<pre> |
|
|
|
|
<?xml version="1.0" encoding="<?php echo api_refine_encoding_id(api_get_system_encoding()); ?>"?>
|
|
|
|
@ -491,5 +659,7 @@ if ($count_fields > 0) { |
|
|
|
|
</Contacts> |
|
|
|
|
</pre> |
|
|
|
|
</blockquote> |
|
|
|
|
|
|
|
|
|
<?php |
|
|
|
|
} |
|
|
|
|
Display :: display_footer(); |
|
|
|
|