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

$flag = $db->getVal("select  teacher_sub_control from tbl_info where id='1'");
$class = $_REQUEST['class'];
$teacher_id = mysql_real_escape_string(($_REQUEST['teacher_id']));
$type = mysql_real_escape_string(($_REQUEST['type']));

$session = $_REQUEST['session'];
$class = mysql_real_escape_string($class);
$session = mysql_real_escape_string($session);

$section = $_REQUEST['section'];

if ($flag == '2') {

    //////////////////////////////////////// Old Query ///////////////////////////////////////////////////////////


    /*
    $result = $db->getRows("select id,subject from subject_mp where id in (select cls_sub_id from teacher_subject_mp where teacher_id='$teacher_id' and session='$session') and class='$class' and session='$session'");

    if ($result) {
        foreach ($result as $row) {

            $total = $db->getVal("select total from examtype where  class='$class' and type='$type' and subject='" . $row['subject'] . "' ");

            $jsonn[] = array("id" => $row['id'], "subject" => $row['subject'], "max_mark" => $total);
            $json['status'] = 1;
            $json['msg'] = "successful";
            $json['data'] = $jsonn;

        }
        echo json_encode($json);
    } else {
        echo json_encode(array('status' => 0, 'msg' => 'detail not found..!'));
    }

    */


    /////////////////////////////////   Updated Query   //////////////////////////////////////////

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

    $result =$db->getRows($qry);
    $temp = array();
    foreach ($result as $row_res) {
        $row_subject = $row_res['subject'];
        $section_array = json_decode($row_res['section']);
        $total = $db->getVal("select total from examtype where  class='$class' and type='$type' and subject='" . $row_subject . "' ");

        if($total==null){$total=0;}

        if ( (empty($section) && empty($section_array)) || (in_array($section, $section_array)) ) {
            array_push($temp, array("id" => $row_res['sub_id'], "subject" =>$row_subject, "max_mark" => $total));
        }

    }

    if ($result) {

        $json['status'] = 1;
        $json['msg'] = "successful";
        $json['data'] = $temp;

        echo json_encode($json);
    } else {

        echo json_encode(array('status' => 0, 'msg' => 'detail not found..!'));
    }






}

if ($flag == '1') {

    $result = $db->getRows("select id,subject from subject_mp where   class='$class' and session='$session'");

    if ($result) {
        foreach ($result as $row) {

            $total = $db->getVal("select total from examtype where  class='$class' and type='$type' and subject='" . $row['subject'] . "' ");

            if($total==null){$total=0;}

            $jsonn[] = array("id" => $row['id'], "subject" => $row['subject'], "max_mark" => $total);
            $json['status'] = 1;
            $json['msg'] = "successful";
            $json['data'] = $jsonn;

        }
        echo json_encode($json);
    } else {
        echo json_encode(array('status' => 0, 'msg' => 'detail not found..!'));
    }


}

?>