<?php

class Answer {
    public $con;

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

    public function getById($id){
        $quest = array();
        $sql = "SELECT * FROM answers WHERE id = ".$id."";

        $result = $this->con->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()){
                array_push($quest, $row);
            }
            return json_encode($quest);
        }
    }

    public function getAllByQuestionId($question_id){
        $quest = array();
        $sql = "SELECT * FROM answers WHERE question_id = $question_id";

        $result = $this->con->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()){
                array_push($quest, $row);
            }
            return json_encode($quest);
        }
    }

    public function addAnswer($answer, $userId, $question_id){
        $date = date("Y-m-d h:i:sa");
        $sql = "INSERT INTO answers (content, date, user_id, question_id) VALUES ('$answer', '$date', $userId, '$question_id')";

        if($this->con->query($sql)){
            return 'successful';
        }else{
            // return "Error: " . $sql . "<br>" . $this->con->error;
            return 'unsuccesful';
        }
    }

    public function UpdateAnswer($answer_id, $new_answer){
        $sql = "UPDATE answers  SET content = $content  WHERE id = $id" ;

        if($this->con->query($sql)){
            return 'successful';
        }else{
            // return "Error: " . $sql . "<br>" . $this->con->error;
            return 'unsuccesful';
        }
    }
}
