avisos_insert.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /*
  3. * Inserta los datos de una nueva carrera
  4. * Recibe:
  5. * fecha_inicial
  6. * fecha_final
  7. * texto
  8. * tipo[]
  9. * usuario[]
  10. */
  11. require_once("../../include/constantes.php");
  12. require_once("../../include/bd_pdo.php");
  13. require_once("../../include/util.php");
  14. require_once("../../classes/ValidaSesion.php");
  15. require_once("../classes/LogActividad.php");//die on error
  16. $pag = "../avisos_crear.php";
  17. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  18. $objSesion = new ValidaSesion($pdo, 80, APSA);
  19. if(!$objSesion->tieneAcceso() || !$objSesion->puedeEditar()){
  20. header("Location: ".$pag."?error=2");
  21. exit();
  22. }
  23. unset($objValida);
  24. if(!isset($_POST["fecha_inicial"]) || !isset($_POST["fecha_final"]) || !isset($_POST["texto"]) || (!isset($_POST["tipo"]) && !isset($_POST["usuario"])) ){
  25. header("Location: ".$pag."?error=0");
  26. exit();
  27. }
  28. $fecha_inicial = trim(filter_input(INPUT_POST, "fecha_inicial", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));//limpia texto
  29. $fecha_inicial = fechaGuion($fecha_inicial);
  30. $fecha_final = trim(filter_input(INPUT_POST, "fecha_final", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW)));//limpia texto
  31. $fecha_final = fechaGuion($fecha_final);
  32. $texto = $_POST["texto"];
  33. $usuarioArr = array();
  34. if(isset($_POST["tipo"])){
  35. $tipoArr = $_POST["tipo"];
  36. foreach($tipoArr as $tipo){
  37. switch ($tipo){
  38. case 1://profesores
  39. $stmt = $pdo->prepare('Select * from fs_profesores()');
  40. if(!$stmt->execute()){
  41. $pdo->rollBack();
  42. //echo "fs_profesores ";
  43. //print_r($stmt->errorInfo());
  44. header("Location:".$pag."?error=1");
  45. exit();
  46. }
  47. $rs = $stmt->fetchAll();
  48. $stmt->closeCursor();
  49. break;
  50. case 2://jefes de carrera
  51. $stmt = $pdo->prepare('Select * from fs_jefescarrera()');
  52. if(!$stmt->execute()){
  53. $pdo->rollBack();
  54. //echo "fs_jefescarrera() ";
  55. //print_r($stmt->errorInfo());
  56. header("Location:".$pag."?error=1");
  57. exit();
  58. }
  59. $rs = $stmt->fetchAll();
  60. $stmt->closeCursor();
  61. break;
  62. }
  63. if(isset($rs) && count($rs) > 0){
  64. foreach($rs as $usr){
  65. $usuarioArr[] = $usr["Usuario_id"];
  66. }
  67. unset($rs);
  68. }
  69. }
  70. }
  71. if(isset($_POST["usuario"])){
  72. foreach($_POST["usuario"] as $usr){
  73. if(!in_array($usr, $usuarioArr)){
  74. $usuarioArr[] = $usr;
  75. }
  76. }
  77. }
  78. try {
  79. $pdo->beginTransaction();
  80. $stmt = $pdo->prepare('Select * from fi_aviso(:fini, :ffin, :texto, :usr)');
  81. $stmt->bindParam(":fini", $fecha_inicial);
  82. $stmt->bindParam(":ffin", $fecha_final);
  83. $stmt->bindParam(":texto", $texto);
  84. $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
  85. if(!$stmt->execute()){
  86. $pdo->rollBack();
  87. //echo "fi_materia ";
  88. //print_r($stmt->errorInfo());
  89. header("Location:".$pag."?error=1");
  90. exit();
  91. }
  92. $rs = $stmt->fetch();
  93. $aviso_id = $rs["fi_aviso"];
  94. $stmt->closeCursor();
  95. unset($rs);
  96. //Inserta usuarios
  97. if(isset($usuarioArr)){
  98. foreach($usuarioArr as $usuario){
  99. $stmt = $pdo->prepare('Select * from fi_avisousuario(:aviso, :usr)');
  100. $stmt->bindParam(":aviso", $aviso_id);
  101. $stmt->bindParam(":usr", $usuario);
  102. if(!$stmt->execute()){
  103. $pdo->rollBack();
  104. //echo "fi_avisousuario";
  105. //print_r($stmt->errorInfo());
  106. header("Location:".$pag."?error=4");
  107. exit();
  108. }
  109. }
  110. $stmt->closeCursor();
  111. }
  112. $stmt = null;
  113. $log = new LogActividad();
  114. $desc_log = "Inserta aviso nuevo ID[".$aviso_id."] Fecha inicial[".$fecha_inicial."] Fecha final[".$fecha_final."] Usuarios[".count($usuarioArr)."]";
  115. $log->appendLog($_SESSION["usuario_id"], $_SESSION["usuario_nombre"]." ".$_SESSION["usuario_apellidos"], $desc_log);
  116. $pdo->commit();
  117. header("Location: ../avisos.php?ok=0");
  118. }catch(PDOException $e) {
  119. $pdo->rollBack();
  120. header("Location:".$pag."?error=1");
  121. }
  122. ?>