<?php
include("../../db.php");
//header("Access-Control-Allow-Origin: * ");

header('Content-Type: application/json');
$class = $_REQUEST['class'];
$section = $_REQUEST['section'];
$term = $_REQUEST['term'];

$session = $_REQUEST['session'];

$criteria = $_REQUEST['criteria'];

// 	 $session=mysql_real_escape_string($session);

if (isset($_REQUEST['section']) && trim($_REQUEST['section']) != "") {

    $conwhere .= " and section='" . $_REQUEST['section'] . "' ";
}


//$allsubject=$db->getRows("select subject from subject where class='$class' and session='$session'");

$allstudent = $db->getRows("select * from criteria where type='" . $criteria . "' and school_id='1'");

$jsonp = array();
$i = 0;

foreach ($allstudent as $stu) {
    $i++;


    $a = array("cr_id" => $stu['id'], "name" => $stu['name']);
    $b = array();


    $gradeData = $db->getRows("select * from grade where school_id='1' and session='$session' and criteria='" . $criteria . "' and sub_criteria='" . $stu['name'] . "'");
//echo $ss="select * from grade where school_id='1' and session='$session' and criteria='".$criteria."' and sub_criteria='".$stu['name']."'";


    foreach ($gradeData as $g) {


        array_push($b, $g['grade']);

    }


    if (count($b) > 0) {
        $a["grade_indicator"] = $b;
        array_push($jsonp, $a);
    }


}


$sqlsec = $db->getRows("select idno, name,fincome,fname,admissionno from student where session='$session' and class='$class' $conwhere and  status=1  and issue_TC=0 order by name");

// 	$result1 = mysql_query($sqlsec);
$jsons = array();
//   while ($rowc = mysql_fetch_assoc($result1))
foreach ($sqlsec as $rowc) {

    $d = array();

    $jsonc = array("idno" => $rowc['idno'], "name" => $rowc['name'], "rollno" => $rowc['fincome'], "fname" => $rowc['fname'], "admissionno" => $rowc['admissionno']);

    $gradeD = $db->getRows("select * from examcriteriarecord where school_id='1' and idno=" . $rowc["idno"] . "  and criteria='$criteria' and session='$session' and term='$term'");
//echo $ss="select * from examcriteriarecord where school_id='1' and idno=".$rowc["idno"]."  and criteria='$criteria' and session='$session' and term='$term'";


    foreach ($gradeD as $aw) {
        $id = $db->getVal("select id from criteria where type='" . $criteria . "' and name='" . $aw['subcriteria'] . "' "." and class = '$class'");

        if($id != null || $id != ''){
                    array_push($d, array("id" => $id, "gradename" => $aw['subcriteria'], "grade" => $aw['grade']));
        }        

    }

    $jsonc["grade_asign"] = $d;
    array_push($jsons, $jsonc);

}

$crName = array();
$datax = $db->getRows("SELECT id,type,name,class FROM criteria WHERE type = '$criteria' AND class = '$class' AND session = '$session'");

$gradeNameArr = array();
$gradeName = $db->getRows("SELECT grade FROM grade WHERE session = '$session'");
foreach ($gradeName as $item_demo) {
    $gradeNameArr[] = $item_demo['grade'];
}

$json['status'] = 1;
$json['msg'] = "Profile found Successfully..!";
$json['data'] = array("criteria" => $jsonp, "student_grade" => $jsons);

$json['criteria_name'] = $datax;
$json['grade_indicator_new'] = $gradeNameArr;

echo json_encode($json);
?>
