<?php

    require_once '../app/model/db.class.php';
    require_once '../dompdf/dompdf_config.inc.php';
    require_once '../dompdf/www/functions.inc.php';

    function decimales($nota){

     if (strlen($nota)==1) {
         $nota=$nota.".000";
     }

     if (strlen($nota)>3) {
         $nota=substr($nota ,0,5);
     }

    return $nota;

   }

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

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

    $periodo=$_REQUEST['periodo'];

    /*-------------  HTML -----------*/

    $s="<html>";
    $s.="<head><meta http-equiv='expires' content='0'><meta http-equiv='Cache-Control' content='no-cache'><meta http-equiv='Pragma' CONTENT='no-cache'>";
    $s.="<title></title>";
    $s.="<link rel='stylesheet' type='text/css' href='style.css'/>";
    $s.="<script type='text/javascript' src='consolidado.js'></script>";
    $s.="</head>";

    $s.="<body>";
    $s.="<center>";

    $s.="<table width='80%'>";

     $s.="<tr>";
     $s.="<td align='right' rowspan='3'><img src='../imagenes/escudo.jpg' width='90' height='90'/></td>";
    
     /*----- NOMBRE DE LA INSTITUCIÓN-SEDE-GRADO-CURSO- ----- */

     $datos=$obj->consulta("select * from registrar");
     $anyo=$obj->consulta("select * from parametros");

     $row1 = $obj->fetch_assoc($anyo);
     $row_inst = $obj->fetch_assoc($datos);
     $s.="<td align='center' colspan='5'>".($row_inst['institucion'])."</td>";
     $s.="</tr>";

     $s.="<tr>";
     $s.="<td align='center' colspan='5'>DETALLE DE  ASIGNATURAS REPROBADAS</td>";
     $s.="</tr>";

     $s.="<tr>";
     $s.="<td align='center' colspan='2'>PERIODO: ". $periodo."</td>";
     $s.="<td align='center' colspan='3'>A&Ntilde;O LECTIVO: ".$row1['anolectivo']."</td>";
     $s.="</tr>";
     $s.="</table></center>";

    /*--------------------------------------*/

    $num_reg="select * from cursos_temporal where activar='1'";

    $result5 = $obj->consulta($num_reg);
    $numero = $obj->numero_de_filas($result5); // obtenemos el numero de registros

    if  ($numero!=0) {

         $c="";
         
         while($reg_ren = $obj->fetch_assoc($result5)){

            $suma=0;             
            $reg = $obj->consulta("select count(*) as cantidad from  rendimiento_general where periodo='".$periodo."' and grado='".$reg_ren['grado']."' and curso='".$reg_ren['codigo']."'");
            $row = $obj->fetch_assoc($reg);

           if ($c!=$reg_ren['grado']){

              $s.="<center>";
              $s.="<table align='center' border=0 cellspacing='0' cellpadding='0' width='80%'>";
              $s.="<tr>";
              $s.="<td>GRADO: ".$reg_ren['grado']."</td>";
              $s.="</tr>";
              $s.="</table></center>";

           }


              //-------------------------------
              $i=0;
              while ($suma<$row['cantidad']){

                  $reg1 = $obj->consulta("select count(*) as total from  rendimiento_general where periodo='".$periodo."' and grado='".$reg_ren['grado']."' and curso='".$reg_ren['codigo']."' and bajo=".$i);
                  $row1 = $obj->fetch_assoc($reg1);

                  if ($row1['total']!=0){

                     $s.="<center>";
                     $s.="<br><table align='center' border=1 cellspacing='0' cellpadding='0' width='80%'>";

                     $s.="<tr align='center'>";
                     $s.="<td>".$reg_ren['descripcion']."</td>";
                     $s.="<td>ASIGN. REPROBADAS :".$i."</td>";
                     $s.="<td>CANTIDAD DE ALUMNOS:".$row1['total']."</td>";
                     $s.="</tr>";

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


                    //-----Mostramos alumnos q reprobaron -------

                    $reg2 = $obj->consulta("select * from  rendimiento_general where periodo='".$periodo."' and grado='".$reg_ren['grado']."' and curso='".$reg_ren['codigo']."' and bajo=".$i." order by promedio desc");

                    $l=1;
                    while($row2 = $obj->fetch_assoc($reg2)){

                      if ($i!=0)
                         $s.="<br>";

                      $s.="<center>";
                      $s.="<table align='center' border=1 cellspacing='0' cellpadding='0' width='80%'>";

                      $s.="<tr>";
                      $s.="<td width='5%' align='center' style='font-size:12px'>".$l."</td>";
                      $s.="<td width='10%' align='center' style='font-size:12px'>".$row2['alumno']."</td>";
                      $s.="<td width='45%' style='font-size:12px'>".$row2['nombres']."</td>";
                      $s.="<td width='20%' align='center' style='font-size:12px'>Promedio : ".decimales($row2['promedio'])."</td>";
                      $s.="<td width='10%' style='font-size:12px'>".$row2['nivel_desempeno']."</td>";
                      $s.="<td width='10%' style='font-size:12px'>Puesto : ".$row2['puesto']."</td>";
                      $s.="</tr>";

                      $s.="</table></center>";
                      $l++;

                      //-----Mostramos asignaturas reprobadas -------

                       $reg3 = $obj->consulta("select detalleboletin.alumno, boletin.asignatura, catalogo.descripcion, detalleboletin.definitiva, detalleboletin.valor_asigna from catalogo inner join (boletin inner join detalleboletin on boletin.numero = detalleboletin.boletin) on catalogo.codigo = boletin.asignatura
                                               where detalleboletin.alumno='".$row2['alumno']."' and boletin.periodo='".$periodo."' and  detalleboletin.definitiva<3");

                       $k=1;
                       while($row3 = $obj->fetch_assoc($reg3)){

                            $s.="<center>";
                            $s.="<table align='center' border=0 cellspacing='0' cellpadding='0' width='80%'>";

                            $s.="<tr>";
                            $s.="<td width='5%' align='center' style='font-size:12px'>*</td>";
                            $s.="<td width='10%' align='center' style='font-size:12px'>".$row3['asignatura']."</td>";
                            $s.="<td width='45%' style='font-size:12px'>".$row3['descripcion']."</td>";
                            $s.="<td width='20%' align='center' style='font-size:12px'>".($row3['definitiva'])."</td>";
                            $s.="<td width='20%' style='font-size:12px'>".$row3['valor_asigna']."</td>";

                            $s.="</tr>";

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

                            $k++;
                      }

                   }

                 }

                  $suma=$suma+$row1['total'];
                  $i++;
              }

              $s.="<center>";
              $s.="<br><table align='center' border=1 cellspacing='0' cellpadding='0' width='80%'>";
              $s.="<tr align='center'>";
              $s.="<td width='70%'>TOTAL ALUMNOS</td>";
              $s.="<td width='30%'>".$row['cantidad']."</td>";
              $s.="</tr>";

              $s.="</table></center><br>";

              $c=$reg_ren['grado'];

         }
    }


     /*--------------------------------------*/

    
     $s.="</body></html>";

   


    $obj->disconnect();

    $dompdf = new DOMPDF();
    $dompdf->load_html($s);
    $dompdf->set_paper('A4', 'portrait');
    $dompdf->render();

    $dompdf->stream("Cantidad_Reprobadas.pdf", array("Attachment" => false));



    $obj->disconnect();

   

?>

