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

class Topic extends DataBaseController{
    public function Topic(){

        parent::DataBaseController();

    }


    public function getTopics($id_course){
        $res = array("title" => array(),
                    "description" => array(),
                    "id_topic" => array()
        );

        $sql = "SELECT t.title, t.description, t.id_topic
				FROM " . _db_prefix . "_topic t,
				" . _db_prefix . "_course c
				WHERE c.id_course = t.id_course AND
				c.id_course = ?
				ORDER BY order_num ASC";
        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i',$id_course);
            $tmp->execute();
            $tmp->bind_result($result1, $result2, $result3);
            while ($tmp->fetch()) {
                array_push($res["title"],$result1);
                array_push($res["description"],$result2);
                array_push($res["id_topic"],$result3);

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

    public function deleteTopic($id_topic)
    {
        $sql = "DELETE FROM " . _db_prefix . "_topic
        WHERE id_topic = ?";


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

            return  true;

        }
        return false;

    }


    public function getTopic($id_topic){

        $res = array("id_topic" => array(),
            "title" => array(),
            "description" => array(),
            "order_num" => array());

        $sql = "SELECT id_topic, title, description, order_num
                FROM ". _db_prefix . "_topic
                WHERE 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);
            while ($tmp->fetch()) {
                array_push($res["id_topic"],$result1);
                array_push($res["title"],$result2);
                array_push($res["description"],$result3);
                array_push($res["order_num"],$result4);

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

    public function getCourseTopics($id_course){

        $res = array("id_topic" => array(),
            "title" => array(),
            "description" => array(),
            "order_num" => array());

        $sql = "SELECT id_topic, title, description, order_num
                FROM ". _db_prefix . "_topic
                WHERE id_course = ?
                ORDER BY order_num";


        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('i',$id_course);
            $tmp->execute();
            $tmp->bind_result($result1, $result2, $result3, $result4);
            while ($tmp->fetch()) {
                array_push($res["id_topic"],$result1);
                array_push($res["title"],$result2);
                array_push($res["description"],$result3);
                array_push($res["order_num"],$result4);

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


    public function insertTopic($id_course, $topic_title , $topic_description, $topic_order){

        $sql = "INSERT INTO " . _db_prefix . "_topic(id_course, title, description, order_num)
        VALUES (?, ?, ?, ?)";


        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('issi', $id_course, $topic_title, $topic_description, $topic_order);
            $tmp->execute();
            $tmp->fetch();

            return  $tmp->insert_id;

        }
        return false;
    }



    public function updateTopic($id_topic, $topic_title , $topic_description, $topic_order){

        $sql = "UPDATE " . _db_prefix . "_topic
                SET title = ?, description = ?, order_num = ?
                WHERE id_topic = ?";


        if (isset($this->db)) {
            $tmp = $this->db->prepare($sql);
            $tmp->bind_param('ssii',  $topic_title, $topic_description, $topic_order, $id_topic);
            $tmp->execute();
            $tmp->fetch();

            return  $tmp->insert_id;

        }
        return false;
    }


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

    }
}