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

include_once './controllers/ModuleController.php';
include_once './classes/Course.php';


class ItsModule extends ModuleController
{

    var $output_historic_form;
    var $output_its_results_form;
    var $errors;
    var $success;

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

        $this->errors = "";
        $this->success = "";

        // MAIN FORM
        if (!isset($_POST["form"])) {




            // DELETE CURRENT COURSE
            if (isset($_POST["delete_current_course"])){
                $this->output_its_results_form = $this->deleteCurrentCourseForm($_POST["delete_current_course"]);
            }else{
                // RESULTS
                $this->output_its_results_form = $this->getITSResultsOutput();
            }

            $this->output_historic_form = $this->getHistoricConfiguredCourses();

            parent::displayModule("its");
        } else {

            //CONFIGURE HISTORIC FORM
            if ($_POST["form"] == "configure_historic") {

                //STEP 1 : Select course for prediction
                if (!isset($_POST["course_for_prediction"])){
                    $this->output_historic_form = $this->getCurrentCourses();
                    parent::displayModule("configure_historic");

                //STEP 2 : Select historic courses
                }elseif (!isset($_POST["historic_courses"])){
                    $this->output_historic_form = $this->getHistoricCourses();
                    parent::displayModule("configure_historic");

                //STEP 3 : Validation
                }elseif(!isset($_POST["save_historic"])){
                    $this->output_historic_form = $this->getSaveForm();
                    parent::displayModule("configure_historic");


                //STEP 4 : Save historic courses and generate historic excel files
                }else {
                    $this->output_historic_form = $this->getHistoricSaveResults();
                    parent::displayModule("configure_historic");
                }



            }else{
                // MAIN FORM
                $this->output_its_results_form = $this->getITSResultsOutput();
                $this->output_historic_form = $this->getHistoricConfiguredCourses();

                parent::displayModule("its");
            }
        }


        if (isset($_POST["form"])) {
            if ($_POST["form"] == "configure_historic") {
                parent::displayModule("configure_historic");
            }
        } else parent::displayModule("its");

        parent::destroy();

    }



    public function deleteCurrentCourseForm($id_current_course){
        $output = "";


        if(isset($_POST["step"])){
            if($_POST["step"] == "1"){
                // Remove Warning
                $course = new Course();

                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns" style="padding: 20px;">';
                $output .= '<h3>' . parent::getTranslates("delete_warning") . '</h3>';
                $output .= '</div>';
                $output .= '</div>';

                $output .= '<div class="row">';
                $output .= '<div class="small-12 large-12 columns">';
                $output .=  '<p style="background-color:#F78181;padding:15px;"><i class="fi-x size-24" style="margin-right: 20px;margin-left: 20px;"></i><b>' . $course->getCourseName($id_current_course) . '</b></p>';
                $output .= '</div>';
                $output .= '</div>';


                // Button
                $output .= '<div class="row">';
                $output .= '<div class="small-6 large-6 columns">';
                $output .= '<form action="main.php" method="post">';
                $output .= '<div onclick="$(this).closest(\'form\').submit()" class="button left"><i class="fi-home size-36"></i>' . parent::getTranslates("main_menu"). '</div>';
                $output .= '</form>';
                $output .= '</div>';
                $output .= '<div class="small-6 large-6 columns">';
                $output .= '<form action="main.php" method="post">';
                $output .= '<input type="hidden" name="delete_current_course" value="'. $id_current_course .'">';
                $output .= '<input type="hidden" name="step" value="2">';
                $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>';

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

                // Remove current course
                $course = new Course();
                $success = false;

                $success = $course->deleteHistoricCourse($id_current_course);

                if ($success){
                    $this->success .= parent::getTranslates('delete_success');
                    parent::displayModule("success");

                }else{
                    $this->errors .= parent::getTranslates('delete_error');
                    parent::displayModule("errors");

                }

                return $this->getITSResultsOutput();

            }else{
                return $this->getITSResultsOutput();
            }
        }else{
            return $this->getITSResultsOutput();
        }
    }

    public function getHistoricSaveResults()
    {
        $course = new Course();

        $courses = $course->getCoursesCreated();
        $historic_courses = array();

        for ($i = 0; $i < count($courses["id_course"]); $i++) {
            if (isset($_POST["historic_" . $courses["id_course"][$i]])) {
                array_push($historic_courses, $_POST["historic_" . $courses["id_course"][$i]]);
            }
        }


        // Delete old assignations
        $course->deleteHistoricCourse((int)$_POST["course_for_prediction"]);

        // Save new assignations
        for ($i = 0; $i < count($historic_courses); $i++) {
            $course->insertHistoric((int)$_POST["course_for_prediction"],(int)$historic_courses[$i]);
        }

        // Generate excel historic files
        for ($i = 0; $i < count($historic_courses); $i++) {
            $this->generateExcelFile((int)$historic_courses[$i], 'historic');
        }


        $output =  "<div class='row'>";
        $output .= "<div class='panel small-12 large-12 columns' style='background-color: #81F79F'>";
        $output .= '<p><h1><i class="fi-check size-36" ></i>'.parent::getTranslates("success_historic") . '</h1></p>';
        $output .= "</div>";
        $output .= "</div>";

        return $output;

    }

    public function generateExcelFile($id_course, $type)
    {


        //PHPExcel library
        include_once "./modules/teacher/" . $this->name  . "/plugins/PHPExcel.php";
        include_once "./modules/teacher/" . $this->name  . "/plugins/PHPExcel/Writer/Excel2007.php";
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        $row_excel = array();
        $row_count = 1;

        //Get course tests
        $course = new Course();
        $tests = $course->getTestsByCourse($id_course);

        //Get users who has inscription
        $users = $course->getStudentInscriptions($id_course);


        $user = new User();


        // Excel Header
        $exercise_num = 0;
        $practice_num = 0;
        $partial_num = 0;

        // "id_user ";
        array_push($row_excel,"ID_user");

            for ($i=0; $i<count($tests["id_test"]); $i++) {
                if($tests["test_type"][$i] == "exercise"){
                    $exercise_num++;
                    array_push($row_excel,"E" . $exercise_num);
                    //echo "E" . $exercise_num;

                }
                if($tests["test_type"][$i] == "practice"){
                    $practice_num++;
                    array_push($row_excel,"P" . $practice_num);
                    //echo "P" . $practice_num;

                }
                if($tests["test_type"][$i] == "partial"){
                    $partial_num++;
                    array_push($row_excel,"Parcial" . $partial_num);
                    //echo "Parcial" . $partial_num;

                }
            }

        if ($type == "historic")array_push($row_excel,"NotaFinal");
        //echo "notaFinal";
        //echo "<br>";
        $objPHPExcel->getActiveSheet()->fromArray($row_excel, NULL, 'A' . $row_count);
        $row_count++;


        // Excel Data with marks and final mark

        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]);
            for ($i=0; $i<count($tests["id_test"]); $i++){

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



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

            // Calculate Final mark
            if ($type == "historic") {
                $final_mark_ex = ($total_ex_mark / $total_ex_n) * ($course->getCourseExerciseWeight($id_course) / 100.);
                $final_mark_pr = ($total_pr_mark / $total_pr_n) * ($course->getCoursePracticeWeight($id_course) / 100.);
                $final_mark_pa = ($total_pa_mark / $total_pa_n) * ($course->getCoursePartialWeight($id_course) / 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);
            }

            $objPHPExcel->getActiveSheet()->fromArray($row_excel, NULL, 'A' . $row_count, true);

            $row_count++;

        }



        //$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
       // $objWriter->save("historic/historic_" . $id_course . ".xls");


        $objWriter->save("./modules/teacher/" . $this->name  . "/" . $type . "/" . $type . "_" . $id_course . ".xls");


        return true;
    }


    public function getITSResultsOutput()
    {

        $success = true;
        $has_courses_configured = false;
        $success_excel = false;
        $success_its = false;
        $courses_configured = array();
        $its_fails = array();

        $course = new Course();

        // Title
        $output = "<h4>" . parent::getTranslates("results") . "</h4>";

        // Delete all current excel files
        $files = glob("./modules/teacher/" . $this->name . "/current/*");
        foreach ($files as $file) {
            if (is_file($file))
                unlink($file);
        }

        // Check if exists courses configured
        $courses = $course->getCoursesCreated();
        for($i =0; $i<count($courses["id_course"]);$i++) {
            if($course->isCurrentCourse((int)$courses["id_course"][$i])){
                $has_courses_configured = true;
                array_push($courses_configured,(int)$courses["id_course"][$i] );

            }
        }
        if (!$has_courses_configured) return parent::getTranslates("no_courses_configured");

        // Generate Excel
        for($i =0; $i<count($courses_configured);$i++) {
            $success_excel = $this->generateExcelFile($courses_configured[$i], 'current');
        }


        // Execute ITS CBR algorithm
        $its_output_folder =" modules//teacher//". $this->name . "//output";
        $its_input_current_folder =" modules//teacher//". $this->name . "//current";
        $its_input_historic_folder =" modules//teacher//". $this->name . "//historic";
        for($i =0; $i<count($courses_configured);$i++) {
            $historic_courses = array();
            $its_historic_arguments = "";
            $historic_courses = $course->getHistoricCourses($courses_configured[$i]);
            for($j =0; $j<count($historic_courses["id_course"]);$j++) {
                $its_historic_arguments .= " historic_" . $historic_courses["id_course"][$j] . ".xls";

            }
            $its_current_arguments = " current_" . $courses_configured[$i] . ".xls";


           exec("java -jar modules//teacher//". $this->name . "//plugins//ITS_algorithm//ITS_algorithm.jar" .  $its_output_folder.
               $its_input_current_folder .
               $its_input_historic_folder.
               $its_current_arguments .
               $its_historic_arguments, $results, $return);

            if ($return)
                array_push($its_fails,$courses_configured);
            $results = array();


        }




        // Dropdown with all configured courses
        if($has_courses_configured && $success_excel) {
            $output .= '<div class="row" >';
            $output .= '<div class="small-12 large-12 columns" >';
            // Dropdown with all configured courses


            $output .= '<a href="#"  class="secondary radius button split" style="float:right"><div id="dd_title">' . $course->getCourseName($courses_configured[0]) . '</div><span data-dropdown="drop"></span></a><br>
                    <ul id="drop" class="f-dropdown" data-dropdown-content>';
            for ($i = 0; $i < count($courses_configured); $i++) {

                    $output .= '<li onclick="changeCharts(' . $courses_configured[$i] . ',\'' . $course->getCourseName($courses_configured[$i]) . '\',\'' . $_SESSION["lang"]. '\')"><a href="#">' . $course->getCourseName($courses_configured[$i]) . '</a></li>';

            }

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


            // Charts
            $output .= '<div class="row">';
            $output .= '<div class="small-12 large-12 columns"  id="its_slider">';
            $output .= ' <div class="its-results-class" style="margin:10px;height:400px;">';

            if (in_array($courses_configured[0],$its_fails, true)){
                $output .= '<div> Can\'t show this chart</div>';
            }else {

                $output .= $this->getChart1($courses_configured[0]);
                $output .= $this->getChart2($courses_configured[0], "Failed");
                $output .= $this->getChart2($courses_configured[0], "Pass");
                $output .= $this->getChart2($courses_configured[0], "Good");
                $output .= $this->getChart2($courses_configured[0], "Remarkable");
                $output .= $this->getChart2($courses_configured[0], "Outstanding");

            }

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





        }else{
            $output .= parent::getTranslates("no_courses_configured");
        }

        return $output;

    }

    public function getChart1($id_course){
        $output = "";
        $headers = "";
        $data = array_fill(0, 5, 0);


        // GET EXCEL VALUES
        $inputFileName = './modules/teacher/' . $this->name . '/output/output_current_' . $id_course. '.xls';

        //  Read your Excel workbook
        try {

            $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }

        //  Get worksheet dimensions
                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow();
                $highestColumn = $sheet->getHighestColumn();

        //  Loop through each row of the worksheet in turn
                for ($row = 1; $row <= $highestRow; $row++) {
                    //  Read a row of data into an array
                    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                        NULL,
                        TRUE,
                        FALSE);



                    if($rowData[0][count($rowData[0])-1] == "Outstanding") $data[4]++;
                    if($rowData[0][count($rowData[0])-1] == "Remarkable") $data[3]++;
                    if($rowData[0][count($rowData[0])-1] == "Good") $data[2]++;
                    if($rowData[0][count($rowData[0])-1] == "Pass") $data[1]++;
                    if($rowData[0][count($rowData[0])-1] == "Failed") $data[0]++;
                    if($rowData[0][count($rowData[0])-1] == "Excelente") $data[4]++;
                    if($rowData[0][count($rowData[0])-1] == "Notable") $data[3]++;
                    if($rowData[0][count($rowData[0])-1] == "Bien") $data[2]++;
                    if($rowData[0][count($rowData[0])-1] == "Aprobado") $data[1]++;
                    if($rowData[0][count($rowData[0])-1] == "Suspenso") $data[0]++;


                }

        $output .= '<div  style="padding:20px;padding-left:50px;">
                    <div class ="row"><b><h5>' . parent::getTranslates("chart_title1") . '</h5></b></div>

                    <canvas id="Chart1_' . $id_course. '" width="550" height="250"  ></canvas>';

        $output .= '<script>
                    var data = [
                        {
                            value: ' . $data[0] . ',
                            color:"#F7464A",
                            highlight: "#FF5A5E",
                            label: "' . parent::getTranslates('failed'). '"
                        },
                        {
                            value: ' . $data[1] . ',
                            color: "#46BFBD",
                            highlight: "#5AD3D1",
                            label: "' . parent::getTranslates('pass'). '"
                        },
                        {
                            value: ' . $data[2] . ',
                            color: "#FAA5C",
                            highlight: "#FFC870",
                            label: "' . parent::getTranslates('good'). '"
                        },
                        {
                            value: ' . $data[3] . ',
                            color: "#FDB4AC",
                            highlight: "#FFC870",
                            label: "' . parent::getTranslates('remarkable'). '"
                        },
                        {
                            value: ' . $data[4] . ',
                            color: "#FDB45C",
                            highlight: "#FFC870",
                            label: "' . parent::getTranslates('outstanding'). '"
                        }];

                   </script>';

                        $output .= '<script> var ctx = document.getElementById("Chart1_' . $id_course. '").getContext("2d");
                    var myNewChart = new Chart(ctx).Pie(data, {

                    })
                </script></div>';
        return $output;
    }

    public function getChart2($id_course, $type){
        $output = "";
        $headers = "";
        $data = array_fill(0, 5, 0);
        $data_all = "";
        $dataset = array();
        $legend_colors= array("color"=> array(),
            "id_user"=> array());
        $user = new User();


        // GET EXCEL VALUES
        $inputFileName = './modules/teacher/' . $this->name . '/output/output_current_' . $id_course. '.xls';

        //  Read your Excel workbook
        try {
            ;
            $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }

        //  Get worksheet dimensions
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();


        $output .= '<div  style="padding:20px;padding-left:50px;">
                    <div class ="row"><b><h5>' . parent::getTranslates("chart_title2_1")  . " " . parent::getTranslates(strtolower( $type)) . " " . parent::getTranslates("chart_title2_2") . '</h5></b></div>

                    <canvas id="Chart2_'. $type . '_' . $id_course. '" width="550" height="250"  ></canvas>';

        //  Loop through each row of the worksheet in turn
        $line_color = 250/($highestRow+1);
        $legend_color = $line_color;

        for ($row = 1; $row <= $highestRow; $row++) {
            $data = array();

            //  Read a row of data into an array
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                NULL,
                TRUE,
                FALSE);

            //print_r($rowData);


            if ($row==1)
                $headers = "labels: [\"" . implode('","', array_slice($rowData[0], 1, count($rowData[0]) - 2)) . "\"]";
            else {


                if ($type == "Failed") {

                    if ($rowData[0][count($rowData[0]) - 1] == $type || $rowData[0][count($rowData[0]) - 1] == "Suspenso") {
                        $data_all = implode(',', array_slice($rowData[0], 1, count($rowData[0]) - 2));
                    }
                }else if ($type ==  "Pass") {
                    if ($rowData[0][count($rowData[0]) - 1] == $type || $rowData[0][count($rowData[0]) - 1] == "Aprobado") {
                        $data_all = implode(',', array_slice($rowData[0], 1, count($rowData[0]) - 2));
                    }
                }else if ($type ==  "Good") {
                    if ($rowData[0][count($rowData[0]) - 1] == $type || $rowData[0][count($rowData[0]) - 1] == "Bien") {
                        $data_all = implode(',', array_slice($rowData[0], 1, count($rowData[0]) - 2));
                    }
                }else if ($type ==  "Remarkable") {
                    if ($rowData[0][count($rowData[0]) - 1] == $type || $rowData[0][count($rowData[0]) - 1] == "Notable") {
                        $data_all = implode(',', array_slice($rowData[0], 1, count($rowData[0]) - 2));
                    }
                }else if ($type ==  "Outstanding") {
                    if ($rowData[0][count($rowData[0]) - 1] == $type || $rowData[0][count($rowData[0]) - 1] == "Excelente") {
                        $data_all = implode(',', array_slice($rowData[0], 1, count($rowData[0]) - 2));
                    }
                }


                //add new datasetwith row values
                array_push($dataset,
                    '
                            {
                                label: "dataset_' . $rowData[0][0] . '",
                                fillColor: "rgba(50,75,50,0.2)",
                                strokeColor: "rgba(0,0,'. (int)$legend_color. ',1)",
                                pointColor: "rgba(0,0,'. (int)$legend_color. ',1)",
                                pointStrokeColor: "#fff",
                                pointHighlightFill: "#fff",
                                pointHighlightStroke: "rgba(220,220,220,1)",
                                data: [' . $data_all . ']
                            } ');

                array_push($legend_colors["color"],(int)$legend_color);
                array_push($legend_colors["id_user"],$rowData[0][0] );
                $data_all = "";
            }



            $legend_color = $legend_color + $line_color ;

        }


        // Chart data
        $output .= '<script>    var data = {
                        ' . $headers . ',
                        datasets: [' . implode(',',$dataset) . ']

                    };</script>';







        $legend = '<div class="row user-legend">';
        for($i = 0; $i< count($legend_colors["id_user"]);$i++) {
            $legend .= '<div class="small-6 large-6 columns">';
            $legend .= '<div style="float:left;width:20px;height:10px;margin:5px;background-color:rgb(0,0,' . $legend_colors["color"][$i] . ');" ></div><div style="float:left">' . $user->getUserName($legend_colors["id_user"][$i]) . '</div>';
            $legend .= '</div>';
        }
        $legend .= '</div>';


        $output .= '<script> var ctx = document.getElementById("Chart2_'. $type . '_' . $id_course. '").getContext("2d");
                    var myNewChart = new Chart(ctx).Line(data, {
                        scaleOverride : true,
                        scaleSteps : 10,
                        scaleStepWidth : 1,
                        scaleStartValue : 0,
                        bezierCurve: false
                })
                </script>';

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

        //$output .= $legend;


        return $output;
    }


    public function getHistoricConfiguredCourses(){
        $course = new Course();
        $courses = $course->getCoursesCreated();
        $output = "";

        $output .= '<hr><h4 style="color:#E0E0E0;margin-bottom: 20px;" >' . parent::getTranslates('configured_courses') . ':</h4>';

        $row_color= "#D8D8D8";


        // IF NO COURSES CONFIGURED
        $configured = false;

        for($i =0; $i<count($courses["id_course"]);$i++) {
            if($course->isCurrentCourse((int)$courses["id_course"][$i])){
                $configured = true;
            }
        }


        if(!$configured) {
            $output .= '<div class="row" style="padding:10px">';
            $output .= '<div class="small-12 medium-12 large-12 columns">';
            $output .=  parent::getTranslates("no_courses_configured");
            $output .= '</div>';
            $output .= '</div>';
            return $output;
        }

        // IF COURSES CONFIGURED
        for($i =0; $i<count($courses["id_course"]);$i++){
            if($course->isCurrentCourse((int)$courses["id_course"][$i])){
                $output .= '<form action="main.php" method="post" id="delete_current_'. (int)$courses["id_course"][$i].'">';
                $output .= '<input type="hidden" name="delete_current_course" value="'. (int)$courses["id_course"][$i].'">';
                $output .= '<input type="hidden" name="step" value="1">';
                $output .= '<div class="row" style="padding:10px;font-weight: bolder;background-color: ' .$row_color .' ">';
                $output .= '<div class="small-9 medium-9 large-9 columns">';
                $output .=  $courses["name"][$i];
                $output .= '</div>';
                $output .= '<div class="small-3 medium-3 large-3 columns">';
                $output .= '<i class="fi-trash" onclick="document.forms[\'delete_current_'. (int)$courses["id_course"][$i].'\'].submit()" style="float:right"></i>';
                $output .= '</div>';
                $output .= '</div>';
                $output .= '</form>';


                if ($row_color == "#D8D8D8") $row_color = "#B0B0B0";
                else $row_color = "#B0B0B0";

            }
        }




        return $output;
    }

    public function getCurrentCourses()
    {

        $course = new Course();
        $courses = $course->getCoursesCreated();
        $output = '<p><h5>' . parent::getTranslates("select_course_for_prediction") . ':</h5></p>';
        $output .= "<form action='main.php' method='POST'>";
        $output .= '<input type="hidden" name="form" value="configure_historic">';


        $row_color = "#B0B0B0";

        for ($i = 0; $i < count($courses["id_course"]); $i++) {
            $output .= "<div class='row' style='background-color: " .$row_color . "; margin-left:20px;margin-right:50px;'>";
            $output .= "<div class='small-12 medium-12 large-12 columns'>";
            $output .= "<input type='radio' id='course_" .$courses["id_course"][$i] . "' name='course_for_prediction' value='" .$courses["id_course"][$i] . "'>";

            $output .= "<label for='course_" .$courses["id_course"][$i]. "' style='font-weight: bolder;'>" . $courses["name"][$i] . "</label>";
            $output .= "</div>";
            $output .= "</div>";

            if ($row_color == "#B0B0B0")$row_color = "#D8D8D8";
            else $row_color = "#B0B0B0";
        }
        $output .= '<input type="submit" style="float:right;margin-top:20px;margin-right:50px;" role="button" aria-label="submit form" class="button" value="' .  parent::getTranslates('continue') . '">';
        $output .= "</form>";

        return $output;
    }


    public function getHistoricCourses()
    {

        // Validate current course
        $course = new Course();
        if (!$course->validateCurrentCourse($_POST["course_for_prediction"])){
            $this->errors .= parent::getTranslates('error1');
            parent::displayModule("errors");
            return $this->getCurrentCourses();
        }


        $courses = $course->getCoursesCreated();

        $output = '<p><h5>' . parent::getTranslates("select_historic_courses") . ':</h5></p>';
        $output .= "<form action='main.php' method='POST'>";
        $output .= '<input type="hidden" name="form" value="configure_historic">';
        $output .= '<input type="hidden" name="course_for_prediction" value="' . $_POST["course_for_prediction"]. '">';
        $output .= '<input type="hidden" name="historic_courses" value="1">';
        $row_color = "#B0B0B0";

        for ($i = 0; $i < count($courses["id_course"]); $i++) {
            if ($courses["id_course"][$i] != (int)$_POST["course_for_prediction"]) {

                $output .= "<div class='row' style='background-color: " . $row_color . ";margin-right:50px;margin-left:20px;'>";
                $output .= "<div class='small-12 medium-12 large-12 columns'>";
                $output .= "<input type='checkbox' id='course_" . $courses["id_course"][$i] . "' name='course_" . $courses["id_course"][$i] . "' value='" . $courses["id_course"][$i] . "'>";

                $output .= "<label for='course_" . $courses["id_course"][$i] . "' style='font-weight: bolder;'>" . $courses["name"][$i] . "</label>";
                $output .= "</div>";
                $output .= "</div>";

                if ($row_color == "#B0B0B0") $row_color = "#D8D8D8";
                else $row_color = "#B0B0B0";
            }
        }

        $output .= '<input type="submit" role="button" style="margin-right:50px;margin-top:20px;float:right;" aria-label="submit form" class="button" value="' .  parent::getTranslates('continue') . '">';
        $output .= "</form>";

        return $output;
    }

    public function getSaveForm(){

        $course = new Course();
        $all_courses = $course->getCoursesCreated();
        $courses = array("id_course" => array(),
                        "is_valid" => array());
        $output = "";
        $is_correct = false;
        //Validate all courses selected
        for ($i = 0; $i < count($all_courses["id_course"]); $i++) {
            if (isset($_POST["course_" . $all_courses["id_course"][$i]]))

                if ($course->validateCurrentCourse($all_courses["id_course"][$i])){
                    array_push($courses["id_course"],$all_courses["id_course"][$i]);
                    array_push($courses["is_valid"],true);
                    $is_correct = true;
                }else{

                    array_push($courses["id_course"],$all_courses["id_course"][$i]);
                    array_push($courses["is_valid"],false);
                }
        }

        // If all invalid set the alert and return to select historic courses form
        if(!$is_correct){
            $this->errors .= parent::getTranslates("all_historic_courses_invalid");
            parent::displayModule("errors");
            return $this->getHistoricCourses();
        }

        // Output with the list of valid
        $output .= '<div class="row">';
        $output .= '<div class="small-12 large-12 columns" style="padding: 20px;">';
        $output .= '<h3>' . parent::getTranslates("sure_continue") . '</h3>';
        $output .= '</div>';
        $output .= '</div>';

        for ($i = 0; $i < count($courses["id_course"]); $i++) {
            $output .= '<div class="row">';
            $output .= '<div class="small-12 large-12 columns">';
            if ($courses["is_valid"][$i]) {
                $output .=  '<p style="background-color:#81F79F;padding:15px;"><i class="fi-check size-24" style="margin-right: 20px;margin-left: 20px;"></i><b>' .$course->getCourseName($courses["id_course"][$i]) .'</b><br>' . parent::getTranslates("is_valid") . '</p>';
           }else{
                $output .=  '<p style="background-color:#F78181;padding:15px;"><i class="fi-x size-24" style="margin-right: 20px;margin-left: 20px;"></i><b>' . $course->getCourseName($courses["id_course"][$i]) . '</b><br>' . parent::getTranslates("is_not_valid") . '</p>';
            }
            $output .= '</div>';
            $output .= '</div>';

        }

        // Form
        $output .= '<form action="main.php" method="post">';
        $output .= '<input type="hidden" name="form" value="configure_historic">';
        $output .= '<input type="hidden" name="course_for_prediction" value="' . $_POST["course_for_prediction"]. '">';
        $output .= '<input type="hidden" name="historic_courses" value="1">';
        $output .= '<input type="hidden" name="save_historic" value="1">';
        for ($i = 0; $i < count($courses["id_course"]); $i++) {
            if ($courses["is_valid"][$i])
                $output .= '<input type="hidden" name="historic_'.$courses["id_course"][$i] .'" value="'.$courses["id_course"][$i] .'">';
        }
        $output .= '<input type="submit" role="button" style="float:right;margin-right:50px;" aria-label="submit form" class="button" value="' .  parent::getTranslates('save_valid_courses') . '">';
        $output .= "</form>";
        return $output;
    }

}