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

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

$chapterids = $_GET['chapter_id'];

$attemptpaper = $db->getRows("select * from assignment_attempt_paper");
if (count($attemptpaper) > 0) {
    $status = 1;
    foreach ($attemptpaper as $paper) {

        $paperdetail = $db->getRow("select * from assignment_paper where id='$paper[pid]'");

        $chapterId = $paperdetail['chapter'];

        if ($chapterId == $chapterids && $paperdetail['paper_type'] == 2) {

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

            $total_que = $paper[correct] + $paper[wrong] + $paper[skipped];

            $chapterName = $db->getVal("SELECT subject FROM subject_new WHERE id = '$chapterId'");

            $total_sum = $db->getVal("SELECT SUM(mark_obtained) FROM assignment_attempt_question WHERE uid = '$paper[uid]' AND pid = '$paper[pid]'");

            $max_mark = $db->getVal("SELECT SUM(quemark) FROM assignment_questions WHERE paper_id = '$paper[pid]'");

            array_push($data, ["row_id" => $paper[id], "student_id" => "$paper[uid]", "name" => "$student[name]", "photo" => "$student[photo]", "class" => "$student[class]",
                "section" => "$student[section]", "fname" => "$student[fname]", "adm_no" => "$student[admissionno]", "roll_no" => "$student[fincome]",
                "paper_id" => "$paper[pid]", "paper_dsc" => "$paperdetail[paper_description]", "total_que" => $total_que, "correct" => $paper[correct], "wrong" => $paper[wrong],
                "skipped" => $paper[skipped], "chapter_name" => $chapterName, "max_mark" => $max_mark, "total_sum"=>$total_sum]);


        }
    }
}

$array['status'] = $status;

$array['data'] = $data;

echo json_encode($array);

?>