<?php
/**
 * User: David Camacho
 * Date: 01/04/2015
 * Time: 1:35
 */

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


    public function removeStoredTest($id_user, $id_test){
        $sql = "DELETE FROM " . _db_prefix . "_test_stored
                WHERE id_user = ? AND
                id_test = ?";

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

        return true;

    }


    public function getExpireDate($id_test){

        $sql = "SELECT expire_date
				FROM " . _db_prefix . "_test,
				" . _db_prefix . "_user u
				WHERE id_test = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_test);
            $tmp->execute();
            $tmp->bind_result($result1);
            $tmp->fetch();
            $tmp->close();
            return $result1;
        }

        return false;
    }

    public function getTopic($id_test){

        $sql = "SELECT id_topic
				FROM " . _db_prefix . "_test
				WHERE id_test = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_test);
            $tmp->execute();
            $tmp->bind_result($result1);
            $tmp->fetch();
            $tmp->close();
            return $result1;
        }

        return false;
    }

    public function getResultsDate($id_test){

        $sql = "SELECT results_date
				FROM " . _db_prefix . "_test,
				" . _db_prefix . "_user u
				WHERE id_test = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_test);
            $tmp->execute();
            $tmp->bind_result($result1);
            $tmp->fetch();
            $tmp->close();
            return $result1;
        }

        return false;
    }

    public function getOpenDate($id_test){

        $sql = "SELECT open_date
				FROM " . _db_prefix . "_test,
				" . _db_prefix . "_user u
				WHERE id_test = ?";

        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i', $id_test);
            $tmp->execute();
            $tmp->bind_result($result1);
            $tmp->fetch();
            $tmp->close();
            return $result1;
        }
        return false;
    }


    public function isStored($id_test){

        $token = $_SESSION["token"];

        $sql = "SELECT count(t.id_test)
				FROM " . _db_prefix . "_test_stored t,
				" . _db_prefix . "_user u
				WHERE t.id_test = ? AND
				u.token = ? AND
				u.id_user = t.id_user";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('is', $id_test, $token);
            $tmp->execute();
            $tmp->bind_result($result1);
            $tmp->fetch();
            if ($result1 > 0) {
                $tmp->close();
                return true;
            }
            $tmp->close();
            return false;
        }
        return false;
    }


    public function isDelivered($id_test){

        $token = $_SESSION["token"];

        $sql = "SELECT count(t.id_test)
				FROM " . _db_prefix . "_test_delivered t,
				" . _db_prefix . "_user u
				WHERE t.id_test = ? AND
				u.token = ? AND
				u.id_user = t.id_user";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('is', $id_test, $token);
            $tmp->execute();
            $tmp->bind_result($result1);
            $tmp->fetch();
            if ($result1 > 0) {
                $tmp->close();
                return true;
            }
            $tmp->close();
            return false;
        }
        return false;
    }

    public function getQuestions($id_test){
        $res = array("id_question" => array(),
            "question" => array(),
            "id_image" => array(),
            "points" => array()
        );

        $sql = "SELECT q.id_question, q.question, q.id_image, q.points
				FROM " . _db_prefix . "_question q
				WHERE q.id_test = ?";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i',$id_test);
            $tmp->execute();
            $tmp->bind_result($result1, $result2, $result3, $result4);
            while ($tmp->fetch()) {
                array_push($res["id_question"],$result1);
                array_push($res["question"],$result2);
                array_push($res["id_image"],$result3);
                array_push($res["points"],$result4);

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

    public function getTest($id_test){
        $res = array("title" => array(),
            "description" => array(),
            "id_test" => array(),
            "expire_date" => array(),
            "open_date" => array(),
            "results_date" => array(),
            "test_type" => array()
        );

        $sql = "SELECT t.title, t.description, t.id_test, t.expire_date, t.open_date, t.results_date, t.test_type
				FROM " . _db_prefix . "_test t
				WHERE t.id_test = ?";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i',$id_test);
            $tmp->execute();
            $tmp->bind_result($result1, $result2, $result3, $result4, $result5, $result6, $result7);
            while ($tmp->fetch()) {
                array_push($res["title"],$result1);
                array_push($res["description"],$result2);
                array_push($res["id_test"],$result3);
                array_push($res["expire_date"],$result4);
                array_push($res["open_date"],$result5);
                array_push($res["results_date"],$result6);
                array_push($res["test_type"],$result7);

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

    public function getTestsByTopic($id_topic){
        $res = array("title" => array(),
            "description" => array(),
            "id_test" => array(),
            "expire_date" => array(),
            "open_date" => array(),
            "results_date" => array()
        );

        $sql = "SELECT t.title, t.description, t.id_test, t.expire_date, t.open_date, t.results_date
				FROM " . _db_prefix . "_test t,
				" . _db_prefix . "_topic top
				WHERE t.id_topic = top.id_topic AND
				t.id_topic = ?";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i',$id_topic);
            $tmp->execute();
            $tmp->bind_result($result1, $result2, $result3, $result4, $result5, $result6);
            while ($tmp->fetch()) {
                array_push($res["title"],$result1);
                array_push($res["description"],$result2);
                array_push($res["id_test"],$result3);
                array_push($res["expire_date"],$result4);
                array_push($res["open_date"],$result5);
                array_push($res["results_date"],$result6);

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

    public function insertDeliveredTest($id_user, $id_test)
    {
        $sql = "INSERT INTO " . _db_prefix . "_test_delivered(id_user, id_test)
        VALUES (?, ?)";

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

        return true;
    }

public function insertTest($id_topic, $test_type, $test_title, $test_description, $test_expire_date, $test_open_date, $test_results_date){

    $sql = "INSERT INTO " . _db_prefix . "_test(id_topic, test_type, title, description, expire_date, open_date, results_date)
            VALUES (?, ?, ?, ?, ?, ?, ?)";

    $tmp = $this->db->prepare($sql);
    $tmp->bind_param('issssss',$id_topic, $test_type, $test_title, $test_description, $test_expire_date, $test_open_date, $test_results_date);
    $tmp->execute();
    $tmp->fetch();

    return  $tmp->insert_id;


}
    public function updateTest( $id_test, $test_type, $test_title, $test_description, $test_expire_date, $test_open_date, $test_results_date){

        $sql = "UPDATE " . _db_prefix  ."_test
            SET test_type=?, title=?, description=?, expire_date=?, open_date=?, results_date=?
            WHERE id_test = ?";

        $tmp = $this->db->prepare($sql);
        $tmp->bind_param('ssssssi', $test_type, $test_title, $test_description, $test_expire_date, $test_open_date, $test_results_date, $id_test);
        $tmp->execute();
        $tmp->fetch();

        return  $tmp->insert_id;


    }


    public function insertStoredTest($id_user, $id_test)
    {
        $sql = "INSERT INTO " . _db_prefix . "_test_stored(id_user, id_test)
        VALUES (?, ?)";

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

        return true;
    }

    public function deleteTest($id_test)
    {
        $sql = "DELETE FROM ". _db_prefix. "_test WHERE id_test= ?;";


        $tmp = $this->db->prepare($sql);
        $tmp->bind_param('i', $id_test);
        $tmp->execute();
        $tmp->close();

        return true;
    }


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

    }
}