<?php
//formato boletin fidel de monclart

//formato sin notas
$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'];
$mayus_nom = $sesion->getVar('configuracion.format_nom');
$mayus_mat = $sesion->getVar('configuracion.format_mat');
$num_est = $academia->num_rows();
$observa = "";

function periodosEmergencia($periodoSelect)
{
    $periodo = '';
    switch ((int)$periodoSelect) {
        case 1 :
            $periodo = 'Informe I Semestre';
            break;
        case 2 :
            $periodo = 'Informe final';
            break;
        default:
            $periodo = '' . $periodoSelect;
    }
    return $periodo;
}

do {
    echo '<div id="pageBoletin">';
    echo $encabezado;
    $notest = str_replace("\n", "<br>", $row['notas']);
    $notest = str_replace("\t", " ", $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');
    $observa = "";
    $prom_periodo = $notest['prom_periodo'];


    ?>
    <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="19%"><strong>Id:</strong>&nbsp;&nbsp;<?php echo formatoPalabras($notest['dc_est'], $mayus_nom); ?>
            </td>
            <td colspan="2"><strong>Estudiante:</strong>&nbsp;&nbsp;<span
                        style="font-size:11pt"><?php echo formatoPalabras($notest['no_est'], $mayus_nom); ?></span></td>
            <td width="20%"><strong>Grado:</strong> &nbsp;&nbsp;<?php echo $grado; ?></td>
        </tr>
        <tr align="left">
            <td><strong>Puesto general:</strong>&nbsp;&nbsp; <?php echo $notest['pto_gru']; ?></td>
            <td width="41%"><strong>Sede:</strong>&nbsp;&nbsp;<?php echo $sede; ?></td>
            <td width="20%"><strong>Jornada:</strong>&nbsp;&nbsp;<?php echo 'Única'; ?></td>
            <td><strong>Periodo:</strong>&nbsp;&nbsp;<?php echo periodosEmergencia($periodon); ?></td>
        </tr>
    </table>
    <?php
    $materias = $notest['materias'];
    $numPeriodos = count($materias[0]['historicos']);
    $bandera = $periodon == $numPeriodos ? true : false;
    $bandera = false;
    ?>
    <table width="100%" cellspacing="1" class='boletin'>
        <tr>
            <td width="27%" rowspan=2 class='spancabecera'>Asignatura</td>
            <td width="5%" rowspan=2 class='spancabecera'>Pto Asig</td>
            <td width="15%" colspan="2" class='spancabecera'><?php echo ($bandera) ? 'Desempeño definitivo' : 'Desempeño' ?></td>
            <td width="5%" rowspan=2 class='spancabecera'>Fal jus</td>
            <td width="5%" rowspan=2 class='spancabecera'>Fal inj</td>
            <td width="20%" colspan="<?php echo $numPeriodos ?>" class='spancabecera'>Periodos Académicos</td>
            <td width="28%" <?php echo ($bandera) ? 'colspan="2"' : 'rowspan=2' ?>
                class='spancabecera'><?php echo $bandera ? 'Promedio final' : 'Docente' ?></td>
        </tr>

        <tr>
            <td width="5%" class="spancabecera">Num</td>
            <td width="10%" class="spancabecera">Val</td>
            <?php
            for ($i = 1; $i <= $numPeriodos; $i++) {
                echo '<td class="bordes_arriba spancabecera" width="5%">' . $i . '</td>';
            }
            ?>
            <?php if ($bandera) { ?>
                <td width="5%" class="spancabecera">Num</td>
                <td width="10%" class="spancabecera">Val</td>
            <?php } ?>
        </tr>


        <?php
        foreach ($materias as $rmat):

            $historicos = $rmat['historicos'];
            $pierdey = $rmat['pierde'];
            ?>
            <tr>
                <td><?php echo formatoPalabras($rmat['no_asi'], $mayus_mat); ?></td>
                <td style="font-size:8pt"><?php echo ucfirst($rmat['puesto']); ?></td>
                <td width="5%" style="font-size:10pt; text-align:center">
                    <?php
                    if ( strtolower($rmat['prom_mat_tot'])=='bajo' ) {
                        echo "<b>" . numberFormat($rmat['prom_mat_tot']) . "</b>";
                    } else {
                        echo numberFormat($rmat['prom_mat_tot']);
                    }
                    ?>
                </td>
                <td width="10%" style="font-size:10pt"><?php
                    //if ($rmat['nn'] <= $pierdey) {
                    if ( strtolower($rmat['prom_mat_tot'])=='bajo' ) {
                        echo "<b>" . ucfirst($rmat['prom_mat_tot_let']) . "</b>";
                    } else {
                        echo ucfirst($rmat['prom_mat_tot_let']);
                    }
                    ?>
                </td>
                <td><?php echo ucfirst($rmat['fj']); ?></td>
                <td><?php echo ucfirst($rmat['fi']); ?></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;
                ?>

                <?php
                $prmSum = empty($prmSum) ? '0.0' : $prmSum;
                if ($bandera) {
                    $txNtNum = ($rmat['prom_mat_tot'] <= $pierdey) ? ('<b>' . numberFormat(round($rmat['prom_mat_tot'], PHP_ROUND_HALF_UP), 1, '.', '') . '</b>') : numberFormat($rmat['prom_mat_tot'], 1, '.', '');
                    $txNtLet = ($rmat['prom_mat_tot'] <= $pierdey) ? ('<b>' . $rmat['prom_mat_tot_let'] . '</b>') : $rmat['prom_mat_tot_let'];
                    $cdAux = '<td style="text-align:center">' . $txNtNum . '</td>';
                    $cdAux .= '<td style="text-align:center">' . $txNtLet . '</td>';
                } else {
                    $cdAux = '<td class="bordes_arriba nom_doc">' . ucwords($rmat['dte']) . '</td>';
                }
                echo $cdAux
                ?>

            </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>';
            }

        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);
    $observa2 = $dtsObserva['obsobserva'];

    echo '<br><table width="100%" align="center" class="boletin">' . $observa . "</table>";

    #print_r($dtsObserva);
    $notaComporta = numberFormat($dtsObserva['notaobserva'], 1); //nota numeros
    echo "<br/><div class='boletin'><strong>NOTA COMPORTAMIENTO:&nbsp;&nbsp;</strong></div>" . $notaComporta ;
    echo " &nbsp;&nbsp;. <div class='boletin'><br><strong>OBSERVACIONES:</strong></div>";
    if (!empty($observa2))
        echo '<table width="100%" align="center" class="observa" style="font-family: Verdana, Geneva, sans-serif;">' . $observa2 . "</table>";
    else
        echo '<table width="100%" align="center" class="observa" style="font-family: Verdana, Geneva, sans-serif;"><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" valign="bottom">
            <td>_________________________________<br>
                <?php echo strtoupper($nom_cordin) ?><br/>
                Coordinador Academico
            </td>
            <td>_________________________________<br>
                <?php echo strtoupper($director) ?><br>
                Director de Grupo
            </td>
        </tr>
        <!--<tr align="center" valign="bottom">
            <td colspan="2">_________________________________<br/>
                <?php echo strtoupper($director) ?><br>
                Director de Grupo
            </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 . '') . '.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();
?>
