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

    $jsondata = $_POST['json'];

    $status = 0;
    $msg = '';
    $array = array();

    $decode = json_decode($jsondata, true);

    $uid = $decode['uid'];
    $pid = $decode['paper_id'];
    $time_taken = $decode['time_taken'];
    $speed = $decode['speed'];
    $site_name = $decode['site_name'];
    $allanswer = $decode['allanswers'];

    $paperArray = array(
        "pid" => "$pid",
        "uid" => "$uid",
        "time_taken" => "$time_taken",
        "speed" => "$speed",
        "paper_mode" => "mobile",
        "site_name" => "$site_name"
        );

    if($pid != "" && $uid !=""){
        $flgIn=$db->insertAry("assignment_attempt_paper",$paperArray);
    }

    if($flgIn > 0){
        foreach($allanswer as $answer){

            $ansArray = array(
                "pid" => "$pid",
                "uid" => "$uid",
                "qid" => "$answer[qid]",
                "att_paper_id" => "$flgIn",
                "answer" => json_encode($answer[answer], true),
                "qtype" => "$answer[qtype]",
                "paper_mode" => "mobile",
                "site_name" => "$site_name"
                );
                
                $ansIn =$db->insertAry("assignment_attempt_question",$ansArray);
            
        }

    // $url = $LinksDetails['url_root']."/api_new/onlinestudy/api_get_result.php?uid=$uid&paper_id=$pid";

    // $context  = stream_context_create($options);
    // $result = file_get_contents($url, false, $context);
    // $a = json_decode($result, true);

        

        // $data = array('uid' => $uid, 'paper_id' => $pid);
        // $options = array(
        //   'http' => array(
        //     'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        //     'method'  => 'GET',
        //     'content' => http_build_query($data),
        //   ),
        // );
        // $context  = stream_context_create($options);
        // $result = file_get_contents($url, false, $context);
        // $a = json_decode($result, true);


    
        $status = 1;
        $msg = 'Paper successfully submitted.';
    }
    else {
        $msg = "something went wrong.";
    }


    $array['status'] = $status;
    $array['msg'] = $msg;

    echo json_encode($array);

    $url = $LinksDetails['url_root']."/api_new/onlinestudy/api_get_result.php?uid=$uid&paper_id=$pid";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    $response = curl_exec($ch);


?>