calendario_select.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /* AJAX
  3. * Inserta datos del objeto para el calendario
  4. * Recibe:
  5. * json
  6. * Return:
  7. * id insertado o cadena de error
  8. */
  9. require_once("../../include/constantes.php");
  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. require_once("../classes/LogActividad.php");//die on error
  15. require_once("../classes/Evento.php");
  16. //--- Objeto para validar usuario. El id de usuario lo lee desde sesión
  17. $objSesion = new ValidaSesion($pdo, 112, GEMA);
  18. if(!$objSesion->tieneAcceso()){
  19. $return["error"] = "Error! No tienes permisos para realizar esta acción.";
  20. }else if(!isset($_POST["mes"], $_POST["anho"], $_POST["perfiles"])){
  21. $return["error"] = "Error! No se recibió la información para generar el calendario.";
  22. }else{
  23. $mes = filter_input(INPUT_POST, "mes", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  24. $anho = filter_input(INPUT_POST, "anho", FILTER_SANITIZE_NUMBER_INT);//limpia texto
  25. //$fecha_min = fechaGuion(trim(filter_input(INPUT_POST, "fecha_min", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  26. //$fecha_max = fechaGuion(trim(filter_input(INPUT_POST, "fecha_max", FILTER_SANITIZE_STRING,array('flags' => FILTER_FLAG_STRIP_LOW))));//limpia texto
  27. $perfiles = implode(',', $_POST["perfiles"]);
  28. $return["error"] = "";
  29. $eventos_display = array();//eventos con todas las fechas para pintarse. Arreglo de Objeto evento
  30. $error = false;
  31. //-------------------------
  32. //Obtiene fechas de periodo
  33. /*$stmt = $pdo->prepare('Select * from fs_periodo(:periodo, NULL, NULL, true)');
  34. $stmt->bindParam(":periodo", $_SESSION["periodo_id"]);
  35. if(!$stmt->execute()){
  36. //print_r($stmt->errorInfo());
  37. $errorDesc = "Ocurrió un error al cargar los datos del periodo";
  38. }else{
  39. $periodo_rs = $stmt->fetch();
  40. }
  41. $stmt->closeCursor();
  42. $eventoObj = new Evento(0, "Inicio de cursos","",0, "Fechas importantes", PERIODO_COLOR, $_SESSION["periodo_id"], $periodo_rs["Periodo_fecha_inicial"], true,"","", false);
  43. $eventoObj->addFecha($periodo_rs["Periodo_fecha_inicial"]);
  44. $eventos_display = array_merge($eventos_display, $eventoObj->getEventList($mes, $anho));
  45. $eventoObj = new Evento(0, "Fin de cursos","",0, "Fechas importantes", PERIODO_COLOR, $_SESSION["periodo_id"], $periodo_rs["Periodo_fecha_final"], true,"","", false);
  46. $eventoObj->addFecha($periodo_rs["Periodo_fecha_final"]);
  47. $eventos_display = array_merge($eventos_display, $eventoObj->getEventList($mes, $anho));
  48. $stmt = null;*/
  49. //-------------------------
  50. // Obtiene fecha máxima y min
  51. $stmt = $pdo->prepare('Select * from fs_fechaimportante(:periodo, 3)');//periodo
  52. $stmt->bindParam(":periodo", $_SESSION["periodo_id"]);
  53. if(!$stmt->execute()){
  54. $errorDesc = "Ocurrió un error al cargar las fechas del periodo";
  55. }else{
  56. $fecha_rs = $stmt->fetch();
  57. $fecha_min = date('d/m/Y', strtotime($fecha_rs["FechaImportante_inicial"].' - 1 month '));
  58. $fecha_max = date('d/m/Y', strtotime($fecha_rs["FechaImportante_final"].' + 1 month '));
  59. }
  60. $stmt->closeCursor();
  61. $stmt = null;
  62. //--Obtiene todos los eventos dentro del rango del periodo para los perfiles indicados
  63. $stmt = $pdo->prepare('Select * from fs_calendarioevento(:per, :fecha_i, :fecha_f, :perf, true)');
  64. $stmt->bindParam(":per", $_SESSION["periodo_id"]);
  65. $stmt->bindParam(":fecha_i", $fecha_min);
  66. $stmt->bindParam(":fecha_f", $fecha_max);
  67. $stmt->bindParam(":perf", $perfiles);
  68. unset($perfiles);
  69. if(!$stmt->execute()){
  70. $t = $stmt->errorInfo();
  71. $return["error"] .= "Ocurrió un error al obtener los eventos.".$t[2];
  72. $error = true;
  73. }
  74. if(!$error){
  75. $eventos_rs = $stmt->fetchAll();
  76. $stmt->closeCursor();
  77. //Recorre eventos
  78. foreach($eventos_rs as $evento){
  79. $eventoObj = new Evento($evento["CalendarioEvento_id"], $evento["CalendarioEvento_titulo"],$evento["CalendarioEvento_desc"], $evento["Periodo_id"] ,$evento["CalendarioEvento_fecha"], $evento["CalendarioEvento_todo_dia"], $evento["CalendarioEvento_hora_inicial"],$evento["CalendarioEvento_hora_final"]);
  80. $stmt = $pdo->prepare('Select * from fs_calendarioevento_perfil(:id)');
  81. $stmt->bindParam(":id", $evento["CalendarioEvento_id"]);
  82. if($stmt->execute()){
  83. $perf_rs = $stmt->fetchAll();
  84. $stmt->closeCursor();
  85. foreach($perf_rs as $perf){
  86. $eventoObj->addPerfil($perf);
  87. }
  88. }
  89. $stmt = $pdo->prepare('Select * from fs_insigniageneral(:id)');
  90. $stmt->bindParam(":id", $evento["CalendarioEvento_id"]);
  91. if($stmt->execute()){
  92. $insignia_rs = $stmt->fetch();
  93. $stmt->closeCursor();
  94. }
  95. $eventoObj->addInsignia($insignia_rs["Insignia_id"], $insignia_rs["InsigniaTipo_id"], $insignia_rs["InsigniaTipo_desc"],$insignia_rs["InsigniaTipo_color"], $insignia_rs["Puesto_id"], $insignia_rs["Puesto_desc"],
  96. $insignia_rs["Insignia_hasEvidencia"], $insignia_rs["Insignia_evidencia_desc"], $insignia_rs["InsigniaGeneral_inscripciones_inicial"], $insignia_rs["InsigniaGeneral_inscripciones_final"]);
  97. //Agrega atributos
  98. $stmt = $pdo->prepare('Select * from fs_insignia_atributoegreso(:id, NULL)');
  99. $stmt->bindParam(":id", $insignia_rs["Insignia_id"]);
  100. if($stmt->execute()){
  101. $atr_rs = $stmt->fetchAll();
  102. $stmt->closeCursor();
  103. foreach($atr_rs as $atr){
  104. $eventoObj->addAtributo($atr);
  105. }
  106. }
  107. //Total de insignias asignadas
  108. $stmt = $pdo->prepare('Select COUNT("Usuario_id") AS total from fs_insigniaalumno(:id)');
  109. $stmt->bindParam(":id", $insignia_rs["Insignia_id"]);
  110. if($stmt->execute()){
  111. $total_rs = $stmt->fetch();
  112. $stmt->closeCursor();
  113. $eventoObj->addTotalInsignias($total_rs["total"]);
  114. }
  115. //-- Calcular posibles fechas e insertar fechas en objeto --
  116. $fecha = fechaGuion($evento["CalendarioEvento_fecha"]);
  117. switch($evento["CalendarioRepeticion_id"]){
  118. case 1: //diario
  119. while (strtotime($fecha) <= strtotime($evento["CalendarioReglas_fecha_final"])) {
  120. $eventoObj->addFecha($fecha);
  121. $fecha = date ("Y-m-d", strtotime("+1 day", strtotime($fecha)));
  122. }
  123. break;
  124. case 2: //semanal
  125. $diasArr = explode(",", $evento["CalendarioReglas_dias_str"]);
  126. while (strtotime($fecha) <= strtotime($evento["CalendarioReglas_fecha_final"])) {
  127. if(in_array(date("w", strtotime($fecha)), $diasArr) ){//si es el día que quiero
  128. $eventoObj->addFecha($fecha);
  129. }
  130. $fecha = date ("Y-m-d", strtotime("+1 day", strtotime($fecha)));
  131. }
  132. break;
  133. case 3://mensual
  134. $diasArr = explode(",", $evento["CalendarioReglas_dias_str"]);
  135. //reglas
  136. $weekTxt = array(1=>"first", 2=>"second", 3=>"third", 4=>"fourth", -1=>"last");
  137. $dayname = array("sun", "mon", "tue", "wed", "thu", "fri", "sat");
  138. $fecha = date ("Y-m-01", strtotime($fecha));//empieza a revisar en el primer día del mes
  139. $semana = $evento["CalendarioReglas_semana"];
  140. while (strtotime($fecha) <= strtotime($evento["CalendarioReglas_fecha_final"])) {
  141. foreach($diasArr as $d){
  142. //echo "-->". intval(date("w", strtotime($fecha)))." == ". intval($d)."[".$fecha."]";
  143. if($semana == 1 && intval(date("w", strtotime($fecha))) == intval($d) ){//si el día actual es el que quiero lo guarda
  144. $eventoObj->addFecha($fecha);
  145. }else{//si no calcula siguiente día
  146. if(intval(date("w", strtotime($fecha))) == intval($d)){
  147. $fechaTmp = date ("Y-m-d", strtotime($weekTxt[$semana-1]." ".$dayname[$d], strtotime($fecha)));
  148. }else{
  149. $fechaTmp = date ("Y-m-d", strtotime($weekTxt[$semana]." ".$dayname[$d], strtotime($fecha)));
  150. }
  151. $eventoObj->addFecha($fechaTmp);
  152. }
  153. }
  154. $fecha = date ("Y-m-01", strtotime("+1 month", strtotime($fecha)));//siguiente mes primer día
  155. }
  156. break;
  157. default: //no se repite
  158. $eventoObj->addFecha($fecha);
  159. }
  160. //Obtiene eventos modificados (fechas que cambiaron)
  161. $stmt = $pdo->prepare('Select * from fs_calendarioeventoeditado(:id)');
  162. $stmt->bindParam(":id", $eventoObj->id_db);
  163. if(!$stmt->execute()){
  164. $t = $stmt->errorInfo();
  165. $return["error"] .= "Ocurrió un error al obtener las fechas editadas.".$t[2];
  166. $error = true;
  167. break;
  168. }
  169. if(!$error){
  170. //modifica fechas
  171. $eventosEditados_rs = $stmt->fetchAll();
  172. $stmt->closeCursor();
  173. foreach($eventosEditados_rs as $modif){
  174. $eventoObj->replaceFecha($modif["CalendarioEventoEditado_fecha_origen"], $modif["CalendarioEventoEditado_fecha_nueva"], $modif["CalendarioEventoEditado_hora_inicial"], $modif["CalendarioEventoEditado_hora_final"]);
  175. }
  176. //Quitar fechas de excepción
  177. foreach(explode(",", $evento["CalendarioEvento_excepcion_str"]) as $ex){
  178. $eventoObj->removeFecha($ex);
  179. }
  180. //Descompone el objeto en un arreglo con todas las fechas y agrega a lista al total del calendario
  181. $eventos_display = array_merge($eventos_display, $eventoObj->getEventList($mes, $anho));
  182. }
  183. }//foreach
  184. }//not error
  185. $return["eventos"] = $eventos_display;
  186. }
  187. $return["json"] = json_encode($return);
  188. echo json_encode($return);
  189. ?>