<?php
function getDetail($what){
	global $db;
	$st=$db->getVal("select $what from ".USER." where school_id='".$_SESSION["admin"]["school_id"]."' and userid='".$_SESSION["user"]["uid"]."'");
	echo $st;
}
function myDetail($what){
	global $db;
	$st=$db->getVal("select $what from ".USER." where school_id='".$_SESSION["admin"]["school_id"]."' and userid='".$_SESSION["user"]["uid"]."'");
	return $st;
}
function getFriendDetail($id,$what){
	global $db;
	$st=$db->getVal("select $what from ".USER." where school_id='".$_SESSION["admin"]["school_id"]."' and userid='".$id."'");
	echo $st;//$db->getLastQuery();
}
function myFriendDetail($id,$what){
	global $db;
	$st=$db->getVal("select $what from ".USER." where school_id='".$_SESSION["admin"]["school_id"]."' and userid='".$id."'");
	return $st;//$db->getLastQuery();
}
function ago($date)  //to find the time in diff format eg. 5 min ago
{
	if(empty($date)) {
       return "No date provided";
    }
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date         = strtotime($date);
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }
    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    return "$difference $periods[$j] {$tense}";
} 
function isFriend($userid){
	global $db;
	$st=$db->getVal("select status from ".CONTACTS." where school_id='".$_SESSION["admin"]["school_id"]."' and status='2' && ((userid='".$_SESSION["user"]["uid"]."' and friendid='".$userid."') || (friendid='".$_SESSION["user"]["uid"]."' and userid='".$userid."'))");
	return $st;
}
function getUsers($first_letter="")
{
	$users = array();
	$first_letter = mysql_real_escape_string($first_letter);
	$query = "select * from ".USER." where userlname like '$first_letter%' order by userfname, userlname";
	$res = mysql_query($query);
	while($row = mysql_fetch_assoc($res))
	  $users[] = $row;
		
	return $users;
}
function notification($notice,$type,$id,$replyon=0){
	global $db;
	$status=myDetail("privacy_activity");
	$incData=array("notice"=>$notice,
					"type"=>$type,
					"userid"=>$_SESSION["user"]["uid"],
					"link_id"=>$id,
					"reply_on"=>$replyon,
					"school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
					"status"=>$status
	);
	$res=$db->insertAry(ACTIVITY,$incData);
	return $res;
}
function getLocation($loc){
	$users = array();
	$loc = mysql_real_escape_string($loc);
	$query = "select * from ".USER." where country ='$loc' order by userfname, userlname ASC";
	$res = mysql_query($query);
	while($row = mysql_fetch_assoc($res))
	  $users[] = $row;
		
	return $users;
}
?>