<?php

class Asignatura extends Conexion {

    /* =============================================
      VER DATOS DE ASIGNATURA POR ID
      ============================================= */

    public function Asignatura_por_ID($asignaturaId) {
        $conectar = parent::conectar();
        parent::set_names();
        $sql = "
                SELECT * FROM asignatura                                 
                WHERE asignaturadbk = ?
                ";
        $sql = $conectar->prepare($sql);
        $sql->bindValue(1, $asignaturaId);
        $sql->execute();
        return $resultado = $sql->fetchAll();
    }
    
    /* =============================================
      ELIMINAR DATOS DE ASIGNATURA POR ID
      ============================================= */

    public function Eliminar_por_ID($asignaturaId) {
        $conectar = parent::conectar();
        parent::set_names();
        $sql = "
                DELETE FROM asignatura WHERE asignaturadbk = ?
                ";
        $sql = $conectar->prepare($sql);
        $sql->bindValue(1, $asignaturaId);
        $sql->execute();
        return $resultado = $sql->fetchAll();
    }    

    /* =============================================
      IMPRIMIR TABLA DE ASIGNATURA
      ============================================= */

    public function Consultar($fil) {
        $conectar = parent::conectar();
        parent::set_names();
        switch ($fil) {
            case "all":
                $sql = "SELECT * FROM asignatura Order by descripcion";
                break;
            case "asignaturaDocente":                
                $sql = "
                        SELECT mallacurriculardbk,carrera.descripcion as carrera,asignatura.descripcion as asignatura
                        FROM mallacurricular 
                        INNER JOIN carrera ON carreradbk = carrera 
                        INNER JOIN asignatura ON asignaturadbk = asignatura 
                        WHERE 
                        docente = ".$_SESSION["personadbk"]."
                        AND periodo =".$_SESSION["periodbk"]."";
                break;
                case "asignaturaEstudiante":                
                $sql = "
                        SELECT detalledbk,asignatura.descripcion as asignatura
                        FROM matricula 
                        INNER JOIN detalle ON matriculadbk = matricula
                        INNER JOIN asignatura ON asignaturadbk = asignatura 
                        WHERE 
                        estudiante = ".$_SESSION["personadbk"]."
                        AND periodo = ".$_SESSION["periodbk"]."";
                break;            
            case "M":
                $sql = "";
                break;
            case "D":
                $sql = "";
                break;
        }
        $sql = $conectar->prepare($sql);
        $sql->execute();
        return $resultado = $sql->fetchAll();
    }

    /* =============================================
      ACTUALIZAR O INSERTAR USUARIO
      ============================================= */

    public function mdlActualizarAsignatura($datos) {
        $conectar = parent::conectar();
        parent::set_names();
        if ($datos["asignaturadbk"] == "") {
            $sql = "INSERT INTO " . $datos["tabla"] . "  
                (descripcion,estado)
                VALUES (?,?);
                ";
            $sql = $conectar->prepare($sql);
            $sql->bindValue(1, $datos["descripcion"]);
            $sql->bindValue(2, "ACTIVO");            
            $sql->execute();
        } else {
            $sql = "UPDATE " . $datos["tabla"] . " SET
                descripcion = ? 
                WHERE asignaturadbk = ?;
                ";
            $sql = $conectar->prepare($sql);
            $sql->bindValue(1, $datos["descripcion"]);
            $sql->bindValue(2, $datos["asignaturadbk"]);
            $sql->execute();
        }
        return $resultado = $sql->fetchAll();
    }

    public function mdlActualizarEstado($datos) {
        $conectar = parent::conectar();
        parent::set_names();
        $sql = "UPDATE " . $datos["tabla"] . "  
                SET estado = ?
                WHERE asignaturadbk = ?";
        $sql = $conectar->prepare($sql);
        $sql->bindValue(1, $datos["estado"]);
        $sql->bindValue(2, $datos["asignaturadbk"]);
        $sql->execute();
        return $resultado = $sql->fetchAll();
    }
    public function ConsultarFiltro($periodbk,$carreradbk) {
        $conectar = parent::conectar();
        parent::set_names();
        $sql = "SELECT mallacurricular.codigo,asignaturadbk,asignatura.descripcion,nivel FROM mallacurricular
                INNER JOIN carrera ON carreradbk = carrera
                INNER JOIN asignatura ON asignaturadbk = asignatura
                WHERE periodo = ?
                AND carreradbk = ?
                ORDER BY mallacurricular.codigo";
        $sql = $conectar->prepare($sql);
        $sql->bindValue(1, $periodbk);
        $sql->bindValue(2, $carreradbk);
        $sql->execute();
        return $resultado = $sql->fetchAll();
    }    

}

?>
