|
|
@ -3,21 +3,21 @@ |
|
|
|
// $Id: class_user_import.php 9018 2006-06-28 15:11:16Z evie_em $ |
|
|
|
// $Id: class_user_import.php 9018 2006-06-28 15:11:16Z evie_em $ |
|
|
|
/* |
|
|
|
/* |
|
|
|
============================================================================== |
|
|
|
============================================================================== |
|
|
|
Dokeos - elearning and course management software |
|
|
|
Dokeos - elearning and course management software |
|
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be> |
|
|
|
Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be> |
|
|
|
|
|
|
|
|
|
|
|
For a full list of contributors, see "credits.txt". |
|
|
|
For a full list of contributors, see "credits.txt". |
|
|
|
The full license can be read in "license.txt". |
|
|
|
The full license can be read in "license.txt". |
|
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
|
|
|
This program is free software; you can redistribute it and/or |
|
|
|
modify it under the terms of the GNU General Public License |
|
|
|
modify it under the terms of the GNU General Public License |
|
|
|
as published by the Free Software Foundation; either version 2 |
|
|
|
as published by the Free Software Foundation; either version 2 |
|
|
|
of the License, or (at your option) any later version. |
|
|
|
of the License, or (at your option) any later version. |
|
|
|
|
|
|
|
|
|
|
|
See the GNU General Public License for more details. |
|
|
|
See the GNU General Public License for more details. |
|
|
|
|
|
|
|
|
|
|
|
Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com |
|
|
|
Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com |
|
|
|
============================================================================== |
|
|
|
============================================================================== |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
/** |
|
|
|
/** |
|
|
@ -32,51 +32,51 @@ |
|
|
|
* Validates imported data. |
|
|
|
* Validates imported data. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function validate_data($user_classes) { |
|
|
|
function validate_data($user_classes) { |
|
|
|
global $purification_option_for_usernames; |
|
|
|
global $purification_option_for_usernames; |
|
|
|
$errors = array (); |
|
|
|
$errors = array (); |
|
|
|
$classcodes = array (); |
|
|
|
$classcodes = array (); |
|
|
|
foreach ($user_classes as $index => $user_class) { |
|
|
|
foreach ($user_classes as $index => $user_class) { |
|
|
|
$user_class['line'] = $index + 1; |
|
|
|
$user_class['line'] = $index + 1; |
|
|
|
// 1. Check whether mandatory fields are set. |
|
|
|
// 1. Check whether mandatory fields are set. |
|
|
|
$mandatory_fields = array ('UserName', 'ClassName'); |
|
|
|
$mandatory_fields = array ('UserName', 'ClassName'); |
|
|
|
foreach ($mandatory_fields as $key => $field) { |
|
|
|
foreach ($mandatory_fields as $key => $field) { |
|
|
|
if (!isset ($user_class[$field]) || strlen($user_class[$field]) == 0) { |
|
|
|
if (!isset ($user_class[$field]) || strlen($user_class[$field]) == 0) { |
|
|
|
$user_class['error'] = get_lang($field.'Mandatory'); |
|
|
|
$user_class['error'] = get_lang($field.'Mandatory'); |
|
|
|
$errors[] = $user_class; |
|
|
|
$errors[] = $user_class; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// 2. Check whether classcode exists. |
|
|
|
// 2. Check whether classcode exists. |
|
|
|
if (isset ($user_class['ClassName']) && strlen($user_class['ClassName']) != 0) { |
|
|
|
if (isset ($user_class['ClassName']) && strlen($user_class['ClassName']) != 0) { |
|
|
|
// 2.1 Check whether code has been allready used in this CVS-file. |
|
|
|
// 2.1 Check whether code has been allready used in this CVS-file. |
|
|
|
if (!isset ($classcodes[$user_class['ClassName']])) { |
|
|
|
if (!isset ($classcodes[$user_class['ClassName']])) { |
|
|
|
// 2.1.1 Check whether code exists in DB. |
|
|
|
// 2.1.1 Check whether code exists in DB. |
|
|
|
$class_table = Database :: get_main_table(TABLE_MAIN_CLASS); |
|
|
|
$class_table = Database :: get_main_table(TABLE_MAIN_CLASS); |
|
|
|
$sql = "SELECT * FROM $class_table WHERE name = '".Database::escape_string($user_class['ClassName'])."'"; |
|
|
|
$sql = "SELECT * FROM $class_table WHERE name = '".Database::escape_string($user_class['ClassName'])."'"; |
|
|
|
$res = Database::query($sql); |
|
|
|
$res = Database::query($sql); |
|
|
|
if (Database::num_rows($res) == 0) { |
|
|
|
if (Database::num_rows($res) == 0) { |
|
|
|
$user_class['error'] = get_lang('CodeDoesNotExists'); |
|
|
|
$user_class['error'] = get_lang('CodeDoesNotExists'); |
|
|
|
$errors[] = $user_class; |
|
|
|
$errors[] = $user_class; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$classcodes[$user_class['CourseCode']] = 1; |
|
|
|
$classcodes[$user_class['CourseCode']] = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// 3. Check username, first, check whether it is empty. |
|
|
|
// 3. Check username, first, check whether it is empty. |
|
|
|
if (!UserManager::is_username_empty($user_class['UserName'])) { |
|
|
|
if (!UserManager::is_username_empty($user_class['UserName'])) { |
|
|
|
// 3.1. Check whether username is too long. |
|
|
|
// 3.1. Check whether username is too long. |
|
|
|
if (UserManager::is_username_too_long($user_class['UserName'])) { |
|
|
|
if (UserManager::is_username_too_long($user_class['UserName'])) { |
|
|
|
$user_class['error'] = get_lang('UserNameTooLong').': '.$user_class['UserName']; |
|
|
|
$user_class['error'] = get_lang('UserNameTooLong').': '.$user_class['UserName']; |
|
|
|
$errors[] = $user_class; |
|
|
|
$errors[] = $user_class; |
|
|
|
} |
|
|
|
} |
|
|
|
$username = UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames); |
|
|
|
$username = UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames); |
|
|
|
// 3.2. Check whether username exists. |
|
|
|
// 3.2. Check whether username exists. |
|
|
|
if (UserManager::is_username_available($username)) { |
|
|
|
if (UserManager::is_username_available($username)) { |
|
|
|
$user_class['error'] = get_lang('UnknownUser').': '.$username; |
|
|
|
$user_class['error'] = get_lang('UnknownUser').': '.$username; |
|
|
|
$errors[] = $user_class; |
|
|
|
$errors[] = $user_class; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return $errors; |
|
|
|
return $errors; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -84,50 +84,50 @@ function validate_data($user_classes) { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function save_data($users_classes) { |
|
|
|
function save_data($users_classes) { |
|
|
|
|
|
|
|
|
|
|
|
global $purification_option_for_usernames; |
|
|
|
global $purification_option_for_usernames; |
|
|
|
|
|
|
|
|
|
|
|
// Table definitions. |
|
|
|
// Table definitions. |
|
|
|
$user_table = Database :: get_main_table(TABLE_MAIN_USER); |
|
|
|
$user_table = Database :: get_main_table(TABLE_MAIN_USER); |
|
|
|
$class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER); |
|
|
|
$class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER); |
|
|
|
$class_table = Database :: get_main_table(TABLE_MAIN_CLASS); |
|
|
|
$class_table = Database :: get_main_table(TABLE_MAIN_CLASS); |
|
|
|
|
|
|
|
|
|
|
|
// Data parsing: purification + conversion (UserName, ClassName) --> (user_is, class_id) |
|
|
|
// Data parsing: purification + conversion (UserName, ClassName) --> (user_is, class_id) |
|
|
|
$csv_data = array (); |
|
|
|
$csv_data = array (); |
|
|
|
foreach ($users_classes as $index => $user_class) { |
|
|
|
foreach ($users_classes as $index => $user_class) { |
|
|
|
$sql1 = "SELECT user_id FROM $user_table WHERE username = '".Database::escape_string(UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames))."'"; |
|
|
|
$sql1 = "SELECT user_id FROM $user_table WHERE username = '".Database::escape_string(UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames))."'"; |
|
|
|
$res1 = Database::query($sql1); |
|
|
|
$res1 = Database::query($sql1); |
|
|
|
$obj1 = Database::fetch_object($res1); |
|
|
|
$obj1 = Database::fetch_object($res1); |
|
|
|
$sql2 = "SELECT id FROM $class_table WHERE name = '".Database::escape_string(trim($user_class['ClassName']))."'"; |
|
|
|
$sql2 = "SELECT id FROM $class_table WHERE name = '".Database::escape_string(trim($user_class['ClassName']))."'"; |
|
|
|
$res2 = Database::query($sql2); |
|
|
|
$res2 = Database::query($sql2); |
|
|
|
$obj2 = Database::fetch_object($res2); |
|
|
|
$obj2 = Database::fetch_object($res2); |
|
|
|
if ($obj1 && $obj2) { |
|
|
|
if ($obj1 && $obj2) { |
|
|
|
$csv_data[$obj1->user_id][$obj2->id] = 1; |
|
|
|
$csv_data[$obj1->user_id][$obj2->id] = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Logic for processing the request (data + UI options). |
|
|
|
// Logic for processing the request (data + UI options). |
|
|
|
$db_subscriptions = array(); |
|
|
|
$db_subscriptions = array(); |
|
|
|
foreach ($csv_data as $user_id => $csv_subscriptions) { |
|
|
|
foreach ($csv_data as $user_id => $csv_subscriptions) { |
|
|
|
$sql = "SELECT class_id FROM $class_user_table cu WHERE cu.user_id = $user_id"; |
|
|
|
$sql = "SELECT class_id FROM $class_user_table cu WHERE cu.user_id = $user_id"; |
|
|
|
$res = Database::query($sql); |
|
|
|
$res = Database::query($sql); |
|
|
|
while ($obj = Database::fetch_object($res)) { |
|
|
|
while ($obj = Database::fetch_object($res)) { |
|
|
|
$db_subscriptions[$obj->class_id] = 1; |
|
|
|
$db_subscriptions[$obj->class_id] = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
$to_subscribe = array_diff(array_keys($csv_subscriptions), array_keys($db_subscriptions)); |
|
|
|
$to_subscribe = array_diff(array_keys($csv_subscriptions), array_keys($db_subscriptions)); |
|
|
|
$to_unsubscribe = array_diff(array_keys($db_subscriptions), array_keys($csv_subscriptions)); |
|
|
|
$to_unsubscribe = array_diff(array_keys($db_subscriptions), array_keys($csv_subscriptions)); |
|
|
|
// Subscriptions for new classes. |
|
|
|
// Subscriptions for new classes. |
|
|
|
if ($_POST['subscribe']) { |
|
|
|
if ($_POST['subscribe']) { |
|
|
|
foreach ($to_subscribe as $class_id) { |
|
|
|
foreach ($to_subscribe as $class_id) { |
|
|
|
ClassManager::add_user($user_id, $class_id); |
|
|
|
ClassManager::add_user($user_id, $class_id); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// Unsubscription from previous classes. |
|
|
|
// Unsubscription from previous classes. |
|
|
|
if ($_POST['unsubscribe']) { |
|
|
|
if ($_POST['unsubscribe']) { |
|
|
|
foreach ($to_unsubscribe as $class_id) { |
|
|
|
foreach ($to_unsubscribe as $class_id) { |
|
|
|
ClassManager::unsubscribe_user($user_id, $class_id); |
|
|
|
ClassManager::unsubscribe_user($user_id, $class_id); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -136,8 +136,8 @@ function save_data($users_classes) { |
|
|
|
* @return array All course-information read from the file |
|
|
|
* @return array All course-information read from the file |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function parse_csv_data($file) { |
|
|
|
function parse_csv_data($file) { |
|
|
|
$courses = Import::csv_to_array($file); |
|
|
|
$courses = Import::csv_to_array($file); |
|
|
|
return $courses; |
|
|
|
return $courses; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$language_file = array('admin', 'registration'); |
|
|
|
$language_file = array('admin', 'registration'); |
|
|
@ -170,26 +170,26 @@ $form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('Subscri |
|
|
|
$form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile')); |
|
|
|
$form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile')); |
|
|
|
$form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"'); |
|
|
|
$form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"'); |
|
|
|
if ($form->validate()) { |
|
|
|
if ($form->validate()) { |
|
|
|
$users_classes = parse_csv_data($_FILES['import_file']['tmp_name']); |
|
|
|
$users_classes = parse_csv_data($_FILES['import_file']['tmp_name']); |
|
|
|
$errors = validate_data($users_classes); |
|
|
|
$errors = validate_data($users_classes); |
|
|
|
if (count($errors) == 0) { |
|
|
|
if (count($errors) == 0) { |
|
|
|
save_data($users_classes); |
|
|
|
save_data($users_classes); |
|
|
|
header('Location: class_list.php?action=show_message&message='.urlencode(get_lang('FileImported'))); |
|
|
|
header('Location: class_list.php?action=show_message&message='.urlencode(get_lang('FileImported'))); |
|
|
|
exit(); |
|
|
|
exit(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Display :: display_header($tool_name); |
|
|
|
Display :: display_header($tool_name); |
|
|
|
api_display_tool_title($tool_name); |
|
|
|
api_display_tool_title($tool_name); |
|
|
|
|
|
|
|
|
|
|
|
if (count($errors) != 0) { |
|
|
|
if (count($errors) != 0) { |
|
|
|
$error_message = "\n"; |
|
|
|
$error_message = "\n"; |
|
|
|
foreach ($errors as $index => $error_class_user) { |
|
|
|
foreach ($errors as $index => $error_class_user) { |
|
|
|
$error_message .= get_lang('Line').' '.$error_class_user['line'].': '.$error_class_user['error'].'</b>: '; |
|
|
|
$error_message .= get_lang('Line').' '.$error_class_user['line'].': '.$error_class_user['error'].'</b>: '; |
|
|
|
$error_message .= "\n"; |
|
|
|
$error_message .= "\n"; |
|
|
|
} |
|
|
|
} |
|
|
|
$error_message .= "\n"; |
|
|
|
$error_message .= "\n"; |
|
|
|
Display :: display_error_message($error_message); |
|
|
|
Display :: display_error_message($error_message); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$form->display(); |
|
|
|
$form->display(); |
|
|
@ -206,7 +206,7 @@ adam;class01 |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
|
============================================================================== |
|
|
|
============================================================================== |
|
|
|
FOOTER |
|
|
|
FOOTER |
|
|
|
============================================================================== |
|
|
|
============================================================================== |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
Display :: display_footer(); |
|
|
|
Display :: display_footer(); |
|
|
|