<?php require_once('Connections/conn.php'); ?>
<?php
//***************************************************************************************
// Nombre del Script: alumnos.php
// Descripción: Permite la gestión de los estudiantes de la ENAHP (Registrar, Modificar
//              y Eliminar). Se prodrá eliminar un estudiante siempre y cuando no este
//              inscrito y no tenga materias cursadas.
//************************************************************************************** 
 session_start();
  // Captura la datos del usuario que se logueó (Cédula, Tipo de Usuario y Username).
  $cedula_usuario=$_SESSION['CEDULA_USUARIO'];
  $username=$_SESSION['MM_Username'];
  $tipo_usuario=$_SESSION['PRIVILEGIO'];
 

  mysql_select_db("pregrado_pregrado");
  mysql_query ("SET NAMES 'utf8'"); 
  $sql = "select apellidos,nombres from usuarios where cedula='$cedula_usuario'";
  $rs=mysql_query($sql,$conn);
  $row=mysql_fetch_array($rs);
  $intruso=$row['apellidos'].', '.$row['nombres'];
  
  if (!isset($_SESSION['MM_Username']) || $_SESSION['MM_Username']=="") {
      // Registra el acceso indebido.
		$consulta=addslashes($sql);
  		$fecha=date("Y-m-d G:i:A");
  		$accion="Intento de acceso sin iniciar sesión.";
  		$script="seleccion_mencion.php";
  		$ip=obtener_ip();
  		$sql1 = "insert into `intrusos` (`username`, `tipo_usuario`,`cedula`, `ip`, `fecha`, `accion`, `script`) values ('$username', '$tipo_usuario', '$cedula_usuario', '$ip', '$fecha', '$accion', '$script')";
  		mysql_query($sql1, $conn) or die(mysql_error());
	    // Destruye la sessión.
	    session_destroy();
	    header ("Location: modulos/inicio/index.php");
		return;
	  }  

  if ($tipo_usuario!="1" && $tipo_usuario!="10") {
        // Registra el acceso indebido.
		$consulta=addslashes($sql);
  		$fecha=date("Y-m-d G:i:A");
  		$accion="Intento de Acceso indebido";
  		$script="seleccion_mencion php";
  		$ip=obtener_ip();
  		$sql1 = "insert into `intrusos` (`username`, `tipo_usuario`,`cedula`, `ip`, `fecha`, `accion`, `script`) values ('$username', '$tipo_usuario', '$cedula_usuario', '$ip', '$fecha', '$accion', '$script')";
  		mysql_query($sql1, $conn) or die(mysql_error());
        // Destruye la sesión.
		session_destroy();
        $inicial = "salida_intruso.php?intruso=$intruso";
        header(sprintf("Location: %s", $inicial));
		return;
  }  
  
  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>siga_pregrado -- seleccion_mencion</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    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>Selecci&oacute;n de Menciones</h2></td></tr></table>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
  require_once('Connections/conn.php');
  mysql_select_db("pregrado_pregrado");
  mysql_query ("SET NAMES 'utf8'");
  
  $showrecs = 250;
  $pagerange = 10;

  $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">!SIGA LA ENAHP!</td></tr></table>
</body>
</html>

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


  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>Registros <?php echo $startrec + 1 ?> - <?php echo $reccount ?> of <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="seleccion_mencion.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 "mencion" ?>"<?php if ($filterfield == "mencion") { echo "selected"; } ?>><?php echo htmlspecialchars("mencion") ?></option>
<option value="<?php echo "nivel" ?>"<?php if ($filterfield == "nivel") { echo "selected"; } ?>><?php echo htmlspecialchars("nivel") ?></option>
<option value="<?php echo "turno" ?>"<?php if ($filterfield == "turno") { echo "selected"; } ?>><?php echo htmlspecialchars("turno") ?></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="seleccion_mencion.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">Ver</td>
<td class="hr">Editar</td>
<td class="hr">Borrar</td>
<td class="hr"><a class="hr" href="seleccion_mencion.php?order=<?php echo "id_alumno" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Matrícula") ?></a></td>
<td class="hr"><a class="hr" href="seleccion_mencion.php?order=<?php echo "cedula" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Cédula") ?></a></td>
<td class="hr"><?php echo htmlspecialchars("Apellidos") ?></td>
<td class="hr"><?php echo htmlspecialchars("Nombres") ?></td>
<td class="hr"><a class="hr" href="seleccion_mencion.php?order=<?php echo "mencion" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Mención") ?></a></td>
<td class="hr"><a class="hr" href="seleccion_mencion.php?order=<?php echo "nivel" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Nivel") ?></a></td>
<td class="hr"><a class="hr" href="seleccion_mencion.php?order=<?php echo "turno" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Turno") ?></a></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
	$id_alumno=$row["id_alumno"];
	$turno=$row["turno"];
	$sql_alumnos = "SELECT apellidos,nombres FROM alumnos WHERE id_alumno='$id_alumno'";
	$rst_alumnos = mysql_query($sql_alumnos, $conn) or die(mysql_error());
    $row_alumnos = mysql_fetch_assoc($rst_alumnos);
	if($turno=="1") $nombre_turno="MATUTINO";
	if($turno=="2") $nombre_turno="VESPERTINO";
	if($turno=="4") $nombre_turno="NOCTURNO";

?>
<tr>
<td class="<?php echo $style ?>" align="center"><a href="seleccion_mencion.php?a=view&recid=<?php echo $i ?>"><img src="imagenes/search.gif" border="0" ></a></td>
<td class="<?php echo $style ?>" align="center"><a href="seleccion_mencion.php?a=edit&recid=<?php echo $i ?>"><img src="imagenes/edit.gif" border="0" ></a></td>
<td class="<?php echo $style ?>" align="center"><a href="seleccion_mencion.php?a=del&recid=<?php echo $i ?>"><img src="imagenes/delete.png" border="0" ></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_alumnos["apellidos"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row_alumnos["nombres"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["mencion"]) ?></td>

<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["nivel"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($nombre_turno) ?></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("mencion")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["mencion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("nivel")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nivel"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("turno")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["turno"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
  $turno=$row['turno'];
  if($turno=="1") $nombre_turno="MATUTINO";
  if($turno=="2") $nombre_turno="VESPERTINO";
  if($turno=="4") $nombre_turno="NOCTURNO";
  $id_alumno=$row['id_alumno'];
  $sql_alumnos = "SELECT id_alumno,cedula,apellidos,nombres FROM alumnos WHERE id_alumno='$id_alumno' ";
  $rst_alumnos = mysql_query($sql_alumnos, $conn) or die(mysql_error());
  $row_alumnos = mysql_fetch_assoc($rst_alumnos);
  $totalRows_alumnos = mysql_num_rows($rst_alumnos);
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Estudiante")."&nbsp;" ?></td>
<td class="dr">
<select name="id_alumno" size="1">
<option value="<?php echo $row['id_alumno']?>"><?php echo $row_alumnos['apellidos'].' '.$row_alumnos['nombres']?>&nbsp;(<?php echo $row['cedula']?>)</option>
</select>
</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Mención Asignada")."&nbsp;" ?></td>
<td class="dr">
<select name="mencion">
  <option value="<?php echo $row["mencion"]; ?>"><?php echo $row["mencion"]; ?></option>
  <option value="ACE">ADUANAS Y COMERCIO EXTERIOR</option>
  <option value="FIN">FINANZAS P&Uacute;BLICAS</option>
  <option value="RT">RENTAS</option>
</select></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Nivel Académico")."&nbsp;" ?></td>
<td class="dr"><select name="nivel">
  <option value="<?php echo $row["nivel"]; ?>"><?php echo $row["nivel"]; ?></option>
  <option value="CP">COMPONENTE PROFESIONAL</option>
</select></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Turno")."&nbsp;" ?></td>
<td class="dr">
<select name="mencion">
  <option value="<?php echo $row["Turno"]; ?>"><?php echo $nombre_turno; ?></option>
  <option value="1">MATUTINO</option>
  <option value="2">VESPERTINO</option>
  <option value="4">NOCTURNO</option>
</select>
</td>
</tr>
</table>
<?php } ?>

<?php function showrowadd($row, $iseditmode)
  {
  global $conn;
  
  if($turno=="1") $nombre_turno="MATUTINO";
  if($turno=="2") $nombre_turno="VESPERTINO";
  if($turno=="4") $nombre_turno="NOCTURNO";
  
  
  $sql_alumnos = "SELECT id_alumno,cedula,apellidos,nombres FROM alumnos WHERE id_alumno not in(select id_alumno from seleccion_mencion) order by apellidos,nombres";
  $rst_alumnos = mysql_query($sql_alumnos, $conn) or die(mysql_error());
  $row_alumnos = mysql_fetch_assoc($rst_alumnos);
  $totalRows_alumnos = mysql_num_rows($rst_alumnos);
  
   $sql_carreras_alumno = "SELECT id_alumno,cedula,apellidos,nombres FROM alumnos WHERE id_alumno not in(select id_alumno from seleccion_mencion) ";
  $rst_alumnos = mysql_query($sql_alumnos, $conn) or die(mysql_error());
  $row_alumnos = mysql_fetch_assoc($rst_alumnos);
  $totalRows_alumnos = mysql_num_rows($rst_alumnos);
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Estudiante")."&nbsp;" ?></td>
<td class="dr">
<select name="id_alumno" size="1">
  <option value="0">SELECCIONE...</option>
  <?php
do {  
?>
  <option value="<?php echo $row_alumnos['id_alumno']?>"><?php echo $row_alumnos['apellidos'].' '.$row_alumnos['nombres']?>&nbsp;(<?php echo $row_alumnos['cedula']?>)</option>
  <?php
} while ($row_alumnos = mysql_fetch_assoc($rst_alumnos));
  $rows = mysql_num_rows($rst_alumnos);
  if($rows > 0) {
      mysql_data_seek($rst_alumnos, 0);
	  $row_alumnos = mysql_fetch_assoc($rst_alumnos);
  }
?>
</select>
</td>
</tr>

<tr>
<td class="hr"><?php echo htmlspecialchars("Mención Seleccionada")."&nbsp;" ?></td>
<td class="dr">
<select name="mencion">
  <option value="<?php echo $row["mencion"]; ?>"><?php echo $row["mencion"]; ?></option>
  <option value="ACE">ADUANAS Y COMERCIO EXTERIOR</option>
  <option value="FIN">FINANZAS P&Uacute;BLICAS</option>
  <option value="RT">RENTAS</option>
</select></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Nivel")."&nbsp;" ?></td>
<td class="dr"><select name="nivel">
  <option value="CP">COMPONENTE PROFESIONAL</option>
</select></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Turno")."&nbsp;" ?></td>
<td class="dr">
<select name="turno">
  <option value="<?php echo $row["turno"]; ?>"><?php echo $nombre_turno; ?></option>
  <option value="1">MATUTINO</option>
  <option value="2">VESPERTINO</option>
  <option value="4">NOCTURNO</option>
</select>
</td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="seleccion_mencion.php?a=add">Agregar</a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="seleccion_mencion.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="seleccion_mencion.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="seleccion_mencion.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="seleccion_mencion.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="seleccion_mencion.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="seleccion_mencion.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="seleccion_mencion.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="seleccion_mencion.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="seleccion_mencion.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id_alumno" => "",
  "cedula" => "",
  "mencion" => "",
  "nivel" => "",
  "turno" => "");
showrowadd($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="seleccion_mencion.php?a=add">Agregar</a></td>
<td><a href="seleccion_mencion.php?a=edit&recid=<?php echo $recid ?>">Editar</a></td>
<td><a href="seleccion_mencion.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="seleccion_mencion.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="xturno" value="<?php echo $row["turno"] ?>">
<?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="seleccion_mencion.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="xturno" value="<?php echo $row["turno"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Conformar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  
}

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`, `mencion`, `nivel`, `turno` FROM `seleccion_mencion`";
  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 (`mencion` like '" .$filterstr ."') or (`nivel` like '" .$filterstr ."') or (`turno` 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 `seleccion_mencion`";
  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 (`mencion` like '" .$filterstr ."') or (`nivel` like '" .$filterstr ."') or (`turno` 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;
  $id_alumno=@$_POST["id_alumno"];
  $sql_alumnos = "SELECT cedula FROM alumnos WHERE id_alumno='$id_alumno'";
  $rst_alumnos = mysql_query($sql_alumnos, $conn) or die(mysql_error());
  $row_alumnos = mysql_fetch_assoc($rst_alumnos);
  $cedula=$row_alumnos["cedula"];
  $totalRows_alumnos = mysql_num_rows($rst_alumnos);

  $sql = "insert into `seleccion_mencion` (`id_alumno`, `cedula`, `mencion`, `nivel`, `turno`) values (" .sqlvalue(@$_POST["id_alumno"], false).", '$cedula', " .sqlvalue(@$_POST["mencion"], true).", " .sqlvalue(@$_POST["nivel"], true).", " .sqlvalue(@$_POST["turno"], false).")";
  mysql_query($sql, $conn) or die(mysql_error());
  
   $sql = "update `carreras_alumno` set `mencion`=" .sqlvalue(@$_POST["mencion"], true).", `nivel`=" .sqlvalue(@$_POST["nivel"], true).", `turno`=" .sqlvalue(@$_POST["turno"], false) .", sede='14-01' where id_alumno='$id_alumno'" ;
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;
  $id_alumno=@$_POST["id_alumno"];
  $sql = "update `seleccion_mencion` set `mencion`=" .sqlvalue(@$_POST["mencion"], true).", `nivel`=" .sqlvalue(@$_POST["nivel"], true).", `turno`=" .sqlvalue(@$_POST["turno"], false) ." where id_alumno='$id_alumno'" ;
  mysql_query($sql, $conn) or die(mysql_error());
  
  $sql = "update `carreras_alumno` set `mencion`=" .sqlvalue(@$_POST["mencion"], true).", `nivel`=" .sqlvalue(@$_POST["nivel"], true).", `turno`=" .sqlvalue(@$_POST["turno"], false) .", sede='14-01' where id_alumno='$id_alumno'" ;
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `seleccion_mencion` 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 .= "(`turno`";
  if (@$_POST["xturno"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xturno"], false);
  };
  $pk .= ")";
  return $pk;
}

function obtener_ip()
  {
 if ($_SERVER) {  
    if ( $_SERVER[HTTP_X_FORWARDED_FOR] ) {  
    $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
   } elseif ( $_SERVER["HTTP_CLIENT_IP"] ) {  
$realip = $_SERVER["HTTP_CLIENT_IP"];  
} else  {  
$realip = $_SERVER["REMOTE_ADDR"];  
}  
} else {  
if ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) {  
$realip = getenv( 'HTTP_X_FORWARDED_FOR' );  
} elseif ( getenv( 'HTTP_CLIENT_IP' ) ) {  
$realip = getenv( 'HTTP_CLIENT_IP' );  
} else {  
$realip = getenv( 'REMOTE_ADDR' );  
}  
}  
return $realip;  
  }
 
 ?>
