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

$teacher_id = "";

if (isset($_GET['tid'])){
    $teacher_id = $_GET['tid'];
}

$whereCase = "";
if ($teacher_id != null && $teacher_id != '') {
    // $whereCase = " AND added_by='$teacher_id'";
    $whereCase = " WHERE added_by='$teacher_id'";
}
$whereCase .= " ORDER BY id DESC";

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

//    $objectivepapers = $db->getRows("select * from assignment_paper where paper_type=1".$whereCase);
$objectivepapers = $db->getRows("select * from assignment_paper" . $whereCase);

if (count($objectivepapers) > 0) {
    $status = 1;

    $papType = "";

    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'];
        $chapter_id = $objective['chapter'];

        $class_name = $objective['class'];

        $subject_id = $objective['parent_subject'];
        $subject_name = $db->getVal("SELECT subject FROM subject_new WHERE id ='$subject_id' AND type = 'subject'");

        $sub_subject_id = $objective['sub_subject'];
        $sub_subject_name = $db->getVal("SELECT subject FROM subject_new WHERE id ='$sub_subject_id' AND type = 'sub_subject'");

        $chapter_name = $db->getVal("SELECT subject FROM subject_new WHERE id ='$chapter_id' AND type = 'chapter'");

        $paper_type = $objective['paper_type'];
        if ($paper_type == 1) {
            $papType = "Objective";
        } else if ($paper_type == 2) {
            $papType = "Subjective";
            $marks = $objective['per_que_marks'];
        }

        array_push($objectiveArray, ["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", "chapter_id" => "$chapter_id",
            "class_name" => $class_name, "subject_name" => $subject_name, "sub_subject_name" => $sub_subject_name, "chapter_name" => $chapter_name, "paper_type" => $papType]);

    }
}

$array['status'] = $status;
$array['objective'] = $objectiveArray;

$flags = $db->getRow("SELECT add_more_flg,edit_flg,delete_flg FROM tbl_info where id = 1");

if ($flags == "" || $flags == null) {
    $array['add_more_flg'] = 0;
    $array['edit_flg'] = 0;
    $array['delete_flg'] = 0;
} else {
    $array['add_more_flg'] = ($flags['add_more_flg']);
    $array['edit_flg'] = ($flags['edit_flg']);
    $array['delete_flg'] = ($flags['delete_flg']);
}

echo json_encode($array);