<?php session_start();
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];
  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];

?>

<html>
<head>
<title>ceulac_sadce -- historico_notas</title>
<meta name="generator" http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    background-color: #FFFFFF01;
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    background-color: #FFFFFF01;
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    background-color: #FFFFFF01;
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #336699;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #FFFFCF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Gesti&oacute;n de Promociones</h2></td></tr></table>
<?php
  $conn = connect();
  $showrecs = 20;
  $pagerange = 20;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
    case "edit":
      editrec($recid);
      break;
    case "del":
      deleterec($recid);
      break;
    default:
      select();
      break;
  }

  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

  mysql_close($conn);
?>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>

<?php function select()
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }

  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>Tabla: historico_notas</td></tr>
<tr><td>N�mero de Promociones <?php echo $startrec + 1 ?> - <?php echo $reccount ?> de <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="record.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>Filtrar por</b>&nbsp;</td>
<td><input type="text" name="filter" value="<?php echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">Todos los Campos</option>
<option value="<?php echo "id_alumno" ?>"<?php if ($filterfield == "id_alumno") { echo "selected"; } ?>><?php echo htmlspecialchars("id_alumno") ?></option>
<option value="<?php echo "cedula" ?>"<?php if ($filterfield == "cedula") { echo "selected"; } ?>><?php echo htmlspecialchars("cedula") ?></option>
<option value="<?php echo "id_carrera" ?>"<?php if ($filterfield == "id_carrera") { echo "selected"; } ?>><?php echo htmlspecialchars("id_carrera") ?></option>
<option value="<?php echo "id_materia" ?>"<?php if ($filterfield == "id_materia") { echo "selected"; } ?>><?php echo htmlspecialchars("id_materia") ?></option>
<option value="<?php echo "id_docente" ?>"<?php if ($filterfield == "id_docente") { echo "selected"; } ?>><?php echo htmlspecialchars("id_docente") ?></option>
<option value="<?php echo "nota" ?>"<?php if ($filterfield == "nota") { echo "selected"; } ?>><?php echo htmlspecialchars("nota") ?></option>
<option value="<?php echo "seccion" ?>"<?php if ($filterfield == "seccion") { echo "selected"; } ?>><?php echo htmlspecialchars("seccion") ?></option>
<option value="<?php echo "periodo" ?>"<?php if ($filterfield == "periodo") { echo "selected"; } ?>><?php echo htmlspecialchars("periodo") ?></option>
<option value="<?php echo "sede" ?>"<?php if ($filterfield == "sede") { echo "selected"; } ?>><?php echo htmlspecialchars("sede") ?></option>
<option value="<?php echo "annio" ?>"<?php if ($filterfield == "annio") { echo "selected"; } ?>><?php echo htmlspecialchars("annio") ?></option>
<option value="<?php echo "aprobacion" ?>"<?php if ($filterfield == "aprobacion") { echo "selected"; } ?>><?php echo htmlspecialchars("aprobacion") ?></option>
<option value="<?php echo "acta" ?>"<?php if ($filterfield == "acta") { echo "selected"; } ?>><?php echo htmlspecialchars("acta") ?></option>
<option value="<?php echo "fecha" ?>"<?php if ($filterfield == "fecha") { echo "selected"; } ?>><?php echo htmlspecialchars("fecha") ?></option>
</select></td>
<td><input type="checkbox" name="wholeonly"<?php echo $checkstr ?>>Palabra Completa</td>
</td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="action" value="Aplicar Filtro"></td>
<td><a href="record.php?a=reset">Quitar Filtro</a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr">&nbsp;</td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "id_alumno" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("id_alumno") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "cedula" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("cedula") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "id_carrera" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("id_carrera") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "id_materia" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("id_materia") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "id_docente" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("id_docente") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "nota" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("nota") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "seccion" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("seccion") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "periodo" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("periodo") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "sede" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("sede") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "annio" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("annio") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "aprobacion" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("aprobacion") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "acta" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("acta") ?></a></td>
<td class="hr"><a class="hr" href="record.php?order=<?php echo "fecha" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("fecha") ?></a></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<td class="<?php echo $style ?>"><a href="record.php?a=view&recid=<?php echo $i ?>">Ver</a></td>
<td class="<?php echo $style ?>"><a href="record.php?a=edit&recid=<?php echo $i ?>">Editar</a></td>
<td class="<?php echo $style ?>"><a href="record.php?a=del&recid=<?php echo $i ?>">Borrar</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_alumno"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["cedula"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_carrera"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_materia"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["id_docente"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nota"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["seccion"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["periodo"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["sede"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["annio"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["aprobacion"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["acta"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["fecha"]) ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php } ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("id_alumno")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_alumno"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("cedula")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["cedula"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_carrera")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_carrera"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_materia")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_materia"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_docente")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["id_docente"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nota")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nota"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("seccion")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["seccion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("periodo")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["periodo"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("sede")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["sede"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("annio")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["annio"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("aprobacion")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["aprobacion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("acta")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["acta"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("fecha")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["fecha"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("id_alumno")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_alumno" value="<?php echo str_replace('"', '&quot;', trim($row["id_alumno"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("cedula")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="cedula" maxlength="12" value="<?php echo str_replace('"', '&quot;', trim($row["cedula"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_carrera")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_carrera" maxlength="5" value="<?php echo str_replace('"', '&quot;', trim($row["id_carrera"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_materia")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_materia" maxlength="15" value="<?php echo str_replace('"', '&quot;', trim($row["id_materia"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("id_docente")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="id_docente" value="<?php echo str_replace('"', '&quot;', trim($row["id_docente"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nota")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="nota" value="<?php echo str_replace('"', '&quot;', trim($row["nota"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("seccion")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="seccion" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["seccion"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("periodo")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="periodo" maxlength="10" value="<?php echo str_replace('"', '&quot;', trim($row["periodo"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("sede")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="sede" maxlength="2" value="<?php echo str_replace('"', '&quot;', trim($row["sede"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("annio")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="annio" value="<?php echo str_replace('"', '&quot;', trim($row["annio"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("aprobacion")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="aprobacion" maxlength="12" value="<?php echo str_replace('"', '&quot;', trim($row["aprobacion"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("acta")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="acta" maxlength="6" value="<?php echo str_replace('"', '&quot;', trim($row["acta"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("fecha")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="fecha" value="<?php echo str_replace('"', '&quot;', trim($row["fecha"])) ?>"></td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="record.php?a=add">Agregar </a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="record.php?page=<?php echo $page - 1 ?>">&lt;&lt;&nbsp;Anterior</a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="record.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="record.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="record.php?page=<?php echo $page + 1 ?>">Siguiente&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="record.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="record.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="record.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Siguiente Registro</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>

<?php function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="record.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="record.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id_alumno" => "",
  "cedula" => "",
  "id_carrera" => "",
  "id_materia" => "",
  "id_docente" => "",
  "nota" => "",
  "seccion" => "",
  "periodo" => "",
  "sede" => "",
  "annio" => "",
  "aprobacion" => "",
  "acta" => "",
  "fecha" => "");
showroweditor($row, false);
?>
<p><input type="submit" name="action" value="Guardar"></p>
</form>
<?php } ?>

<?php function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="record.php?a=add">Agregar </a></td>
<td><a href="record.php?a=edit&recid=<?php echo $recid ?>">Editar </a></td>
<td><a href="record.php?a=del&recid=<?php echo $recid ?>">Borrar </a></td>
</tr>
</table>
<?php
  mysql_free_result($res);
} ?>

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form enctype="multipart/form-data" action="record.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xid_alumno" value="<?php echo $row["id_alumno"] ?>">
<input type="hidden" name="xcedula" value="<?php echo $row["cedula"] ?>">
<input type="hidden" name="xid_carrera" value="<?php echo $row["id_carrera"] ?>">
<input type="hidden" name="xid_materia" value="<?php echo $row["id_materia"] ?>">
<input type="hidden" name="xid_docente" value="<?php echo $row["id_docente"] ?>">
<input type="hidden" name="xnota" value="<?php echo $row["nota"] ?>">
<input type="hidden" name="xseccion" value="<?php echo $row["seccion"] ?>">
<input type="hidden" name="xperiodo" value="<?php echo $row["periodo"] ?>">
<input type="hidden" name="xsede" value="<?php echo $row["sede"] ?>">
<input type="hidden" name="xannio" value="<?php echo $row["annio"] ?>">
<input type="hidden" name="xaprobacion" value="<?php echo $row["aprobacion"] ?>">
<input type="hidden" name="xacta" value="<?php echo $row["acta"] ?>">
<input type="hidden" name="xfecha" value="<?php echo $row["fecha"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Guardar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function deleterec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("del", $recid, $count);
?>
<br>
<form action="record.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xid_alumno" value="<?php echo $row["id_alumno"] ?>">
<input type="hidden" name="xcedula" value="<?php echo $row["cedula"] ?>">
<input type="hidden" name="xid_carrera" value="<?php echo $row["id_carrera"] ?>">
<input type="hidden" name="xid_materia" value="<?php echo $row["id_materia"] ?>">
<input type="hidden" name="xid_docente" value="<?php echo $row["id_docente"] ?>">
<input type="hidden" name="xnota" value="<?php echo $row["nota"] ?>">
<input type="hidden" name="xseccion" value="<?php echo $row["seccion"] ?>">
<input type="hidden" name="xperiodo" value="<?php echo $row["periodo"] ?>">
<input type="hidden" name="xsede" value="<?php echo $row["sede"] ?>">
<input type="hidden" name="xannio" value="<?php echo $row["annio"] ?>">
<input type="hidden" name="xaprobacion" value="<?php echo $row["aprobacion"] ?>">
<input type="hidden" name="xacta" value="<?php echo $row["acta"] ?>">
<input type="hidden" name="xfecha" value="<?php echo $row["fecha"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Confirmar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  $conn = mysql_connect("localhost", "ceula_sadce_admi", "j7BnJdCGzyQBGcud");
  mysql_select_db("ceulac_sadce");
  return $conn;
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT `id_alumno`, `cedula`, `id_carrera`, `id_materia`, `id_docente`, `nota`, `seccion`, `periodo`, `sede`, `annio`, `aprobacion`, `acta`, `fecha` FROM `historico_notas`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`id_alumno` like '" .$filterstr ."') or (`cedula` like '" .$filterstr ."') or (`id_carrera` like '" .$filterstr ."') or (`id_materia` like '" .$filterstr ."') or (`id_docente` like '" .$filterstr ."') or (`nota` like '" .$filterstr ."') or (`seccion` like '" .$filterstr ."') or (`periodo` like '" .$filterstr ."') or (`sede` like '" .$filterstr ."') or (`annio` like '" .$filterstr ."') or (`aprobacion` like '" .$filterstr ."') or (`acta` like '" .$filterstr ."') or (`fecha` like '" .$filterstr ."')";
  }
  if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`";
  if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT COUNT(*) FROM `historico_notas`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`id_alumno` like '" .$filterstr ."') or (`cedula` like '" .$filterstr ."') or (`id_carrera` like '" .$filterstr ."') or (`id_materia` like '" .$filterstr ."') or (`id_docente` like '" .$filterstr ."') or (`nota` like '" .$filterstr ."') or (`seccion` like '" .$filterstr ."') or (`periodo` like '" .$filterstr ."') or (`sede` like '" .$filterstr ."') or (`annio` like '" .$filterstr ."') or (`aprobacion` like '" .$filterstr ."') or (`acta` like '" .$filterstr ."') or (`fecha` like '" .$filterstr ."')";
  }
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;

  $sql = "insert into `historico_notas` (`id_alumno`, `cedula`, `id_carrera`, `id_materia`, `id_docente`, `nota`, `seccion`, `periodo`, `sede`, `annio`, `aprobacion`, `acta`, `fecha`) values (" .sqlvalue(@$_POST["id_alumno"], false).", " .sqlvalue(@$_POST["cedula"], true).", " .sqlvalue(@$_POST["id_carrera"], true).", " .sqlvalue(@$_POST["id_materia"], true).", " .sqlvalue(@$_POST["id_docente"], false).", " .sqlvalue(@$_POST["nota"], false).", " .sqlvalue(@$_POST["seccion"], true).", " .sqlvalue(@$_POST["periodo"], true).", " .sqlvalue(@$_POST["sede"], true).", " .sqlvalue(@$_POST["annio"], true).", " .sqlvalue(@$_POST["aprobacion"], true).", " .sqlvalue(@$_POST["acta"], true).", " .sqlvalue(@$_POST["fecha"], true).")";
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;

  $sql = "update `historico_notas` set `id_alumno`=" .sqlvalue(@$_POST["id_alumno"], false).", `cedula`=" .sqlvalue(@$_POST["cedula"], true).", `id_carrera`=" .sqlvalue(@$_POST["id_carrera"], true).", `id_materia`=" .sqlvalue(@$_POST["id_materia"], true).", `id_docente`=" .sqlvalue(@$_POST["id_docente"], false).", `nota`=" .sqlvalue(@$_POST["nota"], false).", `seccion`=" .sqlvalue(@$_POST["seccion"], true).", `periodo`=" .sqlvalue(@$_POST["periodo"], true).", `sede`=" .sqlvalue(@$_POST["sede"], true).", `annio`=" .sqlvalue(@$_POST["annio"], true).", `aprobacion`=" .sqlvalue(@$_POST["aprobacion"], true).", `acta`=" .sqlvalue(@$_POST["acta"], true).", `fecha`=" .sqlvalue(@$_POST["fecha"], true) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `historico_notas` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`id_alumno`";
  if (@$_POST["xid_alumno"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_alumno"], false);
  };
  $pk .= ") and ";
  $pk .= "(`cedula`";
  if (@$_POST["xcedula"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xcedula"], true);
  };
  $pk .= ") and ";
  $pk .= "(`id_carrera`";
  if (@$_POST["xid_carrera"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_carrera"], true);
  };
  $pk .= ") and ";
  $pk .= "(`id_materia`";
  if (@$_POST["xid_materia"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_materia"], true);
  };
  $pk .= ") and ";
  $pk .= "(`id_docente`";
  if (@$_POST["xid_docente"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_docente"], false);
  };
  $pk .= ") and ";
  $pk .= "(`nota`";
  if (@$_POST["xnota"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xnota"], false);
  };
  $pk .= ") and ";
  $pk .= "(`seccion`";
  if (@$_POST["xseccion"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xseccion"], true);
  };
  $pk .= ") and ";
  $pk .= "(`periodo`";
  if (@$_POST["xperiodo"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xperiodo"], true);
  };
  $pk .= ") and ";
  $pk .= "(`sede`";
  if (@$_POST["xsede"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xsede"], true);
  };
  $pk .= ") and ";
  $pk .= "(`annio`";
  if (@$_POST["xannio"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xannio"], true);
  };
  $pk .= ") and ";
  $pk .= "(`aprobacion`";
  if (@$_POST["xaprobacion"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xaprobacion"], true);
  };
  $pk .= ") and ";
  $pk .= "(`acta`";
  if (@$_POST["xacta"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xacta"], true);
  };
  $pk .= ") and ";
  $pk .= "(`fecha`";
  if (@$_POST["xfecha"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xfecha"], true);
  };
  $pk .= ")";
  return $pk;
}
 ?>
