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


$session = "";
$class = "";

if (isset($_REQUEST['session'])) {
    $session = $_REQUEST['session'];
}

if (isset($_REQUEST['class'])) {
    $class = $_REQUEST['class'];
}


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

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


$examTerms = array();
$examRes = $db->getRows("select id,term_name,term_type from exam_term_result where status = '1' AND term_type = '$resPattern'");
if (count($examRes) > 0) {
    $examTerms = $examRes;
}


if ($att_cl_ctr == 1) {

    $arr = $db->getRows("select class from class");
    $arr_sec = $db->getRows("select name from section");


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

    // $result1=mysql_query($sqlsec);
    $jsonc = array();
    // 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']);

    }


    $cri = $db->getRows("select * from criteria where school_id='1' group by type");

    // $result_criteria = mysql_query($cri);
    // while ($rowp = mysql_fetch_assoc($result_criteria))
    foreach ($cri as $rowp) {
        $jsonp[] = $rowp['type'];

    }

    $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("res_pattern" => $att_cl_ctr, "class" => $jsonn, "section" => $jsonc, "exam_term" => $jsond, "criteria" => $jsonp);
        //	$json['section']=$jsonc;
        $json['term'] = $examTerms;

    }
    $exam_name = array("Term 1","Term 2");
    $ee = $db->getRows("SELECT name FROM examname");
        foreach ($ee as $item_x) {
            array_push($exam_name, $item_x['name']);
        }

    $json['ex_names'] = $exam_name;

    ////

    $examTerms = array();
    $examRes = $db->getRows("select id,name AS term_name, 0 AS term_type from examname where app_visibilty = '1'");

    if ($examRes && count($examRes) > 0){
        $examTerms = $examRes;
    }

    $termArray = array("Select Term");

    $termsList = $db->getRows("SELECT DISTINCT(term) FROM term_permission WHERE result_view = '1' AND session = '$session' ORDER BY id ASC ");
    foreach ($termsList as $item) {
        $termArray[] = $item['term'];
    }

    if (in_array('Term2', $termArray)){
        $termArray[] = "Final";
    }

    $json['exam_wise'] = $examTerms;
    $json['term_wise'] = $termArray;


    ///


    echo json_encode($json);
} elseif ($att_cl_ctr == 2) {
    $sqlsec = $db->getRows("select  type,total from examtype group by type order by id ");
    // $result_term = mysql_query($sqlsec);
    // while ($rowt = mysql_fetch_assoc($result_term))
    foreach ($sqlsec as $rowt) {

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


        $jsond[] = array("exam_name" => $rowt['type'], "max_mark" => $rowt['total']);


    }


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

    // $result1 = mysql_query($sqlsec);
    // while ($rowc = mysql_fetch_assoc($result1))
    foreach ($sqlsec as $rowc) {

        $jsonc[] = $rowc['name'];

    }

    $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("res_pattern" => $att_cl_ctr, "class" => $jsonn, "section" => $jsonc, "exam_term" => $jsond);
        //	$json['section']=$jsonc;
        $json['term'] = $examTerms;

    }
    echo json_encode($json);
}