<?php
include("../../db.php");

header('Content-Type: application/json');

$class = $_REQUEST['class'];
$section = $_REQUEST['section'];
$term = $_REQUEST['term'];
$session = $_REQUEST['session'];
$criteria = $_REQUEST['criteria'];

$conwhere = "";

if (isset($_REQUEST['section']) && trim($_REQUEST['section']) != "") {
    $conwhere .= " and section='" . $_REQUEST['section'] . "' ";
}

$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");

$jsons = array();

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'");

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

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

    }

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

}

$criteriaArray = $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("student_grade" => $jsons, "criteria" => $criteriaArray, "grade_indicator" => $gradeNameArr);

echo json_encode($json);