<?php

include_once "../../../config/settings.php";
include_once "../../../controllers/DataBaseController.php";
include_once "../../../classes/User.php";
include_once "../../../classes/Course.php";

include_once "./plugins/PHPExcel.php";

class ChartSwitcherJSON extends DataBaseController{

    var $lang;

    public function ChartSwitcherJSON(){

        parent::DataBaseController();


       if (isset($_POST["id_course"]) && isset($_POST["lang"])) {
            include_once "./translates/" .$_POST["lang"] . "/translate.php";
            //include_once "./translates/cat/translate.php";
            $this->lang = new ItsModuleTranslate();



            //return student list

           // echo json_encode($this->getChart1((int)$_POST["id_course"]));
           echo json_encode($this->getChart1((int)$_POST["id_course"]) .
                $this->getChart2((int)$_POST["id_course"], "Failed") .
                $this->getChart2((int)$_POST["id_course"], "Pass") .
                $this->getChart2((int)$_POST["id_course"], "Good") .
                $this->getChart2((int)$_POST["id_course"], "Remarkable") .
                $this->getChart2((int)$_POST["id_course"], "Outstanding")
                 );
           //echo json_encode($this->getChart1((int)$_POST["id_course"]));

            //echo json_encode ($this->getChart2((int)$_POST["id_course"], "Failed"));


            exit();
        }



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


        // GET EXCEL VALUES
        $inputFileName = './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>' . $this->lang->getTranslate("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: "' . $this->lang->getTranslate('failed'). '"
                        },
                        {
                            value: ' . $data[1] . ',
                            color: "#46BFBD",
                            highlight: "#5AD3D1",
                            label: "' . $this->lang->getTranslate('pass'). '"
                        },
                        {
                            value: ' . $data[2] . ',
                            color: "#FAA5C",
                            highlight: "#FFC870",
                            label: "' .$this->lang->getTranslate('good'). '"
                        },
                        {
                            value: ' . $data[3] . ',
                            color: "#FDB4AC",
                            highlight: "#FFC870",
                            label: "' . $this->lang->getTranslate('remarkable'). '"
                        },
                        {
                            value: ' . $data[4] . ',
                            color: "#FDB45C",
                            highlight: "#FFC870",
                            label: "' . $this->lang->getTranslate('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 = './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>' .$this->lang->getTranslate("chart_title2_1")  . " "  .$this->lang->getTranslate(strtolower( $type)) . " " .$this->lang->getTranslate("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;
    }

}

new ChartSwitcherJSON();

?>