<?php

session_start();
include_once "../config/database.php";
// include objects
include_once "../resources/survey.php";
$conn = DBConnection();
$survey = new Survey();
$conn->beginTransaction();

    if (isset($_SESSION['uid'])) {
        $id = $_SESSION['uid'];
    }
    $survey_id= $_GET['survey_id'];
    $allanswers = $_GET['allanswers'];

    $survey->survey_id = $survey_id;
    $survey->user_id = $id;
    
    $json = json_decode($allanswers);
//   print_r($json);
    
    foreach ($json as $item)
    {
        echo $item->qid;
        $survey->question_id = $item->qid;
        $question_type = $survey->getSurvey_question_typeid();        
        foreach ($item->option as $opt)
        {
            if ($question_type == "2" || $question_type == "3" || $question_type == "4") {
                if($opt->s_optid !=""){
                    if($opt->comment === ""){ 
                        $opt->comment = NULL;
                    }
                    $survey->comment = $opt->comment;
                    $survey->user_response = $opt->s_optid;
                    $survey->last_updated_ts = $date_time;
                    if ($survey->insert_survey_response()) {

                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                }
            }
            if ($question_type == "1" || $question_type == "5" ) {
                if ($opt->comment != "") {
                    $survey->comment = $opt->comment;
                    $survey->user_response = '0';
                    $survey->last_updated_ts = $date_time;
                    if ($survey->insert_survey_response()) {

                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }
                }
                
            }
            if ($question_type == "6" || $question_type == "7" ) {             
                    $survey->comment = $opt->s_optid;
                    $survey->user_response = $opt->optid;
                    $survey->last_updated_ts = $date_time;
                    if ($survey->insert_survey_response()) {

                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }               
                
            }
            if ($question_type == "8") {    
                if($opt->s_optid === ""){ 
                        $opt->s_optid = NULL;
                    }
                    $survey->comment =  $opt->s_optid;
                    $survey->user_response = $opt->optid;
                    $survey->last_updated_ts = $date_time;
                    if ($survey->insert_survey_response()) {

                    } else {
                        $conn->rollBack();
                        echo 'error';
                        return -1;
                    }               
                
            }

        }
    }
     $conn->commit();
    

?>