<?php
//die('dfjskabkjabvkjb');
function get_user($item)




{	global $_SYSTEM,$DefaultYear,$_LDAP,$Selected_school_year;
//print_r($_SYSTEM);die;
//echo "--".$_SESSION['get_school_year']."-->".$Selected_school_year."<-";
	if(!$_SESSION['get_school_year'])
		$_SESSION['get_school_year'] = $DefaultYear;
	
	// it doesn't seem like a good idea to have this in the _SYSTEM array unless we actually need it.  Right now, it's only used for webmail
	if($item=='PASSWORD' && $_LDAP['auth']==true)
	{
    
		return $_SESSION['USER_PASSWORD'];
    
	}
	elseif($item=='PASSWORD')
	{
		$password_extra = ',PASSWORD';
   // print_r($password_extra);die('jbhjkbhkjbhjb');
		unset($_SYSTEM['User']);
	}
 
 //die;
	if(!$_SYSTEM['User'])
	{
		if($_SESSION['STUDENT_ID'])
		{
			$sql = db_limit("SELECT s.FIRST_NAME".CCAT."' '".CCAT."s.LAST_NAME AS NAME,'student' AS PROFILE,','".CCAT."CAST(se.SCHOOL_ID AS VARCHAR)".CCAT."',' AS SCHOOLS,se.SCHOOL_YEAR,se.SCHOOL_ID,s.PROFILE_ID,s.USERNAME,s.LAST_NOTE_SEEN,".db_to_char('s.LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN,CFIELD_50012 AS EMAIL ".$password_extra." FROM STUDENTS s,STUDENT_REGISTRATION se WHERE s.STUDENT_ID='$_SESSION[STUDENT_ID]' AND se.SCHOOL_YEAR='$_SESSION[get_school_year]' AND (s.PROFILE IS NULL OR s.PROFILE!='applicant') AND se.STUDENT_ID=s.STUDENT_ID ORDER BY se.END_DATE DESC ",1);
			$_SYSTEM['User'] = db_request(db_execute($sql));

        
        
			if(!$_SYSTEM['User'])
			{
				$sql = db_limit("SELECT s.FIRST_NAME".CCAT."' '".CCAT."s.LAST_NAME AS NAME,'applicant' AS PROFILE,','".CCAT."CAST(se.SCHOOL_ID AS VARCHAR)".CCAT."',' AS SCHOOLS,se.SCHOOL_YEAR,se.SCHOOL_ID,s.PROFILE_ID,s.USERNAME,s.LAST_NOTE_SEEN,".db_to_char('s.LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN,CFIELD_50012 AS EMAIL ".$password_extra." FROM STUDENTS s,STUDENT_REGISTRATION se WHERE s.STUDENT_ID='$_SESSION[STUDENT_ID]' AND se.SCHOOL_YEAR='$_SESSION[get_school_year]' AND s.PROFILE='applicant' AND se.STUDENT_ID=s.STUDENT_ID ORDER BY se.END_DATE DESC ",1);
				$_SYSTEM['User'] = db_request(db_execute($sql));

            
           // print_r($sql);die;
			}
			$_SESSION['get_school_id'] = $_SYSTEM['User'][1]['SCHOOL_ID'];
		}
		elseif($_SESSION['ADDRESS_ID'])
		{
			if(!$_SYSTEM['address_user_name'])
			{
				$contacts_array = db_request(db_execute("SELECT DISTINCT p.PERSON_ID,p.FIRST_NAME,p.MIDDLE_NAME,p.LAST_NAME,p.EMAIL FROM STUDENT_RELATIONS p,STUDENTS_IN_SR sjp WHERE p.PERSON_ID=sjp.PERSON_ID AND sjp.CUSTODY='Y' AND sjp.ADDRESS_ID='".$_SESSION['ADDRESS_ID']."'"),array(),array('LAST_NAME'));
				//print_r($contacts_array);die;
           
            if(count($contacts_array))
				{
					$contact_names_title = '';
					$o = 0;
					foreach($contacts_array as $last_name=>$people)
					{
						if($o>0)
							$contact_names_title .= ', ';
						$o++;
						for($i=1;$i<count($people);$i++)
							$contact_names_title .= $people[$i]['FIRST_NAME'].' &amp; ';
						$contact_names_title .= $people[$i]['FIRST_NAME'].' '.$people[$i]['LAST_NAME'];
						
					}
				}
				$_SYSTEM['address_user_name'] = $contact_names_title;
			}
			
			$sql = "SELECT '".str_replace("'","''",$_SYSTEM['address_user_name'])."' AS NAME,'parent' AS PROFILE,NULL AS SCHOOLS,a.USERNAME,a.LAST_NOTE_SEEN,a.PROFILE_ID,".db_to_char(LAST_LOGIN,'YYYY-MM-DD')." AS LAST_LOGIN FROM ADDRESS a WHERE a.ADDRESS_ID='$_SESSION[ADDRESS_ID]'";
			$_SYSTEM['User'] = db_request(db_execute($sql));
			//$_SESSION['get_school_id'] = $_SYSTEM['User'][1]['SCHOOL_ID'];
		}
		elseif($_SESSION['USER_NAME'])
		{
//		var_dump($_REQUEST);
//		var_dump($_SESSION);
//		echo "SY:".$_SESSION[get_school_year]."|".get_school_year()."|".$_REQUEST['sel_school_year']."|";
//echo "-->".$_REQUEST[sel_school_year];
//			$sql = "SELECT USER_ID,USERNAME,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS NAME,PROFILE,PROFILE_ID,SCHOOLS,CURRENT_SCHOOL_ID,SCHOOL_YEAR,LAST_NOTE_SEEN,".db_to_char('LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN,CFIELD_40001 AS EMAIL".$password_extra." FROM USERS WHERE SCHOOL_YEAR='$_REQUEST[sel_school_year]' AND USERNAME='$_SESSION[USER_NAME]'";
			$user_query=db_request(db_execute("select * from users where username='".$_SESSION['USER_NAME']."'"));
        if($user_query) {
         $sql = "SELECT USER_ID,USERNAME,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS NAME,PROFILE,PROFILE_ID,SCHOOLS,CURRENT_SCHOOL_ID,SCHOOL_YEAR,LAST_NOTE_SEEN,".db_to_char('LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN,CFIELD_40001 AS EMAIL".$password_extra." FROM USERS WHERE USERNAME='$_SESSION[USER_NAME]'";

        
        } else {
         $sql = "SELECT STUDENT_ID,USERNAME,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS NAME,LAST_NOTE_SEEN,".db_to_char('LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN FROM STUDENTS WHERE USERNAME='$_SESSION[USER_NAME]'";

        }
       // $sql = "SELECT USER_ID,USERNAME,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS NAME,PROFILE,PROFILE_ID,SCHOOLS,CURRENT_SCHOOL_ID,SCHOOL_YEAR,LAST_NOTE_SEEN,".db_to_char('LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN,CFIELD_40001 AS EMAIL".$password_extra." FROM USERS WHERE USERNAME='$_SESSION[USER_NAME]'";
//print_r($sql);die;
//			$sql = "SELECT USER_ID,USERNAME,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS NAME,PROFILE,PROFILE_ID,SCHOOLS,CURRENT_SCHOOL_ID,SCHOOL_YEAR,LAST_NOTE_SEEN,".db_to_char('LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN,CFIELD_40001 AS EMAIL".$password_extra." FROM USERS WHERE SCHOOL_YEAR='$_SESSION[get_school_year]' AND USERNAME='$_SESSION[USER_NAME]'";
//			$sql = "SELECT USER_ID,USERNAME,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS NAME,PROFILE,PROFILE_ID,SCHOOLS,CURRENT_SCHOOL_ID,SCHOOL_YEAR,LAST_NOTE_SEEN,".db_to_char('LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN,CFIELD_40001 AS EMAIL".$password_extra." FROM USERS WHERE SCHOOL_YEAR='".get_school_year()."' AND USERNAME='$_SESSION[USER_NAME]'";
			$_SYSTEM['User'] = db_request(db_execute($sql));
//			var_dump($_SYSTEM['User']);
			// I'm not sure if this is quite correct.  This allows users to go backwards in time even if they didn't have access to the system in past years.  However, the user's user ID will be the user ID of the current year.
			if(!count($_SYSTEM['User']))
			{
				$sql = "SELECT USER_ID,USERNAME,FIRST_NAME".CCAT."' '".CCAT."LAST_NAME AS NAME,PROFILE,PROFILE_ID,SCHOOLS,CURRENT_SCHOOL_ID,SCHOOL_YEAR,LAST_NOTE_SEEN,".db_to_char('LAST_LOGIN','YYYY-MM-DD')." AS LAST_LOGIN FROM USERS WHERE USERNAME='$_SESSION[USER_NAME]'";
				$_SYSTEM['User'] = db_request(db_execute($sql));
			}
		}
		//else
		//	exit('Error');

		$_SYSTEM['User'][1]['USERNAME'] = str_replace("'","''",$_SYSTEM['User'][1]['USERNAME']);
	}
//var_dump($_SYSTEM['User'][1]);
	return $_SYSTEM['User'][1][$item];
}
?>