tieneAcceso() || !$objSesion->puedeEditar()){ header("Location: ../materias.php?error=3"); exit(); } unset($objValida); if(!isset($_POST["id"]) || !isset($_POST["desc"]) || !isset($_POST["clave"]) || !isset($_POST["area"]) || !isset($_POST["plan"]) || !isset($_POST["sem"]) ){ header("Location: ../materias.php?error=0"); exit(); } $id = filter_input(INPUT_POST, "id", FILTER_SANITIZE_NUMBER_INT);//limpia texto $desc = mb_strtoupper( trim(filter_input(INPUT_POST, "desc", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))) );//limpia texto $clave = trim(filter_input(INPUT_POST, "clave", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));//limpia texto $gpo = trim(filter_input(INPUT_POST, "gpo", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));//limpia texto $area = filter_input(INPUT_POST, "area", FILTER_SANITIZE_NUMBER_INT);//limpia texto $plan = filter_input(INPUT_POST, "plan", FILTER_SANITIZE_NUMBER_INT);//limpia texto $sem = filter_input(INPUT_POST, "sem", FILTER_SANITIZE_NUMBER_INT);//limpia texto $horas_clase = filter_input(INPUT_POST, "horas_clase", FILTER_SANITIZE_NUMBER_FLOAT, array('flags' => FILTER_FLAG_ALLOW_FRACTION));//limpia texto //$horas_lab = filter_input(INPUT_POST, "horas_lab", FILTER_SANITIZE_NUMBER_FLOAT, array('flags' => FILTER_FLAG_ALLOW_FRACTION));//limpia texto $creditos = filter_input(INPUT_POST, "creditos", FILTER_SANITIZE_NUMBER_FLOAT, array('flags' => FILTER_FLAG_ALLOW_FRACTION));//limpia texto $ods = filter_input(INPUT_POST, "ods", FILTER_SANITIZE_NUMBER_INT);//limpia texto $horas_lab = 0; $tiposub = 0; if(isset($_POST["tiposub"])) $tiposub = intval(filter_input(INPUT_POST, "tiposub", FILTER_SANITIZE_NUMBER_INT));//limpia texto if(isset($_POST["electiva"])){ $electiva_arr = $_POST["electiva"];//arreglo de IDs enteros } if(isset($_POST["relacionada"])) $relacionada_arr = $_POST["relacionada"];//arreglo de IDs enteros if($tiposub != 0){ if(count($electiva_arr) == 0){ header("Location: ".$pag."?id=".$id."&error=4");//debe elegir submaterias exit(); } } function in_arrayAsociativo($busca, $arreglo, $key ){ for($i = 0; $i < count($arreglo); $i++){ if($arreglo[$i][$key] == $busca){ return true; } } return false; } function ordenaAtributos($a, $b)//a return<0 { $attrA = explode("|", $a); $attrB = explode("|", $b); if($attrA[1] > $attrB[1] || ($attrA[1] == $attrB[1] && $attrA[0] < $attrB[0])){ return -1; } return 1; } if(isset($_POST["atributos"])){ $atributos_arr = $_POST["atributos"]; $i=0; usort($atributos_arr, "ordenaAtributos"); } try { $pdo->beginTransaction(); if($tiposub != 0){ $stmt = $pdo->prepare('Select * from fu_materia(:id, :desc, :area, :plan, :clave, :hclase, :hlab, :sem, :tipo, :cred, :gpo)'); $stmt->bindParam(":tipo", $tiposub); }else{ $stmt = $pdo->prepare('Select * from fu_materia(:id, :desc, :area, :plan, :clave, :hclase, :hlab, :sem, NULL, :cred, :gpo)'); } $stmt->bindParam(":id", $id); $stmt->bindParam(":desc", $desc); $stmt->bindParam(":gpo", $gpo); $stmt->bindParam(":area", $area); $stmt->bindParam(":plan", $plan); $stmt->bindParam(":clave", $clave); $stmt->bindParam(":hclase", $horas_clase); $stmt->bindParam(":hlab", $horas_lab); $stmt->bindParam(":sem", $sem); $stmt->bindParam(":cred", $creditos); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fu_materia "; //print_r($stmt->errorInfo()); header("Location:".$pag."?id=".$id."&error=1"); exit(); } $rs = $stmt->fetch(); $stmt->closeCursor(); unset($rs); //Actualiza submaterias if($tiposub != 0 && isset($electiva_arr)){ //Obtiene arreglo de submaterias anteriores $stmt = $pdo->prepare('Select * from fs_submateria(:id, NULL)'); $stmt->bindParam(":id", $id); $stmt->execute(); $submateria_rs = $stmt->fetchAll();//materias que están actualmente $stmt->closeCursor(); $i = 0; foreach($submateria_rs as $submateria){//recorre actuales if(!in_array($submateria["Submateria_id"], $electiva_arr)){ //no esta en la nueva, borra $stmt = $pdo->prepare('Select * from fd_submateria(:mat, :sub)'); $stmt->bindParam(":mat", $id); $stmt->bindParam(":sub", $submateria["Submateria_id"]); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fd submateria"; //print_r($stmt->errorInfo()); header("Location:".$pag."?id=".$id."&error=5"); exit(); } $stmt->closeCursor(); } } foreach($electiva_arr as $electiva){//recorre nuevos if(!in_arrayAsociativo($electiva, $submateria_rs, "Submateria_id")){ //no esta en la anterior, inserta $stmt = $pdo->prepare('Select * from fi_submateria(:mat, :sub)'); $stmt->bindParam(":mat", $id); $stmt->bindParam(":sub", $electiva); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fi electiva"; //print_r($stmt->errorInfo()); header("Location:".$pag."?id=".$id."&error=5"); exit(); } $stmt->closeCursor(); } } }//fin submaterias if($tiposub == 0){ $stmt = $pdo->prepare('Select * from fd_submateria(:mat, NULL)'); $stmt->bindParam(":mat", $id); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fd_relacionada"; //print_r($stmt->errorInfo()); header("Location:".$pag."?id=".$id."&error=5"); exit(); } } //Borra materias relacionadas y reinserta $stmt = $pdo->prepare('Select * from fd_materiarelacionada(:mat, NULL)'); $stmt->bindParam(":mat", $id); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fd_mat_relacionada"; //print_r($stmt->errorInfo()); header("Location:".$pag."?id=".$id."&error=6"); exit(); } //Inserta materias relacionadas if(isset($relacionada_arr)){ foreach($relacionada_arr as $relacionada){ $stmt = $pdo->prepare('Select * from fi_materiarelacionada(:mat, :mat_rel)'); $stmt->bindParam(":mat", $id); if(trim($relacionada) != "" && $id != $relacionada){ $stmt->bindParam(":mat_rel", $relacionada); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fi_relacionada"; //print_r($stmt->errorInfo()); header("Location:".$pag."?id=".$id."&error=6"); exit(); } } } $stmt->closeCursor(); } //Borra atributos $stmt = $pdo->prepare('Select * from fd_materia_atributoegreso(NULL, :mat)'); $stmt->bindParam(":mat", $id); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fi_relacionada"; //print_r($stmt->errorInfo()); header("Location:".$pag."?error=8"); exit(); } //Inserta atributos if(isset($_POST["atributos"])){ $atributos_arr = $_POST["atributos"]; $i=0; usort($atributos_arr, "ordenaAtributos"); foreach($atributos_arr as $atributo){ $atributoMat = explode("|", $atributo); $stmt = $pdo->prepare('Select * from fi_materia_atributoegreso(:plan, :id, :mat, :contribuye ,:orden)'); $stmt->bindParam(":plan", $plan); $stmt->bindParam(":id", $atributoMat[0]); $stmt->bindParam(":mat", $id); $stmt->bindParam(":contribuye", $atributoMat[1]); $stmt->bindParam(":orden", $i); $i++; if(!$stmt->execute()){ $pdo->rollBack(); //echo "fi_relacionada"; //print_r($stmt->errorInfo()); header("Location:".$pag."?error=7"); exit(); } } $stmt->closeCursor(); } //Borra ODs $stmt = $pdo->prepare('Select * from fd_materia_ods(NULL, :mat)'); $stmt->bindParam(":mat", $id); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fi_relacionada"; //print_r($stmt->errorInfo()); header("Location:".$pag."?error=9"); exit(); } //Inserta ODs if(!empty($ods) && $ods>=1){ $stmt = $pdo->prepare('Select * from fi_materia_ods(:id, :mat)'); $stmt->bindParam(":id", $ods); $stmt->bindParam(":mat", $id); if(!$stmt->execute()){ $pdo->rollBack(); //echo "fi_relacionada"; //print_r($stmt->errorInfo()); header("Location:".$pag."?error=9"); exit(); } } $stmt = null; $log = new LogActividad(); $desc_log = "Actualiza materia ID[".$id."] Nombre[".$desc."] Clave[".$clave."] Area[".$area."] Plan[".$plan."] Semestre[".$sem."] Horas de clase[".$horas_clase."] Créditos[".$creditos."]"; $log->appendLog($_SESSION["usuario_id"], $_SESSION["usuario_nombre"]." ".$_SESSION["usuario_apellidos"], $desc_log); $pdo->commit(); header("Location: ../materias.php?ok=1"); }catch(PDOException $e) { $pdo->rollBack(); //print_r($e);exit(); header("Location:".$pag."?id=".$id."&error=1"); } ?>