<?php
//ini_set("error_reporting",E_ALL);
require('../fpdf.php');

class PDF extends FPDF
{
var $B;
var $I;
var $U;
var $HREF;


function WriteHTML($html)
{
        //HTML parser
        $html=str_replace("\n",' ',$html);
        $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
        foreach($a as $i=>$e)
        {
                if($i%2==0)
                {
                        //Text
                        if($this->HREF)
                                $this->PutLink($this->HREF,$e);
                        else
                                $this->Write(5,$e);
//                              $this->Cell(180,5,$e,1,2,'J');
                }
                else
                {
                        //Tag
                        if($e{0}=='/')
                                $this->CloseTag(strtoupper(substr($e,1)));
                        else
                        {
                                //Extract attributes
                                $a2=explode(' ',$e);
                                $tag=strtoupper(array_shift($a2));
                                $attr=array();
                                foreach($a2 as $v)
                                        if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3))
                                                $attr[strtoupper($a3[1])]=$a3[2];
                                $this->OpenTag($tag,$attr);
                        }
                }
        }
}

function OpenTag($tag,$attr)
{
        //Opening tag
        if($tag=='B' or $tag=='I' or $tag=='U')
                $this->SetStyle($tag,true);
        if($tag=='A')
                $this->HREF=$attr['HREF'];
        if($tag=='BR')
                $this->Ln(5);
}

function CloseTag($tag)
{
        //Closing tag
        if($tag=='B' or $tag=='I' or $tag=='U')
                $this->SetStyle($tag,false);
        if($tag=='A')
                $this->HREF='';
}

function SetStyle($tag,$enable)
{
        //Modify style and select corresponding font
        $this->$tag+=($enable ? 1 : -1);
        $style='';
        foreach(array('B','I','U') as $s)
                if($this->$s>0)
                        $style.=$s;
        $this->SetFont('',$style);
}

function PutLink($URL,$txt)
{
        //Put a hyperlink
        $this->SetTextColor(0,0,255);
        $this->SetStyle('U',true);
        $this->Write(5,$txt,$URL);
        $this->SetStyle('U',false);
        $this->SetTextColor(0);
}
//Page header
function Header2($texto)
{
        //Logo
        $this->Image('../logo_colegio.png',10,8,20);
        $this->Image('../logo_colegio2.png',180,8,18);
        //Arial bold 15
        $this->SetFont('Arial','B',15);
        //Move to the right
        $this->Cell(80);
        //Title
        $this->Cell(30,10,$texto,0,0,'C');
        //Line break
        $this->Ln(20);
}

//Page footer
function Footer2()
{
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //Arial italic 8
        $this->SetFont('Arial','I',8);
        //Page number
//        $this->Cell(0,10,'Pagina '.$this->PageNo().'/{nb}',0,0,'C');
}
}

include("../fnc.php");
$id_gr=dec($idg);

$pdf=new PDF('L','mm','Legal');
$pdf->SetMargins(10,10,10);
$pdf->SetFillColor(255,255,255);
$pdf->AddPage();
$pdf->Encabezado($nombre_colegio,"FORMATO PARA CONTROL DE NOTAS");

$pdf->Ln(10);
$pdf->SetFont('Arial','',12);

$qgrado=mysql_query("select grados.grado, niveles.nivel, jornadas.jornada
                     from grados inner join niveles ON niveles.id = grados.nivel
                     inner join jornadas ON jornadas.id = grados.jornada
                     where grados.id = $id_gr");
$igrado=mysql_fetch_array($qgrado);

$qund=mysql_query("select * from unidades_grados where grado = '$id_gr'");
$unidades=Array();
while($iund=mysql_fetch_array($qund)){
 $unidades[]=$iund[unidad];
}

$pdf->Cell(0,5,"PROFESOR: ____________________________________________     MATERIA: __________________________________________  ",0,1,"R");
$pdf->Ln();
$seccion=dec($sid);
$pdf->Cell(0,5,"Grado: ".$igrado[grado].", Nivel: ".$igrado[nivel].", Sección: $seccion, Jornada: ".$igrado[jornada]."               AÑO: ".get_ciclo_activo(),0,1,"L");

$columna[0]=7;
$columna[1]=21;
$columna[2]=85;

$pdf->cell(7+21+85,15,"",1,1);
$pdf->Ln(-15);
$pdf->SetStyle("B",true);
$pdf->Cell($columna[0],10,"No.",0,0);
$pdf->Cell($columna[1],10,"CARNET",0,0);
$pdf->Cell($columna[2],10,"NOMBRE(S) DEL ALUMNO",0,0);
$pdf->Cell(2,10,"",0,1);


$izquierda=7+21+85+0;
for($i=0;$i<count($unidades);$i++){
  $pdf->Ln(-10);
  $pdf->cell($izquierda,5,"");
  $pdf->cell(30,10,"",1,1);
  $pdf->Ln(-10);
  
  
  $tmp=explode(" ",$unidades[$i]);
  $pdf->cell($izquierda,5,"");
  $pdf->cell(30,5,$tmp[0],0,1,"C");
  $pdf->cell($izquierda,5,"");
  $pdf->cell(30,5,$tmp[1],0,1,"C");
  //$pdf->Cell(2,10,"",0,1);
  //$pdf->Ln(-10);
  $izquierda+=30;
}

$pdf->Ln(-10);
$pdf->cell($izquierda,5,"");
$pdf->cell(20,15,"",1,1);
$pdf->Ln(-15);
$pdf->cell($izquierda,5,"");
$pdf->Cell(20,10,"TOTAL",0,0,"C");
$pdf->Ln();

$izquierda=7+21+85+0;
for($i=0;$i<count($unidades);$i++){
  if($i==0){
  $pdf->cell($izquierda,5,"");
  }
  $pdf->Setfont("Arial","",9);
  $pdf->cell(8,5,"Zona",1,0,"C");
  $pdf->cell(12,5,"Examen",1,0,"C");
  $pdf->cell(10,5,"Total",1,0,"C");
  //$pdf->Cell(2,10,"",0,0);
  $pdf->Setfont("Arial","",12);
//  $pdf->Ln(-10);
}

$pdf->SetStyle("B",false);

$pdf->Ln(5);

$qalumnos=mysql_query("select *
                       from alumnos
                       where grado_estudio = '$id_gr'
                       and seccion='$seccion'
                       and year_estudio = '$df_year_actual'
                       order by apellido, nombre");

$j=0;
while($ialumno=mysql_fetch_array($qalumnos)){
 $j++;
 $pdf->Cell($columna[0],5,$j,1,0);
 $pdf->Cell($columna[1],5,$ialumno[carnet],1,0);
 $pdf->Cell($columna[2],5,$ialumno[apellido].", ".$ialumno[nombre],1,0,1);
 //$pdf->Cell(2,10,"",0,0);
 
 $izquierda=21+85;
for($i=0;$i<count($unidades);$i++){
  if($i==0){
  //$pdf->cell($izquierda,5,"");
  }

  $pdf->cell(8,5,"",1,0,"C",1);
  $pdf->cell(12,5,"",1,0,"C",1);
  $pdf->cell(10,5,"",1,0,"C",1);
  //$pdf->Cell(2,10,"",0,0);

//  $pdf->Ln(-10);
}

 $pdf->Cell(20,5,"",1,1,1);
}


//$pdf->Output("control_zonas.pdf","D");
$pdf->Output();
exit();
?>
