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


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

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

       $result=$obj->consulta("select areas.codigo as area, areas.descripcion as nom_area, catalogo.codigo as asignatura, catalogo.descripcion as nom_asign, curso_asignatura_profesor.peso
                     from (areas inner join catalogo on areas.codigo = catalogo.area) inner join curso_asignatura_profesor on catalogo.codigo = curso_asignatura_profesor.asignatura
                     where (((curso_asignatura_profesor.grado)='".$grado."') and ((curso_asignatura_profesor.curso)='".$curso."')) order by areas.orden,catalogo.orden");

        $cadena="<center><table style='font-size: 12px' border='0' cellspacing='3' cellpadding='2' class='t_info'>";
        $cadena.="<tr>";
        $cadena.="<th>&nbsp; AREA &nbsp;</th>";
        $cadena.="<th>&nbsp; NOMBRE AREA &nbsp;</th>";
        $cadena.="<th>&nbsp; ASIGN. &nbsp;</th>";
        $cadena.="<th>NOMBRE ASIGNATURA</th>";
        $cadena.="<th>&nbsp; PESO &nbsp;</th>";
        $cadena.="<th>&nbsp; GRABAR &nbsp;</th>";
        $cadena.="</tr>";


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


            while ( $row = $obj->fetch_assoc($result) ) {

                $cadena.="<tr onmouseover=\"this.style.backgroundColor='#89F2AC';\" onmouseout=\"this.style.backgroundColor='#BBE9EE';\">";
                $cadena.="<td>&nbsp;".$row['area']." &nbsp;</td>";
                $cadena.="<td>&nbsp;".($row['nom_area'])."&nbsp;</td>";
                $cadena.="<td>&nbsp;".$row['asignatura']."&nbsp;</td>";                
                $cadena.="<td>&nbsp;".($row['nom_asign'])." &nbsp;</td>";
                
                $id_peso= $grado.$curso.$row['area'].$row['asignatura'];
                
                $cadena.="<td align='center'><INPUT style='font-weight:bold;text-align:center;' VALUE='".$row['peso']."' id='".$id_peso."' TYPE='TEXT' SIZE='1' \">%</td>";
                
                $cadena.='<td align="center"> <img id="elemento2"  class="confirmar" src="../imagenes/diskette.jpg" height="20" width="20" title="Grabar"
                    onclick="grabar(\''.$grado.'\',\''.$curso.'\',\''.$row['area'].'\',\''.$row['asignatura'].'\')">  </td>';

            }


        }else{

            $cadena.="<tr>";
            $cadena.="<td colspan='7' align='center'>LABOR ACAD&Eacute;MICA NO ASIGNADA</td>";
            $cadena.="</tr>";
        }


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

    echo $cadena;
    
    $obj->disconnect();
    }

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

if ($_REQUEST['accion']=='grabar_peso'){
  
        $grado=$_REQUEST['grado'];
        $curso=$_REQUEST['curso'];
        $area=$_REQUEST['area'];
        $asignatura=$_REQUEST['asignatura'];
        $peso=$_REQUEST['peso'];
                
        $obj=new database();
        $obj->conectar(); $obj->consulta("set names 'utf8'");

        $result=$obj->consulta("select sum(curso_asignatura_profesor.peso) as total
                                from (areas inner join catalogo on areas.codigo = catalogo.area) inner join curso_asignatura_profesor on catalogo.codigo = curso_asignatura_profesor.asignatura
                                where catalogo.area='".$area."' and catalogo.codigo<>'".$asignatura."' and curso_asignatura_profesor.grado='".$grado."' and curso_asignatura_profesor.curso='".$curso."'");
        
        $row = $obj->fetch_assoc($result);
        
        if  (($row['total']+$peso)>100)
              echo "1";
        else{
             $obj->consulta("UPDATE curso_asignatura_profesor SET peso=".$peso." where asignatura='".$asignatura."' and grado='".$grado."' and curso='".$curso."'");
             $faltante=(100-($peso+$row['total']));
             
             if ($faltante!=0)
                 echo "PESO FALTANTE DEL AREA ".$faltante."%";
             else
                 echo "PESO COMPLETO DEL AREA";
        }      
 
        $obj->disconnect();

}

?>
