<?php
include("../../db.php");
header('Content-Type: application/json');

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

$class_name = $_REQUEST['class'];
$uid = $_REQUEST['uid'];
$session = $_REQUEST['session'];
$viewresult = 0;

$objectivepapers = $db->getRows("select * from assignment_paper where class='$class_name' 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;
        }

        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 class='$class_name' 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;
        }

        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"]);


    }
}


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

$tblinfo = $db->getRow("select exam_due_month, exam_resultview,take_exam_time_window from tbl_info");

$duemonth = $tblinfo['exam_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);

$_REQUEST['due_month'] = $conv[$duemonth];

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

//$resultview = $tblinfo['resultview'];
$resultview = $tblinfo['exam_resultview'];


$fStudent = $db->getRow("select * from student where idno='$uid' and  school_id='1' and session='$session' and status=1 and issue_TC=0");


if ($resultview == 1) {
    $viewresult = 1;
} elseif ($resultview == 2) {
    if ($total_comp > 0) {
        $viewresult = 0;
    } else {
        $viewresult = 1;
    }
} elseif ($resultview == 3) {
    if ($total_comp > 0) {
        if ($fStudent['view_result'] == 1) {
            $viewresult = 1;
        } else {
            $viewresult = 0;
        }
    } else {
        $viewresult = 1;
    }
} elseif ($resultview == 4) {
    if ($fStudent['view_result'] == 1) {
        $viewresult = 1;
    } else {
        $viewresult = 0;
    }
} else {
    $viewresult = 0;
}


$array['status'] = 1;
$array['due'] = $total_comp;
$array['viewresult'] = $viewresult;
$array['resultsetting'] = $resultview;
$array['month'] = $monthsList1[$duemonth];
$array['objective'] = $objectiveArray;

$array['subjective'] = $subjectiveArray;

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

echo json_encode($array);