, Ghent University, Belgium
 */
function status_filter($status) {
	$statusname = api_get_status_langvars();
	return $statusname[$status];
}
if (isset($_GET['keyword']) || isset($_GET['keyword_firstname'])) {
    $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
    $interbreadcrumb[] = array ("url" => 'user_list.php', "name" => get_lang('UserList'));
    $tool_name = get_lang('SearchUsers');
} else {
    $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
    $tool_name = get_lang('UserList');
}
$message = '';
if (!empty($action)) {
    $check = Security::check_token('get');
	if ($check) {
		switch ($action) {
            case 'add_user_to_my_url':
                $user_id = $_REQUEST["user_id"];
                $result = UrlManager::add_user_to_url($user_id, $current_access_url_id);
                if ($result ) {
                    $user_info = api_get_user_info($user_id);
                    $message = get_lang('UserAdded').' '.$user_info['firstname'].' '.$user_info['lastname'].' ('.$user_info['username'].')';
                    $message  = Display::return_message($message, 'confirmation');
                }
                break;
			case 'delete_user':
				if (api_is_platform_admin()) {
                    $user_to_delete = $_GET['user_id'];
                    $current_user_id = api_get_user_id();
					if ($deleteUserAvailable && api_global_admin_can_edit_admin($_GET['user_id'])) {
						if ($user_to_delete != $current_user_id && UserManager :: delete_user($_GET['user_id'])) {
							$message = Display :: return_message(get_lang('UserDeleted'), 'confirmation');
						} else {
							$message = Display :: return_message(get_lang('CannotDeleteUserBecauseOwnsCourse'), 'error');
						}
					} else {
						$message = Display :: return_message(get_lang('CannotDeleteUser'),'error');
					}
				}
				break;
            case 'delete':
				if (api_is_platform_admin()) {
					$number_of_selected_users = count($_POST['id']);
					$number_of_deleted_users = 0;
					if (is_array($_POST['id'])) {
						foreach ($_POST['id'] as $index => $user_id) {
							if ($user_id != $_user['user_id']) {
								if (UserManager::delete_user($user_id)) {
									$number_of_deleted_users++;
								}
							}
						}
					}
					if ($number_of_selected_users == $number_of_deleted_users) {
                        $message = Display :: return_message(get_lang('SelectedUsersDeleted'), 'confirmation');
					} else {
                        $message = Display :: return_message(get_lang('SomeUsersNotDeleted'), 'error');
					}
				}
				break;
		}
		Security::clear_token();
	}
}
// Create a search-box
$form = new FormValidator('search_simple', 'get', '', '', array(), FormValidator::LAYOUT_INLINE);
$form->addElement('text', 'keyword');
$form->addButtonSearch(get_lang('Search'));
$form->addElement(
    'static',
    'search_advanced_link',
    null,
    '
         '.
        Display::return_icon('div_show.gif', get_lang('Show'), array('style'=>'vertical-align:middle')).' '.get_lang('AdvancedSearch').'
        
    '
);
$actions  = '';
if (api_is_platform_admin()) {
	$actions .= '';
}
$actions .= $form->return_form();
if (isset ($_GET['keyword'])) {
	$parameters = array ('keyword' => Security::remove_XSS($_GET['keyword']));
} elseif (isset ($_GET['keyword_firstname'])) {
	$parameters['keyword_firstname'] 	= Security::remove_XSS($_GET['keyword_firstname']);
	$parameters['keyword_lastname']	 	= Security::remove_XSS($_GET['keyword_lastname']);
	$parameters['keyword_username']	 	= Security::remove_XSS($_GET['keyword_username']);
	$parameters['keyword_email'] 	 	= Security::remove_XSS($_GET['keyword_email']);
	$parameters['keyword_officialcode'] = Security::remove_XSS($_GET['keyword_officialcode']);
	$parameters['keyword_status'] 		= Security::remove_XSS($_GET['keyword_status']);
	$parameters['keyword_active'] 		= Security::remove_XSS($_GET['keyword_active']);
	$parameters['keyword_inactive'] 	= Security::remove_XSS($_GET['keyword_inactive']);
}
// Create a sortable table with user-data
$parameters['sec_token'] = Security::get_token();
// get the list of all admins to mark them in the users list
$admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
$sql_admin = "SELECT user_id FROM $admin_table";
$res_admin = Database::query($sql_admin);
$_admins_list = array();
while ($row_admin = Database::fetch_row($res_admin)) {
	$_admins_list[] = $row_admin[0];
}
// Display Advanced search form.
$form = new FormValidator('advanced_search', 'get', '', '', array(), FormValidator::LAYOUT_HORIZONTAL);
$form->addElement('html','');
$form->addElement('header', get_lang('AdvancedSearch'));
$form->addText('keyword_firstname',get_lang('FirstName'),false);
$form->addText('keyword_lastname',get_lang('LastName'),false);
$form->addText('keyword_username',get_lang('LoginName'),false);
$form->addText('keyword_email',get_lang('Email'),false);
$form->addText('keyword_officialcode',get_lang('OfficialCode'),false);
$status_options = array();
$status_options['%'] = get_lang('All');
$status_options[STUDENT] = get_lang('Student');
$status_options[COURSEMANAGER] = get_lang('Teacher');
$status_options[DRH] = get_lang('Drh');
$status_options[SESSIONADMIN] = get_lang('SessionsAdmin');
$status_options[PLATFORM_ADMIN] = get_lang('Administrator');
$form->addElement('select','keyword_status',get_lang('Profile'), $status_options    );
$active_group = array();
$active_group[] = $form->createElement('checkbox','keyword_active','', get_lang('Active'));
$active_group[] = $form->createElement('checkbox','keyword_inactive','', get_lang('Inactive'));
$form->addGroup($active_group,'',get_lang('ActiveAccount'), '
',false);
$form->addElement('checkbox', 'check_easy_passwords', null, get_lang('CheckEasyPasswords'));
$form->addButtonSearch(get_lang('SearchUsers'));
$defaults = array();
$defaults['keyword_active'] = 1;
$defaults['keyword_inactive'] = 1;
$form->setDefaults($defaults);
$form->addElement('html','
');
$form = $form->returnForm();
$table = new SortableTable(
    'users',
    'get_number_of_users',
    'get_user_data',
    (api_is_western_name_order() xor api_sort_by_first_name()) ? 3 : 2
);
$table->set_additional_parameters($parameters);
$table->set_header(0, '', false, 'width="18px"');
$table->set_header(1, get_lang('Photo'), false);
$table->set_header(2, get_lang('OfficialCode'));
if (api_is_western_name_order()) {
	$table->set_header(3, get_lang('FirstName'));
	$table->set_header(4, get_lang('LastName'));
} else {
	$table->set_header(3, get_lang('LastName'));
	$table->set_header(4, get_lang('FirstName'));
}
$table->set_header(5, get_lang('LoginName'));
$table->set_header(6, get_lang('Email'));
$table->set_header(7, get_lang('Profile'));
$table->set_header(8, get_lang('Active'), true, 'width="15px"');
$table->set_header(9, get_lang('RegistrationDate'), true, 'width="90px"');
$table->set_header(10, get_lang('Action'), false, 'width="220px"');
$table->set_column_filter(3, 'user_filter');
$table->set_column_filter(4, 'user_filter');
$table->set_column_filter(6, 'email_filter');
$table->set_column_filter(7, 'status_filter');
$table->set_column_filter(8, 'active_filter');
$table->set_column_filter(10, 'modify_filter');
// Only show empty actions bar if delete users has been blocked
if (api_is_platform_admin() && !(api_get_configuration_value('deny_delete_users'))) {
    $table->set_form_actions(array('delete' => get_lang('DeleteFromPlatform')));
} else {
    $table->set_form_actions(array('none' => get_lang('NoActionAvailable')));
}
$table_result = $table->return_table();
$extra_search_options = '';
//Try to search the user everywhere
if ($table->get_total_number_of_items() == 0) {
    if (api_get_multiple_access_url() && isset($_REQUEST['keyword'])) {
        $keyword = Database::escape_string($_REQUEST['keyword']);
        $conditions = array('username' => $keyword);
        $user_list = UserManager::get_user_list($conditions, array(), false, ' OR ');
        if (!empty($user_list)) {
            $extra_search_options = Display::page_subheader(get_lang('UsersFoundInOtherPortals'));
            $table = new HTML_Table(array('class' => 'data_table'));
            $column = 0;
            $row = 0;
            $headers = array(get_lang('User'), 'URL', get_lang('Actions'));
            foreach ($headers as $header) {
                $table->setHeaderContents($row, $column, $header);
                $column++;
            }
            $row++;
            foreach ($user_list as $user) {
                $column = 0;
                $access_info = UrlManager::get_access_url_from_user($user['id']);
                $access_info_to_string = '';
                $add_user = true;
                if (!empty($access_info)) {
                    foreach ($access_info as $url_info) {
                        if ($current_access_url_id == $url_info['access_url_id']) {
                            $add_user = false;
                        }
                        $access_info_to_string .= $url_info['url'].'
';
                    }
                }
                if ($add_user) {
                    $row_table = array();
                    $row_table[] =  api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].') ';
                    $row_table[] =  $access_info_to_string;
                    $url = api_get_self().'?action=add_user_to_my_url&user_id='.$user['id'].'&sec_token='.$_SESSION['sec_token'];
                    $row_table[] =  Display::url(get_lang('AddUserToMyURL'), $url, array('class' => 'btn'));
                    foreach ($row_table as $cell) {
                        $table->setCellContents($row, $column, $cell);
                        $table->updateCellAttributes($row, $column, 'align="center"');
                        $column++;
                    }
                    $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
                    $row++;
                }
            }
            $extra_search_options .= $table->toHtml();
            $table_result = '';
        }
    }
}
$tpl = new Template($tool_name);
$tpl->assign('actions', $actions);
$tpl->assign('message', $message);
$tpl->assign('content', $form.$table_result.$extra_search_options);
$tpl->display_one_col_template();