<?php
require_once "../../../app/model/db.class.php";

function descrip($tabla,$cod){
         $obj = new database();
         $obj->conectar(); $obj->consulta("set names 'utf8'");
         $result=$obj->consulta("select * from ".$tabla." where codigo='".$cod."';");
         $rs=$obj->fetch_assoc($result);
         $obj->disconnect();
         return $rs['descripcion'];
    }


function descrip_curso($tabla,$grad,$cod){
         $obj = new database();
         $obj->conectar(); $obj->consulta("set names 'utf8'");
         $result=$obj->consulta("select * from cursos where grado='".$grad."' and codigo='".$cod."';");
         $rs=$obj->fetch_assoc($result);
         $obj->disconnect();
         return $rs['descripcion'];
    }

function descrip_prof($tabla,$cod){
         $obj = new database();
         $obj->conectar(); $obj->consulta("set names 'utf8'");
         $result=$obj->consulta("select * from ".$tabla." where cedula='".$cod."';");
         $rs=$obj->fetch_assoc($result);
         $obj->disconnect();
         return $rs['nombres'];
    }


if($_REQUEST['accion']=='tabla'){

        $grado=$_REQUEST['grado'];
        $curso=$_REQUEST['curso'];
        $dia=$_REQUEST['dia'];

        $v1='';
        $v2='';
        $v3='';
        $v4='';


        if($grado!='')//---grado
        {
            $v1="='".$grado."'";
        }else{
            $v1=" like '%' ";
        }


        if($curso!='')//---curso
        {
            $v2="='".$curso."'";
        }else{
            $v2=" like '%' ";
        }

        if($dia!='')//---dia
        {
            $v3="='".$dia."'";
        }else{
            $v3=" like '%' ";
        }

        $obj=new database();
        $obj->conectar(); $obj->consulta("set names 'utf8'");

        $result=$obj->consulta("select * from horario where grado ".$v1." and curso ".$v2." and dia ".$v3." order by orden asc;");

        $cadena="<center><br><table border='0' cellspacing='3' cellpadding='2' class='t_info'>";
        $cadena.="<tr>";
        $cadena.="<th style='font-size: 13px;text-align:center;'>&nbsp; DIA &nbsp;</th>";
        $cadena.="<th style='font-size: 13px;text-align:center;'>&nbsp; GRADO &nbsp;</th>";
        $cadena.="<th style='font-size: 13px;text-align:center;'>&nbsp; CURSO &nbsp;</th>";
        $cadena.="<th style='font-size: 13px;text-align:center;'>&nbsp; CODIGO &nbsp;</th>";
        $cadena.="<th style='font-size: 13px;text-align:center;'>ASIGNATURA</th>";
        $cadena.="<th style='font-size: 13px;text-align:center;'>&nbsp; I.H &nbsp;</th>";
        $cadena.="<th style='font-size: 13px;text-align:center;'>&nbsp; ELIMINAR&nbsp; </th>";
        $cadena.="<th style='font-size: 13px;text-align:center;'>&nbsp; ACTUALIZAR&nbsp; </th>";
        $cadena.="</tr>";

        if($obj->numero_de_filas($result)){

            while ( $tsArray = $obj->fetch_assoc($result) )
            {
            $data[] = $tsArray;

            }
            //print_r($data);


            for($i=0;$i<count($data);$i++){
                $cadena.="<tr onmouseover=\"this.style.backgroundColor='#89F2AC';\" onmouseout=\"this.style.backgroundColor='#BBE9EE';\">";

                $cadena.="<td align='center'>&nbsp;".$data[$i]['dia']." &nbsp;</td>";
                $cadena.="<td align='center'>&nbsp;".$data[$i]['grado']."&nbsp;</td>";
                $cadena.="<td align='center'>&nbsp;".$data[$i]['curso']."&nbsp;</td>";
                $cadena.="<td align='center'>&nbsp;".$data[$i]['asignatura']." &nbsp;</td>";
                $cadena.="<td style='font-size:12px'>&nbsp; ".(descrip('catalogo',$data[$i]['asignatura']))." &nbsp;</td>";
                
                $cadena.="<td align='center'><INPUT style='font-size: 13px;text-align:center;' VALUE='".$data[$i]['ih']."' TYPE='TEXT' ID='IH_".$data[$i]['orden']."' SIZE='3'></td>";              

                $cadena.='<td align="center"> <img id="elemento1"  class="confirmar" src="imagenes/foto1.jpg" height="20" width="20"
                    onclick="eliminar(\''.$data[$i]['dia'].'\',\''.$data[$i]['grado'].'\',\''.$data[$i]['curso'].'\',\''.$data[$i]['asignatura'].'\','.$data[$i]['orden'].')">  </td>';

                $cadena.='<td align="center"> <img id="elem1"  class="confirmar" src="imagenes/boton.png" height="20" width="30"
                    onClick="actualizar_horario('.$data[$i]['orden'].')"></td>';

            }

        }else{

            $cadena.="<tr>";
            $cadena.="<td colspan='7' align='center'></td>";
            $cadena.="</tr>";
        }

    $cadena.="</table>";
    $cadena.="</center>";

    echo $cadena;

}



if($_REQUEST['accion']=='grabar_horario'){

    $dia=$_REQUEST['dia'];
    $grado=$_REQUEST['grado'];
    $curso=$_REQUEST['curso'];
    $asignatura=$_REQUEST['asignatura'];
    $i_h=$_REQUEST['i_h'];
    //$orden=$_REQUEST['orden'];
    $ope=$_REQUEST['operacion'];

    $obj=new database();
    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    
     /*----- Nombre de la Asignatura ---*/

     $asign=$obj->consulta("select * from catalogo where codigo='".$asignatura."'");
     $row = $obj->fetch_assoc($asign);

     if($ope==='x'){

         $result=$obj->consulta("select * from horario where dia='".$dia."' and grado='".$grado."' and curso='".$curso."' and asignatura='".$asignatura."'");

         if($obj->numero_de_filas($result)>0){

             echo '0';

         }else{

            $result=$obj->consulta("insert into horario (dia,grado,curso,asignatura,descripcion,ih) values('".$dia."','".$grado."','".$curso."','".$asignatura."','".($row['descripcion'])."','".$i_h."')");

             if($result)
                {
                    echo '1';
                }
         }

     }

     $obj->disconnect();
}

//--------------------------------------------

if($_REQUEST['accion']=='eliminar_horario'){

    $dia=$_REQUEST['dia'];
    $grado=$_REQUEST['grado'];
    $curso=$_REQUEST['curso'];
    $asignatura=$_REQUEST['asignatura'];
    $codigo=$_REQUEST['codigo'];

  
    $obj=new database();
    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    
    $result=$obj->consulta("delete from horario where  orden='".$codigo."' and grado='".$grado."' and curso='".$curso."' and dia='".$dia."' and asignatura='".$asignatura."'");

    if($result){
        echo 'Registro Eliminado';
    }

    $obj->disconnect();

}

//--------------------------------------------

if($_REQUEST['accion']=='actualizar_horario'){

    $orden=$_REQUEST['orden'];
    $horas=$_REQUEST['horas'];
 
    $obj=new database();
    $obj->conectar(); $obj->consulta("set names 'utf8'");
    
    
    $result=$obj->consulta("UPDATE horario SET ih='".$horas."' WHERE orden='".$orden."'");

    if($result){
        echo 'Intensidad Horaria Actualizada';
    }

    $obj->disconnect();

}

//--------------------------------------------

if($_REQUEST['accion']=='editar_labor_aca'){

    $profesor=$_REQUEST['profesor'];
    $grado=$_REQUEST['grado'];
    $curso=$_REQUEST['curso'];
    $asignatura=$_REQUEST['asignatura'];
    $codigo=$_REQUEST['codigo'];


    $obj=new database();
    $obj->conectar(); $obj->consulta("set names 'utf8'");
    $result=$obj->consulta("select * from curso_asignatura_profesor where  orden='".$codigo."' and grado='".$grado."' and curso='".$curso."' and profesor='".$profesor."' and asignatura='".$asignatura."';");
    $obj->disconnect();
    $info='';
    if($obj->numero_de_filas($result))
        {
            $info[]=$obj->fetch_assoc($result);
        }


    $cadena="".$info[0]['profesor'].";";
    $cadena.="".(descrip_prof('profesores',$info[0]['profesor'])).";";

    $cadena.="".$info[0]['grado'].";";
    $cadena.="".(descrip('grados',$info[0]['grado'])).";";

    $cadena.="".$info[0]['curso'].";";
    
    $cadena.="".(descrip_curso('cursos',$info[0]['grado'],$info[0]['curso'])).";";

    $cadena.="".$info[0]['asignatura'].";";
    $cadena.="".(descrip('catalogo',$info[0]['asignatura'])).";";

    $cadena.="".$info[0]['intensidad'].";";
    $cadena.="".$info[0]['dias']."";

    echo $cadena;
}


?>
