<?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';
include_once './classes/Question.php';
include_once './classes/Answer.php';


class CoursesoptionsModule extends ModuleController
{
    var $output;
    var $col_width;
    var $alerts;
    var $alerts_output;

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

        $this->output = "";
        $this->col_width = 3;
        $this->alerts = array();



        //Show add edit delete forms
        if (isset($_POST["course_option"]))
                $this->output = $this->getCourseForm((int)$_POST["course_option"]);

        elseif (isset($_POST["topic_option"]))
                $this->output =$this->getTopicForm((int)$_POST["topic_option"]);

        elseif (isset($_POST["test_option"]))
                $this->output =$this->getTestForm((int)$_POST["test_option"]);
        else $this->output = $this->getStudentsMarks();


        parent::displayModule($this->name);
        parent::destroy();


    }

    private function getStudentsMarks(){
 
        $output = "";
        $course = new Course();
        $courses = $course->getCoursesCreated();
        $row_count = 1;
        $output .= '<div class="small-9 medium-9 large-9 columns">
                    <p><h3>' . parent::getTranslates('current_marks') . '</h3></p>
                    <ul class="accordion" data-accordion>';
        for($i = 0; $i<count($courses["id_course"]); $i++) {
            $output .= '<li class="accordion-navigation">

            <a href="#panel_curs_' . $courses["id_course"][$i] . '"><b>'  . parent::getTranslates('course') . ' ' . $courses["name"][$i]  . '</b></a>
            <div id="panel_curs_' . $courses["id_course"][$i] . '" class="content">';

            //HEADER MARKS TABLE
            $row_excel = array();
            $tests = $course->getTestsByCourse($courses["id_course"][$i]);
            //Get users who has inscription
            $users = $course->getStudentInscriptions($courses["id_course"][$i]);
            $user = new User();

            $exercise_num = 0;
            $practice_num = 0;
            $partial_num = 0;
            //  Header
            // "id_user ";
            $output .= '<table style="width:100%%">';
            $output .= '<thead>';
            $output .= '<tr>';
            array_push($row_excel,"ID_user");
            $output.='<th>' . parent::getTranslates('student_name'). '</th>';

             for ($k=0; $k<count($tests["id_test"]); $k++) {
                 $output .='<th>';
                 if($tests["test_type"][$k] == "exercise"){
                     $exercise_num++;
                     array_push($row_excel,"E" . $exercise_num);
                     $output.='E' . $exercise_num;

                 }
                 if($tests["test_type"][$k] == "practice"){
                     $practice_num++;
                     array_push($row_excel,"P" . $practice_num);
                     $output.='P' . $practice_num;

                 }
                 if($tests["test_type"][$k] == "partial"){
                     $partial_num++;
                     array_push($row_excel,"Parcial" . $partial_num);
                     $output.=parent::getTranslates('partial') . $partial_num;

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

            array_push($row_excel,"NotaFinal");
            $output.='<th>' . parent::getTranslates('current_final_mark') . '</th>';
            $output.='<br>';
            $row_count++;

            $output.='</tr></thead><tbody></tr>';

            //Row marks for every user
            for ($j=0; $j<count($users["id_user"]); $j++) {
                unset($row_excel);
                $row_excel = array();

                $final_mark = 0;

                $total_ex_mark = 0;
                $total_pr_mark = 0;
                $total_pa_mark = 0;

                $total_ex_n = 0;
                $total_pr_n = 0;
                $total_pa_n = 0;


                //printf($users["id_user"][$j] . " ");
                array_push($row_excel, $users["id_user"][$j]);
                $output .='<tr>';
                $output .= '<td>' . $user->getUserName($users["id_user"][$j]) . "</td>";

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

                    $mark = $user->getUserTestMark($users["id_user"][$j], $tests["id_test"][$k]);
                    $mark = round($mark, 2);
                    //printf($mark. " ");
                    array_push($row_excel, $mark);
                    $output .= '<td>' . $mark . "</td>";



                    if ($tests["test_type"][$k] == "exercise") {
                        $total_ex_mark += $mark;
                        $total_ex_n++;
                    }
                    if ($tests["test_type"][$k] == "practice") {
                        $total_pr_mark += $mark;
                        $total_pr_n++;
                    }
                    if ($tests["test_type"][$k] == "partial") {
                        $total_pa_mark += $mark;
                        $total_pa_n++;
                    }
                }

                // Calculate Final mark
                if ( $total_ex_n == 0) $final_mark_ex = 0;
                else $final_mark_ex = ($total_ex_mark / $total_ex_n) * ($course->getCourseExerciseWeight($courses["id_course"][$i]) / 100.);
                if ( $total_pr_n == 0) $total_pr_n = 0;
                else $final_mark_pr = ($total_pr_mark / $total_pr_n) * ($course->getCoursePracticeWeight($courses["id_course"][$i]) / 100.);
                if ( $total_pa_n == 0) $total_pa_n = 0;
                else $final_mark_pa = ($total_pa_mark / $total_pa_n) * ($course->getCoursePartialWeight($courses["id_course"][$i]) / 100.);

                $final_mark = $final_mark_ex + $final_mark_pr + $final_mark_pa;
                $final_mark = round($final_mark, 2); //Round 2 decimals

                //printf(" " . $final_mark . "<br>");
                array_push($row_excel, $final_mark);
                $output .= '<td>' . $final_mark . "</td>";


               // print_r($row_excel);

                $row_count++;

                $output .= "<tr>";
            }



            $output.=' </tbody></table></div>
            </li>';
        }


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

        return $output;
    }



    private function setAlert($alert)
    {
        array_push($this->alerts, $alert);

        $this->alerts_output = '<div data-alert class="alert-box alert">';


        for ($i = 0; $i < count($this->alerts);$i++){
            $this->alerts_output .= $this->alerts[$i] . "<br>";
        }

        $this->alerts_output .= '<a href="#" class="close">&times;</a></div>';
    }

    private function getSuccessMessage($msg)
    {


        $output = '<div class="row">';
        $output .= '<div class="small-12 large-12 columns" >';
        $output .= '<h2>'. $msg . '<br>';
        $output .= '</div></div>';

        return $output;
    }



    public function getCourseForm($option){
        switch ($option) {
            case 1:
                return $this->courseAddForm();
                break;
            case 2:
                return $this->courseEditForm();
                break;
            case 3:
                return $this->courseDeleteForm();
                break;
        }

    }


    public function courseDeleteForm(){
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {
            if ($_POST["step"] == 1) {
                $user = new User();
                $course = new Course();
                $courses = $course->getCoursesCreated();

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("delete_course") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_option" value="3">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<label>' . parent::getTranslates("select_delete_course") .'</label>';
                if (!$courses){
                    $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <p>' . parent::getTranslates("no_courses_created") .'</p>
                                </div>

                            </div>';
                }else{

                    for ($i=0;$i<count($courses["id_course"]);$i++){
                        if($i%2==0){
                            $row_color = '#D8D8D8';
                        }else{
                            $row_color = '#B0B0B0';
                        }
                        $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                                <div class="small-12 large-12 columns">
                                <input type="radio" name="course_selected" value="' .$courses["id_course"][$i] .  '"  id="delete_course_' .$courses["id_course"][$i] .  '" style="margin-top: 15px; margin-left: 45px;"><label for="delete_course_' . $courses["id_course"][$i] .  '" style="
    font-weight: bolder;">' . $courses["name"][$i] . '</label>
                                </div>
                            </div>';
                    }
                }
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-trash size-36"></i>' .parent::getTranslates("delete") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';

            }elseif ($_POST["step"] == 2) {
                // Course_name field validation
                $is_correct = true;

                if (!isset($_POST["course_selected"])){
                    $this->setAlert(parent::getTranslates("course_not_selected"));
                    $is_correct = false;
                }


                if (!$is_correct) {
                    $_POST["step"] = 1;
                    return $this->courseDeleteForm();
                }

                $id_course = $_POST["course_selected"];
                $course = new Course();
                $name = $course->getCourseName((int)$id_course);

                // Add course form
                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= '<h1>' . parent::getTranslates("delete_course") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_option" value="3">';
                $output .= '<input type="hidden" name="step" value="3">';
                $output .= '<input type="hidden" name="course_selected" value="' .$id_course . '">';
                $output .= '<div class="row collapse">
                        <div class="small-12 large-12 columns">
                            <h3>' .parent::getTranslates("course_delete_warning") .  '<br>' . $name .'</h3>
                        </div>

                    </div>';

                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-trash size-36"></i>' .parent::getTranslates("delete") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';

            }elseif ($_POST["step"] == 3){
                $is_correct = true;

                if (!isset($_POST["course_selected"])){
                    $this->setAlert(parent::getTranslates("course_not_selected"));
                    $is_correct = false;
                }
                $course = new Course();
                $id_course = $_POST["course_selected"];
                $is_correct = $course->deleteCourse((int)$id_course);
                $is_correct = $course->deleteAllCourseInscription((int)$id_course);

                if (!$is_correct) {
                    $_POST["step"] = 1;
                    return $this->courseDeleteForm();
                }

                //header("Location: main.php");
            }
        }


        return $output;
    }

    public function courseAddForm()
    {
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {

            if ($_POST["step"] == 1) {

                // Add course form
                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("add_new_course") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_option" value="1">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("course_name") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' .parent::getTranslates("course_name_hint") .  '" name="course_name">
                        </div>
                    </div>';
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("course_description") .  '</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' .parent::getTranslates("course_description_hint") .  '" name="course_description">
                        </div>
                    </div>';

                $output .= '<div class="row collapse">
                        <div class="small-12 large-12 columns">
                            <p><strong>' . parent::getTranslates("course_weight_warning") . ': </strong></p>
                        </div>
                    </div>';

                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("exercise_weight") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input style="width:3em;" type="text" placeholder="' .parent::getTranslates("exercise_weight_hint") .  '" name="course_exercise" >
                        </div>
                    </div>';

                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("practice_weight") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input style="width:3em;" type="text" placeholder="' .parent::getTranslates("practice_weight_hint") .  '" name="course_practice" >
                        </div>
                    </div>';

                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("partial_weight") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input style="width:3em;" type="text" placeholder="' .parent::getTranslates("partial_weight_hint") .  '" name="course_partial" >
                        </div>
                    </div>';


                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' .parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';

            }elseif ($_POST["step"] == 2){

                // Validate fields
                $course_name = "";
                $course_desc = "";
                $is_correct = true;

                // Course_name field validation
                if (!isset($_POST["course_name"]) || strlen($_POST["course_name"])<3){
                    $this->setAlert(parent::getTranslates("course_field_errors"));
                    $is_correct = false;
                }
                $course_name = $_POST["course_name"];
                if(isset($_POST["course_name"])) $course_name = $_POST["course_name"];

                // Course description field validation
                if (isset($_POST["course_description"]))
                    $course_desc = $_POST["course_description"];

                // Exercise weight validation
                if (!isset($_POST["course_exercise"])){
                    $this->setAlert(parent::getTranslates("no_exercise_weight"));
                    $is_correct = false;
                }

                // Practice weight validation
                if (!isset($_POST["course_practice"])){
                    $this->setAlert(parent::getTranslates("no_practice_weight"));
                    $is_correct = false;
                }

                // Partial weight validation
                if (!isset($_POST["course_partial"])){
                    $this->setAlert(parent::getTranslates("no_partial_weight"));
                    $is_correct = false;
                }


                // If fields and save incorrect return to step 1 showing alerts
                if (!$is_correct) {
                    $_POST["step"] = 1;
                    return $this->courseAddForm();
                }

                if (((int)$_POST["course_exercise"] + (int)$_POST["course_practice"] + (int)$_POST["course_partial"])!=100 ){
                    $this->setAlert(parent::getTranslates("total_not_100"));
                    $_POST["step"] = 1;
                    return $this->courseaddForm();
                }

                // Save fields
                $user = new User();
                $course_id = $course->insertCourse($user->getIdUser(), $course_name, $course_desc,(int)$_POST["course_exercise"],(int)$_POST["course_practice"], (int)$_POST["course_partial"]  );

                $output .= $this->loadInscriptionForm($course_id);





                $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' .parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';




            }
        }

        return $output;
    }










    public function courseEditForm()
    {
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {
            if ($_POST["step"] == 1) {
                $user = new User();
                $course = new Course();
                $courses = $course->getCoursesCreated();

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_course") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_option" value="2">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<label>' . parent::getTranslates("select_edit_course") .'</label>';
                if (!$courses){
                    $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <p>' . parent::getTranslates("no_courses_created") .'</p>
                                </div>

                            </div>';
                }else{

                    for ($i=0;$i<count($courses["id_course"]);$i++){
                        if($i%2==0){
                            $row_color = '#D8D8D8';
                        }else{
                            $row_color = '#B0B0B0';
                        }
                        $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                                <div class="small-12 large-12 columns">
                                <input type="radio" name="course_selected" value="' .$courses["id_course"][$i] .  '"  id="edit_course_' .$courses["id_course"][$i] .  '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $courses["id_course"][$i] .  '" style="font-weight: bolder;">' . $courses["name"][$i] . '</label>

                                </div>

                            </div>';
                    }
                }
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' .parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';
            }elseif ($_POST["step"] == 2) {
                // Course_name field validation
                $is_correct = true;

                if (!isset($_POST["course_selected"])){
                    $this->setAlert(parent::getTranslates("course_not_selected"));
                    $is_correct = false;
                }


                if (!$is_correct) {
                    $_POST["step"] = 1;
                    return $this->courseEditForm();
                }

                $id_course = $_POST["course_selected"];

                $course = new Course();
                $name = $course->getCourseName((int)$id_course);
                $desc = $course->getCourseDescription((int)$id_course);

                $exercise_weight = $course->getCourseExerciseWeight((int)$id_course);
                $practice_weight = $course->getCoursePracticeWeight((int)$id_course);
                $partial_weight = $course->getCoursePartialWeight((int)$id_course);

                // Edit course form
                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_course") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_option" value="2">';
                $output .= '<input type="hidden" name="step" value="3">';
                $output .= '<input type="hidden" name="course_selected" value="' .$id_course . '">';
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("course_name") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' .parent::getTranslates("course_name_hint") .  '" name="course_name" value="' . $name . '">
                        </div>
                    </div>';

                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("course_description") .  '</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' .parent::getTranslates("course_description_hint") .  '" name="course_description" value="'. $desc . '">
                        </div>
                    </div>';


                $output .= '<div class="row collapse">
                        <div class="small-12 large-12 columns">
                            <p><strong>' . parent::getTranslates("course_weight_warning") . ': </strong></p>
                        </div>
                    </div>';

                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("exercise_weight") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input style="width:3em;" type="text" placeholder="' .parent::getTranslates("exercise_weight_hint") .  '" name="course_exercise" value="'. $exercise_weight . '">
                        </div>
                    </div>';

                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("practice_weight") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input style="width:3em;" type="text" placeholder="' .parent::getTranslates("practice_weight_hint") .  '" name="course_practice" value="'. $practice_weight . '">
                        </div>
                    </div>';

                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("partial_weight") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input style="width:3em;" type="text" placeholder="' .parent::getTranslates("partial_weight_hint") .  '" name="course_partial" value="'. $partial_weight . '">
                        </div>
                    </div>';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' .parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';


            }elseif ($_POST["step"] == 3){

                // Validate fields
                $course_name = "";
                $course_desc = "";
                $is_correct = true;


                // Course_id field validation
                if (!isset($_POST["course_name"]) || strlen($_POST["course_name"])<3){
                    $this->setAlert(parent::getTranslates("no_course_selected"));
                    $is_correct = false;
                }
                $id_course = $_POST["course_selected"];

                // Course_name field validation
                if (!isset($_POST["course_name"]) || strlen($_POST["course_name"])<3){
                    $this->setAlert(parent::getTranslates("course_field_errors"));
                    $is_correct = false;
                }
                $course_name = $_POST["course_name"];
                if(isset($_POST["course_name"])) $course_name = $_POST["course_name"];

                // Course description field validation
                if (isset($_POST["course_description"]))
                    $course_desc = $_POST["course_description"];

                // Exercise weight validation
                if (!isset($_POST["course_exercise"])){
                    $this->setAlert(parent::getTranslates("no_exercise_weight"));
                    $is_correct = false;
                }

                // Practice weight validation
                if (!isset($_POST["course_practice"])){
                    $this->setAlert(parent::getTranslates("no_practice_weight"));
                    $is_correct = false;
                }

                // Partial weight validation
                if (!isset($_POST["course_partial"])){
                    $this->setAlert(parent::getTranslates("no_partial_weight"));
                    $is_correct = false;
                }



                // If fields and save incorrect return to step 1 showing alerts
                if (!$is_correct) {
                    $_POST["step"] = 2;
                    return $this->courseEditForm();
                }

                if (((int)$_POST["course_exercise"] + (int)$_POST["course_practice"] + (int)$_POST["course_partial"])!=100 ){
                    $this->setAlert(parent::getTranslates("total_not_100"));
                    $_POST["step"] = 2;
                    return $this->courseEditForm();
                }

                // Save fields
                $course->updateCourse($id_course, $course_name, $course_desc, (int)$_POST["course_exercise"], (int)$_POST["course_practice"], (int)$_POST["course_partial"] );

                $output .= $this->loadInscriptionForm($id_course);


                $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';

                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' .parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';



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

        return $output;
    }



    public function     loadAnswerForm($id_question)
    {


        // Answer edit form
        $question = new Question();
        $test = new Test();
        $id_test = $question->getTestByQuestion($id_question);


        $output = "";
        $output .= '<div id="question_id" style="display:none;">' . $id_question . '</div>';
        $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
        $output .= '<div class="row">';
        $output .= '<div class="small-12 large-12 columns" >';
        $output .= $this->alerts_output;
        $output .= '<div class="small-12 large-12 alert" style="display: none;" id="answer_form_alerts"></div>';
        $output .= '<h1>' . parent::getTranslates("answer_edit_title") . '</h1><hr>';
        $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
        $output .= '<div class="row collapse">
                        <div class="small-12 large-12 columns">
                            <p><h5><b>' .parent::getTranslates("question") . ':</b></h5></p>
                            <p>' .$question->getQuestion($id_question) . '</p>
                        </div>

                    </div>';
        $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix" style="border-right: solid 1px #c8c8c8;">' .parent::getTranslates("is_correct") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                             <div class="switch small" style="margin-left:10px;">
                             <input  type="checkbox" id="is_correct_input" onclick="if ($(\'#is_correct_input\').val() ==0) $(\'#is_correct_input\').val(1); else $(\'#is_correct_input\').val(0);" value="0">
                             <label for="is_correct_input"></label>
                             </div>

                        </div>
                    </div>';
        $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' . parent::getTranslates("answer") . '*</span>
                        </div>
                         <div class="small-9 large-9 columns">
                              <textarea style="min-height:150px;" placeholder="' . parent::getTranslates("answer_hint") . '" id="answer_input"></textarea>
                        </div>

                    </div>
                    <div class="row collapse">
                        <div class="small-6 large-6 columns" >
                            <form method="post" action="main.php#coursesoptions_anchor">
                            <input type="hidden" name="test_option" value="2">
                            <input type="hidden" name="step" value="5">
                            <input type="hidden" name="edit" value="1">
                             <input type="hidden" name="test_selected" value="' . $id_test . '">

                            <a href="#coursesoptions_anchor" onclick="$(this).closest(\'form\').submit()"  class="button" type="text" style="margin-top:10px;height:50px;width:250px;padding-top: 1rem;float:left;" id="add_answer_button"><i class="fi-arrow-left size-32"></i>'. parent::getTranslates("go_back") .'</a>
                            </form>
                        </div>
                        <div class="small-6 large-6 columns" >
                            <a href="#coursesoptions_anchor" onclick="saveAnswer(' . $id_question. ')"  class="button" type="text" style="margin-top:10px;height:50px;width:250px;padding-top: 1rem;float:right;" id="add_answer_button"><i class="fi-plus size-32"></i>'. parent::getTranslates("add_answer") .'</a>
                        </div>
                    </div>
                     ';




        //Questions stored
        //list of students inscriptions
        $answer = new Answer();
        $answers = $answer->getAnswers($id_question);



        $output .= '<div class="row collapse" style="margin-top:3em;">
                    <div class="small-3 large-3 columns">';
        $output .= '<span class="label">' . parent::getTranslates("answers_stored") . ':</span>';
        $output .= '</div>';
        $output .= '</div>';
        $output .= '<div class="row collapse">
                    <div class="small-12 large-12 columns">';
        $output .= '<div id="answers_list" class="panel small-12 large-12">';

        // loop answer list
        for ($i = 0; $i < count($answers["id_answer"]); $i++){
            $output .= '<div class="row panel" >'.
                    '<div class="small-12 large-12 columns">' .
                    '<div class="columns" style=" width: 15px;" onclick="removeAnswer('. $answers["id_answer"][$i]. ')">' .
                    '<i class="fi-x size-18"></i></div><div class="columns" style="width:600px;float:left;">' .
                    '<p style="padding-top: 1px; float: left; width: 406px; font-weight:bolder;"">';
            if ($answers["is_correct"][$i]==1){
                $output .='<i class="fi-check size-18" style="float:left; color: green;"></i>';
            }else{
                $output .='<i class="fi-x size-18" style="float:left; color: red;"></i>';
            }

            $output .= $answers["text"][$i] . '</p></div></div></div>';
        }
        $output .= '</div>';
        $output .= '</div>';
        $output .= '</div>';

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

        return $output;
    }



    public function loadQuestionForm($id_test)
    {
        // Course Inscription form
        $output = "";
        $output .= '<div id="test_id" style="display:none;">' . $id_test . '</div>';
        $output .= '<div id="step" style="display:none;">' . ((int)$_POST["step"]+ 1) . '</div>';
        $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
        $output .= '<div class="row">';
        $output .= '<div class="small-12 large-12 columns" >';
        $output .= $this->alerts_output;
        $output .= '<div class="small-12 large-12 alert" style="display: none;" id="question_form_alerts"></div>';
        $output .= '<h1>' . parent::getTranslates("question_edit_title") . '</h1><hr>';
        $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
        $output .= '<div id="step" style="display:none;">' . ((int)$_POST["step"]+ 1) . '</div>';
        $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("points") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" style="width:50px" placeholder="' .parent::getTranslates("points_hint") .  '" id="points_input" value="">
                        </div>
                    </div>';
        $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' . parent::getTranslates("question") . '*</span>
                        </div>
                         <div class="small-9 large-9 columns">
                              <textarea style="min-height:150px;" placeholder="' . parent::getTranslates("question_hint") . '" id="question_input"></textarea>
                        </div>

                    </div>
                    <div class="row collapse">
                        <div class="small-3 large-3 columns" style="float:right;">
                            <a href="#coursesoptions_anchor" onclick="saveQuestion(' . $id_test. ')"  class="button" type="text" style="margin-top:10px;height:50px;width:250px;padding-top: 1rem;float:right;" id="add_question_button"><i class="fi-plus size-32"></i>'. parent::getTranslates("add_question") .'</a>
                        </div>
                    </div>
                     ';




        //Questions stored
        //list of students inscriptions
        $test = new Test();
        $questions = $test->getQuestions($id_test);



        $output .= '<div class="row collapse">
                    <div class="small-3 large-3 columns">';
        $output .= '<span class="label">' . parent::getTranslates("questions_stored") . ':</span>';
        $output .= '</div>';
        $output .= '</div>';
        $output .= '<div class="row collapse">
                    <div class="small-12 large-12 columns">';
        $output .= '<div id="questions_list" class="panel small-12 large-12">';
        // loop question list

        for ($i = 0; $i < count($questions["id_question"]); $i++){
            $output .= '<div class="row panel" ><div class="small-12 large-12 columns"><div class="columns" style=" width: 15px;" onclick="removeQuestion('. $questions["id_question"][$i]. ')"><i class="fi-x size-18"></i></div><div class="columns" style="width:500px;float:left;"><p style="padding-top: 1px;font-weight:bolder;">(' .$questions["points"][$i] . ' points)' .$questions["question"][$i] . '</p>';
            $output .= '<form action="main.php#coursesoptions_anchor" method="post">
                <input type="hidden" name="id_test" value="$(\'#test_id\').html()">
                <input type="hidden" name="id_question" value="' . $questions["id_question"][$i] . '">
            <input type="hidden" name="test_option" value="' . $_POST["test_option"]. '">
            <input type="hidden" name="step" value="' . ((int)$_POST["step"]+ 1). '">
            <div class="button" onclick="$(this).closest(\'form\').submit()" style="height: 40px; padding: 11px;" >
            <i class="fi-page-edit size-18"></i>Edit answers</div>
            </form>';
            $output .= '</div></div></div>';
        }
        $output .= '</div>';
        $output .= '</div>';
        $output .= '</div>';

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

        return $output;
    }










    public function loadInscriptionForm($id_course)
    {
        // Course Inscription form
        $output = "";
        $output .= '<div id="course_id" style="display:none;">' . $id_course . '</div>';
        $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
        $output .= '<div class="row">';
        $output .= '<div class="small-12 large-12 columns" >';
        $output .= $this->alerts_output;
        $output .= '<h1>' . parent::getTranslates("student_inscription_title") . '</h1><hr>';
        $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
        $output .= '<div class="row collapse">
                                        <div class="small-3 large-3 columns">
                                            <span class="prefix">' . parent::getTranslates("student_inscription") . '</span>
                                        </div>
                                         <div class="small-6 large-6 columns">
                                              <input type="text" placeholder="' . parent::getTranslates("student_inscription_hint") . '" id="search_student_input">
                                        </div>
                                        <div class="small-3 large-3 columns">
                                            <a href="#coursesoptions_anchor" onclick="searchStudentName(document.getElementById(\'search_student_input\').value)"  class="button" type="text" style="height:2.3125rem;padding-top: 0.5rem;" placeholder="' . parent::getTranslates("student_inscription_hint") . '" id="search_student"><i class="fi-magnifying-glass size-32"></i></a>
                                        </div>
                                     </div>';

        //Students found box
        $output .= '<div id="found_box" class="row collapse" style="display:none;">
                                    <div class="small-12 large-12 columns">';
        $output .= '<div class="row collapse" >
                                    <div class="small-3 large-3 columns">';
        $output .= '<span class="label">' . parent::getTranslates("students_found") . ':</span>';
        $output .= '</div>';
        $output .= '</div>';
        $output .= '<div class="row collapse">
                                    <div class="small-12 large-12 columns">';
        $output .= '<div id="found_box_results" class="panel small-12 large-12">';
        $output .= '</div>';
        $output .= '</div>';
        $output .= '</div>';
        $output .= '</div>';
        $output .= '</div>';


        // Course inscription list
        //list of students inscriptions
        $course = new Course();
        $students = $course->getStudentInscriptions($id_course);
        $user = new User();


        $output .= '<div class="row collapse">
                                    <div class="small-3 large-3 columns">';
        $output .= '<span class="label">' . parent::getTranslates("students_with_inscription") . ':</span>';
        $output .= '</div>';
        $output .= '</div>';
        $output .= '<div class="row collapse">
                                    <div class="small-12 large-12 columns">';
        $output .= '<div id="course_inscription_list" class="panel small-12 large-12">';
        // loop inscription list

        for ($i = 0; $i < count($students["id_user"]); $i++){
            $output .= '<div class="row" ><div class="small-12 large-12 columns"><div class="columns" style=" width: 15px;" onclick="removeStudentInscription('. $students["id_user"][$i]. ')"><i class="fi-x size-18"></i></div><div class="columns" style="width:500px;float:left;"><p style="padding-top: 1px;">' .$students["name"][$i] . '</p></div></div></div>';
        }
        $output .= '</div>';
        $output .= '</div>';
        $output .= '</div>';
        return $output;
}

    public function getTopicForm($option)
    {
        switch ($option) {
            case 1:
                return $this->topicAddForm();
                break;
            case 2:
                return $this->topicEditForm();
                break;
            case 3:
                return $this->topicDeleteForm();
                break;
        }

    }

    public function getTestForm($option)
    {
        switch ($option) {
            case 1:
                return $this->testAddForm();
                break;
            case 2:
                return $this->testEditForm();
                break;
            case 3:
                return $this->testDeleteForm();
                break;
        }

    }



    public function testDeleteForm(){
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {

            if ($_POST["step"] == 1) {

                $user = new User();
                $course = new Course();
                $courses = $course->getCoursesCreated();

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("delete_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="test_option" value="3">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<label>' . parent::getTranslates("select_test_course") . '</label>';
                if (!$courses) {
                    $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <p>' . parent::getTranslates("no_courses_created") . '</p>
                                </div>

                            </div>';
                } else {

                    for ($i = 0; $i < count($courses["id_course"]); $i++) {
                        if ($i % 2 == 0) {
                            $row_color = '#D8D8D8';
                        } else {
                            $row_color = '#B0B0B0';
                        }
                        $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                                <div class="small-12 large-12 columns">
                                <input type="radio" name="course_selected" value="' . $courses["id_course"][$i] . '"  id="edit_course_' . $courses["id_course"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $courses["id_course"][$i] . '" style="font-weight: bolder;">' . $courses["name"][$i] . '</label>

                                </div>

                            </div>';
                    }
                }
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';


            }elseif ($_POST["step"] == 2) {

                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("no_course_selected"));
                    $is_correct = false;
                    $_POST["step"] = 1;
                    return $this->testDeleteForm();
                }
                $id_course = (int)$_POST["course_selected"];



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

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("delete_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_selected" value="' .$id_course . '">';
                $output .= '<input type="hidden" name="test_option" value="3">';
                $output .= '<input type="hidden" name="step" value="3">';
                $output .= '<label>' . parent::getTranslates("select_topic") . '</label>';
                if (!$topics) {
                    $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                } else {
                    if (count($topics["id_topic"])<1){
                        $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                    }else {

                        for ($i = 0; $i < count($topics["id_topic"]); $i++) {
                            if ($i % 2 == 0) {
                                $row_color = '#D8D8D8';
                            } else {
                                $row_color = '#B0B0B0';
                            }
                            $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                            <div class="small-12 large-12 columns">
                            <input type="radio" name="topic_selected" value="' . $topics["id_topic"][$i] . '"  id="edit_course_' . $topics["id_topic"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $topics["id_topic"][$i] . '" style="font-weight: bolder;">' . $topics["title"][$i] . '</label>

                            </div>

                        </div>';
                        }
                        $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                        $output .= '</form>';
                        $output .= '</div>';
                        $output .= '</div>';
                        $output .= '</div>';
                    }
                }


            } elseif ($_POST["step"] == 3) {

                if (!isset($_POST["topic_selected"])) {
                    $this->setAlert(parent::getTranslates("no_topic_selected"));
                    $is_correct = false;
                    $_POST["step"] = 2;
                    return $this->testDeleteForm();
                }

                $id_topic = (int)$_POST["topic_selected"];

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

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("delete_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="topic_selected" value="' .$id_topic . '">';
                $output .= '<input type="hidden" name="test_option" value="3">';
                $output .= '<input type="hidden" name="step" value="4">';
                $output .= '<label>' . parent::getTranslates("select_test") . '</label>';
                if (!$tests) {
                    $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_tests_created") . '</p>
                            </div>

                        </div>';
                } else {
                    if (count($tests["id_test"])<1){
                        $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_tests_created") . '</p>
                            </div>

                        </div>';
                    }else {

                        for ($i = 0; $i < count($tests["id_test"]); $i++) {
                            if ($i % 2 == 0) {
                                $row_color = '#D8D8D8';
                            } else {
                                $row_color = '#B0B0B0';
                            }
                            $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                            <div class="small-12 large-12 columns">
                            <input type="radio" name="test_selected" value="' . $tests["id_test"][$i] . '"  id="edit_test_' . $tests["id_test"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_test_' . $tests["id_test"][$i] . '" style="font-weight: bolder;">' . $tests["title"][$i] . '</label>

                            </div>

                        </div>';
                        }
                        $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                        $output .= '</form>';
                        $output .= '</div>';
                        $output .= '</div>';
                        $output .= '</div>';
                    }
                }

            } elseif ($_POST["step"] == 4) {


                if (!isset($_POST["test_selected"])) {
                    $this->setAlert(parent::getTranslates("no_test_selected"));
                    $_POST["step"] = 3;
                    return $this->testDeleteForm();
                }


                $id_test = (int)$_POST["test_selected"];


                //Add Question Form
                $test = new Test();
                $test_name = $test->getTest($id_test);
                $test_name = $test_name["title"][0];

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">';
                $output .= '<h1>' .parent::getTranslates("delete_test") . '<br><br>' . $test_name. '</h1>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="test_option" value="3">';
                $output .= '<input type="hidden" name="id_test" value="' .$id_test .'">';
                $output .= '<input type="hidden" name="step" value="5">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-trash size-36"></i>' .parent::getTranslates("delete") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';




                //header("Location: main.php");


            } elseif ($_POST["step"] == 5) {
                if (!isset($_POST["id_test"])) {
                    $this->setAlert(parent::getTranslates("test_not_found"));
                    $_POST["step"] = 4;
                    return $this->testDeleteForm();
                }

                $test = new Test();
                $test->deleteTest((int)$_POST["id_test"]);
               // header("Location: main.php");
            }
        }

        return $output;
    }


    public function testAddForm(){
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {

            if ($_POST["step"] == 1) {

                $user = new User();
                $course = new Course();
                $courses = $course->getCoursesCreated();

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("add_new_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="test_option" value="1">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<label>' . parent::getTranslates("select_test_course") . '</label>';
                if (!$courses) {
                    $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <p>' . parent::getTranslates("no_courses_created") . '</p>
                                </div>

                            </div>';
                } else {

                    for ($i = 0; $i < count($courses["id_course"]); $i++) {
                        if ($i % 2 == 0) {
                            $row_color = '#D8D8D8';
                        } else {
                            $row_color = '#B0B0B0';
                        }
                        $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                                <div class="small-12 large-12 columns">
                                <input type="radio" name="course_selected" value="' . $courses["id_course"][$i] . '"  id="edit_course_' . $courses["id_course"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $courses["id_course"][$i] . '" style="font-weight: bolder;">' . $courses["name"][$i] . '</label>

                                </div>

                            </div>';
                    }
                }
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';


            }elseif ($_POST["step"] == 2) {

                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("no_course_selected"));
                    $is_correct = false;
                    $_POST["step"] = 1;
                    return $this->testAddForm();
                }
                $id_course = (int)$_POST["course_selected"];



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

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("add_new_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_selected" value="' .$id_course . '">';
                $output .= '<input type="hidden" name="test_option" value="1">';
                $output .= '<input type="hidden" name="step" value="3">';
                $output .= '<label>' . parent::getTranslates("select_topic") . '</label>';
                if (!$topics) {
                    $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                } else {
                    if (count($topics["id_topic"])<1){
                        $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                    }else {

                        for ($i = 0; $i < count($topics["id_topic"]); $i++) {
                            if ($i % 2 == 0) {
                                $row_color = '#D8D8D8';
                            } else {
                                $row_color = '#B0B0B0';
                            }
                            $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                            <div class="small-12 large-12 columns">
                            <input type="radio" name="topic_selected" value="' . $topics["id_topic"][$i] . '"  id="edit_course_' . $topics["id_topic"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $topics["id_topic"][$i] . '" style="font-weight: bolder;">' . $topics["title"][$i] . '</label>

                            </div>

                        </div>';
                        }
                        $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                        $output .= '</form>';
                        $output .= '</div>';
                        $output .= '</div>';
                        $output .= '</div>';
                    }
                }


            } elseif ($_POST["step"] == 3) {

                // Validate fields
                if (!isset($_POST["topic_selected"])) {
                    $this->setAlert(parent::getTranslates("no_topic_selected"));
                    $is_correct = false;
                    $_POST["step"] = 2;
                    return $this->testAddForm();
                }




                $id_topic = (int)$_POST["topic_selected"];


                // Add test form
                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("add_new_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="test_option" value="1">';
                $output .= '<input type="hidden" name="step" value="4">';
                $output .= '<input type="hidden" name="topic_selected" value="' . $id_topic .'">';

                // Test type Dropdown
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_type") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input style="width:150px; float:left;" readonly id="test_type" type="text" placeholder="' .parent::getTranslates("test_type_hint") .  '" name="test_type">
                            <a href="#coursesoptions_anchor" style="width:70px;height:37px;padding-top:10px;" class="button fi-arrow-down" data-dropdown="drop_test_type"></a>
                            <ul id="drop_test_type" class="tiny f-dropdown" data-dropdown-content>
                              <li><a href="#coursesoptions_anchor" onclick="$(\'#test_type\').val(\'Exercise\')">' .parent::getTranslates("exercise") .  '</a></li>
                              <li><a href="#coursesoptions_anchor" onclick="$(\'#test_type\').val(\'Practice\')">' .parent::getTranslates("practice") .  '</a></li>
                              <li><a href="#coursesoptions_anchor" onclick="$(\'#test_type\').val(\'Partial\')">' .parent::getTranslates("partial") .  '</a></li>
                        </ul>
                        </div>
                    </div>';

                // Test title
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_title") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' .parent::getTranslates("test_title_hint") .  '" name="test_title">
                        </div>
                    </div>';

                // Test Description
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_description") .  '</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' .parent::getTranslates("test_description_hint") .  '" name="test_description">
                        </div>
                    </div>';



                // Test Expire Date
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_expire_date") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" id="datetimepicker1" style="width:155px" placeholder="' .parent::getTranslates("test_expire_date_hint") .  '" name="test_expire_date">
                            <script> jQuery(\'#datetimepicker1\').datetimepicker({format:\'d/m/Y H:i:s\', mask:true, defaultTime:\'00:00\'});</script>
                        </div>
                    </div>';

                // Test Open Date
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_open_date") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" id="datetimepicker2" style="width:155px" placeholder="' .parent::getTranslates("test_open_date_hint") .  '" name="test_open_date">
                            <script> jQuery(\'#datetimepicker2\').datetimepicker({format:\'d/m/Y H:i:s\', mask:true, defaultTime:\'00:00\'});</script>
                        </div>
                    </div>';

                // Test Results Date
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_results_date") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" id="datetimepicker3" style="width:155px" placeholder="' .parent::getTranslates("test_results_date_hint") .  '" name="test_results_date">
                            <script> jQuery(\'#datetimepicker3\').datetimepicker({format:\'d/m/Y H:i:s\', mask:true, defaultTime:\'00:00\'});</script>
                        </div>
                    </div>';



                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' .parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';





            } elseif ($_POST["step"] == 4) {
                $is_correct = true;
                $test_title = "";
                $test_type = "";
                $test_description = "";
                $test_expire = "";
                $test_open = "";
                $test_results = "";

                // Fields validation
                if (!isset($_POST["topic_selected"])) {
                    $this->setAlert(parent::getTranslates("topic_not_selected"));
                    $_POST["step"] = 2;
                    return $this->testAddForm();
                }

                // Test type validation
                if (!isset($_POST["test_type"])) {
                    $this->setAlert(parent::getTranslates("test_type_error"));
                    $is_correct = false;
                }else{
                    if ($_POST["test_type"]=="") {
                        $this->setAlert(parent::getTranslates("test_type_error"));
                        $is_correct = false;
                    }
                    $test_type = $_POST["test_type"];
                }

                // Test description validation
                if (isset($_POST["test_description"])) {
                    $test_description = $_POST["test_description"];

                }

                // Test title validation
                if (!isset($_POST["test_title"])) {
                    $this->setAlert(parent::getTranslates("test_title_error"));
                    $is_correct = false;
                }else{
                    $test_title = $_POST["test_title"];
                    if (strlen($test_title)<3){
                        $this->setAlert(parent::getTranslates("test_title_error"));
                        $is_correct = false;
                    }
                }


                // Expire Date validation
                $format = 'd/m/Y H:i:s';

                if (!isset($_POST["test_expire_date"])) {
                    $this->setAlert(parent::getTranslates("test_expire_date_error"));
                    $is_correct = false;
                }else{

                    if(($test_expire = DateTime::createFromFormat($format, $_POST["test_expire_date"]))==false){
                        $this->setAlert(parent::getTranslates("test_expire_date_error"));
                        $is_correct = false;
                    }
                }

                // Open Date validation
                $format = 'd/m/Y H:i:s';
                if (!isset($_POST["test_open_date"])) {
                    $this->setAlert(parent::getTranslates("test_open_date_error"));
                    $is_correct = false;
                }else{

                    if(($test_open = DateTime::createFromFormat($format, $_POST["test_open_date"]))==false){
                        $this->setAlert(parent::getTranslates("test_open_date_error"));
                        $is_correct = false;
                    }
                }

                // Results Date validation
                $format = 'd/m/Y H:i:s';
                if (!isset($_POST["test_results_date"])) {
                    $this->setAlert(parent::getTranslates("test_results_date_error"));
                    $is_correct = false;
                }else{

                    if(($test_results = DateTime::createFromFormat($format, $_POST["test_results_date"]))==false){
                        $this->setAlert(parent::getTranslates("test_results_date_error"));
                        $is_correct = false;
                    }
                }

                if(!$is_correct){
                    $_POST["step"] = 3;
                    return $this->testAddForm();
                }

                //Validate dates
                //UK format conversion
                $format = 'Y-m-d H:i:s';

                if ($test_open >= $test_expire){
                    $this->setAlert(parent::getTranslates("test_open_date_error2"));
                    $is_correct = false;
                }

                if ($test_results < $test_expire){
                    $this->setAlert(parent::getTranslates("test_results_date_error2"));
                    $is_correct = false;
                }

                if(!$is_correct){
                    $_POST["step"] = 3;
                    return $this->testAddForm();
                }


                $test = new Test();
                $id_topic = (int)$_POST["topic_selected"];


                $id_test = $test->insertTest($id_topic, strtolower($test_type),$test_title,$test_description, $test_expire->format($format), $test_open->format($format), $test_results->format($format));


                //Add Question Form
                $output .= $this->loadQuestionForm($id_test);



                $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-home size-36"></i>' .parent::getTranslates("main_menu") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';


                //header("Location: main.php");


            } elseif ($_POST["step"] == 5) {
                if (!isset($_POST["id_test"])) {
                    $this->setAlert(parent::getTranslates("test_not_found"));
                    $_POST["step"] = 3;
                    return $this->testAddForm();
                }
                if (!isset($_POST["id_question"])) {
                    $this->setAlert(parent::getTranslates("question_not_found"));
                    $_POST["step"] = 4;
                    return $this->testAddForm();
                }

                //Edit Answer Form
                $output .= $this->loadAnswerForm($_POST["id_question"]);



                $output .= '<div class="row collapse">
                            <div class="small-6 large-6 columns">';
                /*$output .= '<form action="main.php" method="post">';
                $output .= '<input type="hidden" name="test_option" value="2">';
                $output .= '<input type="hidden" name="id_test" value="' .$_POST["id_test"] .'">';
                $output .= '<input type="hidden" name="no_save" value="1">';
                $output .= '<input type="hidden" name="step" value="4">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-arrow-left size-36"></i>' .parent::getTranslates("return_question_edit") . '</div>';
                $output .= '</form>';*/
                $output .= '</div>';
                $output .= '<div class="small-6 large-6 columns">';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-home size-36"></i>' .parent::getTranslates("main_menu") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
            }
        }

        return $output;
    }

    public function testEditForm(){
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {

            if ($_POST["step"] == 1) {

                $user = new User();
                $course = new Course();
                $courses = $course->getCoursesCreated();

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="test_option" value="2">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<label>' . parent::getTranslates("select_test_course") . '</label>';
                if (!$courses) {
                    $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <p>' . parent::getTranslates("no_courses_created") . '</p>
                                </div>

                            </div>';
                } else {

                    for ($i = 0; $i < count($courses["id_course"]); $i++) {
                        if ($i % 2 == 0) {
                            $row_color = '#D8D8D8';
                        } else {
                            $row_color = '#B0B0B0';
                        }
                        $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                                <div class="small-12 large-12 columns">
                                <input type="radio" name="course_selected" value="' . $courses["id_course"][$i] . '"  id="edit_course_' . $courses["id_course"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $courses["id_course"][$i] . '" style="font-weight: bolder;">' . $courses["name"][$i] . '</label>

                                </div>

                            </div>';
                    }
                }
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';


            }elseif ($_POST["step"] == 2) {

                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("no_course_selected"));
                    $is_correct = false;
                    $_POST["step"] = 1;
                    return $this->testEditForm();
                }
                $id_course = (int)$_POST["course_selected"];



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

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_selected" value="' .$id_course . '">';
                $output .= '<input type="hidden" name="test_option" value="2">';
                $output .= '<input type="hidden" name="step" value="3">';
                $output .= '<label>' . parent::getTranslates("select_topic") . '</label>';
                if (!$topics) {
                    $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                } else {
                    if (count($topics["id_topic"])<1){
                        $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                    }else {

                        for ($i = 0; $i < count($topics["id_topic"]); $i++) {
                            if ($i % 2 == 0) {
                                $row_color = '#D8D8D8';
                            } else {
                                $row_color = '#B0B0B0';
                            }
                            $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                            <div class="small-12 large-12 columns">
                            <input type="radio" name="topic_selected" value="' . $topics["id_topic"][$i] . '"  id="edit_course_' . $topics["id_topic"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $topics["id_topic"][$i] . '" style="font-weight: bolder;">' . $topics["title"][$i] . '</label>

                            </div>

                        </div>';
                        }
                        $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                        $output .= '</form>';
                        $output .= '</div>';
                        $output .= '</div>';
                        $output .= '</div>';
                    }
                }


            }elseif ($_POST["step"] == 3) {

                if (!isset($_POST["topic_selected"])) {
                    $this->setAlert(parent::getTranslates("no_topic_selected"));
                    $is_correct = false;
                    $_POST["step"] = 2;
                    return $this->testEditForm();
                }

                $id_topic = (int)$_POST["topic_selected"];

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

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="topic_selected" value="' .$id_topic . '">';
                $output .= '<input type="hidden" name="test_option" value="2">';
                $output .= '<input type="hidden" name="step" value="4">';
                $output .= '<label>' . parent::getTranslates("select_test") . '</label>';
                if (!$tests) {
                    $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_tests_created") . '</p>
                            </div>

                        </div>';
                } else {
                    if (count($tests["id_test"])<1){
                        $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_tests_created") . '</p>
                            </div>

                        </div>';
                    }else {

                        for ($i = 0; $i < count($tests["id_test"]); $i++) {
                            if ($i % 2 == 0) {
                                $row_color = '#D8D8D8';
                            } else {
                                $row_color = '#B0B0B0';
                            }
                            $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                            <div class="small-12 large-12 columns">
                            <input type="radio" name="test_selected" value="' . $tests["id_test"][$i] . '"  id="edit_test_' . $tests["id_test"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_test_' . $tests["id_test"][$i] . '" style="font-weight: bolder;">' . $tests["title"][$i] . '</label>

                            </div>

                        </div>';
                        }
                        $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                        $output .= '</form>';
                        $output .= '</div>';
                        $output .= '</div>';
                        $output .= '</div>';
                    }
                }


            } elseif ($_POST["step"] == 4) {

                // Validate fields
                if (!isset($_POST["test_selected"])) {
                    $this->setAlert(parent::getTranslates("no_test_selected"));
                    $is_correct = false;
                    $_POST["step"] = 3;
                    return $this->testEditForm();
                }



                $id_test = (int)$_POST["test_selected"];
                $test  = new Test();
                $tests = $test->getTest($id_test);

                if(count($tests["id_test"])<1){
                    $this->setAlert(parent::getTranslates("no_test_selected"));
                   $is_correct = false;
                    $_POST["step"] = 3;
                    return $this->testEditForm();
                }

                //UK format conversion
                $format = 'Y-m-d H:i:s';
                $expire_date = DateTime::createFromFormat($format, $tests["expire_date"][0]);
                $open_date =DateTime::createFromFormat($format, $tests["open_date"][0]);
                $results_date =DateTime::createFromFormat($format, $tests["results_date"][0]);;


                $format = 'd/m/Y H:i:s';



                // edit test form
                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_test") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="test_option" value="2">';
                $output .= '<input type="hidden" name="step" value="5">';
                $output .= '<input type="hidden" name="test_selected" value="' . $id_test .'">';


                    // Test type Dropdown
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_type") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                                <input style="width:150px; float:left;" readonly id="test_type" type="text" placeholder="' .parent::getTranslates("test_type_hint") .  '" name="test_type" value="' . $tests["test_type"][0].'">
                                <a href="#coursesoptions_anchor" style="width:70px;height:37px;padding-top:10px;" class="button fi-arrow-down" data-dropdown="drop_test_type"></a>
                            <ul id="drop_test_type" class="tiny f-dropdown" data-dropdown-content>
                              <li><a href="#coursesoptions_anchor" onclick="$(\'#test_type\').val(\'Exercise\')">' .parent::getTranslates("exercise") .  '</a></li>
                              <li><a href="#coursesoptions_anchor" onclick="$(\'#test_type\').val(\'Practice\')">' .parent::getTranslates("practice") .  '</a></li>
                              <li><a href="#coursesoptions_anchor" onclick="$(\'#test_type\').val(\'Partial\')">' .parent::getTranslates("partial") .  '</a></li>
                        </ul>
                        </div>
                    </div>';

                // Test title
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_title") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' .parent::getTranslates("test_title_hint") .  '" name="test_title" value="' . $tests["title"][0].'">
                        </div>
                    </div>';

                // Test Description
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_description") .  '</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' .parent::getTranslates("test_description_hint") .  '" name="test_description" value="' . $tests["description"][0].'">
                        </div>
                    </div>';



                // Test Expire Date
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_expire_date") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" id="datetimepicker1" style="width:155px" placeholder="' .parent::getTranslates("test_expire_date_hint") .  '" name="test_expire_date" value="' . $expire_date->format($format) .'">
                            <script> jQuery(\'#datetimepicker1\').datetimepicker({format:\'d/m/Y H:i:s\', mask:true, defaultTime:\'00:00\'});</script>
                        </div>
                    </div>';

                // Test Open Date
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_open_date") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" id="datetimepicker2" style="width:155px" placeholder="' .parent::getTranslates("test_open_date_hint") .  '" name="test_open_date" value="' . $open_date->format($format) . '">
                            <script> jQuery(\'#datetimepicker2\').datetimepicker({format:\'d/m/Y H:i:s\', mask:true, defaultTime:\'00:00\'});</script>
                        </div>
                    </div>';

                // Test Results Date
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' .parent::getTranslates("test_results_date") .  '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" id="datetimepicker3" style="width:155px" placeholder="' .parent::getTranslates("test_results_date_hint") .  '" name="test_results_date" value="' . $results_date->format($format) . '">
                            <script> jQuery(\'#datetimepicker3\').datetimepicker({format:\'d/m/Y H:i:s\', mask:true, defaultTime:\'00:00\'});</script>
                        </div>
                    </div>';



                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' .parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';





            } elseif ($_POST["step"] == 5) {
                $is_correct = true;
                $test_title = "";
                $test_type = "";
                $test_description = "";
                $test_expire = "";
                $test_open = "";
                $test_results = "";

                // Fields validation
                if (!isset($_POST["test_selected"])) {
                    $this->setAlert(parent::getTranslates("topic_not_selected"));
                    $_POST["step"] = 2;
                    return $this->testEditForm();
                }

                // Test type validation
                if(!isset($_POST["edit"])){
                    if (!isset($_POST["test_type"])) {
                        $this->setAlert(parent::getTranslates("test_type_error"));
                        $is_correct = false;
                    } else {
                        if ($_POST["test_type"] == "") {
                            $this->setAlert(parent::getTranslates("test_type_error"));
                            $is_correct = false;
                        }
                        $test_type = $_POST["test_type"];
                    }

                    // Test description validation
                    if (isset($_POST["test_description"])) {
                        $test_description = $_POST["test_description"];

                    }

                    // Test title validation
                    if (!isset($_POST["test_title"])) {
                        $this->setAlert(parent::getTranslates("test_title_error"));
                        $is_correct = false;
                    } else {
                        $test_title = $_POST["test_title"];
                        if (strlen($test_title) < 3) {
                            $this->setAlert(parent::getTranslates("test_title_error"));
                            $is_correct = false;
                        }
                    }


                    // Expire Date validation
                    $format = 'd/m/Y H:i:s';
                    if (!isset($_POST["test_expire_date"])) {
                        $this->setAlert(parent::getTranslates("test_expire_date_error"));
                        $is_correct = false;
                    } else {

                        if (($test_expire = DateTime::createFromFormat($format, $_POST["test_expire_date"])) == false) {
                            $this->setAlert(parent::getTranslates("test_expire_date_error"));
                            $is_correct = false;
                        }
                    }

                    // Open Date validation
                    $format = 'd/m/Y H:i:s';
                    if (!isset($_POST["test_open_date"])) {
                        $this->setAlert(parent::getTranslates("test_open_date_error"));
                        $is_correct = false;
                    } else {

                        if (($test_open = DateTime::createFromFormat($format, $_POST["test_open_date"])) == false) {
                            $this->setAlert(parent::getTranslates("test_open_date_error"));
                            $is_correct = false;
                        }
                    }

                    // Results Date validation
                    $format = 'd/m/Y H:i:s';
                    if (!isset($_POST["test_results_date"])) {
                        $this->setAlert(parent::getTranslates("test_results_date_error"));
                        $is_correct = false;
                    } else {

                        if (($test_results = DateTime::createFromFormat($format, $_POST["test_results_date"])) == false) {
                            $this->setAlert(parent::getTranslates("test_results_date_error"));
                            $is_correct = false;
                        }
                    }

                    if (!$is_correct) {
                        $_POST["step"] = 3;
                        return $this->testEditForm();
                    }

                    //Validate dates
                    //UK format conversion
                    $format = 'Y-m-d H:i:s';

                    if ($test_open >= $test_expire) {
                        $this->setAlert(parent::getTranslates("test_open_date_error2"));
                        $is_correct = false;
                    }

                    if ($test_results < $test_expire) {
                        $this->setAlert(parent::getTranslates("test_results_date_error2"));
                        $is_correct = false;
                    }

                    if (!$is_correct) {
                        $_POST["step"] = 3;
                        return $this->testEditForm();
                    }
                }

                $test = new Test();
                $id_test = (int)$_POST["test_selected"];

                if(!isset($_POST["edit"]))
                    $test->updateTest( $id_test, strtolower($test_type),$test_title,$test_description, $test_expire->format($format), $test_open->format($format), $test_results->format($format));

                //Add Question Form
                $output .= $this->loadQuestionForm($id_test);



                $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-home size-36"></i>' .parent::getTranslates("main_menu") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';


                //header("Location: main.php");


            } elseif ($_POST["step"] == 6) {
                if (!isset($_POST["id_test"])) {
                    $this->setAlert(parent::getTranslates("test_not_found"));
                    $_POST["step"] = 3;
                    return $this->testEditForm();
                }
                if (!isset($_POST["id_question"])) {
                    $this->setAlert(parent::getTranslates("question_not_found"));
                    $_POST["step"] = 5;
                    return $this->testEditForm();
                }

                //Edit Answer Form
                $output .= $this->loadAnswerForm($_POST["id_question"]);



                $output .= '<div class="row collapse">
                            <div class="small-6 large-6 columns">';
                /*$output .= '<form action="main.php" method="post">';
                $output .= '<input type="hidden" name="test_option" value="2">';
                $output .= '<input type="hidden" name="id_test" value="' .$_POST["id_test"] .'">';
                $output .= '<input type="hidden" name="step" value="' . ((int)$_POST["step"]-1). '">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-arrow-left size-36"></i>' .parent::getTranslates("return_question_edit") . '</div>';
                $output .= '</form>';*/
                $output .= '</div>';
                $output .= '<div class="small-6 large-6 columns">';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-home size-36"></i>' .parent::getTranslates("main_menu") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
            }
        }

        return $output;
    }

    public function topicDeleteForm(){
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {

            if ($_POST["step"] == 1) {

                $user = new User();
                $course = new Course();
                $courses = $course->getCoursesCreated();

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("delete_topic") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="topic_option" value="3">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<label>' . parent::getTranslates("select_topic_course") . '</label>';
                if (!$courses) {
                    $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <p>' . parent::getTranslates("no_courses_created") . '</p>
                                </div>

                            </div>';
                } else {

                    for ($i = 0; $i < count($courses["id_course"]); $i++) {
                        if ($i % 2 == 0) {
                            $row_color = '#D8D8D8';
                        } else {
                            $row_color = '#B0B0B0';
                        }
                        $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                                <div class="small-12 large-12 columns">
                                <input type="radio" name="course_selected" value="' . $courses["id_course"][$i] . '"  id="edit_course_' . $courses["id_course"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $courses["id_course"][$i] . '" style="font-weight: bolder;">' . $courses["name"][$i] . '</label>

                                </div>

                            </div>';
                    }
                }
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';


            }elseif ($_POST["step"] == 2) {

                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("no_course_selected"));
                    $is_correct = false;
                    $_POST["step"] = 1;
                    return $this->topicDeleteForm();
                }
                $id_course = (int)$_POST["course_selected"];



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

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("delete_topic") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_selected" value="' .$id_course . '">';
                $output .= '<input type="hidden" name="topic_option" value="3">';
                $output .= '<input type="hidden" name="step" value="3">';
                $output .= '<label>' . parent::getTranslates("select_topic") . '</label>';
                if (!$topics) {
                    $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                } else {
                    if (count($topics["id_topic"])<1){
                        $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                    }else {

                        for ($i = 0; $i < count($topics["id_topic"]); $i++) {
                            if ($i % 2 == 0) {
                                $row_color = '#D8D8D8';
                            } else {
                                $row_color = '#B0B0B0';
                            }
                            $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                            <div class="small-12 large-12 columns">
                            <input type="radio" name="topic_selected" value="' . $topics["id_topic"][$i] . '"  id="edit_course_' . $topics["id_topic"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $topics["id_topic"][$i] . '" style="font-weight: bolder;">' . $topics["title"][$i] . '</label>

                            </div>

                        </div>';
                        }
                        $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                        $output .= '</form>';
                        $output .= '</div>';
                        $output .= '</div>';
                        $output .= '</div>';
                    }
                }


            } elseif ($_POST["step"] == 3) {

                // Validate fields
                 if (!isset($_POST["topic_selected"])) {
                    $this->setAlert(parent::getTranslates("no_topic_selected"));
                    $is_correct = false;
                    $_POST["step"] = 2;
                    return $this->topicDeleteForm();
                }




                $id_topic = (int)$_POST["topic_selected"];

                $topic = new Topic();
                $topics = $topic->getTopic($id_topic);




                // delete topic form
                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= '<h1>' . parent::getTranslates("delete_topic") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="topic_option" value="3">';
                $output .= '<input type="hidden" name="step" value="4">';
                $output .= '<input type="hidden" name="topic_selected" value="' .$id_topic . '">';
                $output .= '<div class="row collapse">
                        <div class="small-12 large-12 columns">
                            <h3>' .parent::getTranslates("topic_delete_warning") .  '<br>' . $topics["title"][0] .'</h3>
                        </div>

                    </div>';

                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-trash size-36"></i>' .parent::getTranslates("delete") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';

            } elseif ($_POST["step"] == 4) {
                $is_correct = true;
                $topic_title = "";
                $topic_description = "";
                $topic_order = 0;

                if (!isset($_POST["topic_selected"])) {
                    $this->setAlert(parent::getTranslates("topic_not_selected"));

                    $_POST["step"] = 2;
                    return $this->topicEditForm();
                }


                $topic = new Topic();
                $id_topic = (int)$_POST["topic_selected"];


                $topic->deleteTopic($id_topic);


                //header("Location: main.php");
            }
        }

        return $output;

    }

    public function topicAddForm(){
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {

            if ($_POST["step"] == 1) {

                $user = new User();
                $course = new Course();
                $courses = $course->getCoursesCreated();

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("add_new_topic") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="topic_option" value="1">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<label>' . parent::getTranslates("select_topic_course") . '</label>';
                if (!$courses) {
                    $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <p>' . parent::getTranslates("no_courses_created") . '</p>
                                </div>

                            </div>';
                } else {

                    for ($i = 0; $i < count($courses["id_course"]); $i++) {
                        if ($i % 2 == 0) {
                            $row_color = '#D8D8D8';
                        } else {
                            $row_color = '#B0B0B0';
                        }
                        $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                                <div class="small-12 large-12 columns">
                                <input type="radio" name="course_selected" value="' . $courses["id_course"][$i] . '"  id="edit_course_' . $courses["id_course"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $courses["id_course"][$i] . '" style="font-weight: bolder;">' . $courses["name"][$i] . '</label>

                                </div>

                            </div>';
                    }
                }
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';

            } elseif ($_POST["step"] == 2) {

                // Validate fields
                $is_correct = true;

                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("no_course_selected"));
                    $is_correct = false;
                }


                // If fields and save incorrect return to step 1 showing alerts
                if (!$is_correct) {
                    $_POST["step"] = 1;
                    return $this->topicAddForm();
                }
                $id_course = (int)$_POST["course_selected"];

                // Add topic form
                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("add_new_topic") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_selected" value="' . $id_course . '">';
                $output .= '<input type="hidden" name="topic_option" value="1">';
                $output .= '<input type="hidden" name="step" value="3">';
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' . parent::getTranslates("topic_title") . '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' . parent::getTranslates("topic_title_hint") . '" name="topic_title">
                        </div>
                    </div>';
                $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <span class="prefix">' . parent::getTranslates("topic_description") . '</span>
                                </div>
                                <div class="small-9 large-9 columns">
                                    <input type="text" placeholder="' . parent::getTranslates("topic_description_hint") . '" name="topic_description">
                                </div>
                            </div>';
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' . parent::getTranslates("topic_order") . '</span>
                        </div>
                        <div class="columns" = style="width:50px;float:left;">
                            <input type="text" placeholder="' . parent::getTranslates("topic_order_hint") . '" name="topic_order">
                        </div>
                    </div>';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';
            } elseif ($_POST["step"] == 3) {
                $is_correct = true;
                $topic_title = "";
                $topic_description = "";
                $topic_order = 0;

                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("course_not_selected"));
                    $is_correct = false;
                }
                if (!$is_correct) {
                    $_POST["step"] = 1;
                    return $this->topicAddForm();
                }

                if (!isset($_POST["topic_title"])) {
                    $this->setAlert(parent::getTranslates("topic_title_error"));
                    $is_correct = false;
                }else{
                    $topic_title = $_POST["topic_title"];
                    if (strlen($topic_title)< 1 ){
                        $this->setAlert(parent::getTranslates("topic_title_error"));
                        $is_correct = false;
                    }

                }

                if (!isset($_POST["topic_description"])){
                    $this->setAlert(parent::getTranslates("topic_description_error"));
                    $is_correct = false;
                }else{
                    $topic_description = $_POST["topic_description"];
                }

                if (!$is_correct) {
                    $_POST["step"] = 2;
                    return $this->topicAddForm();
                }

                if (!isset($_POST["topic_order"])){
                    $topic_order = 0;
                }else{
                    $topic_order = (int)$_POST["topic_order"];
                }


                $topic = new Topic();
                $id_course = $_POST["course_selected"];

                $is_correct = $topic->insertTopic((int)$id_course, $topic_title , $topic_description, $topic_order);



                //header("Location: main.php");

            }
        }

        return $output;
    }


    public function topicEditForm(){
        $this->col_width = 3;
        $output = "";
        $course = new Course();

        if(isset($_POST["step"])) {

            if ($_POST["step"] == 1) {

                $user = new User();
                $course = new Course();
                $courses = $course->getCoursesCreated();

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_topic") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="topic_option" value="2">';
                $output .= '<input type="hidden" name="step" value="2">';
                $output .= '<label>' . parent::getTranslates("select_topic_course") . '</label>';
                if (!$courses) {
                    $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <p>' . parent::getTranslates("no_courses_created") . '</p>
                                </div>

                            </div>';
                } else {

                    for ($i = 0; $i < count($courses["id_course"]); $i++) {
                        if ($i % 2 == 0) {
                            $row_color = '#D8D8D8';
                        } else {
                            $row_color = '#B0B0B0';
                        }
                        $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                                <div class="small-12 large-12 columns">
                                <input type="radio" name="course_selected" value="' . $courses["id_course"][$i] . '"  id="edit_course_' . $courses["id_course"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $courses["id_course"][$i] . '" style="font-weight: bolder;">' . $courses["name"][$i] . '</label>

                                </div>

                            </div>';
                    }
                }
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';


            }elseif ($_POST["step"] == 2) {

                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("no_course_selected"));
                    $is_correct = false;
                    $_POST["step"] = 1;
                    return $this->topicEditForm();
                }
                $id_course = (int)$_POST["course_selected"];



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

                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_topic") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_selected" value="' .$id_course . '">';
                $output .= '<input type="hidden" name="topic_option" value="2">';
                $output .= '<input type="hidden" name="step" value="3">';
                $output .= '<label>' . parent::getTranslates("select_topic") . '</label>';
                if (!$topics) {
                    $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                } else {
                    if (count($topics["id_topic"])<1){
                        $output .= '<div class="row collapse">
                            <div class="small-12 large-12 columns">
                                <p>' . parent::getTranslates("no_topics_created") . '</p>
                            </div>

                        </div>';
                    }else {

                        for ($i = 0; $i < count($topics["id_topic"]); $i++) {
                            if ($i % 2 == 0) {
                                $row_color = '#D8D8D8';
                            } else {
                                $row_color = '#B0B0B0';
                            }
                            $output .= '<div class="row collapse" style="background-color: ' . $row_color . '">
                            <div class="small-12 large-12 columns">
                            <input type="radio" name="topic_selected" value="' . $topics["id_topic"][$i] . '"  id="edit_course_' . $topics["id_topic"][$i] . '" style="margin-top: 15px; margin-left: 45px;"><label for="edit_course_' . $topics["id_topic"][$i] . '" style="font-weight: bolder;">' . $topics["title"][$i] . '</label>

                            </div>

                        </div>';
                        }
                        $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                        $output .= '</form>';
                        $output .= '</div>';
                        $output .= '</div>';
                        $output .= '</div>';
                    }
                }


                } elseif ($_POST["step"] == 3) {

                // Validate fields
                $is_correct = true;

                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("no_course_selected"));
                    $is_correct = false;
                    $_POST["step"] = 1;
                    return $this->topicEditForm();
                }

                if (!isset($_POST["topic_selected"])) {
                    $this->setAlert(parent::getTranslates("no_topic_selected"));
                    $is_correct = false;
                    $_POST["step"] = 2;
                    return $this->topicEditForm();
                }



                // If fields and save incorrect return to step 1 showing alerts
                if (!$is_correct) {
                    $_POST["step"] = 1;
                    return $this->topicEditForm();
                }
                $id_course = (int)$_POST["course_selected"];
                $id_topic = (int)$_POST["topic_selected"];

                $topic = new Topic();
                $topics = $topic->getTopic($id_topic);



                // Edit topic form
                $output .= '<div class="small-9 large-9 columns" style="border-left:solid 1px;border-left-color: #DDDDDD;min-height: 40rem;">';
                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" >';
                $output .= $this->alerts_output;
                $output .= '<h1>' . parent::getTranslates("edit_topic") . '</h1><hr>';
                $output .= '<p><strong><h3>' . parent::getTranslates("step") . ' ' . (int)$_POST["step"] . '</h3><strong><p></p>';
                $output .= '<form action="main.php#coursesoptions_anchor" method="post">';
                $output .= '<input type="hidden" name="course_selected" value="' . $id_course . '">';
                $output .= '<input type="hidden" name="topic_selected" value="' . $id_topic . '">';
                $output .= '<input type="hidden" name="topic_option" value="2">';
                $output .= '<input type="hidden" name="step" value="4">';
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' . parent::getTranslates("topic_title") . '*</span>
                        </div>
                        <div class="small-9 large-9 columns">
                            <input type="text" placeholder="' . parent::getTranslates("topic_title_hint") . '" name="topic_title" value="' . $topics["title"][0] . '">
                        </div>
                    </div>';
                $output .= '<div class="row collapse">
                                <div class="small-3 large-3 columns">
                                    <span class="prefix">' . parent::getTranslates("topic_description") . '</span>
                                </div>
                                <div class="small-9 large-9 columns">
                                    <input type="text" placeholder="' . parent::getTranslates("topic_description_hint") . '" name="topic_description" value="' . $topics["description"][0] . '">
                                </div>
                            </div>';
                $output .= '<div class="row collapse">
                        <div class="small-3 large-3 columns">
                            <span class="prefix">' . parent::getTranslates("topic_order") . '</span>
                        </div>
                        <div class="columns" = style="width:50px;float:left;">
                            <input type="text" placeholder="' . parent::getTranslates("topic_order_hint") . '" name="topic_order" value="' . $topics["order_num"][0] . '">
                        </div>
                    </div>';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button right"><i class="fi-save size-36"></i>' . parent::getTranslates("save") . '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</div>';
            } elseif ($_POST["step"] == 4) {
                $is_correct = true;
                $topic_title = "";
                $topic_description = "";
                $topic_order = 0;

                if (!isset($_POST["topic_selected"])) {
                    $this->setAlert(parent::getTranslates("topic_not_selected"));

                    $_POST["step"] = 2;
                    return $this->topicEditForm();
                }
                if (!isset($_POST["course_selected"])) {
                    $this->setAlert(parent::getTranslates("topic_not_selected"));

                    $_POST["step"] = 1;
                    return $this->topicEditForm();
                }


                if (!isset($_POST["topic_title"])) {
                    $this->setAlert(parent::getTranslates("topic_title_error"));
                    $is_correct = false;
                }else{
                    $topic_title = $_POST["topic_title"];
                    if (strlen($topic_title)< 1 ){
                        $this->setAlert(parent::getTranslates("topic_title_error"));
                        $is_correct = false;
                    }

                }

                if (!isset($_POST["topic_description"])){
                    $this->setAlert(parent::getTranslates("topic_description_error"));
                    $is_correct = false;
                }else{
                    $topic_description = $_POST["topic_description"];
                }

                if (!$is_correct) {
                    $_POST["step"] = 3;
                    return $this->topicEditForm();
                }

                if (!isset($_POST["topic_order"])){
                    $topic_order = 0;
                }else{
                    $topic_order = (int)$_POST["topic_order"];
                }


                $topic = new Topic();
                $id_topic = (int)$_POST["topic_selected"];


                $topic->updateTopic($id_topic, $topic_title , $topic_description, $topic_order);


                //header("Location: main.php");
            }
        }

        return $output;
    }

}