<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 4/26/2015
 * Time: 1:24 PM
 */

require('../../inc/db.php');
include('../../inc/functions.php');

$params                         =   getParams();
$output                         =   array('status' => 1);

if(!isset($_SESSION['loggedin'])){
    dj($output);
}

if( !isReady( $params, array("question") ) || count($params['answers']) === 0 ){
    dj($output);
}

$question                       =   secure($params['question']);
$answers                        =   $params['answers'];

foreach($answers as $ak => $av){
    if(!isset($av['answer']) || empty($av['answer'])){
        dj($output);
    }
    $answers[$ak]['answer']         =   secure($av['answer']);
    $answers[$ak]['total_votes']    =   0;
}

$answers                        =   json_encode($answers);

$insertQuery                    =   $db->prepare("INSERT INTO polls(question,answers,uid)
                                                  VALUES( :q, :a, :uid )");
$insertQuery->execute(array(
    ":q"                        =>  $question,
    ":a"                        =>  $answers,
    ":uid"                      =>  $_SESSION['uid']
));

$output['status']               =   2;
$output['poll']                 =   array(
    "id"                        =>  $db->lastInsertId(),
    "question"                  =>  $question
);
dj($output);