<?php

class GatewayAnswer
{
    private $con;

    public function __construct($con)
    {
        $this->con = $con;
    }

    public function addAnswer($answer)
    {
        $query = "insert into answers(content,idquestion) values (:content,:idquestion);";
        $this->con->executeQuery(
            $query,
            array(
                ':content' => array($answer->getContent(), PDO::PARAM_STR),
                ':idquestion' => array($answer->getIdQuestion(), PDO::PARAM_INT),              
            )
        );
        $answerId = $this->con->lastInsertId();
        return $answerId;
    }

    public function getAnswersByIDQuestions($idQuestions)
    {
        $query = "SELECT answers.content,answers.id FROM answers, questions WHERE questions.id = :idquestions AND answers.idquestion = questions.id ;";
        $this->con->executeQuery(
            $query,
             array(
                ':idquestions' => array($idQuestions, PDO::PARAM_INT)
            )
        );
        $results = $this->con->getResults();
        
        return $results;
    }

    public function updateAnswer($id,$answer)
    {
        $query = "UPDATE answers SET content = :content WHERE id = :id;";
        $this->con->executeQuery(
            $query,
            array(
                ':id' => array($id, PDO::PARAM_INT),
                ':content' => array($answer->getContent(), PDO::PARAM_STR)
            )
        );
    }

    public function deleteAnswer($id)
    {
        $query = "DELETE FROM answers WHERE id = :id;";
        $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT)));
    }
}
