<?php

class GatewayQuestion
{
    private $con;

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

    public function addQuestion($question) 
    {
        $query = "insert into questions(content,idchapter,difficulty,nbfails) values (:content,:idchapter,:difficulty,:nbfails);";
        $this->con->executeQuery(
            $query,
            array(
                ':content' => array($question->getContent(), PDO::PARAM_STR),
                ':idchapter' => array($question->getIdChapter(), PDO::PARAM_INT),
                ':difficulty' => array($question->getDifficulty(), PDO::PARAM_INT),
                ':nbfails' => array($question->getNbFails(), PDO::PARAM_INT)
            )
        );
        $questionId = $this->con->lastInsertId();
        return $questionId;
    }

    public function getQuestionByID($id)
    {
        $query = "SELECT * FROM questions WHERE id = :id;";
        $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT)));
        $results = $this->con->getResults();

        return $results[0];
    }

    public function getQuestions()
    {
        $query = "SELECT * FROM questions";
        $this->con->executeQuery($query);
        $results = $this->con->getResults();
                
        return $results;
    }

    public function updateQuestion($id,$question)
    {
        $query = "UPDATE questions SET content = :content, idchapter = :idchapter, idanswergood = :idanswergood, difficulty = :difficulty, nbfails = :nbfails WHERE id = :id;";
        $this->con->executeQuery(
            $query,
            array(
                ':content' => array($question->getContent(), PDO::PARAM_STR),
                ':idchapter' => array($question->getIdChapter(), PDO::PARAM_INT),
                ':idanswergood' => array($question->getIdAnswerGood(), PDO::PARAM_INT),
                ':difficulty' => array($question->getDifficulty(), PDO::PARAM_INT),
                ':nbfails' => array($question->getNbFails(), PDO::PARAM_INT),
                ':id' => array($id, PDO::PARAM_INT),
            )
        );
    }

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