<?php
ini_set("display_errors","1");

include_once("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."config.php");


$table = "_plugin_multiresponse_with_text";

if($_POST['action'] == "get")
{
    $sql = "SELECT * FROM " .  DB_WARPIT_WEBCATI_BASE . "." . $table . "  
    WHERE id_project = " . $_POST['projectId'] . " AND id_question = " . $_POST['questionId'] . " AND deleted = 0";
    
    
    //print $sql;
    $res= $db->SQLexecute($sql);
    while ($dat = $db->fetchAssoc($res)) {  
        $data[] = $dat;
    }

    $result['success'] = TRUE;
    $result['results'] = sizeof($data);
    $result['rows'] = $data;
    print json_encode($result);
    exit;
}
else if($_POST['action'] == "Save")
{   
    $data = json_decode($_POST['data']);
    
    foreach($data as $key => $value)
    {
        $tmpSql[] = $key . " = '" . $value . "'";
    }
    $tmpSql[] = "id_project = '" . $_POST['projectId'] . "'";
    $tmpSql[] = "id_question = '" . $_POST['questionId']. "'"; 

    $sql = "INSERT INTO " . DB_WARPIT_WEBCATI_BASE . ".". $table ." 
            SET " . implode(",", $tmpSql) ." ON DUPLICATE KEY UPDATE " . implode(",", $tmpSql) . ", deleted = 0";
    //  print $sql;
    $db->SQLexecute($sql);
    exit;
}
?>