x_horariogrupo_insert.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /* AJAX
  3. * Selecciona los datos de la carrera
  4. * Recibe:
  5. * id - ID de grupo,
  6. * json
  7. * Return:
  8. * resultado o cadena de error
  9. */
  10. require_once("../include/nocache.php");
  11. require_once("../include/bd_pdo.php");
  12. require_once("../include/util.php");
  13. require_once("../classes/ValidaSesion.php");
  14. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  15. $objSesion = new ValidaSesion($pdo, 51);
  16. if(!$objSesion->tieneAcceso()){
  17. $return["error"] = "Error! No tienes permisos para realizar esta acción.";
  18. }else if(!isset($_POST["id"])){
  19. $return["error"] = "Error! No se recibió la información del grupo.";
  20. }else{
  21. $grupo = filter_input(INPUT_POST, "id", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  22. $json = json_decode($_POST["json"], true);
  23. $return["error"] = "";
  24. try {
  25. $pdo->beginTransaction();
  26. $error = false;
  27. $arregloQuery = array();
  28. //---- Valida que no haya conflicto de salones ----------
  29. foreach($json as $clase){
  30. if($clase["salon"] == ""){
  31. $stmt = $pdo->prepare('Select * from fs_horariogrupovalida(:gpo, :dia, :hora, :mat, NULL, :fecha_ini, :fecha_fin, :duracion)');
  32. }else{
  33. $stmt = $pdo->prepare('Select * from fs_horariogrupovalida(:gpo, :dia, :hora, :mat, :salon, :fecha_ini, :fecha_fin, :duracion)');
  34. $stmt->bindParam(":salon", $clase["salon"]);
  35. }
  36. $stmt->bindParam(":gpo", $grupo);
  37. $stmt->bindParam(":dia", $clase["dia"]);
  38. $stmt->bindParam(":hora", $clase["hora"]);
  39. $stmt->bindParam(":mat", $clase["materia"]);
  40. $fecha_tmp = fechaGuion($clase["fecha_inicial"]);
  41. $stmt->bindParam(":fecha_ini", $fecha_tmp);
  42. $fecha_tmp = fechaGuion($clase["fecha_final"]);
  43. $stmt->bindParam(":fecha_fin", $fecha_tmp);
  44. $stmt->bindParam(":duracion", $clase["duracion"]);
  45. if(!$stmt->execute()){
  46. $t = $stmt->errorInfo();
  47. $return["error"] .= "No se puede grabar el horario. Ocurrió un error al validar la materia '".$clase["materia_nombre"]."'. ".$t[2];
  48. $error = true;
  49. $return["json"] = json_encode($return);
  50. echo json_encode($return);
  51. exit();
  52. }
  53. $invalido_rs = $stmt->fetchAll();
  54. $stmt->closeCursor();
  55. if(count($invalido_rs) > 0 && isset($invalido_rs[0]["Materia_desc"])){
  56. $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.";
  57. $error = true;
  58. $return["json"] = json_encode($return);
  59. echo json_encode($return);
  60. exit();
  61. }
  62. if(is_array($clase["profesores"]) ){
  63. foreach($clase["profesores"] as $profesor){
  64. if($profesor["salon"] == ""){
  65. $stmt = $pdo->prepare('Select * from fs_horariogrupovalida(:gpo, :dia, :hora, :mat, NULL, :fecha_ini, :fecha_fin, :duracion)');
  66. }else{
  67. $stmt = $pdo->prepare('Select * from fs_horariogrupovalida(:gpo, :dia, :hora, :mat, :salon, :fecha_ini, :fecha_fin, :duracion)');
  68. $stmt->bindParam(":salon", $profesor["salon"]);
  69. }
  70. $stmt->bindParam(":mat", $clase["materia"]);
  71. $stmt->bindParam(":gpo", $grupo);
  72. $stmt->bindParam(":dia", $clase["dia"]);
  73. $stmt->bindParam(":hora", $clase["hora"]);
  74. if($profesor["fecha_inicial"] != "")
  75. $fecha_tmp = fechaGuion($profesor["fecha_inicial"]);
  76. else
  77. $fecha_tmp = fechaGuion($clase["fecha_inicial"]);
  78. $stmt->bindParam(":fecha_ini", $fecha_tmp);
  79. if($profesor["fecha_final"] != "")
  80. $fecha_tmp = fechaGuion($profesor["fecha_final"]);
  81. else
  82. $fecha_tmp = fechaGuion($clase["fecha_final"]);
  83. $stmt->bindParam(":fecha_fin", $fecha_tmp);
  84. $stmt->bindParam(":duracion", $clase["duracion"]);
  85. if(!$stmt->execute()){
  86. $t = $stmt->errorInfo();
  87. $return["error"] .= "No se puede grabar el horario. Ocurrió un error al validar la materia '".$clase["materia_nombre"]."'. ".$t[2];
  88. $error = true;
  89. $return["json"] = json_encode($return);
  90. echo json_encode($return);
  91. exit();
  92. }
  93. $invalido_rs = $stmt->fetchAll();
  94. $stmt->closeCursor();
  95. if(count($invalido_rs) > 0 && isset($invalido_rs[0]["Materia_desc"])){
  96. $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.";
  97. $error = true;
  98. $return["json"] = json_encode($return);
  99. echo json_encode($return);
  100. exit();
  101. }
  102. }
  103. }
  104. }
  105. //---- Valida que no haya conflicto de profesores ----------
  106. foreach($json as $clase){
  107. if(is_array($clase["profesores"]) ){
  108. foreach($clase["profesores"] as $profesor){
  109. if($profesor["profesor"] == ""){
  110. $stmt = $pdo->prepare('Select * from fs_profesorhorariogrupovalida(:gpo, :dia, :hora, NULL, :fecha_ini, :fecha_fin, :duracion)');
  111. }else{
  112. $stmt = $pdo->prepare('Select * from fs_profesorhorariogrupovalida(:gpo, :dia, :hora, :prof, :fecha_ini, :fecha_fin, :duracion)');
  113. $stmt->bindParam(":prof", $profesor["profesor"]);
  114. }
  115. $stmt->bindParam(":gpo", $grupo);
  116. $stmt->bindParam(":dia", $clase["dia"]);
  117. $stmt->bindParam(":hora", $clase["hora"]);
  118. if($profesor["fecha_inicial"] != "")
  119. $fecha_tmp = fechaGuion($profesor["fecha_inicial"]);
  120. else
  121. $fecha_tmp = fechaGuion($clase["fecha_inicial"]);
  122. $stmt->bindParam(":fecha_ini", $fecha_tmp);
  123. if($profesor["fecha_final"] != "")
  124. $fecha_tmp = fechaGuion($profesor["fecha_final"]);
  125. else
  126. $fecha_tmp = fechaGuion($clase["fecha_final"]);
  127. $stmt->bindParam(":fecha_fin", $fecha_tmp);
  128. $stmt->bindParam(":duracion", $clase["duracion"]);
  129. if(!$stmt->execute()){
  130. $t = $stmt->errorInfo();
  131. $return["error"] .= "No se puede grabar el horario. Ocurrió un error al validar el profesor de la materia '".$clase["materia_nombre"]."'. ".$t[2];
  132. $error = true;
  133. $return["json"] = json_encode($return);
  134. echo json_encode($return);
  135. exit();
  136. }
  137. $invalido_rs = $stmt->fetchAll();
  138. $stmt->closeCursor();
  139. if(count($invalido_rs) > 0 && isset($invalido_rs[0]["Materia_desc"])){
  140. $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.";
  141. $error = true;
  142. $return["json"] = json_encode($return);
  143. echo json_encode($return);
  144. exit();
  145. }
  146. }
  147. }
  148. }
  149. //---- Insertar ----------
  150. /*
  151. $return["query"] = $arregloQuery;
  152. $return["error"] = "Modo Prueba.";
  153. $return["json"] = json_encode($return);
  154. echo json_encode($return);
  155. exit();*/
  156. //Quita todo y vuelve a insertar
  157. $stmt = $pdo->prepare('Select * from fd_horariogrupo(:gpo)');
  158. $stmt->bindParam(":gpo", $grupo);
  159. if(!$stmt->execute()){
  160. $t = $stmt->errorInfo();
  161. $return["error"] .= "Ocurrió un error al borrar los horarios del grupo ".$t[2];
  162. $error = true;
  163. }
  164. $stmt->closeCursor();
  165. if(!$error){
  166. foreach($json as $clase){
  167. //Inserta Horario grupo
  168. if($clase["salon"] == "")
  169. $stmt = $pdo->prepare('Select * from fi_horariogrupo(:gpo, :dia, :hora, :mat, NULL, :usr, :fecha_ini, :fecha_fin, :duracion)');
  170. else{
  171. $stmt = $pdo->prepare('Select * from fi_horariogrupo(:gpo, :dia, :hora, :mat, :salon, :usr, :fecha_ini, :fecha_fin, :duracion)');
  172. $stmt->bindParam(":salon", $clase["salon"]);
  173. }
  174. $stmt->bindParam(":gpo", $grupo);
  175. $stmt->bindParam(":dia", $clase["dia"]);
  176. $stmt->bindParam(":hora", $clase["hora"]);
  177. $stmt->bindParam(":mat", $clase["materia"]);
  178. $stmt->bindParam(":usr", $_SESSION["usuario_id"]);
  179. $fecha_tmpI = fechaGuion($clase["fecha_inicial"]);
  180. $stmt->bindParam(":fecha_ini", $fecha_tmpI);
  181. $fecha_tmpF = fechaGuion($clase["fecha_final"]);
  182. $stmt->bindParam(":fecha_fin", $fecha_tmpF);
  183. $stmt->bindParam(":duracion", $clase["duracion"]);
  184. if(!$stmt->execute()){
  185. $t = $stmt->errorInfo();
  186. $return["error"] .= "Ocurrió un error al insertar el horario de la materia '".$clase["materia_nombre"]."'. ".$t[2];
  187. $error = true;
  188. }
  189. $stmt->closeCursor();
  190. //Inserta Profesores/submaterias
  191. if(!$error && is_array($clase["profesores"]) ){
  192. foreach($clase["profesores"] as $profesor){
  193. if(!$error && ($profesor["profesor"] != "" || $profesor["submateria"] != "") && ($profesor["profesor"] > 0 || $profesor["submateria"] > 0)){
  194. //si los 2 son vacios no guarda
  195. $query = ":gpo, :dia, :hora, :mat, ";
  196. if($profesor["profesor"] != "" && intval($profesor["profesor"]) != 0){
  197. $query .=":prof, ";
  198. }else{
  199. $query .="NULL, ";
  200. }
  201. if($profesor["submateria"] != "" && intval($profesor["submateria"]) != 0){
  202. $query .=":sub, ";
  203. }else{
  204. $query .="NULL, ";
  205. }
  206. if($profesor["fecha_inicial"] != "" && $profesor["fecha_final"] != ""){
  207. $query .=":fecha_ini, :fecha_fin, ";
  208. }else{
  209. $query .="NULL, NULL, ";
  210. }
  211. if($profesor["salon"] != ""){
  212. $query .=":salon";
  213. }else{
  214. $query .="NULL";
  215. }
  216. $stmt = $pdo->prepare('Select * from fi_profesorhorariogrupo('.$query.')');
  217. $stmt->bindParam(":gpo", $grupo);
  218. $stmt->bindParam(":dia", $clase["dia"]);
  219. $stmt->bindParam(":hora", $clase["hora"]);
  220. $stmt->bindParam(":mat", $clase["materia"]);
  221. if($profesor["profesor"] != "" && intval($profesor["profesor"]) != 0) $stmt->bindParam(":prof", $profesor["profesor"]);
  222. if($profesor["submateria"] != "" && intval($profesor["submateria"]) != 0) $stmt->bindParam(":sub", $profesor["submateria"]);
  223. if($profesor["fecha_inicial"] != "" && $profesor["fecha_final"] != ""){ $stmt->bindParam(":fecha_ini", $profesor["fecha_inicial"]); $stmt->bindParam(":fecha_fin", $profesor["fecha_final"]);}
  224. if($profesor["salon"] != "") $stmt->bindParam(":salon", $profesor["salon"]);
  225. if(!$stmt->execute()){
  226. $t = $stmt->errorInfo();
  227. $return["error"] .= "Ocurrió un error al insertar los profesores/submaterias ".$t[2];
  228. $error = true;
  229. }
  230. $stmt->closeCursor();
  231. }
  232. }//fin foreach
  233. }//fin error
  234. }//foreach json
  235. }//no error
  236. if(!$error){
  237. $pdo->commit();
  238. unset($return["error"]);
  239. $return["ok"] = "El horario se guardó correctamente";
  240. }else
  241. $pdo->rollBack();
  242. $stmt = null; // cierra conexion
  243. } catch(PDOException $e) {
  244. $pdo->rollBack();
  245. $return["error"] = "Ocurrió un error al insertar los datos de los horarios.";
  246. }
  247. }
  248. $return["json"] = json_encode($return);
  249. echo json_encode($return);
  250. ?>