<?php
/**
 * User: David Camacho
 * Date: 03/04/2015
 * Time: 0:05
 */

class Question extends DataBaseController{
    public function Question(){
        parent::DataBaseController();
    }


    public function getAnswers($id_question){
        $res = array("id_answer" => array(),
            "text" => array(),
            "is_correct" => array()
        );

        $sql = "SELECT id_answer, text, is_correct
				FROM " . _db_prefix . "_answer
				WHERE id_question = ?
				";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i',$id_question);
            $tmp->execute();
            $tmp->bind_result($result1, $result2, $result3);
            while ($tmp->fetch()) {
                array_push($res["id_answer"],$result1);
                array_push($res["text"],$result2);
                array_push($res["is_correct"],$result3);

            }
            $tmp->close();
            return $res;
        }
    }

    public function getQuestion($id_question){

        $sql = "SELECT question
				FROM " . _db_prefix . "_question
				WHERE id_question = ?
				";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i',$id_question);
            $tmp->execute();
            $tmp->bind_result($result);
            $tmp->fetch();
            $tmp->close();
            return $result;
        }
    }

    public function getTestByQuestion($id_question){

        $sql = "SELECT id_test
				FROM " . _db_prefix . "_question
				WHERE id_question = ?
				";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i',$id_question);
            $tmp->execute();
            $tmp->bind_result($result);
            $tmp->fetch();
            $tmp->close();
            return $result;
        }
    }


    public function isCorrectAnswer($id_answer){

        $sql = "SELECT is_correct
				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->bind_result($res);
            $tmp->fetch();
            $tmp->close();

            if ($res == 1) return true;
            return false;


        }
        return false;
    }

    public function insertQuestion($id_test, $points, $question){

        $sql = "INSERT INTO " . _db_prefix . "_question(id_test, points, question)
    VALUES (?, ?, ?)";


        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('ids', $id_test, $points, $question);
            $tmp->execute();
            $tmp->fetch();

            return  $tmp->insert_id;

        }
        return false;
    }

    public function removeQuestion($id_question){
        $sql = "DELETE FROM " . _db_prefix . "_question
        WHERE id_question = ?";


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

            $sql = "DELETE FROM " . _db_prefix . "_answer
            WHERE id_question = ?";
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_question);
            $tmp->execute();
            $tmp->fetch();

            return true;

        }
        return false;
    }



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

    }
}