<?php
error_reporting(0);
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );

$class_ar[6]="ชั้นประถมศึกษาปีที่ 6";
$class_ar[9]="ชั้นมัธยมศึกษาปีที่ 3";
$class_ar[12]="ชั้นมัธยมศึกษาปีที่ 6";


if(!(isset($_POST['urlGET']))){
echo "<br />";
echo "<form method='post' action='' id='frm1' name='frm1'>";
echo "<table align='center'>";
echo "<tr>";
echo "<td>";
$sql = "select * from system_sync_smss left join system_school on system_sync_smss.office_code=system_school.school_code order by system_school.school_type,system_school.school_code";
$dbquery = mysqli_query($connect,$sql);
$ROW=1;
$COLUMN=1;
echo "<fieldset>";
echo "<legend>&nbsp;<B>เลือกสถานศึกษา</B>: &nbsp;</legend>";
echo "<table>";
While ($result = mysqli_fetch_array($dbquery)){
	if($ROW==1){
	echo "<tr>";
	}
	echo "<td><input type='checkbox' name='chk' onclick='Check()'>$result[school_code] $result[school_name]<input type='hidden' name='urlGET[]'  value='$result[url]/export/xml.php?username=$result[requester_username]&password=$result[sync_code]&smss_url=$result[url]&order=onet&school_code=$result[school_code]' disabled></td>";
	if($COLUMN==4){
	echo "</tr>";
	$COLUMN=0;
	$ROW=0;
	}
	$ROW++;
	$COLUMN++;
}
echo "</table>";
echo "</fieldset>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<br />";
echo "<div align='center'><input type='submit'></div>";
echo "</form>";
}


if(isset($_POST['urlGET'])){
$urlGET=$_POST['urlGET'];

foreach ($urlGET as &$value) {
   		$url = $value;
		$xmlDoc = new DOMDocument();
		$xmlDoc->load($url);
		$announce = $xmlDoc->getElementsByTagName('item');
		$announce_class = $xmlDoc->getElementsByTagName('test_class');
		
		$warning=base64_decode($xmlDoc->getElementsByTagName('warning')->item(0)->nodeValue);
		if($warning!="1"){
		echo "<br>";
		echo "<div align='center'>";
		echo $warning;
		echo "</div>";
		}
		
		//ตรวจสอบการเข้าถุึงเว็บsmss
		$smss_fond="0";
		if($warning!=""){
		$smss_fond="1";
		}
		if($smss_fond=="0"){
		$smss_url = explode("?",$url);
		echo "<br>";
		echo "<div align='center'>";
		echo "ไม่สามารถเข้าถึง".$smss_url[0];
		echo "</div>";
		}
		
if($warning=="1" and $smss_fond=="1"){
echo "<br />";		
echo "<table align='center' width='65%' border='0'>";
echo "<tr><td align='center'><b>ผลการสอบระดับชาติ O-NET " ;
		echo base64_decode($xmlDoc->getElementsByTagName('school_name')->item(0)->nodeValue);
		echo " ";
		echo base64_decode($xmlDoc->getElementsByTagName('office_code')->item(0)->nodeValue);
echo "</b></td></tr>";
echo "</table>";


echo "<table align='center' width='90%' border='1' borderColor='#FF0033' style='border-collapse: collapse'>";
  echo "<Tr align='center' bgcolor='#FBD562'><Td width='10%'>ปีการศึกษา</Td><Td width='7%'>ภาษาไทย</Td><td width='7%'>คณิตศาสตร์</td><Td width='7%'>วิทยาศาสตร์</Td><Td width='7%'>สังคมศึกษา</Td><Td width='7%'>ภาษาอังกฤษ</Td><Td width='7%'>สุขศึกษา</Td><Td width='7%'>ศิลปะ</Td><Td width='7%'>การงาน</Td><Td width='7%'>เฉลี่ย</Td><Td>การประเมิน</Td></Tr>";

foreach ($announce_class as $ann_class){

$class_item=base64_decode($ann_class->getElementsByTagName('class_item')->item(0)->nodeValue);

echo "<tr><td colspan='11' align='left'>&nbsp;<b>$class_ar[$class_item]</b></td></tr>";


		$i=1;
		foreach ($announce as $ann)
		{
		if(($i%2) == 0)
		$color="#CCFFFF";
		else $color="#FFFFFF";
		$class=base64_decode($ann->getElementsByTagName('class')->item(0)->nodeValue);
		$ed_year=base64_decode($ann->getElementsByTagName('ed_year')->item(0)->nodeValue);
		$thai=base64_decode($ann->getElementsByTagName('thai')->item(0)->nodeValue);
		$math=base64_decode($ann->getElementsByTagName('math')->item(0)->nodeValue);
		$science=base64_decode($ann->getElementsByTagName('science')->item(0)->nodeValue);
		$social=base64_decode($ann->getElementsByTagName('social')->item(0)->nodeValue);
		$english=base64_decode($ann->getElementsByTagName('english')->item(0)->nodeValue);
		$health=base64_decode($ann->getElementsByTagName('health')->item(0)->nodeValue);
		$art=base64_decode($ann->getElementsByTagName('art')->item(0)->nodeValue);
		$vocation=base64_decode($ann->getElementsByTagName('vocation')->item(0)->nodeValue);
		$score_avg=base64_decode($ann->getElementsByTagName('score_avg')->item(0)->nodeValue);		
		
		if($class==$class_item){
		echo "<tr bgcolor=$color align='left'>";
			echo "<td align='center'>$ed_year</td>";
			echo "<td align='center'>$thai</td>";
			echo "<td align='center'>$math</td>";
			echo "<td align='center'>$science</td>";
			echo "<td align='center'>$social</td>";
			echo "<td align='center'>$english</td>";
			echo "<td align='center'>$health</td>";
			echo "<td align='center'>$art</td>";
			echo "<td align='center'>$vocation</td>";
			echo "<td align='center'>$score_avg</td>";
			echo "<td align='left'>";
			
			 //คะแนนจุดตัด
		$a=60;
		$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>";

		echo "</tr>";
		$i++;
		}
		} //if $class==$class_item
	}//foreach $announce_class	
		echo "<tr><td colspan='11' align='right'>น้อยกว่าหรือเท่ากับ 30% สีแดง มากกว่า 30% สีเหลือง มากกว่าหรือเท่ากับ 60% สีเขียว</td></tr>";
	echo "</table>";
	echo "<br>";
	}
} //if not warning	
unset($value); // break the reference with the last element
}
?>
<script>
function Check() {
		var x = document.getElementsByName("chk");
		var y = document.getElementsByName("urlGET[]");
		var i;
		for (i = 0; i < x.length; i++) {
			if (x[i].type == "checkbox") {
				if(x[i].checked == true){
				y[i].disabled=false;
				}
				if(x[i].checked == false){
				y[i].disabled=true;
				}
			}
		}	
}
</script>
