<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');

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

$section = $_REQUEST['section'];

$type = $_REQUEST['type'];

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

$teacher_control = $db->getVal("select teacher_sub_control from tbl_info where id='1'");

if ($teacher_control == '1') {

    $whereSec = "";
    if (!empty($section)) {
        $whereSec = " and section='$section'";
    }

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

    // if (count($result) == 0) {
    //     $result = $db->getRows("select id,subject, section from subject where  class='$class' and session='$session'");
    // }

    $checksection = $db->getVal("select count(section) from subject where class='$class' and section!='' and session='$session' ");
    $wheresection = '';
    if ($checksection > 0) {
        $wheresection = " and section='$section' ";
    }

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

        foreach ($result as $row) {

            $total = $db->getVal("select total from examterm where class='$class' and section='$row[section]' and subject='$row[subject]' and session='$session' and name = '$type' ");

            $jsonn[] = array("subject" => $row['subject'], "total" => $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..!'));
    }

}
if ($teacher_control == '2') {
    //$result =$db->getRows("select id,subject from subject where id in (select cls_sub_id from teacher_subject where teacher_id='$teacher_id' and  session='$session') and class='$class' and session='$session'");

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

    $result = $db->getRows($qry);
    $temp = array();
    foreach ($result as $row_res) {

        $section_array = json_decode($row_res['section']);

        $total = $db->getVal("select total from examterm where class='$class' and subject='$row_res[subject]' and session='$session' and name = '$type' ");

        //$row_subject = $row_res['subject'];

        $row_subject = array("subject" => $row_res['subject'], "total" => $total);

        if (empty($section) && empty($section_array)) {
            array_push($temp, $row_subject);
        } else if (in_array($section, $section_array)) {
            array_push($temp, $row_subject);
        }
    }

    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..!'));
    }


}
