<?php
/**
 * User: David Camacho
 * Date: 03/04/2015
 * Time: 3:36
 */

class Answer extends DataBaseController
{

    public function Answer(){
        parent::DataBaseController();

    }

    public function deleteUserAnswer($id_user,$id_answer)
    {

        $sql = "DELETE
                FROM " . _db_prefix . "_user_answer
                WHERE id_user = ?
                AND id_answer = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('ii', $id_user, $id_answer);
            $tmp->execute();
            $tmp->close();
        }
    }


    public function removeAnswer($id_answer)
    {

        $sql = "DELETE
                FROM " . _db_prefix . "_answer
                WHERE id_answer = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_answer);
            $tmp->execute();
            $tmp->close();
        }


    }


    public function updateUserAnswer($id_user,$id_question,$id_answer)
    {
        $answers = $this->getPossibleAnswers($id_question);  //Get the possible answers of the result question

        //remove previous answers
        for($i=0; $i < count($answers);$i++) {
            $this->deleteUserAnswer($id_user, $answers[$i]);
        }
        $this->insertAnswer($id_user, $id_answer);

    }

    public function isAnsweredBefore($id_user, $id_question)
    {
        $answers = $this->getPossibleAnswers($id_question);  //Get the possible answers of the result question

        $query = " (";
        for($i=0; $i < count($answers);$i++) {
            if ($i != 0) $query = $query . " OR ";
            $query = $query . "id_answer = " . $answers[$i];
        }
        $query = $query . ") ";

        if (isset($this->db)) {


            $sql = "SELECT COUNT(id_user)
                    FROM " . _db_prefix . "_user_answer
                    WHERE id_user = ?
                    AND " . $query;

            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_user);
            $tmp->execute();
            $tmp->bind_result($total_answers);
            $tmp->fetch();
            $tmp->close();

            if ($total_answers>0)return true;
            else return false;

        }
        return false;

    }

    public function getPossibleAnswers($id_question){

        $res = array();
        $sql = "SELECT a.id_answer
                FROM " . _db_prefix . "_answer a
                WHERE a.id_question = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_question);
            $tmp->execute();
            $tmp->bind_result($possible_answers);
            while ($tmp->fetch()) {
                array_push($res, $possible_answers);
            }
            $tmp->close();
        }
        return $res;

    }

    public function getAnswers($id_question){

        $res = array("id_answer" => array(),
            "id_question" => array(),
            "text" => array(),
            "is_correct" => array()
        );
        $sql = "SELECT a.id_answer, a.id_question, a.text, a.is_correct
                FROM " . _db_prefix . "_answer a
                WHERE a.id_question = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_question);
            $tmp->execute();
            $tmp->bind_result($res1,$res2,$res3,$res4);
            while ($tmp->fetch()) {
                array_push($res["id_answer"], $res1);
                array_push($res["id_question"], $res2);
                array_push($res["text"], $res3);
                array_push($res["is_correct"], $res4);
            }
            $tmp->close();
        }
        return $res;

    }


    public function hasUserAnsweredThis($id_user, $id_answer)
    {
        $sql = "SELECT count(id_user_answer)
                FROM " . _db_prefix . "_user_answer
                WHERE id_user = ? AND
                id_answer = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('ii', $id_user, $id_answer);
            $tmp->execute();
            $tmp->bind_result($total_answers);
            $tmp->fetch();
            $tmp->close();
            if($total_answers>0)return true;
            return false;

        }
        return false;
    }

    public function getQuestion($id_answer)
    {
        $sql = "SELECT a.id_question
                FROM " . _db_prefix . "_answer a
                WHERE a.id_answer = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_answer);
            $tmp->execute();
            $tmp->bind_result($id_question);
            $tmp->fetch();
            $tmp->close();

        }
        return $id_question;
    }

    public function addNewAnswer($id_question, $text, $is_correct)
    {

        $sql = "INSERT INTO " . _db_prefix . "_answer(id_question, text, is_correct)
        VALUES (?, ?, ?)";

        $tmp = $this->db->prepare($sql);
        $tmp->bind_param('isi',$id_question, $text, $is_correct);
        $tmp->execute();
        $tmp->close();

        return true;
    }


    public function insertAnswer($id_user, $id_answer)
    {

        $sql = "INSERT INTO " . _db_prefix . "_user_answer(id_user, id_answer)
        VALUES (?, ?)";

        $tmp = $this->db->prepare($sql);
        $tmp->bind_param('ii',$id_user, $id_answer);
        $tmp->execute();
        $tmp->close();

        return true;
    }


    public function destroy()
    {
        // close the mysqli connection
        $this->db->close();

    }


}