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

$status = 0;
$msg = "Unable to execute operation at this time..! Please retry later";

if (isset($_POST['json'])) {

    $raw_json   = $_POST['json'];
    $post_array = json_decode($raw_json, true);

    $session       = $post_array['session'];
    $added_by      = $post_array['added_by'];
    $selected_date = $post_array['selected_date'];

    $class         = $post_array['class'];
    $section       = $post_array['section'];

    $attendance_table = "attandenc";
    $current_month    = date("F");
    $school_id        = 1;
    $user_type        = 3; // 3 For class or (class and section)

    /////////////////////////////////////

    $aryData = array("date" => $selected_date, "type" => $user_type, "status" => 0, "school_id" => $school_id, "added_by" => $added_by,
                     "session" => $session, "month" => $current_month, "class" => $class, "section" => $section);

    $existing_row_check = $db->getVal("select id from $attendance_table where class='$class' and section = '$section' and date='$selected_date' and type='$user_type' and session = '$session' and status='0' ");

    if (empty($existing_row_check)){
        $insert_id = $db->insertAry($attendance_table, $aryData);
        if ($insert_id > 0){
            $status = 1;
            $msg = 'Attendance marked successfully';
        }else{
            $msg = "Failed to mark attendance..! Please try again later";
        }
    }else{
        $msg = 'Attendance already marked for class '.$class;
    }

}

echo json_encode(array('status' => $status, 'msg' => $msg));