<?php

/*!
This script saves a question in the MySQL database after a user creates it. It uses parameters like, author's username,
problem statement, testcases, start and end time, etc. 
*/
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");

include_once("../database.php");
$postData = file_get_contents("php://input");

if(isset($postData) && !empty($postData)){
    $request = json_decode($postData);

    $title = trim($request->title);
    $username = trim($request->username);  //string
    $statement = trim($request->statement); //string
    $tc1 = trim($request->tc1);
    $out1 = trim($request->out1);
    $tc2 = trim($request->tc2);
    $out2 = trim($request->out2);
    $stime = trim($request->stime); //string
    $etime = trim($request->etime);

    $sql = "INSERT INTO questions(title,username,statement,tc1,out1,tc2,out2,stime,etime) VALUES ('$title','$username','$statement','$tc1','$out1','$tc2','$out2','$stime','$etime')";

    if($result = $mysqli->query($sql)){
        $msg = "question uploaded";
        echo json_encode($msg);
    }
    else{
        $msg = "question already exists";
        echo json_encode($msg);
    }

}

?>
