<?php

$nomArch = "programacion.csv";

$recurso = false;
if (file_exists($nomArch)) {
    $recurso = fopen($nomArch,"r");
}
if ($recurso == false) {
    echo "<p>No existe la planilla. Ejecute primero el codigo2.php</p>";
} else {
    echo "<table border = '1'>";
    echo "<tr>";
    echo "<th>Carnet</th>";
    echo "<th>Paterno</th>";
    echo "<th>Materno</th>";
    echo "<th>Nombres</th>";
    echo "<th>Curso</th>";
    echo "<th>Ciudad</th>";
    echo "<th>Nota Final</th>";
    echo "<th>Estado</th>";
    echo "</tr>";
    while (!feof($recurso)) {
        $linea = fgets($recurso);
        if (trim($linea)!="") {
            $registro = explode(",",$linea);

            $nota1 = trim($registro[5]);
            $nota2 = trim($registro[6]);
            $nota3 = trim($registro[7]);
            $nota4 = trim($registro[8]);

            $notaFinal = ($nota1 + $nota2 + $nota3 + $nota4) / 4;
            $notaFinal = round($notaFinal);

            if ($notaFinal >= 51) {
                $estado = "Aprobado";
            } else {
                $estado = "Reprobado";
            }

            echo "<tr>";
            echo "<td>".trim($registro[0])."</td>";
            echo "<td>".trim($registro[1])."</td>";
            echo "<td>".trim($registro[2])."</td>";
            echo "<td>".trim($registro[3])."</td>";
            echo "<td>Programación</td>";
            echo "<td>".trim($registro[10])."</td>";
            echo "<td>".$notaFinal."</td>";
            echo "<td>".$estado."</td>";
            echo "</tr>";
        }
    }
    echo "</table>";
    fclose($recurso);
}
echo "<p><a href='index.php'>Volver al menu</a></p>";
?>