<?php
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );
include("FusionCharts/FusionCharts.php");

$class_ar[1]="ชั้นประถมศึกษาปีที่ 1";
$class_ar[2]="ชั้นประถมศึกษาปีที่ 2";
$class_ar[3]="ชั้นประถมศึกษาปีที่ 3";
$class_ar[4]="ชั้นประถมศึกษาปีที่ 4";
$class_ar[5]="ชั้นประถมศึกษาปีที่ 5";
$class_ar[6]="ชั้นประถมศึกษาปีที่ 6";
$class_ar[7]="ชั้นมัธยมศึกษาปีที่ 1";
$class_ar[8]="ชั้นมัธยมศึกษาปีที่ 2";
$class_ar[9]="ชั้นมัธยมศึกษาปีที่ 3";
$class_ar[10]="ชั้นมัธยมศึกษาปีที่ 4";
$class_ar[11]="ชั้นมัธยมศึกษาปีที่ 5";
$class_ar[12]="ชั้นมัธยมศึกษาปีที่ 6";

if(!(isset($_REQUEST['school_code']))){
$_REQUEST['school_code']="";
}
$sql = "select distinct test_class from achievement_main where  test_type='1' order by test_class limit 1";
$qury = mysqli_query($connect,$sql);
$result=mysqli_fetch_array($qury);
if(!(isset($_REQUEST['class_index']))){
$_REQUEST['class_index']=$result['test_class'];
}
if(!(isset($_REQUEST['ed_year']))){
$_REQUEST['ed_year']="";
}

?>
	<SCRIPT LANGUAGE="Javascript" SRC="FusionCharts/FusionCharts.js"></SCRIPT>
	<style type="text/css">
	<!--
	body {
		font-family: Arial, Helvetica, sans-serif;
		font-size: 12px;
	}
	.text{
		font-family: Arial, Helvetica, sans-serif;
		font-size: 12px;
	}
	-->
	</style>

<CENTER>
<h3>ผลการสอบ O-NET  จำแนกตามกลุ่มโรงเรียนและโรงเรียน</h3>
<h3><?php
if(isset($_REQUEST['class_index'])){
 echo $class_ar[$_REQUEST['class_index']];
} 
 ?></h3>
<?php
echo "<form id='frm1' name='frm1'>";
echo "<table width='95%' align='center'>";
echo"<tr><td align='right'>";
echo "<Select  name='class_index' size='1'>";
				if($_REQUEST['class_index']=='1'){
				$select_class_1="selected";
				}
				else if($_REQUEST['class_index']=='2'){
				$select_class_2="selected";
				}
				else if($_REQUEST['class_index']=='3'){
				$select_class_3="selected";
				}
				else if($_REQUEST['class_index']=='4'){
				$select_class_4="selected";
				}
				else if($_REQUEST['class_index']=='5'){
				$select_class_5="selected";
				}
				else if($_REQUEST['class_index']=='6'){
				$select_class_6="selected";
				}

				else if($_REQUEST['class_index']=='7'){
				$select_class_7="selected";
				}
				else if($_REQUEST['class_index']=='8'){
				$select_class_8="selected";
				}
				else if($_REQUEST['class_index']=='9'){
				$select_class_9="selected";
				}
				else if($_REQUEST['class_index']=='10'){
				$select_class_10="selected";
				}
				else if($_REQUEST['class_index']=='11'){
				$select_class_11="selected";
				}
				else if($_REQUEST['class_index']=='12'){
				$select_class_12="selected";
				}
				
echo  "<option value =6 $select_class_6>ชั้น ป.6</option>";
echo  "<option value =9 $select_class_9>ชั้น ม.3</option>";
echo  "<option value =12 $select_class_12>ชั้น ม.6</option>";
echo "</select>";

echo "<Select  name='ed_year' size='1'>";
$sql = "select distinct ed_year from achievement_main where  test_type='1' and test_class='$_REQUEST[class_index]' order by ed_year desc ";
$dbquery = mysqli_query($connect,$sql);
$year=1;
While ($result_year = mysqli_fetch_array($dbquery)){
			$year_ar[$year]=$result_year[ed_year];
			if($_REQUEST['ed_year']==""){
			echo "<option value=$result_year[ed_year]>ปีการศึกษา $result_year[ed_year]</option>"; 
			}
			else{
					if($_REQUEST['ed_year']==$result_year['ed_year']){
					echo "<option value=$result_year[ed_year] selected>ปีการศึกษา $result_year[ed_year]</option>"; 
					}
					else{
					echo "<option value=$result_year[ed_year]>ปีการศึกษา $result_year[ed_year]</option>"; 
					}
			}		
$year++;			
}
	echo "</select>";
	echo "&nbsp;<INPUT TYPE='button' name='smb' value='เลือก' onclick='goto_display(1)' class='entrybutton'>";
echo "</td></tr></table>";
echo "</form>";

$sql_group = "select  * from system_school_group order by code";
$dbquery_group = mysqli_query($connect,$sql_group);
$group_no=1;
While ($result_group = mysqli_fetch_array($dbquery_group)){
$group_code=$result_group['code'];
$group_name=$result_group['name'];

echo "<Table width='95%' Border='0'>";
echo "<Tr><Td  align='left'><b>$group_no กลุ่ม$group_name</b></Td></Tr>";
echo  "</table>";

echo "<Table width='95%' border='1' align='center' style='border-collapse: collapse'>";
echo "<Tr bgcolor='#FFCCCC'><Td  align='center' width='4%'>ที่</Td><Td  align='center' width='17%'>โรงเรียน</Td><Td  align='center' width='7%'>ภาษาไทย</Td><td align='center' width='7%' >คณิตศาสตร์</td><Td align='center'  width='7%'>วิทยาศาสตร์</Td><Td align='center' width='7%' >สังคมศึกษา</Td><Td align='center'  width='7%'>ภาษาอังกฤษ</Td><Td align='center' width='7%' >สุขศึกษา</Td><Td align='center'  width='7%'>ศิลปะ</Td><Td align='center' width='7%' >การงาน</Td><Td align='center' width='7%' >เฉลี่ย</Td><Td align='center' >ผลการประเมิน</Td></Tr>";
  
$sql = "select  * from system_school  where school_group='$group_code' order by school_code ";
$dbquery = mysqli_query($connect,$sql);
$M=1;
$M_thai=0;
$M_math=0;
$M_science=0;
$M_social=0;
$M_english=0;
$M_health=0;
$M_art=0;
$M_vocation=0;
$M_avg=0;

$thai_g=0; $math_g=0; $science_g=0; $social_g=0; $english_g=0; $health_g=0; $art_g=0; $vocation_g=0; $score_avg_g=0;
While ($result_1 = mysqli_fetch_array($dbquery)){
			if($_REQUEST['ed_year']==""){
			$strQuery = "select thai, math, science, social, english, health, art, vocation, score_avg from achievement_main where test_type='1' and test_class='$_REQUEST[class_index]' and ed_year='$year_ar[1]' and school='$result_1[school_code]' ";
			}
			else{
			$strQuery = "select thai, math, science, social, english, health, art, vocation, score_avg from achievement_main where test_type='1' and test_class='$_REQUEST[class_index]' and ed_year='$_REQUEST[ed_year]' and school='$result_1[school_code]' ";
			}
			$result = mysqli_query($connect,$strQuery);
			$ors = mysqli_fetch_array($result);  
			if(($M%2) == 0)
			$color="#CCCCCC";
			else  $color="#FFFFFF";
			
		$thai_g=	$thai_g+$ors['thai'];
		$math_g=$math_g+$ors['math'];
		$science_g=	$science_g+$ors['science'];
		$social_g=	$social_g+$ors['social'];
		$english_g=$english_g+$ors['english'];
		$health_g=	$health_g+$ors['health'];
		$art_g=$art_g+$ors['art'];
		$vocation_g=$vocation_g+$ors['vocation'];
		$score_avg_g=	$score_avg_g+$ors['score_avg'];	
		
		$thai=number_format($ors['thai'],2);
		$math=number_format($ors['math'],2);
		$science=number_format($ors['science'],2);
		$social=number_format($ors['social'],2);
		$english=number_format($ors['english'],2);
		$health=number_format($ors['health'],2);
		$art=number_format($ors['art'],2);
		$vocation=number_format($ors['vocation'],2);
		$score_avg=number_format($ors['score_avg'],2);			
		
		if($thai=="0.00"){
		$thai="";
		}		
		if($math=="0.00"){
		$math="";
		}		
		if($science=="0.00"){
		$science="";
		}		
		if($social=="0.00"){
		$social="";
		}		
		if($english=="0.00"){
		$english="";
		}		
		if($health=="0.00"){
		$health="";
		}				
		if($art=="0.00"){
		$art="";
		}				
		if($vocation=="0.00"){
		$vocation="";
		}				
							
		if($score_avg>0){
		 echo "<Tr bgcolor=$color><Td  align='center' >$M</Td><Td  align='left' >$result_1[school_name]</Td><Td  align='center'>$thai</Td><td align='center' >$math</td><Td align='center'>$science</Td><Td align='center'>$social</Td><Td align='center'>$english</Td><Td align='center' >$health</Td><Td align='center'>$art</Td><Td align='center'>$vocation</Td><Td align='center' >$score_avg</Td><Td align='left' >";
		 //คะแนนจุดตัด
		$a=50;
		$b=30;
		$color[2]="#00CC00";
        echo "<table  border='0' cellspacing='0' cellpadding='0'>";
        echo "<tr><td></td>";
				$score_ceil=$score_avg/2;
				if($score_ceil>50){
				$score_ceil=0;   //error
				}
				for($j=0;$j<=$score_ceil;$j++){
						 		if ($j<=($b/2)){
	  							$cl='#FF0000';
						  		}
						 		else if (($j>($b/2)) and ($j<($a/2))){
						  		$cl='#FFFF00';
						  		}
						   		else if ($j>=($a/2)){
						  		$cl='#00CC00';
						  		}
          	 echo "<td bgcolor='$cl' width='1' >&nbsp;</td>";
			 }
          	echo "</tr>";
         	echo "</table>";
	echo "</Td></Tr>";
	$M++;	 
			if($ors['thai']!=0){
			$M_thai++;
			}
			if($ors['math']!=0){
			$M_math++;
			}
			if($ors['science']!=0){
			$M_science++;
			}
			if($ors['social']!=0){
			$M_social++;
			}
			if($ors['english']!=0){
			$M_english++;
			}
			if($ors['health']!=0){
			$M_health++;
			}
			if($ors['art']!=0){
			$M_art++;
			}
			if($ors['vocation']!=0){
			$M_vocation++;
			}
			if($ors['score_avg']!=0){
			$M_avg++;
			}
	 }
}	 
		if($M_thai!=0){	 
		$thai_g=	$thai_g/$M_thai;
		}
		if($M_math!=0){	
		$math_g=$math_g/$M_math;
		}
		if($M_science!=0){	
		$science_g=	$science_g/$M_science;
		}
		if($M_social!=0){	
		$social_g=	$social_g/$M_social;
		}
		if($M_english!=0){	
		$english_g=$english_g/$M_english;
		}
		if($M_health!=0){	
		$health_g=	$health_g/$M_health;
		}
		if($M_art!=0){	
		$art_g=$art_g/$M_art;
		}
		if($M_vocation!=0){	
		$vocation_g=$vocation_g/$M_vocation;
		}
		if($M_avg!=0){	
		$score_avg_g=	$score_avg_g/$M_avg;
		}
		
		$thai_g=number_format($thai_g,2);
		$math_g=number_format($math_g,2);
		$science_g=number_format($science_g,2);
		$social_g=number_format($social_g,2);
		$english_g=number_format($english_g,2);
		$health_g=	number_format($health_g,2);
		$art_g=number_format($art_g,2);
		$vocation_g=number_format($vocation_g,2);
		$score_avg_g=	number_format($score_avg_g,2);
		
		if($thai_g=="0.00"){
		$thai_g="";
		}		
		if($math_g=="0.00"){
		$math_g="";
		}		
		if($science_g=="0.00"){
		$science_g="";
		}		
		if($social_g=="0.00"){
		$social_g="";
		}		
		if($english_g=="0.00"){
		$english_g="";
		}		
		if($health_g=="0.00"){
		$health_g="";
		}				
		if($art_g=="0.00"){
		$art_g="";
		}				
		if($vocation_g=="0.00"){
		$vocation_g="";
		}				
		if($score_avg_g>0){
		 echo "<Tr bgcolor='#FFFFCC'><td></td><td align='center'>เฉลี่ยกลุ่ม</td><Td  align='center'>$thai_g</Td><td align='center'>$math_g</td><Td align='center'>$science_g</Td><Td align='center'>$social_g</Td><Td align='center'>$english_g</Td><Td align='center' >$health_g</Td><Td align='center'>$art_g</Td><Td align='center'>$vocation_g</Td><Td align='center' >$score_avg_g</Td><Td align='left'>";
		 
        echo "<table  border='0' cellspacing='0' cellpadding='0'>";
        echo "<tr><td></td>";
				$score_ceil=$score_avg_g/2;
				if($score_ceil>50){
				$score_ceil=0;   //error
				}
				for($j=0;$j<=$score_ceil;$j++){
						 		if ($j<=($b/2)){
	  							$cl='#FF0000';
						  		}
						 		else if (($j>($b/2)) and ($j<($a/2))){
						  		$cl='#FFFF00';
						  		}
						   		else if ($j>=($a/2)){
						  		$cl='#00CC00';
						  		}
          	 echo "<td bgcolor='$cl' width='1' >&nbsp;</td>";
			 }
          	echo "</tr>";
         	echo "</table>";
		 
		 echo "</td></tr>";
		}
echo "</Table>";		
echo "<br>";
$group_no++;
} //group
echo "<br>";
echo "<Table width='95%' Border='0'>";
echo "<Tr><Td  align='left'><b>หมายเหตุ </b>ค่าเฉลี่ยกลุ่มเป็นแบบไม่ถ่วงน้ำหนักจำนวนนักเรียนของแต่ละโรงเรียน</Td></Tr>";
echo  "</table>";
?>
</CENTER>

<script>
function goto_display(val){
	if(val==1){
		callfrm("?option=achievement&task=main/report1_2"); 
		}
}

</script>

