<?php

require_once("../config/conexion.php");
require_once("../models/MallaCurricular.php");
require_once("../models/Permisos.php");
require_once("../models/Evaluacion.php");
$mallaCurricular = new MallaCurricular();
$evaluacion = new Evaluacion();
/* =============================================
  JAVASCRIPT
  ============================================= */
switch ($_GET["op"]) {
    case "consultar_malla":
        $datos = $mallaCurricular->Malla_por_ID($_POST["mallacurriculardbk"]);
        if (is_array($datos) == true and count($datos) <> 0) {
            foreach ($datos as $row) {
                $output["mallacurriculardbk"] = $row["mallacurriculardbk"];
                $output["codigo"] = $row["codigo"];
                $output["asignatura"] = $row["asignatura"];
                $output["nivel"] = $row["nivel"];
                $output["creditos"] = $row["creditos"];
                $output["tipo"] = $row["tipo"];
                $output["periodo"] = $row["periodo"];
                $output["carrera"] = $row["carrera"];
                $output["cd"] = $row["cd"];
                $output["cp"] = $row["cp"];
                $output["ca"] = $row["ca"];
                $output["docente"] = $row["docente"];
                $output["chat"] = $row["chat"];
                $output["video"] = $row["video"];
            }
            echo json_encode($output);
        }
        break;
    case "listar":
        $datos = $mallaCurricular->Consultar($_GET["fil"], "");
        $data = Array();
        $permiso = new Permisos();
        $resultado = $permiso->Permisos_por_Perfil("MALLA CURRICULAR", $_SESSION["perfil"]);
        foreach ($resultado as $row) {
            $estado = $row["estado"];
            $actualizar = $row["actualizar"];
            $eliminar = $row["eliminar"];
            $contacto = $row["contacto"];
            $leer = $row["leer"];
            $asignar = $row["asignar"];
        }
        $accesos = "";
        $count = 1;
        foreach ($datos as $row) {
            $sub_array = array();
            $sub_array[] = $count;
            $sub_array[] = $row["descripcion"];
            $sub_array[] = $row["descrip"];
            $sub_array[] = $row["modalidad"];
            if ($estado == 1) {
                if ($row["estado"] != "INACTIVO") {
                    $sub_array[] = '<td><button class="btn btn-success btn-xs btnActivarAsignatura" idAsignatura="' . $row["mallacurriculardbk"] . '" estadoAsignatura="INACTIVO">Activo</button></td>';
                } else {
                    $sub_array[] = '<td><button class="btn btn-danger btn-xs btnActivarAsignatura" idAsignatura="' . $row["mallacurriculardbk"] . '" estadoAsignatura="ACTIVO">Inactivo</button></td>';
                }
            }
            if ($asignar == 1) {
                $accesos = $accesos . '
                <button type="button" onClick="configurar(' . $row["mallacurriculardbk"] . ');" id="' . $row["mallacurriculardbk"] . '" class="btn btn-outline-success btn-icon"><div><i class="fa fa-cogs"></i></div></button>';
            }
            if ($leer == 1) {
                $accesos = $accesos . '
                <button type="button" onClick="editar(' . $row["mallacurriculardbk"] . ');" id="' . $row["mallacurriculardbk"] . '" class="btn btn-outline-primary btn-icon"><div><i class="fa fa-eye"></i></div></button>';
            }
            $sub_array[] = $accesos;
            $data[] = $sub_array;
            $accesos = "";
            $count++;
        }
        $results = array(
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "aaData" => $data
        );
        echo json_encode($results);
        break;
    case "plataforma":        
        if($_SESSION["perfil"]=="DOCENTE"){
            $datos = $mallaCurricular->Consultar("pla", "");       
            $data = Array();
            $permiso = new Permisos();
            $resultado = $permiso->Permisos_por_Perfil("PLATAFORMAS", $_SESSION["perfil"]);
            foreach ($resultado as $row) {
                $asignar = $row["asignar"];
                $leer = $row["leer"];
            }
            $accesos = "";
            $count = 1;
            foreach ($datos as $row) {
                $sub_array = array();
                $sub_array[] = $count;
                if($asignar == 1){
                    $sub_array[] = $row["carrera"];
                }
                $sub_array[] = $row["descripcion"];
                if($leer == 1){
                    $sub_array[] = $row["docente"];
                }
                if($asignar == 1){
                    $sub_array[] = $row["chat"];
                }else{
                    if($row["chat"]){
                        $sub_array[] = '<a href="'.$row["chat"].'" target="_blank" class="btn btn-primary btn-lg active" role="button" aria-pressed="true">Unirse</a>';
                    }else{
                        $sub_array[] = '<a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">No disponible</a>';
                    }

                }
                $sub_array[] = $row["video"];
                if ($asignar == 1) {
                    $accesos = $accesos . '
                    <button type="button" onClick="editar(' . $row["mallacurriculardbk"] . ');" id="' . $row["mallacurriculardbk"] . '" class="btn btn-outline-warning btn-icon"><div><i class="fa fa-edit"></i></div></button>';
                }           
                $sub_array[] = $accesos;
                $data[] = $sub_array;
                $accesos = "";
                $count++;
            }
            $results = array(
                "sEcho" => 1,
                "iTotalRecords" => count($data),
                "iTotalDisplayRecords" => count($data),
                "aaData" => $data
            );
            echo json_encode($results);
        }
        if($_SESSION["perfil"]=="ESTUDIANTE"){
            $datos = $mallaCurricular->Consultar("plaEst", "");       
            $data = Array();
            $permiso = new Permisos();
            $resultado = $permiso->Permisos_por_Perfil("PLATAFORMAS", $_SESSION["perfil"]);
            foreach ($resultado as $row) {
                $asignar = $row["asignar"];
                $leer = $row["leer"];
            }
            $accesos = "";
            $count = 1;
            foreach ($datos as $row) {
                $datos2 = $mallaCurricular->ConsultarEstDetalle($row["asignatura"],$row["codcar"]);
                foreach ($datos2 as $row2) {
                    $asignatura = $row2["asignatura"];
                    $docente = $row2["docente"];
                    $chat=$row2["chat"];
                    $video=$row2["video"];
                }
                $sub_array = array();
                $sub_array[] = $count;                
                $sub_array[] = $asignatura;                                
                $sub_array[] = $docente;
                if($chat){
                        $sub_array[] = '<a href="'.$chat.'" target="_blank" class="btn btn-primary btn-lg active" role="button" aria-pressed="true">Unirse</a>';
                    }else{
                        $sub_array[] = '<a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">No disponible</a>';
                    }              
                $sub_array[] = $video;                
                $sub_array[] = $accesos;
                $data[] = $sub_array;
                $accesos = "";
                $count++;
            }
            $results = array(
                "sEcho" => 1,
                "iTotalRecords" => count($data),
                "iTotalDisplayRecords" => count($data),
                "aaData" => $data
            );
            echo json_encode($results);
        }
        break;    
    case "clases":        
        if($_SESSION["perfil"]=="DOCENTE"){
            $datos = $mallaCurricular->Consultar("pla", "");       
            $data = Array();
            $permiso = new Permisos();
            $resultado = $permiso->Permisos_por_Perfil("CLASES", $_SESSION["perfil"]);
            foreach ($resultado as $row) {
                $crear = $row["crear"];
                $actualizar = $row["actualizar"];
                $eliminar = $row["eliminar"];
                $leer = $row["leer"];    
                $asignar = $row["asignar"];    
            }
            $accesos = "";
            $count = 1;
            foreach ($datos as $row) {
                $sub_array = array();
                $sub_array[] = $count;
                if($leer == 1){                    
                    $sub_array[] = $row["carrera"];
                    $sub_array[] = $row["descripcion"];
                    if ($actualizar == 1) {
                       $accesos = $accesos . '
                        <button type="button" onClick="editar(' . $row["mallacurriculardbk"] . ');" id="' . $row["mallacurriculardbk"] . '" class="btn btn-outline-warning btn-icon"><div><i class="fa fa-edit"></i></div></button>';
                    }
                }                
                $count++;
                $sub_array[] = $accesos;
                $data[] = $sub_array;
                $accesos = "";
            }
            $results = array(
                "sEcho" => 1,
                "iTotalRecords" => count($data),
                "iTotalDisplayRecords" => count($data),
                "aaData" => $data
            );
            echo json_encode($results);
        }
        if($_SESSION["perfil"]=="ESTUDIANTE"){
            $datos = $mallaCurricular->Consultar("plaEst", "");       
            $data = Array();
            $permiso = new Permisos();
            $resultado = $permiso->Permisos_por_Perfil("CLASES", $_SESSION["perfil"]);
            foreach ($resultado as $row) {
                $asignar = $row["asignar"];
                $leer = $row["leer"];
            }
            $accesos = "";
            $count = 1;
            foreach ($datos as $row) {
                $datos2 = $mallaCurricular->ConsultarEstDetalle($row["asignatura"],$row["codcar"]);
                foreach ($datos2 as $row2) {
                    $mallacurriculardbk = $row2["mallacurriculardbk"];
                    $carrera = $row2["carrera"];
                    $asignatura = $row2["asignatura"];
                    $docente = $row2["docente"];
                    $chat=$row2["chat"];
                    $video=$row2["video"];
                    $sub_array = array();
                    $sub_array[] = $count;                
                    $sub_array[] = $carrera;                                
                    $sub_array[] = $asignatura;
                }                
                if($leer == 1){
                        $sub_array[] ='<button type="button" onClick="editar(' . $mallacurriculardbk . ');" id="' . $mallacurriculardbk . '" class="btn btn-outline-success btn-icon"><div><i class="fa fa-window-maximize"></i></div></button>';

                    }else{
                        $sub_array[] = '<a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">No disponible</a>';
                    }              
                $sub_array[] = $video;                
                $sub_array[] = $accesos;
                $data[] = $sub_array;
                $accesos = "";
                $count++;
            }
            $results = array(
                "sEcho" => 1,
                "iTotalRecords" => count($data),
                "iTotalDisplayRecords" => count($data),
                "aaData" => $data
            );
            echo json_encode($results);
        }
        
        break;        
        case "evaluacion":        
        if($_SESSION["perfil"]=="ESTUDIANTE"){
            $datos = $mallaCurricular->Consultar("plaEst", "");       
            $data = Array();
            $permiso = new Permisos();
            $resultado = $permiso->Permisos_por_Perfil("VALORACION", $_SESSION["perfil"]);
            foreach ($resultado as $row) {
                $asignar = $row["asignar"];
                $leer = $row["leer"];
            }
            $accesos = "";
            $count = 1;
            foreach ($datos as $row) {
                $datos2 = $mallaCurricular->ConsultarEstDetalle($row["asignatura"],$row["codcar"]);
                foreach ($datos2 as $row2) {
                    $mallacurriculardbk = $row2["mallacurriculardbk"];
                    $carrera = $row2["carrera"];
                    $asignatura = $row2["asignatura"];
                    $docente = $row2["docente"];
                    $chat=$row2["chat"];
                    $video=$row2["video"];
                    $sub_array = array();
                    $sub_array[] = $count;                
                    //$sub_array[] = $carrera;                                
                    $sub_array[] = $asignatura;
                    $sub_array[] = $docente;
                }                
                if($leer == 1){
                    $evaluaciones="";
                    $datos3 = $evaluacion->EvaluacionDisponible($mallacurriculardbk,1,$_SESSION['personadbk']);
                    //$sub_array[] = '<button type="button" onClick="inicio(' . $mallacurriculardbk . ');" id="' . $mallacurriculardbk . '" class="btn btn-outline-success btn-icon"><div>SEMANA 1-2</div></button>';
                    foreach ($datos3 as $row3) {
                        if($row3["registro"]==0){
                            $evaluaciones = $evaluaciones.'<button type="button" onClick="inicio(' . $mallacurriculardbk . ');" id="' . $mallacurriculardbk . '" class="btn btn-outline-success btn-icon"><div>SEMANA 1-2</div></button>';
                        }
                    }
                    $datos3 = $evaluacion->EvaluacionDisponible($mallacurriculardbk,2,$_SESSION['personadbk']);
                    foreach ($datos3 as $row3) {
                        if($row3["registro"]==0){
                            $evaluaciones = $evaluaciones.'<button type="button" onClick="medio(' . $mallacurriculardbk . ');" id="' . $mallacurriculardbk . '" class="btn btn-outline-info btn-icon"><div>SEMANA 3-5</div></button>';
                        }
                    }
                    $datos3 = $evaluacion->EvaluacionDisponible($mallacurriculardbk,3,$_SESSION['personadbk']);
                    foreach ($datos3 as $row3) {
                        if($row3["registro"]==0){
                            $evaluaciones = $evaluaciones.'<button type="button" onClick="fin(' . $mallacurriculardbk . ');" id="' . $mallacurriculardbk . '" class="btn btn-outline-primary btn-icon"><div>SEMANA 6-8</div></button>';
                        }                        
                    }
                }else{
                    $sub_array[] = '<a href="#" class="btn btn-secondary btn-lg active" role="button" aria-pressed="true">No disponible</a>';
                }   
                if($evaluaciones!=""){
                    $sub_array[] = $evaluaciones;    
                }
                else{
                    $sub_array[] = '<a href="#" class="btn btn-success btn-lg active" role="button" aria-pressed="true">Valoraciones realizadas</a>';
                }                
                $sub_array[] = $video;                
                $sub_array[] = $accesos;
                $data[] = $sub_array;
                $accesos = "";
                $count++;
            }
            $results = array(
                "sEcho" => 1,
                "iTotalRecords" => count($data),
                "iTotalDisplayRecords" => count($data),
                "aaData" => $data
            );
            echo json_encode($results);
        }
        
        break;                
    case "configurar":
        $datos = $mallaCurricular->Consultar($_GET["fil"], $_GET["m"]);
        $data = Array();
        $permiso = new Permisos();
        $resultado = $permiso->Permisos_por_Perfil("MALLA CURRICULAR", $_SESSION["perfil"]);
        foreach ($resultado as $row) {
            $estado = $row["estado"];
            $actualizar = $row["actualizar"];
            $eliminar = $row["eliminar"];
            $contacto = $row["contacto"];
            $leer = $row["leer"];
            $asignar = $row["asignar"];
            $habilitar = $row["habilitar"];
        }
        $accesos = "";
        $count = 1;
        foreach ($datos as $row) {
            $sub_array = array();
            $sub_array[] = $row["codigo"];
            $sub_array[] = $row["nivel"];
            $sub_array[] = $row["asignatura"];
            $sub_array[] = $row["creditos"];
            $sub_array[] = $row["tipo"];
            $sub_array[] = $row["cd"];
            $sub_array[] = $row["cp"];
            $sub_array[] = $row["ca"];
            $sub_array[] = $row["docente"];            
            if ($habilitar == 1) {
                if ($row["calificar"] != "INACTIVO") {
                    $sub_array[] = '<td><button class="btn btn-success btn-xs btnActivarCalificar" idCalificar="' . $row["mallacurriculardbk"] . '" estadoCalificar="INACTIVO">Activo</button></td>';
                } else {
                    $sub_array[] = '<td><button class="btn btn-danger btn-xs btnActivarCalificar" idCalificar="' . $row["mallacurriculardbk"] . '" estadoCalificar="ACTIVO">Inactivo</button></td>';
                }
            }
            if ($actualizar == 1) {
                $accesos = $accesos . '
                <button type="button" onClick="editar(' . $row["mallacurriculardbk"] . ');" id="' . $row["mallacurriculardbk"] . '" class="btn btn-outline-warning btn-icon"><div><i class="fa fa-edit"></i></div></button>';
            }
            if ($asignar == 1) {
                $accesos = $accesos . '
                <button type="button" onClick="asignar(' . $row["mallacurriculardbk"] . ');" id="' . $row["mallacurriculardbk"] . '" class="btn btn-outline-success btn-icon"><div><i class="fa fa-briefcase"></i></div></button>';
            }
            if ($eliminar == 1) {
                $accesos = $accesos . '
                <button type="button" onClick="eliminar(' . $row["mallacurriculardbk"] . ');" id="' . $row["mallacurriculardbk"] . '" class="btn btn-outline-danger btn-icon"><div><i class="fa fa-times"></i></div></button>';
            }            
            $sub_array[] = $accesos;
            $data[] = $sub_array;
            $accesos = "";
        }
        $results = array(
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "aaData" => $data
        );
        echo json_encode($results);
        break;
    case "guardar":
        $datos = array("tabla" => "mallacurricular",
            "mallacurriculardbk" => $_POST["mallacurriculardbk"],
            "asignatura" => $_POST["asignatura"],
            "nivel" => $_POST["nivel"],
            "periodo" => $_POST["periodo"],
            "carrera" => $_POST["carrera"],
            "creditos" => $_POST["creditos"],
            "tipo" => $_POST["tipo"],
            "codigo" => $_POST["codigo"],
            "cd" => $_POST["cd"],
            "cp" => $_POST["cp"],
            "ca" => $_POST["ca"]
        );
        $mallaCurricular->mdlActualizarMalla($datos);
        break;
    case "guardarPlataforma":
        $datos = array("tabla" => "mallacurricular",
            "mallacurriculardbk" => $_POST["mallacurriculardbk"],            
            "chat" => $_POST["chat"],
            "video" => $_POST["video"]
        );
        $mallaCurricular->mdlActualizarPlataforma($datos);
        break;
    case "asignar":
        $datos = array("tabla" => "mallacurricular",
            "mallacurriculardbk" => $_POST["mallacurriculardbk2"],
            "docente" => $_POST["docente"]
        );
        $mallaCurricular->mdlActualizarAsignar($datos);
        break;    
    case "nuevo":
        $datos = array("tabla" => "mallacurricular",
            "periodo" => $_POST["periodo"],
            "carrera" => $_POST["carrera"],
            "asignatura" => $_POST["asignatura"],
            "nivel" => $_POST["nivel"],
            "creditos" => $_POST["creditos"],
            "tipo" => $_POST["tipo"],
            "codigo" => $_POST["codigo"]
        );
        $mallaCurricular->mdlNuevaMalla($datos);
        break;
    case "eliminar_malla":
        $datos = $mallaCurricular->Eliminar_por_ID($_POST["mallacurriculardbk"]);
        break;
    case "calificar":
        if (isset($_POST["activarId"]) && isset($_POST["activarCalificar"])) {
            $datos = array("tabla" => "mallacurricular",
                "mallacurriculardbk" => $_POST["activarId"],
                "estado" => $_POST["activarCalificar"]
            );
            $mallaCurricular->mdlActualizarCalificar($datos);
        }
        break;    
}
?>


