<?php

function Percent($num,$decimals=2)
{
	if(!is_numeric($deimals))	
	    $decimals = 0;
	    
	if(SystemPreferences('ROUND_AVERAGE_POINTS')!=$deimals)
	    $decimals = SystemPreferences('ROUND_AVERAGE_POINTS');
	    
	if(SystemPreferences('DECIMAL')==',')
		return (number_format($num*100,$decimals,',','.')).'%';
	else
		return (number_format($num*100,$decimals,'.','')*1).'%';
}

function getLetterGrade($percent)
{
    global $lgrades_array, $_REQUEST;
    $percent = round($percent*100);
//    var_dump($_REQUEST);
    if(!$lgrades_array)
    {
	if(!$_REQUEST['grading_scale'] || $_REQUEST['grading_scale'] == 'auto')
	    $sql = "SELECT DEFAULT_BREAKOFF,SCHOOL_ID,TITLE FROM GRADES_CARD_SETTINGS WHERE SCALE_ID = (SELECT ID FROM GRADES_CARD_TYPES WHERE SCHOOL_YEAR = ".get_school_year()." AND SCHOOL_ID = ".get_school_id()." AND DEFAULT_SCALE = 'Y') ORDER BY DEFAULT_BREAKOFF DESC";
	else
	    $sql = "SELECT DEFAULT_BREAKOFF,SCHOOL_ID,TITLE FROM GRADES_CARD_SETTINGS WHERE SCALE_ID = '".$_REQUEST['grading_scale']."' ORDER BY DEFAULT_BREAKOFF DESC";
        $lgrades_array = db_request(db_execute($sql));
    }
    foreach($lgrades_array as $num=>$garray)
    {
	$lg = strval($garray['DEFAULT_BREAKOFF']);
	$nlg = strval($lgrades_array[$num+1]['DEFAULT_BREAKOFF']);
//	echo $garray['TITLE']."==".$num."===$lg===>$nlg==================>$percent<br>";
	if($percent >= $lg)
	    return $garray['TITLE'];
	elseif($percent <= $lg && $percent >= $nlg)
	    return $lgrades_array[$num+1]['TITLE'];
    }
}

function getLetterGradeDesc($percent)
{
    global $ldgrades_array, $_REQUEST;
//    var_dump($percent);
    $percent = round($percent*100);
//    var_dump($_REQUEST);
    if(!$ldgrades_array)
    {
	if(!$_REQUEST['grading_scale'] || $_REQUEST['grading_scale'] == 'auto')
	    $sql = "SELECT DEFAULT_BREAKOFF,SCHOOL_ID,TITLE,DESCRIPTION FROM GRADES_CARD_SETTINGS WHERE SCALE_ID = (SELECT ID FROM GRADES_CARD_TYPES WHERE SCHOOL_YEAR = ".get_school_year()." AND SCHOOL_ID = ".get_school_id()." AND DEFAULT_SCALE = 'Y') ORDER BY DEFAULT_BREAKOFF DESC";
	else
	    $sql = "SELECT DEFAULT_BREAKOFF,SCHOOL_ID,TITLE,DESCRIPTION FROM GRADES_CARD_SETTINGS WHERE SCALE_ID = '".$_REQUEST['grading_scale']."' ORDER BY DEFAULT_BREAKOFF DESC";
        $ldgrades_array = db_request(db_execute($sql));
    }
    foreach($ldgrades_array as $num=>$garray)
    {
	$lg = strval($garray['DEFAULT_BREAKOFF']);
	$nlg = strval($ldgrades_array[$num+1]['DEFAULT_BREAKOFF']);
//	echo $garray['DESCRIPTION']."==".$num."===$lg===>$nlg=======: $percent<br>";
	if($percent >= $lg)
	    return $garray['DESCRIPTION'];
	elseif($percent <= $lg && $percent >= $nlg)
	    return $ldgrades_array[$num+1]['DESCRIPTION'];
    }
}


?>