<?php
/**
 * User: David Camacho
 * Date: 31/03/2015
 * Time: 18:01
 */

include_once './controllers/ModuleController.php';

include_once './classes/User.php';
include_once './classes/Course.php';
include_once './classes/Topic.php';
include_once './classes/Test.php';


class MainpanelModule extends ModuleController
{
    var $output;

    public function Mainpanelmodule()
    {
        $this->name = "mainpanel";
        parent::init();


        $this->output = "";
        $this->output = $this->getMainPanel();


        parent::displayModule("mainpanel");
        parent::destroy();


    }

    public function getMainPanel()
    {

        $output = "";
        $courses = array();

        $course = new Course();
        $courses= $course->getInscriptionCourses();

        if(count($courses["id_course"])<1)
            return "<p>" . parent::getTranslates("no_courses_inscription") . "</p>";

        $output = '<ul class="accordion" data-accordion>';
        for($i=0; $i < count($courses["id_course"]);$i++)
        {
            $output = $output . '<li class="accordion-navigation">';
            $output = $output . '<a href="#panel' . $i .'a"><strong>' . parent::getTranslates("course"). ' ' . $courses["name"][$i] . '</strong></a>';
            $output = $output . '<div id="panel' . $i .'a" class="content">';
            $output = $output . $this->getTopics($courses["id_course"][$i]);
            $output = $output . '</div></li>';


        }
        $output = $output . '</ul>';
        return $output;

    }

    public function getTopics($id_course)
    {
        $output = "";
        $topics = array();

        $topic = new Topic();
        $topics= $topic->getTopics($id_course);

        if(count($topics["id_topic"])<1)
            return "<p>" . parent::getTranslates("no_topics_found") . "</p>";


        for($i=0; $i < count($topics["id_topic"]);$i++)
        {
            $output = $output . '<div class="row">';
            $output = $output . '<div class="small-12 large-12 columns">';
            $output = $output . '<strong>' . $topics["title"][$i] . '</strong><hr>';
            $output = $output . '</div></div>';

            $output = $output . $this->getTests($topics["id_topic"][$i]);
        }

        return $output;

    }
    public function getTests($id_topic)
    {
        $output = "";
        $topics = array();

        $test = new Test();
        $tests= $test->getTestsByTopic($id_topic);

        if(count($tests["id_test"])<1)
            return '<p>' . parent::getTranslates("no_tests_found") . '</p>';


        $output = '<div class="row" data-equalizer>';

        for($i=0; $i < count($tests["id_test"]);$i++) {

            $status = "";
            $color = "";
            if (date("Y-m-d H:i:s", strtotime($tests["expire_date"][$i])) <= date("Y-m-d H:i:s")) {
                $status = parent::getTranslates("closed_status_test");
                $color = "#FF6666";
            } elseif ($test->isDelivered($tests["id_test"][$i])) {
                $status = parent::getTranslates("delivered_status_test");
                $color = "#B6A8E0";

            } elseif (date("Y-m-d H:i:s", strtotime($tests["open_date"][$i])) <= date("Y-m-d H:i:s")) {
                $status = parent::getTranslates("open_status_test");
                $color = "#66FF66";
            } elseif (date("Y-m-d H:i:s", strtotime($tests["open_date"][$i])) > date("Y-m-d H:i:s")) {
                $status = parent::getTranslates("waiting_status_test");
                $color = "#FFFF66";
            } else {
                $status = parent::getTranslates("unknown_status_test");
                $color = "#FFFFFF";
            }


            if ($i % 3 == 0) $output = $output . '</div><div class="row" data-equalizer>';

            $output = $output . '<div class="panel small-12 medium-4 large-4 columns left" style="border-radius:5px;font-size:1em;" data-equalizer-watch>';
            $output = $output . '<div class="small-12 medium-12 large-12 centered"><p><strong><h4>' . $tests["title"][$i] . '</h4></strong></p></div>';
            $output = $output . '<div class="small-12 medium-12 large-12 centered" ><p class="text-center" style="margin-bottom: 0.2rem;"><strong>' . parent::getTranslates("status") . '</strong></p></div>';
            $output = $output . '<div class="small-12 medium-12 large-12 centered"><p class="text-center" style="padding:10px;background-color:' . $color . '"><strong>' . $status . '</strong></p></div>';

            $output = $output . '<div class="small-12 medium-12 large-12"><p>';
            $output = $output . '<strong>' . parent::getTranslates("description") . ': </strong>' . $tests["description"][$i] . '<br>';
            $output = $output . '<strong>' . parent::getTranslates("opened_since") . ': </strong>' . date("d/m/Y H:i:s", strtotime($tests["open_date"][$i])) . '<br>';
            $output = $output . '<strong>' . parent::getTranslates("ends_in") . ": </strong>" . date("d/m/Y H:i:s", strtotime($tests["expire_date"][$i])) . '<br>';
            $output = $output . '<strong>' . parent::getTranslates("results_in") . ": </strong>" . date("d/m/Y H:i:s", strtotime($tests["results_date"][$i]));

            $output = $output . '</p><div class="small-12 medium-12 large-12">';
            if ($status == parent::getTranslates("closed_status_test") && date("Y-m-d H:i:s", strtotime($tests["results_date"][$i])) <= date("Y-m-d H:i:s")) {
                $output = $output . '<form method="post" action="test.php">';
                $output = $output . '<input type="hidden" name="id_test" value="' . $tests["id_test"][$i] .'">';
                $output = $output . '<input type="submit"  aria-label="submit form" tabindex="0" value="' . parent::getTranslates("check_answers") . '" class="button small-12 medium-12 large-12">';
                $output = $output . '</form>';

            }elseif ( $status == parent::getTranslates("open_status_test")) {
                $output = $output . '<form method="post" action="test.php">';
                $output = $output . '<input type="hidden" name="id_test" value="' . $tests["id_test"][$i] .'">';
                $output = $output . '<input type="submit"  aria-label="submit form" tabindex="0" value="' . parent::getTranslates("answer_now") . '" class="button small-12 medium-12 large-12">';
                $output = $output . '</form>';
            }
            $output = $output . '</div>';
            $output = $output . '</div></div>';

        }
        $output = $output . '</div>';

        return $output;

    }
}