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


//error_reporting(E_ALL);
//ini_set('display_errors', 1);


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

$teacher_id = $_REQUEST['teacher_id'];

$session = mysql_real_escape_string($session);

$tableinfo = $db->getRow("select * from tbl_info where id='1'");
$teacher_control = $tableinfo["teacher_sub_control"];
$result_pattern = $tableinfo["result_pattern"];

$json = array("status" => 0, "msg" => "Unable to get data");

$subject_table_name = "";
$exam_mark_table_name = "";
$teacher_subject_table = "";
if ($result_pattern == '1') {
    $subject_table_name = "subject";
    $exam_mark_table_name = "exammarks";
    $teacher_subject_table = "teacher_subject";
} elseif ($result_pattern == '2') {
    $subject_table_name = "subject_mp";
    $exam_mark_table_name = "exammarks_mp";
    $teacher_subject_table = "teacher_subject_mp";
}

$sub_assigned_array = array();
if ($teacher_control == '2') {


    $qry = "SELECT ".$subject_table_name.".subject, ".$teacher_subject_table.".section FROM ".$teacher_subject_table." INNER JOIN ".$subject_table_name." ON ".$teacher_subject_table.".cls_sub_id = ".$subject_table_name.".id 
                AND ".$teacher_subject_table.".session = ".$subject_table_name.".session WHERE (".$teacher_subject_table.".teacher_id = '$teacher_id' 
                AND ".$teacher_subject_table.".session = '$session' AND ".$teacher_subject_table.".class = '$class') ORDER BY ".$subject_table_name.".id";

    $result = $db->getRows($qry);
    $temp = array();
    foreach ($result as $row_res) {
        $row_subject = $row_res['subject'];
        $section_array = json_decode($row_res['section']);
        if (empty($section) && empty($section_array)) {
            array_push($temp, $row_subject);
        } else if (in_array($section, $section_array)) {
            array_push($temp, $row_subject);
        }
    }

}

$conwhere = "";
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'");
$sqlsec = "select idno, name,fincome,fname,admissionno,photo 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)) {

    $d = array();

    $photo = "";
    if ($rowc["photo"] != null) {
        $photo = "media/photo/" . $rowc["photo"];
    }

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

    $gradeD = $db->getRows("select * from " . $subject_table_name . " where class='$class'  and school_id='1' and session='$session' order by id");
    //echo $ss="select * from subject where class='$class'  and school_id='1' and session='$session' order by id'";

    $subTotalObtained = 0;
    $grandTotal = 0;
    foreach ($gradeD as $sub) {
        if ($teacher_control == '1' || ($teacher_control == '2' && in_array($sub["subject"], $temp))) {

            $examResultData = $db->getRow("select * from " . $exam_mark_table_name . " where school_id='1' and idno='" . $rowc["idno"] . "' and type='$term' and subject='" . $sub["subject"] . "'");
            if (count($examResultData) > 0) {
                array_push($d, array("name" => $examResultData["subject"], "obtain" => $examResultData["obtain"], "total" => $examResultData["total"]));
                if (is_numeric($examResultData["obtain"])) {
                    $subTotalObtained += $examResultData["obtain"];
                }
                //max mark or total doesn't require numeric check coz db is int type check null only
                if ($examResultData["total"] != null) {
                    $grandTotal += $examResultData["total"];
                }
            } else {
                //student record not found so make obtain 0 and max mark of subject
                $subject_name = $sub["subject"];
                if ($result_pattern == '1') {
                    //cbse pattern
                    $exam_total_cbse = $db->getRow("select total from examterm where name='$term'");
                    array_push($d, array("name" => $subject_name, "obtain" => "0", "total" => $exam_total_cbse["total"]));
                    if ($exam_total_cbse["total"] != null) {
                        $grandTotal += $exam_total_cbse["total"];
                    }
                } elseif ($result_pattern == '2') {
                    //mp pattern
                    $exam_total_mark = 0;
                    $exam_total_mp = $db->getRow("select total from examtype where class='$class' and subject='$subject_name' and type='$term' and session='$session'");

                    if ($exam_total_mp["total"] != null) {
                        $grandTotal += $exam_total_mp["total"];
                        $exam_total_mark = $exam_total_mp["total"];
                    }

                    array_push($d, array("name" => $subject_name, "obtain" => "0", "total" => $exam_total_mark));
                }
            }

        }
    }

    //$percentage = ($subTotalObtained / $grandTotal) * 100;

    $jsonc["sub_total_obt"] = $subTotalObtained;
    $jsonc["grand_total"] = $grandTotal;
    //$jsonc["percentage"] = number_format($percentage, 2) . "%";
    $jsonc["marks"] = $d;
    array_push($jsons, $jsonc);

}
$json['status'] = 1;
$json['msg'] = "Profile found Successfully..!";
$json['data'] = $jsons;

echo json_encode($json);
?>
