<?php require_once('Connections/cn.php'); ?>
<?php session_start();
  if (!isset($_SESSION['MM_Username'])) {
      header ("Location: modulos/inicio/index.php");
	  }
	  
	  $cedula_usuario=$_SESSION['CEDULA_USUARIO'];


mysql_select_db($database_cn, $cn);
mysql_query ("SET NAMES 'utf8'");

//para tomar el id del alumno
$sqlx = "SELECT id_docente,cedula,apellidos, nombres FROM docentes where cedula='$cedula_usuario'";
$rsx = mysql_query($sqlx, $cn) or die(mysql_error());
$rowx = mysql_fetch_assoc($rsx);
$id_docente= $rowx['id_docente'];
$cedula= $rowx['cedula'];
$apellidos= $rowx['apellidos'];
$nombres= $rowx['nombres'];




  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];

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

?>

<html>
<head>
<title>enaph_siace -- materias_a_dictar</title>
<meta 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;
  }
  .bd1 {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 14px;
  }
  .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>Unidades Curriculares</h2></tr></table>
<table  class="bd" width="100%"><tr><td>(Unidades curriculares que puede dictar en la ENAHP-IUT)</td></tr></table>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td class="bd1"><b>DOCENTE:</b>&nbsp;&nbsp;<?php echo $apellidos ?>,&nbsp;<?php echo $nombres ?></td></tr>
</table>
<?php
  $conn = connect();
  $showrecs = 100;
  $pagerange = 100;

  $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;

  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 $order;
  global $ordtype;


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

  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);
?>

<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr" align="center">Ver</td>
<td class="hr" align="center">Editar</td>
<td class="hr" align="center">Borrar</td>
<td class="hr" align="center"><a class="hr" href="materias_a_dictar.php?order=<?php echo "nombre_materia" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Nombre de la Unidad Curricular") ?></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 ?>" align="center"><a href="materias_a_dictar.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="materias_a_dictar.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="materias_a_dictar.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["nombre_materia"]) ?></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("nombre_materia")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["nombre_materia"]) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">

<tr>
<td class="hr"><?php echo htmlspecialchars("Nombre de la Unidad Curricular")."&nbsp;" ?></td>
<td class="dr"><input name="nombre_materia" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["nombre_materia"])) ?>" size="80" maxlength="80"></td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="materias_a_dictar.php?a=add">Agregar </a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="materias_a_dictar.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="materias_a_dictar.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="materias_a_dictar.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="materias_a_dictar.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="materias_a_dictar.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="materias_a_dictar.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Registro Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="materias_a_dictar.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="materias_a_dictar.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form enctype="multipart/form-data" action="materias_a_dictar.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "id_docente" => "",
  "nombre_materia" => "");
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="materias_a_dictar.php?a=add">Agregar </a></td>
<td><a href="materias_a_dictar.php?a=edit&recid=<?php echo $recid ?>">Editar </a></td>
<td><a href="materias_a_dictar.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="materias_a_dictar.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xid_docente" value="<?php echo $row["id_docente"] ?>">
<input type="hidden" name="xid" value="<?php echo $row["id"] ?>">
<?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="materias_a_dictar.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xid_docente" value="<?php echo $row["id_docente"] ?>">
<input type="hidden" name="xid" value="<?php echo $row["id"] ?>">
<?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", "siace_admin", "j7BnJdCGzyQBGcud");
  mysql_select_db("enaph_siace");
  mysql_query ("SET NAMES 'utf8'");
  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 $id_docente;
  
  $sql = "SELECT `id`,`id_docente`, `nombre_materia` FROM `materias_a_dictar` where id_docente='$id_docente'";
  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 $id_docente;
  
  $sql = "SELECT COUNT(*) FROM `materias_a_dictar` where id_docente='$id_docente'";
  $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;
  global $id_docente;
  global $cedula_usuario;

  $sql = "insert into `materias_a_dictar` (`id_docente`, `nombre_materia`) values ('$id_docente', " .sqlvalue(@$_POST["nombre_materia"], true).")";
  mysql_query($sql, $conn) or die(mysql_error());
  
  // Guarda el Registro de Auditoría
  $consulta=addslashes($sql);
  $fecha=date("Y-m-d G:i:A");
  $accion="Agregar Materia a Dictar";
  $script="materias_a_dictar.php";
  $ip=obtener_ip();
  $sql1 = "insert into `auditoria` (`username`, `ip`, `fecha`, `accion`, `script`, `query`) values ('$cedula_usuario','$ip','$fecha','$accion','$script','$consulta')";
  mysql_query($sql1, $conn) or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;
  global $cedula_usuario;
  
  $id=@$_POST["xid"];
  $sql = "update `materias_a_dictar` set `nombre_materia`=" .sqlvalue(@$_POST["nombre_materia"], true) ." where id='$id'";
  mysql_query($sql, $conn) or die(mysql_error());

  // Guarda el Registro de Auditoría
  $consulta=addslashes($sql);
  $fecha=date("Y-m-d G:i:A");
  $accion="Modificar Materia a Dictar";
  $script="materias_a_dictar.php";
  $ip=obtener_ip();
  $sql1 = "insert into `auditoria` (`username`, `ip`, `fecha`, `accion`, `script`, `query`) values ('$cedula_usuario','$ip','$fecha','$accion','$script','$consulta')";
  mysql_query($sql1, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;
  global $cedula_usuario;
   global $_POST;
   
   $id=@$_POST["xid"]; 
  $sql = "delete from `materias_a_dictar` where id='$id'";
  mysql_query($sql, $conn) or die(mysql_error());
  
  // Guarda el Registro de Auditoría
  $consulta=addslashes($sql);
  $fecha=date("Y-m-d G:i:A");
  $accion="Eliminar Materia a Dictar";
  $script="materias_a_dictar.php";
  $ip=obtener_ip();
  $sql1 = "insert into `auditoria` (`username`, `ip`, `fecha`, `accion`, `script`, `query`) values ('$cedula_usuario','$ip','$fecha','$accion','$script','$consulta')";
  mysql_query($sql1, $conn) or die(mysql_error()); 
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`id_docente`";
  if (@$_POST["xid_docente"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xid_docente"], false);
  };
  $pk .= ") and ";
  $pk .= "(`nombre_materia`";
  if (@$_POST["xnombre_materia"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xnombre_materia"], true);
  };
  $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;  
  }
 ?>
