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

$id = $_REQUEST['id'];

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

if ($att_cl_ctr == '2') {

    $section_array = array();

    $sqlsec = $db->getRows("select distinct(section) from Homework_role where teacher_id='$id'");
    // $result1 = mysql_query($sqlsec);
    // while ($rowc = mysql_fetch_assoc($result1)) 
    foreach($sqlsec as $rowc)
    {

        //$jsonc[] = $rowc['section'];
        $section_name = $rowc["section"];
        if (strlen($section_name)>0) {
            array_push($section_array, $section_name);
        }

    }

    $sql = $db->getRows("select distinct(class) from Homework_role where teacher_id='$id'");
    // $result = mysql_query($sql);
    // while ($row = mysql_fetch_assoc($result)) 
    foreach($sql as $row)
    {

        $jsonn[] = $row['class'];
        $json['status'] = 1;
        $json['msg'] = "successful";
        $json['data'] = array("class" => $jsonn, "section" => $section_array, "att_cl_ctr" => $att_cl_ctr);

    }

}

if ($att_cl_ctr == '1') {

    $sqlsec = $db->getRows("select name from section");

    // $result1 = mysql_query($sqlsec);
    // while ($rowc = mysql_fetch_assoc($result1)) 
    foreach($sqlsec as $rowc)
    {
        $jsonc[] = $rowc['name'];
    }

    $sqlsec = $db->getRows("select name,total from examterm");
    // $result_term = mysql_query($sqlsec);
    // while ($rowt = mysql_fetch_assoc($result_term)) 
    foreach($sqlsec as $rowt)
    {
        $jsond[] = array("exam_name" => $rowt['name'], "max_mark" => $rowt['total']);

    }

    $sql = $db->getRows("select class from class");

    // $result = mysql_query($sql);
    // while ($row = mysql_fetch_assoc($result)) 
    foreach($sql as $row)
    {
        $jsonn[] = $row['class'];
        $json['status'] = 1;
        $json['msg'] = "successful";
        $json['data'] = array("class" => $jsonn, "section" => $jsonc, "exam_term" => $jsond, "att_cl_ctr" => $att_cl_ctr);

    }

}

//get class section mapping
$sql_qry = $db->getRows("select id,class,section from Homework_role where teacher_id='$id'");
// $res_qry = mysql_query($sql_qry);
$class_sec_arr = array();
// while ($row_data = mysql_fetch_assoc($res_qry)) 
foreach($sql_qry as $row_data)
{
    if (strlen($row_data["section"])>0) {
        array_push($class_sec_arr, $row_data);
    }
}

$json["class_sec_mapping"]=$class_sec_arr;


if (empty($json)) {
    $json['status'] = 0;
    $json['msg'] = "data not found";
}

echo json_encode($json);
?>