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

$chapter = $_REQUEST['chapter_id'];
$uid = $_REQUEST['uid'];


//in minute
$time_frame = 240;

$array = array();
$status = 0;
$objectiveArray = array();
$subjectiveArray = array();

$student = $db->getVal("select session from student where idno='$uid'");

$objectivepapers = $db->getRows("select * from assignment_paper where chapter='$chapter' and paper_type=1");
if(count($objectivepapers) > 0){
    $status =1;
    foreach($objectivepapers as $objective){

        $allquestions = $db->getRows("select * from assignment_questions where paper_id='$objective[id]' ");

        $tq = count($allquestions);
        $marks = $objective['per_que_marks']*$tq;
        $time = $objective['duration'];
        $level = $objective['level'];
        $description = $objective['paper_description'];
        $paper_status = $objective['paper_status'];
        $livedate = $objective['livedate'];
        $livetime = $objective['livetime'];

        $checkattempt = $db->getRow("select * from assignment_attempt_paper where pid='$objective[id]' and uid='$uid' ");
        if(count($checkattempt)>1){
            $flg = 1;
        }
        else {
            $flg = 0;
        }

        if($objective['session']==$student) {

            $allow = true;

            /*if ($livedate != '0000-00-00'){
                $combinedDT = date('Y-m-d H:i:s', strtotime("$livedate $livetime"));

                $curr_seconds = round(microtime(true));
                $stamp = strtotime($combinedDT)+($time_frame * 60);

                if ($curr_seconds > $stamp){
                    $allow = false;
                }

            }*/

            if ($allow){
                array_push($objectiveArray, ["flag" => $flg, "pid" => "$objective[id]", "totalque" => "$tq", "per_que_marks" => "$objective[per_que_marks]",
                    "totalmarks" => "$marks", "time" => "$time", "level" => "$level", "description" => "$description", "status" => "$paper_status", "livedate" => "$livedate", "livetime" => "$livetime"]);
            }

        }
    }
}

$subjectivepapers = $db->getRows("select * from assignment_paper where chapter='$chapter' and paper_type=2 and paper_status='Active'");
if(count($subjectivepapers) > 0){
    $status =1;
    foreach($subjectivepapers as $subjective){

        $allquestions = $db->getRows("select * from assignment_questions where paper_id='$subjective[id]' ");

        $tq = count($allquestions);
        $marks = $subjective['per_que_marks'];//*$tq;
        $time = $subjective['duration'];
        $level = $subjective['level'];
        $description = $subjective['paper_description'];
        $paper_status = $subjective['paper_status'];
        $livedate = $subjective['livedate'];
        $livetime = $subjective['livetime'];

        $checkattempt = $db->getRow("select * from assignment_attempt_paper where pid='$subjective[id]' and uid='$uid' ");
        if(count($checkattempt)>1){
            $flg = 1;
        }
        else {
            $flg = 0;
        }
        if($subjective['session']==$student) {

            $allow_sub = true;
            /*
            //not working check logic
            if ($livedate != '0000-00-00'){
                $combinedDT = date('Y-m-d H:i:s', strtotime("$livedate $livetime"));

                $curr_seconds = round(microtime(true));
                $stamp = strtotime($combinedDT)+($time_frame * 60);

                if ($curr_seconds > $stamp){
                    $allow = false;
                }

            }*/

            if ($allow_sub) {
                array_push($subjectiveArray, ["flag" => $flg, "pid" => "$subjective[id]", "totalque" => "$tq", "per_que_marks" => "$subjective[per_que_marks]", "totalmarks" => "$marks", "time" => "$time", "level" => "$level", "description" => "$description", "status" => "$paper_status", "livedate" => "$livedate", "livetime" => "$livetime"]);
            }
        }
    }
}


$array['status'] = $status;

$array['objective'] = $objectiveArray;
$array['subjective'] = $subjectiveArray;

$array['time_delay'] = $time_frame;


////


$tblinfo = $db->getRow("select due_month, resultview from tbl_info");

$duemonth = $tblinfo['due_month'];

$monthsList1 = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6 => 'June',
    7 => 'July ', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');

$conv = array(1=>10,2=>11,3=>12,4=>1,5=>2,6=>3,7=>4,8=>5,9=>6,10=>7,11=>8,12=>9);

$fStudent = $db->getRow("select * from student where idno='$uid' ");

$_REQUEST['due_month'] = $conv[$duemonth];
$_REQUEST['class'] = $fStudent['class'];
$_REQUEST['section'] = $fStudent['section'];
$_REQUEST['session'] = $fStudent['session'];

$total_comp = include('../student/fee_calculate.php');

$resultview = $tblinfo['resultview'];
$msg = "";
if ($resultview == 1) {
    $viewresult = 1;
} elseif ($resultview == 2) {
    if ($total_comp > 0) {
        $viewresult = 0;
        $msg = "Please clear your dues till $monthsList1[$duemonth]";
    } else {
        $viewresult = 1;
    }
} elseif ($resultview == 3) {
    if ($total_comp > 0) {
        if ($fStudent['view_result'] == 1) {
            $viewresult = 1;
        } else {
            $viewresult = 0;
            $msg = "Please clear your dues till $monthsList1[$duemonth]";
        }
    } else {
        $viewresult = 1;
    }
} elseif ($resultview == 4) {
    if ($fStudent['view_result'] == 1) {
        $viewresult = 1;
    } else {
        $viewresult = 0;
        $msg = "Result is not available now";
    }
} else {
    $viewresult = 0;
    $msg = "Result is not available now";
}


////

$array['show_result'] = $viewresult;
$array['due'] = $total_comp;
$array['msg'] = $msg;

$array['server_time'] =  round(microtime(true) * 1000);

echo json_encode($array);