123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- /* AJAX
- * Selecciona los datos de la carrera
- * Recibe:
- * id - ID de grupo,
- * json
- * Return:
- * resultado o cadena de error
- */
- require_once("../include/nocache.php");
- require_once("../include/bd_pdo.php");
- require_once("../include/util.php");
- require_once("../classes/ValidaSesion.php");
- //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
- $objSesion = new ValidaSesion($pdo, 51);
- if(!$objSesion->tieneAcceso()){
- $return["error"] = "Error! No tienes permisos para realizar esta acción.";
- }else if(!isset($_POST["id"])){
- $return["error"] = "Error! No se recibió la información del grupo.";
- }else{
- $grupo = filter_input(INPUT_POST, "id", FILTER_SANITIZE_NUMBER_INT);//limpia texto
- $json = json_decode($_POST["json"], true);
-
- $return["error"] = "";
-
- try {
- $pdo->beginTransaction();
- $error = false;
- $arregloQuery = array();
-
- //---- Valida que no haya conflicto de salones ----------
- foreach($json as $clase){
- if($clase["salon"] == ""){
- $stmt = $pdo->prepare('Select * from fs_horariogrupovalida(:gpo, :dia, :hora, :mat, NULL, :fecha_ini, :fecha_fin, :duracion)');
- }else{
- $stmt = $pdo->prepare('Select * from fs_horariogrupovalida(:gpo, :dia, :hora, :mat, :salon, :fecha_ini, :fecha_fin, :duracion)');
- $stmt->bindParam(":salon", $clase["salon"]);
- }
- $stmt->bindParam(":gpo", $grupo);
- $stmt->bindParam(":dia", $clase["dia"]);
- $stmt->bindParam(":hora", $clase["hora"]);
- $stmt->bindParam(":mat", $clase["materia"]);
- $fecha_tmp = fechaGuion($clase["fecha_inicial"]);
- $stmt->bindParam(":fecha_ini", $fecha_tmp);
- $fecha_tmp = fechaGuion($clase["fecha_final"]);
- $stmt->bindParam(":fecha_fin", $fecha_tmp);
- $stmt->bindParam(":duracion", $clase["duracion"]);
- if(!$stmt->execute()){
- $t = $stmt->errorInfo();
- $return["error"] .= "No se puede grabar el horario. Ocurrió un error al validar la materia '".$clase["materia_nombre"]."'. ".$t[2];
- $error = true;
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }
-
- $invalido_rs = $stmt->fetchAll();
- $stmt->closeCursor();
- if(count($invalido_rs) > 0 && isset($invalido_rs[0]["Materia_desc"])){
- $return["error"] .= "No se puede grabar el horario. La materia <strong>".$clase["materia_nombre"]."</strong> utiliza el mismo salón (<strong>".$invalido_rs[0]["Salon_desc"]."</strong>) que <strong>".$invalido_rs[0]["Materia_desc"]."</strong> del grupo <strong>".$invalido_rs[0]["Grupo_desc"]."</strong> el mismo día a la misma hora.";
- $error = true;
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }
-
- if(is_array($clase["profesores"]) ){
- foreach($clase["profesores"] as $profesor){
- if($profesor["salon"] == ""){
- $stmt = $pdo->prepare('Select * from fs_horariogrupovalida(:gpo, :dia, :hora, :mat, NULL, :fecha_ini, :fecha_fin, :duracion)');
- }else{
- $stmt = $pdo->prepare('Select * from fs_horariogrupovalida(:gpo, :dia, :hora, :mat, :salon, :fecha_ini, :fecha_fin, :duracion)');
- $stmt->bindParam(":salon", $profesor["salon"]);
- }
- $stmt->bindParam(":mat", $clase["materia"]);
- $stmt->bindParam(":gpo", $grupo);
- $stmt->bindParam(":dia", $clase["dia"]);
- $stmt->bindParam(":hora", $clase["hora"]);
- if($profesor["fecha_inicial"] != "")
- $fecha_tmp = fechaGuion($profesor["fecha_inicial"]);
- else
- $fecha_tmp = fechaGuion($clase["fecha_inicial"]);
- $stmt->bindParam(":fecha_ini", $fecha_tmp);
- if($profesor["fecha_final"] != "")
- $fecha_tmp = fechaGuion($profesor["fecha_final"]);
- else
- $fecha_tmp = fechaGuion($clase["fecha_final"]);
- $stmt->bindParam(":fecha_fin", $fecha_tmp);
- $stmt->bindParam(":duracion", $clase["duracion"]);
- if(!$stmt->execute()){
- $t = $stmt->errorInfo();
- $return["error"] .= "No se puede grabar el horario. Ocurrió un error al validar la materia '".$clase["materia_nombre"]."'. ".$t[2];
- $error = true;
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }
- $invalido_rs = $stmt->fetchAll();
- $stmt->closeCursor();
- if(count($invalido_rs) > 0 && isset($invalido_rs[0]["Materia_desc"])){
- $return["error"] .= "No se puede grabar el horario. La materia <strong>".$clase["materia_nombre"]."</strong> utiliza el mismo salón (<strong>".$invalido_rs[0]["Salon_desc"]."</strong>) que <strong>".$invalido_rs[0]["Materia_desc"]."</strong> del grupo <strong>".$invalido_rs[0]["Grupo_desc"]."</strong> el mismo día a la misma hora.";
- $error = true;
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }
- }
- }
- }
-
- //---- Valida que no haya conflicto de profesores ----------
- foreach($json as $clase){
- if(is_array($clase["profesores"]) ){
- foreach($clase["profesores"] as $profesor){
- if($profesor["profesor"] == ""){
- $stmt = $pdo->prepare('Select * from fs_profesorhorariogrupovalida(:gpo, :dia, :hora, NULL, :fecha_ini, :fecha_fin, :duracion)');
- }else{
- $stmt = $pdo->prepare('Select * from fs_profesorhorariogrupovalida(:gpo, :dia, :hora, :prof, :fecha_ini, :fecha_fin, :duracion)');
- $stmt->bindParam(":prof", $profesor["profesor"]);
- }
- $stmt->bindParam(":gpo", $grupo);
- $stmt->bindParam(":dia", $clase["dia"]);
- $stmt->bindParam(":hora", $clase["hora"]);
- if($profesor["fecha_inicial"] != "")
- $fecha_tmp = fechaGuion($profesor["fecha_inicial"]);
- else
- $fecha_tmp = fechaGuion($clase["fecha_inicial"]);
- $stmt->bindParam(":fecha_ini", $fecha_tmp);
- if($profesor["fecha_final"] != "")
- $fecha_tmp = fechaGuion($profesor["fecha_final"]);
- else
- $fecha_tmp = fechaGuion($clase["fecha_final"]);
- $stmt->bindParam(":fecha_fin", $fecha_tmp);
- $stmt->bindParam(":duracion", $clase["duracion"]);
- if(!$stmt->execute()){
- $t = $stmt->errorInfo();
- $return["error"] .= "No se puede grabar el horario. Ocurrió un error al validar el profesor de la materia '".$clase["materia_nombre"]."'. ".$t[2];
- $error = true;
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }
- $invalido_rs = $stmt->fetchAll();
- $stmt->closeCursor();
- if(count($invalido_rs) > 0 && isset($invalido_rs[0]["Materia_desc"])){
- $return["error"] .= "No se puede grabar el horario. La materia <strong>".$clase["materia_nombre"]."</strong> utiliza el mismo salón (<strong>".$invalido_rs[0]["Salon_desc"]."</strong>) que <strong>".$invalido_rs[0]["Materia_desc"]."</strong> del grupo <strong>".$invalido_rs[0]["Grupo_desc"]."</strong> el mismo día a la misma hora.";
- $error = true;
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();
- }
- }
- }
- }
-
-
- //---- Insertar ----------
- /*
- $return["query"] = $arregloQuery;
- $return["error"] = "Modo Prueba.";
- $return["json"] = json_encode($return);
- echo json_encode($return);
- exit();*/
-
-
- //Quita todo y vuelve a insertar
- $stmt = $pdo->prepare('Select * from fd_horariogrupo(:gpo)');
- $stmt->bindParam(":gpo", $grupo);
- if(!$stmt->execute()){
- $t = $stmt->errorInfo();
- $return["error"] .= "Ocurrió un error al borrar los horarios del grupo ".$t[2];
- $error = true;
- }
- $stmt->closeCursor();
-
- if(!$error){
- foreach($json as $clase){
- //Inserta Horario grupo
- if($clase["salon"] == "")
- $stmt = $pdo->prepare('Select * from fi_horariogrupo(:gpo, :dia, :hora, :mat, NULL, :usr, :fecha_ini, :fecha_fin, :duracion)');
- else{
- $stmt = $pdo->prepare('Select * from fi_horariogrupo(:gpo, :dia, :hora, :mat, :salon, :usr, :fecha_ini, :fecha_fin, :duracion)');
- $stmt->bindParam(":salon", $clase["salon"]);
- }
- $stmt->bindParam(":gpo", $grupo);
- $stmt->bindParam(":dia", $clase["dia"]);
- $stmt->bindParam(":hora", $clase["hora"]);
- $stmt->bindParam(":mat", $clase["materia"]);
- $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
- $fecha_tmpI = fechaGuion($clase["fecha_inicial"]);
- $stmt->bindParam(":fecha_ini", $fecha_tmpI);
- $fecha_tmpF = fechaGuion($clase["fecha_final"]);
- $stmt->bindParam(":fecha_fin", $fecha_tmpF);
- $stmt->bindParam(":duracion", $clase["duracion"]);
- if(!$stmt->execute()){
- $t = $stmt->errorInfo();
- $return["error"] .= "Ocurrió un error al insertar el horario de la materia '".$clase["materia_nombre"]."'. ".$t[2];
- $error = true;
- }
- $stmt->closeCursor();
- //Inserta Profesores/submaterias
- if(!$error && is_array($clase["profesores"]) ){
- foreach($clase["profesores"] as $profesor){
- if(!$error && ($profesor["profesor"] != "" || $profesor["submateria"] != "") && ($profesor["profesor"] > 0 || $profesor["submateria"] > 0)){
- //si los 2 son vacios no guarda
- $query = ":gpo, :dia, :hora, :mat, ";
- if($profesor["profesor"] != "" && intval($profesor["profesor"]) != 0){
- $query .=":prof, ";
- }else{
- $query .="NULL, ";
- }
- if($profesor["submateria"] != "" && intval($profesor["submateria"]) != 0){
- $query .=":sub, ";
- }else{
- $query .="NULL, ";
- }
- if($profesor["fecha_inicial"] != "" && $profesor["fecha_final"] != ""){
- $query .=":fecha_ini, :fecha_fin, ";
- }else{
- $query .="NULL, NULL, ";
- }
- if($profesor["salon"] != ""){
- $query .=":salon";
- }else{
- $query .="NULL";
- }
- $stmt = $pdo->prepare('Select * from fi_profesorhorariogrupo('.$query.')');
- $stmt->bindParam(":gpo", $grupo);
- $stmt->bindParam(":dia", $clase["dia"]);
- $stmt->bindParam(":hora", $clase["hora"]);
- $stmt->bindParam(":mat", $clase["materia"]);
- if($profesor["profesor"] != "" && intval($profesor["profesor"]) != 0) $stmt->bindParam(":prof", $profesor["profesor"]);
- if($profesor["submateria"] != "" && intval($profesor["submateria"]) != 0) $stmt->bindParam(":sub", $profesor["submateria"]);
- if($profesor["fecha_inicial"] != "" && $profesor["fecha_final"] != ""){ $stmt->bindParam(":fecha_ini", $profesor["fecha_inicial"]); $stmt->bindParam(":fecha_fin", $profesor["fecha_final"]);}
- if($profesor["salon"] != "") $stmt->bindParam(":salon", $profesor["salon"]);
- if(!$stmt->execute()){
- $t = $stmt->errorInfo();
- $return["error"] .= "Ocurrió un error al insertar los profesores/submaterias ".$t[2];
- $error = true;
- }
- $stmt->closeCursor();
- }
- }//fin foreach
- }//fin error
- }//foreach json
- }//no error
- if(!$error){
- $pdo->commit();
- unset($return["error"]);
- $return["ok"] = "El horario se guardó correctamente";
- }else
- $pdo->rollBack();
- $stmt = null; // cierra conexion
- } catch(PDOException $e) {
- $pdo->rollBack();
- $return["error"] = "Ocurrió un error al insertar los datos de los horarios.";
- }
- }
- $return["json"] = json_encode($return);
- echo json_encode($return);
- ?>
|