<?php
session_start();
date_default_timezone_set('America/Bogota');
error_reporting(E_ALL);
ini_set('display_errors', 1);

$_POST['per2'] = '9';
$_POST['cur2'] = '12';
$_POST['estudiante2'] = '160008';

ob_start();
include '../conector.php';
include 'fpdf.php';

class PDF extends FPDF
{
    function Header() {
        global $_POST;
        
        $sel_estudiante="select nombre from estudiante where estudiante_id=".$_POST['estudiante2']." ";
        $res = mysql_query($sel_estudiante);
        if($res) $est = @mysql_fetch_array($res);
        
        $sel_curso="select codigo,nombre from curso where curso_id=".$_POST['cur2']." ";
        $res2 = mysql_query($sel_curso);
        if($res2) $curso = @mysql_fetch_array($res2);
        
        $sel_planilla="select anno,periodo from planilla where id=".$_POST['per2']." ";
        $res3 = mysql_query($sel_planilla);
        if($res3) $planilla = @mysql_fetch_array($res3);
        
        $this->SetFont('Arial','B',14);
        $this->Cell(0,8,'CONCEPTOS EVALUATIVOS FINALES - ' . (isset($est[0]) ? utf8_decode($est[0]) : 'Estudiante'),0,1,'C');
        $this->SetFont('Arial','',10);
        $this->Cell(0,5,'Curso: ' . (isset($curso[0]) ? $curso[0] : '') . ' ' . (isset($curso[1]) ? $curso[1] : '') . ' | Periodo: ' . (isset($planilla[1]) ? $planilla[1] : '') . ' | Año: ' . (isset($planilla[0]) ? $planilla[0] : ''),0,1,'C');
        $this->Ln(5);
        
        $this->SetFont('Arial','B',9);
        $this->Cell(7,0.5,'Materia',1,0,'C');
        $this->Cell(1.1,0.5,'Nota',1,0,'C');
        $this->Cell(4.1,0.5,'Concepto',1,1,'C');
    }
}

$pdf = new PDF('L','cm','letter');
$pdf->AddPage();

$seleccion_pen=" select distinct pensum.subject_id
            from estudiante,pensum,curso,planilla_detalle,asignatura 
                         where pensum.class_id=".$_POST['cur2']." 
                        and estudiante.estudiante_id=".$_POST['estudiante2']." 
                        and planilla_detalle.planilla='".$_POST['per2']."' 
                        and pensum.class_id=curso.curso_id 
                        and estudiante.curso_id=pensum.class_id 
                        and pensum.estado='A' 
                        and planilla_detalle.codigo_pensum=pensum.subject_id 
                        and planilla_detalle.codigo_est=estudiante.estudiante_id 
                        and asignatura.subject_id=pensum.asignatura_id
                        order by pensum.nombre";

$res_seleccion_pen = @mysql_query($seleccion_pen);
$num_filas_pen = @mysql_num_rows($res_seleccion_pen);

$pdf->SetFont('Arial','',10);
$pdf->Cell(0,5,'Materias encontradas: ' . $num_filas_pen,0,1);

if($num_filas_pen > 0){
    while($rs_sel_pen = @mysql_fetch_array($res_seleccion_pen)){
        
        $select_curso_nota=" select distinct a.nombre as materia, 
                    pd.nota as D, pd.nota as I, pd.nota as F, pd.nota as IP, 
                    pd.nota as FP, pd.comp_1 as C1, pd.comp_2 as C2, pd.comp_3 as CG, pd.nota as niv,
                    pensum.intensidad
                from pensum 
                LEFT JOIN planilla_detalle pd ON pd.codigo_pensum = pensum.subject_id AND pd.planilla = '".$_POST['per2']."' AND pd.codigo_est = ".$_POST['estudiante2']."
                JOIN estudiante e ON e.estudiante_id = ".$_POST['estudiante2']."
                JOIN curso c ON e.curso_id = c.curso_id
                JOIN asignatura a ON a.subject_id = pensum.asignatura_id
                where pensum.class_id=".$_POST['cur2']." 
                and pensum.estado='A' 
                and pensum.subject_id=".$rs_sel_pen[0]."
                order by a.nombre";

        $res_select_curso_nota = @mysql_query($select_curso_nota);
        
        if($res_select_curso_nota && @mysql_num_rows($res_select_curso_nota) > 0){
            while($rs_curso_nota = @mysql_fetch_array($res_select_curso_nota)){
                $materia = isset($rs_curso_nota[0]) ? utf8_decode($rs_curso_nota[0]) : '-';
                $nota = isset($rs_curso_nota[1]) && $rs_curso_nota[1] > 0 ? $rs_curso_nota[1] : '-';
                
                $pdf->SetFont('Arial','',9);
                $pdf->Cell(7,0.5,$materia,1,0,'L');
                $pdf->Cell(1.1,0.5,$nota,1,0,'C');
                $pdf->Cell(4.1,0.5,'-',1,1,'L');
            }
        }
    }
}

$output = $pdf->Output('', 'S');
ob_end_clean();

header('Content-Type: application/pdf');
echo $output;