<?php
//FORMATO DEL BOLETIN GENERAL PARA EL GABRIELA MISTRAL
$academia->instruccion("SELECT ide_adtivo, LCASE(CONCAT_WS(' ', no1_adtivo, no2_adtivo,ape1_adtivo, ape2_adtivo)) nom_ape FROM adm_administrativos WHERE cod_cargo=1 or cod_cargo=2 order by cod_cargo");
$regFirmantes = $academia->fetchAll();
$nom_rector = $regFirmantes[0]['nom_ape'];
$nom_cordin = $regFirmantes[1]['nom_ape'];

$sql = "call SP_BOLETINES_INDIVIDUAL_GENERICO('$cod_estudiantes',$grupo,$periodon,'$iduser')";

$academia->instruccion($sql);
echo '<div style="display:none">' . $sql . '</div>';
unset($sql);
$row = $academia->extraer_registro();
$dtsColegio = $sesion->get('info_colegio');
$encabezado = $dtsColegio['enca_boletin'];
$observa = "";
$mayus_nom = $sesion->getVar('configuracion.format_nom');
$mayus_mat = $sesion->getVar('configuracion.format_mat');
do {
    $observa = "";
    echo '<div id="pageBoletin">';
    echo $encabezado;
    $notest = str_replace("\n", "<br>", $row['notas']);
    $notest = str_replace("\t", " ", $notest);
    $notest = parseString($notest);
    $notest = json_decode($notest, true);

    $infcom = json_decode($row['comportamiento'], true);
    $cod_est = $notest['cd_est'];
    $msgPbar = "id-" . $notest[dc_est] . "-pto-" . $notest[pto_gru];
    $msgPbar .= "-grado-" . $grado . "-per-" . $periodon . '-val-si-anio' . $sesion->get('anio');
    $dtsObserva = json_decode($row['observaciones'], true);
    ?>
    <link href="../css/formato_boletin.css" rel="stylesheet" type="text/css">
    <table width="100%"
           style="border:1px solid; margin-bottom:2px; font-size: 9pt;  font-family: Arial, Helvetica, sans-serif;  vertical-align:top;">
        <tr align="left">
            <td width="29%"><strong>Estudiante:</strong>&nbsp;&nbsp;</td>
            <td width="28%"><strong>Grado:</strong> &nbsp;&nbsp;</td>
            <!--            <td width="20%"><strong>Puesto en el grupo:</strong>&nbsp;&nbsp;</td>-->
            <td width="13%"><strong>Periodo:</strong>&nbsp;&nbsp;</td>
            <td width="10%"><strong>Añod:</strong></td>
        </tr>
        <tr align="left">
            <td><span style="font-size:11pt"><?php echo formatoPalabras($notest['no_est'], $mayus_nom); ?></span></td>
            <td><?php echo $grado; ?></td>
            <!--            <td><?php echo $notest[pto_gru]; ?></td>-->
            <td><?php echo $periodo; ?></td>
            <td><?php echo $sesion->get('anio') ?></td>
        </tr>
    </table>
    <?php
    $materias = $notest['materias'];
    $numPeriodos = count($materias[0]['historicos']);
    $bandera = $periodon == $numPeriodos ? true : false;
    ?>
    <table width="100%" cellspacing="1" class='boletin'>
        <tr>
            <td colspan="3" rowspan="2">&nbsp;</td>
            <?php
            for ($i = 1; $i <= $numPeriodos; $i++) {
                echo '<td width="6%" rowspan="2" align="center" valign="middle" class="spancabecera">' . $i . '°</td>';
            }
            ?>


            <td rowspan="2" align="center">
                <div class="spancabecera nom_doc">VALORACION</div>
            </td>
            <td rowspan="2" align="center">
                <div class="spancabecera nom_doc">NIVEL</div>
            </td>
            <td rowspan="2" align="center">
                <div class="spancabecera nom_doc">PROMEDIO</div>
            </td>
            <td colspan="2" align="center" class="spancabecera nom_doc">FALTAS</td>
        </tr>
        <tr>
            <td align="center" class="spancabecera nom_doc">F.J</td>
            <td align="center" class="spancabecera nom_doc">F.S.J</td>
        </tr>
        <td colspan="12" style="height:2px !important; border:0px"></td>
        </tr>

        <?php
        $materias = $notest['materias'];
        $cdant = -1;
        foreach ($materias as $rmat):
            $historicos = $rmat['historicos'];
            $pierdey = $rmat['pierde'];
            if ($cdant != $rmat['cd_area']) {
                echo '<tr><td colspan="12" style="height:2px !important; border:0px"></td></tr>';
            }
            ?>
            <tr>
                <?php if ($cdant != $rmat['cd_area'] && $rmat['cant_asig'] > 1): ?>
                    <td width="12%" rowspan="<?php echo 2 * $rmat['cant_asig'] ?>" align="center"
                        valign="middle"><?php echo formatoPalabras($rmat['no_area'], $mayus_mat); ?></td>
                <?php endif; ?>
                <td width="<?php echo ($rmat['cant_asig'] == 1) ? '12%' : '24%' ?>" rowspan="2"
                    colspan="<?php echo ($rmat['cant_asig'] == 1) ? 2 : 1 ?>"><?php echo formatoPalabras($rmat['no_asi'], $mayus_mat); ?></td>

                <td colspan="5"><strong class="nom_doc">PROF.</strong> <span
                            class="bordes_arriba nom_doc"><?php echo ucwords($rmat['dte']); ?></span></td>
                <?php if ($cdant != $rmat['cd_area']): ?>
                    <td rowspan="<?php echo 2 * $rmat['cant_asig'] ?>" align="center" valign="middle"><span
                                style="font-size:10pt; text-align:center">
                        <?php
                        if ($rmat['prom_area'] <= $pierdey) {
                            //echo "<b>" . numberFormat($rmat['prom_area'], 1, '.', '') . "</b>";
                        } else {
                           // echo "" . numberFormat($rmat['prom_area'], 1, '.', '') . "";
                        }
                        ?>  
                    </span></td>

                    <td rowspan="<?php echo 2 * $rmat['cant_asig'] ?>" align="center" valign="middle"><span
                                style="font-size:10pt">
                        <?php
                        if ($rmat['nn'] <= $pierdey) {
                            echo "<b>" . ucfirst($rmat['promlet_area']) . "</b>";
                        } else {
                            echo ucfirst($rmat['promlet_area']);;
                        }
                        ?> 
                    </span>
                    </td>
                    <!--                Promedio del area-->
                    <td rowspan="<?php echo 2 * $rmat['cant_asig'] ?>" align="center"
                        valign="middle"><?php echo numberFormat($rmat['prom_area_tot'], 1, '.', '') ?></td>
                <?php endif; ?>
                <td width="5%" rowspan="2" align="center" valign="middle"><?php echo ucfirst($rmat['fj']); ?></td>
                <td width="5%" rowspan="2" align="center" valign="middle"><?php echo ucfirst($rmat['fi']); ?></td>
            </tr>
            <tr>

                <td width="10%"><strong class="nom_doc">PERIODOS</strong></td>
                <?php
                $sum = 0;
                for ($ix = 0; $ix < $numPeriodos; $ix++) {
                    $notah = empty($historicos[$ix]) ? '' : numberFormat($historicos[$ix], 1, '.', '');
                    $sum += $notah;
                    echo '<td class="bordes_arriba">';
                    echo $notah <= $pierdey ? ('<b>' . $notah . '</b>') : $notah;
                    echo '</td>';
                }
                $prmSum = $sum / $numPeriodos;
                ?>
            </tr>
            <?php
            if (!empty($rmat['obs_asi']) && strlen($rmat['obs_asi']) > 5) {
                $observa .= '<tr><td> <strong>- ' . ucfirst($rmat['no_asi']) . ':</strong>' . ucfirst($rmat['obs_asi']) . '</td></tr>';
            }
            $cdant = $rmat['cd_area'];
        endforeach;
        ?>
    </table>
    <?php
    #echo $academia->escapar($row['observaciones']);
    $dtsObserva = str_replace("\n", "<br/>", $row['observaciones']);
    $dtsObserva = str_replace("\t", "  ", $dtsObserva);
    $dtsObserva = parseString($dtsObserva);
    #echo $dtsObserva.'<br>';
    $dtsObserva = json_decode($dtsObserva, true);
    $observa = $dtsObserva['obsobserva'];
    #echo $observa;
    #print_r($dtsObserva);  
    $notaComporta = "___";
    if (!empty($dtsObserva['notaobserva'])) {
        $notaComporta = numberFormat($dtsObserva['notaobserva'], 1); //nota numeros
    }
    echo "<br/><div class='boletin'><strong>NOTA COMPORTAMIENTO:&nbsp;&nbsp;</strong>" . $notaComporta . " <br><strong>OBSERVACIONES:</strong></div>";
    if (!empty($observa))
        echo '<table width="100%" align="center" class="observa">' . $observa . "</table>";
    else
        echo '<table width="100%" align="center" class="observa"><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr></table>';
    ?>
    <?php

//    echo "<br/><div class='boletin'><strong>OBSERVACIONES:</strong></div>";
//    if (!empty($observa))
//        echo '<table width="100%" align="center" class="observa">' . $observa . "</table>";
//    else
//        echo '<table width="100%" align="center" class="observa"><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr><tr><td>&nbsp;</td></tr></table>';
    ?>
    <br><br>

    <table width="100%" border="0" cellspacing="0" cellpadding="0"
           style="margin-bottom:2px; font-size: 9pt;  font-family: Arial, Helvetica, sans-serif;  vertical-align:top;">
        <tr align="center">
            <td><br><br>_________________________________<br>
                <?php echo strtoupper($director) ?><br>
                Director de Grupo
            </td>
            <td>
                <?php
                if ($tipouser == $codrector || $tipouser == $codadmin || $tipouser == $codsecre) {
                    if (file_exists("../images/firma_cor.png")) {
                        echo "<img src='../images/firma_cor.png' width='160' height='40' alt='Firma rector' class='codbar' style='vertical-align:bottom'>";
                        echo "<br> _________________________________<br/>" . strtoupper($nom_cordin) . "<br>Cordinador Académico";
                    }
                } else {
                    echo "<br> _________________________________<br/>" . strtoupper($nom_cordin) . "<br>Cordinador Académico";
                }
                ?>
                <!--                _________________________________<br>
                <?php echo strtoupper($nom_cordin) ?><br>
                Cordinador Académico-->
            </td>
        </tr>
    </table>

    <table id="foot" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td width="6%" rowspan="2" align="center" valign="middle">&nbsp;</td>
            <td align="center" valign="middle"><?php echo $sesion->getVar('info_colegio.pie_boletin') ?></td>
            <td width="6%" rowspan="2" align="right"><?php
                // user data
                $PNG_TEMP_DIR = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;
                $PNG_WEB_DIR = 'temp/';
                //ofcourse we need rights to create temp dir
                if (!file_exists($PNG_TEMP_DIR))
                    mkdir($PNG_TEMP_DIR);

                $errorCorrectionLevel = 'L';
                $matrixPointSize = 1;
                //echo $msgPbar;
                $filename = $PNG_TEMP_DIR . 'Boletin' . $cod_est . '_' . $sesion->get('academia_succes') . "-" . md5($errorCorrectionLevel . '|' . $matrixPointSize . '' . time()) . '.png';
                QRcode::png($msgPbar, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
                echo '<img src="' . $PNG_WEB_DIR . basename($filename) . '" class="codbar" />';
                ?></td>
        </tr>
        <tr>
            <td align="center"><span
                        class="copy"><?php echo $dtsLang['project']['nom_soft'] . ' ' . $dtsLang['project']['ver_soft'] . ' ' . $dtsLang['project']['footertext'] . " " . $dtsLang['project']['anio_ini'] . ' - ' . date('Y') ?></span>
            </td>
        </tr>
    </table>

    <?php
    echo "</div>"; //cirra la pagina
} while ($row = $academia->extraer_registro());
$academia->liberar();
?>
