debugOn();
$xajax -> registerFunction ('search_coachs');
// setting the section (for the tabs)
$this_section=SECTION_PLATFORM_ADMIN;
api_protect_admin_script(true);
$formSent=0;
$errorMsg='';
$interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
$interbreadcrumb[]=array('url' => "session_list.php","name" => get_lang('SessionList'));
// Database Table Definitions
$tbl_user		= Database::get_main_table(TABLE_MAIN_USER);
$tbl_session	= Database::get_main_table(TABLE_MAIN_SESSION);
function search_coachs($needle)
{
	global $tbl_user;
	$xajax_response = new XajaxResponse();
	$return = '';
	if(!empty($needle))
	{
		// xajax send utf8 datas... datas in db can be non-utf8 datas
		$charset = api_get_setting('platform_charset');
		$needle = api_convert_encoding($needle, $charset, 'utf-8');
		$order_clause = api_sort_by_first_name() ? ' ORDER BY firstname, lastname, username' : ' ORDER BY lastname, firstname, username';
		// search users where username or firstname or lastname begins likes $needle
		$sql = 'SELECT username, lastname, firstname FROM '.$tbl_user.' user
				WHERE (username LIKE "'.$needle.'%"
				OR firstname LIKE "'.$needle.'%"
				OR lastname LIKE "'.$needle.'%")
				AND status=1'.
				$order_clause.
				' LIMIT 10';
		global $_configuration;
		if ($_configuration['multiple_access_urls']==true) {
			$tbl_user_rel_access_url= Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
			$access_url_id = api_get_current_access_url_id();
			if ($access_url_id != -1){
				$sql = 'SELECT username, lastname, firstname FROM '.$tbl_user.' user
				INNER JOIN '.$tbl_user_rel_access_url.' url_user ON (url_user.user_id=user.user_id)
				WHERE access_url_id = '.$access_url_id.'  AND (username LIKE "'.$needle.'%"
				OR firstname LIKE "'.$needle.'%"
				OR lastname LIKE "'.$needle.'%")
				AND status=1'.
				$order_clause.
				' LIMIT 10';
			}
		}
		$rs = Database::query($sql, __FILE__, __LINE__);
		while ($user = Database :: fetch_array($rs)) {
			$return .= ''.api_get_person_name($user['firstname'], $user['lastname']).' ('.$user['username'].')
';
		}
	}
	$xajax_response -> addAssign('ajax_list_coachs','innerHTML', api_utf8_encode($return));
	return $xajax_response;
}
$xajax -> processRequests();
$htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
$htmlHeadXtra[] = '
';
if ($_POST['formSent']) {
	$formSent=1;
	$name= $_POST['name'];
	$year_start= $_POST['year_start'];
	$month_start=$_POST['month_start'];
	$day_start=$_POST['day_start'];
	$year_end=$_POST['year_end'];
	$month_end=$_POST['month_end'];
	$day_end=$_POST['day_end'];
	$nb_days_acess_before = $_POST['nb_days_acess_before'];
	$nb_days_acess_after = $_POST['nb_days_acess_after'];
	$nolimit=$_POST['nolimit'];
	$coach_username=$_POST['coach_username'];
	$id_session_category = $_POST['session_category'];
	$id_visibility = $_POST['session_visibility'];
	$return = SessionManager::create_session($name,$year_start,$month_start,$day_start,$year_end,$month_end,$day_end,$nb_days_acess_before,$nb_days_acess_after,$nolimit,$coach_username, $id_session_category,$id_visibility);
	if ($return == strval(intval($return))) {
		// integer => no error on session creation
		header('Location: add_courses_to_session.php?id_session='.$return.'&add=true&msg=');
		exit();
	}
}
$nb_days_acess_before = 0;
$nb_days_acess_after = 0;
$thisYear=date('Y');
$thisMonth=date('m');
$thisDay=date('d');
$tool_name = get_lang('AddSession');
//display the header
Display::display_header($tool_name);
// display the tool title
// api_display_tool_title($tool_name);
if (!empty($return)) {
	Display::display_error_message($return,false);
}
?>