<?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'])){

    $feedback_month = date('F');

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

    $check_qry = $db->getRows("SELECT id FROM teacher_feedback_by_student WHERE student_id = '$json[student_id]' AND teacher_id = '$json[teacher_id]' AND session = '$json[session]' AND feedback_month = '$feedback_month'");

    if (count($check_qry) == 0) {

        $json['feedback_month'] = $feedback_month;

        $insert_response = $db->insertAry("teacher_feedback_by_student", $json);

        if ($insert_response > 0) {
            $status = 1;
            $msg = "Record added successfully";
        } else {
            $msg = "Failed to add record..! Please try again later";
        }

    } else {

        $row_id = $check_qry[0]['id'];

        $flgIn = $db->updateAry("teacher_feedback_by_student", $json, "where id = '$row_id' ");

        if ($flgIn > 0) {
            $status = 1;
            $msg = 'Record updated successfully';
        } else {
            $msg = "Unable to update record...! Please try again later";
        }

    }

}

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