diff --git a/main/admin/add_users_to_usergroup.php b/main/admin/add_users_to_usergroup.php index 765788dfe1..36f22fb9d6 100644 --- a/main/admin/add_users_to_usergroup.php +++ b/main/admin/add_users_to_usergroup.php @@ -220,6 +220,9 @@ if ($add_type == 'multiple') { echo '
'; echo ''.Display::return_icon('back.png',get_lang('Back'), array(), ICON_SIZE_MEDIUM).''; + +echo ''.Display::return_icon('import_csv.png',get_lang('Import'), array(), ICON_SIZE_MEDIUM).''; + echo '
'; ?>
@@ -416,4 +419,4 @@ function makepost(select){ --> display();
name;description
- User group 1;My user group 1 + User group 1;My user group description
$user_class) { + $user_class['line'] = $index + 1; + // 1. Check whether mandatory fields are set. + $mandatory_fields = array ('UserName', 'ClassName'); + + foreach ($mandatory_fields as $key => $field) { + if (!isset ($user_class[$field]) || strlen($user_class[$field]) == 0) { + $user_class['error'] = get_lang($field.'Mandatory'); + $errors[] = $user_class; + } + } + + // 2. Check whether classcode exists. + if (isset ($user_class['ClassName']) && strlen($user_class['ClassName']) != 0) { + // 2.1 Check whether code has been allready used in this CVS-file. + if (!isset ($classcodes[$user_class['ClassName']])) { + // 2.1.1 Check whether code exists in DB + $exists = $usergroup->usergroup_exists($user_class['ClassName']); + if (!$exists) { + $user_class['error'] = get_lang('CodeDoesNotExists').': '.$user_class['ClassName']; + $errors[] = $user_class; + } else { + $classcodes[$user_class['CourseCode']] = 1; + } + } + } + + // 3. Check username, first, check whether it is empty. + if (!UserManager::is_username_empty($user_class['UserName'])) { + // 3.1. Check whether username is too long. + if (UserManager::is_username_too_long($user_class['UserName'])) { + $user_class['error'] = get_lang('UserNameTooLong').': '.$user_class['UserName']; + $errors[] = $user_class; + } + + $username = UserManager::purify_username($user_class['UserName'], $purification_option_for_usernames); + // 3.2. Check whether username exists. + if (UserManager::is_username_available($username)) { + $user_class['error'] = get_lang('UnknownUser').': '.$username; + $errors[] = $user_class; + } + } + } + return $errors; +} + +/** + * Saves imported data. + */ +function save_data($users_classes) { + + global $purification_option_for_usernames; + + // Table definitions. + $user_table = Database :: get_main_table(TABLE_MAIN_USER); + + $usergroup = new UserGroup(); + + // Data parsing: purification + conversion (UserName, ClassName) --> (user_is, class_id) + $csv_data = array (); + + foreach ($users_classes as $user_class) { + + $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); + $obj1 = Database::fetch_object($res1); + + $usergroup = new UserGroup(); + $id = $usergroup->get_id_by_name($user_class['ClassName']); + if ($obj1 && $id) { + $csv_data[$id]['user_list'][] = $obj1->user_id; + } + } + + // Logic for processing the request (data + UI options). + + foreach ($csv_data as $class_id => $user_data) { + $user_list = $user_data['user_list']; + $usergroup->subscribe_users_to_usergroup($class_id, $user_list); + + + /* + $sql = "SELECT class_id FROM $class_user_table cu WHERE cu.user_id = $user_id"; + $res = Database::query($sql); + while ($obj = Database::fetch_object($res)) { + $db_subscriptions[$obj->class_id] = 1; + } + $to_subscribe = array_diff(array_keys($csv_subscriptions), array_keys($db_subscriptions)); + $to_unsubscribe = array_diff(array_keys($db_subscriptions), array_keys($csv_subscriptions)); + + // Subscriptions for new classes. + if ($_POST['subscribe']) { + foreach ($to_subscribe as $class_id) { + ClassManager::add_user($user_id, $class_id); + } + } + // Unsubscription from previous classes. + if ($_POST['unsubscribe']) { + foreach ($to_unsubscribe as $class_id) { + ClassManager::unsubscribe_user($user_id, $class_id); + } + }*/ + } +} + +/** + * Reads a CSV-file. + * @param string $file Path to the CSV-file + * @return array All course-information read from the file + */ +function parse_csv_data($file) { + $courses = Import::csv_to_array($file); + return $courses; +} + +$language_file = array('admin', 'registration'); +$cidReset = true; + +require_once '../inc/global.inc.php'; + +$this_section = SECTION_PLATFORM_ADMIN; +api_protect_admin_script(true); + +require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php'; +require_once api_get_path(LIBRARY_PATH).'import.lib.php'; + +$tool_name = get_lang('AddUsersToAClass').' CSV'; + +$interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin')); +$interbreadcrumb[] = array ('url' => 'usergroups.php', 'name' => get_lang('Classes')); + +// Set this option to true to enforce strict purification for usenames. +$purification_option_for_usernames = false; + +set_time_limit(0); + +$form = new FormValidator('class_user_import'); +$form->addElement('file', 'import_file', get_lang('File')); +//$form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('SubscribeUserIfNotAllreadySubscribed')); +//$form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile')); +$form->addElement('style_submit_button', 'submit', get_lang('Import'), 'class="save"'); + +if ($form->validate()) { + $users_classes = parse_csv_data($_FILES['import_file']['tmp_name']); + $errors = validate_data($users_classes); + if (count($errors) == 0) { + save_data($users_classes); + header('Location: usergroup_user_import.php?action=show_message&message='.urlencode(get_lang('FileImported'))); + exit(); + } +} + +Display :: display_header($tool_name); +echo Display::page_header($tool_name); + +if (count($errors) != 0) { + $error_message = "\n"; + foreach ($errors as $index => $error_class_user) { + $error_message .= get_lang('Line').' '.$error_class_user['line'].': '.$error_class_user['error'].''; + $error_message .= "
"; + } + $error_message .= "\n"; + Display :: display_error_message($error_message, false); +} +$form->display(); +?> +

:

+
+UserName;ClassName
+jdoe;class01
+adam;class01
+
+table, array(),'first'); return $row['count']; } + + public function get_id_by_name($name) { + $row = Database::select('id', $this->table, array('where' => array('name = ?', $name)),'first'); + return $row['id']; + } /** @@ -37,8 +42,12 @@ class UserGroup extends Model { // action links echo '
'; echo ''.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'),'','32').''; - //echo ''.Display::return_icon('back.png',get_lang('Back')).get_lang('Back').''; + echo ''.Display::return_icon('new_class.png',get_lang('langAddClasses'),'','32').''; + + echo Display::url(Display::return_icon('import_csv.png', get_lang('Import'), array(), ICON_SIZE_MEDIUM), 'usergroup_import.php'); + echo Display::url(Display::return_icon('export_csv.png', get_lang('Export'), array(), ICON_SIZE_MEDIUM), 'usergroup_export.php'); + echo '
'; echo Display::grid_html('usergroups'); } @@ -236,7 +245,7 @@ class UserGroup extends Model { * @param array list of user ids */ function subscribe_users_to_usergroup($usergroup_id, $list) { - $current_list = self::get_users_by_usergroup($usergroup_id); + $current_list = self::get_users_by_usergroup($usergroup_id); $course_list = self::get_courses_by_usergroup($usergroup_id); $session_list = self::get_sessions_by_usergroup($usergroup_id); @@ -297,4 +306,10 @@ class UserGroup extends Model { } } } + + function usergroup_exists($name) { + $sql = "SELECT * FROM $this->table WHERE name='".Database::escape_string($name)."'"; + $res = Database::query($sql); + return Database::num_rows($res) != 0; + } }