EventoInsignia.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. require_once 'Evento.php';
  3. require_once 'Fechas.php';
  4. class EventoInsignia {
  5. private static function getEventos ($pdo, $mes, $anio, $insignia_id = null, $evento_id = null) {
  6. $eventos = array();
  7. if (!is_null($mes)){
  8. $fecha_min = $anio. '-' . $mes . '-1';
  9. $fecha_max = $anio. '-' . $mes . '-' . cal_days_in_month(CAL_GREGORIAN, $mes, $anio);
  10. if (is_null($insignia_id) && is_null($evento_id))
  11. $stmt = $pdo->prepare('Select * from alu_fs_calendarioevento(null, null, :Fecha_inicio, :Fecha_fin)');
  12. else {
  13. if (is_null($insignia_id)){
  14. $stmt = $pdo->prepare('Select * from alu_fs_calendarioevento(null, :Evento, :Fecha_inicio, :Fecha_fin)');
  15. $stmt->bindParam(':Evento', $evento_id);
  16. } else {
  17. if (is_null($evento_id)) {
  18. $stmt = $pdo->prepare('Select * from alu_fs_calendarioevento(:Insignia, null, :Fecha_inicio, :Fecha_fin)');
  19. $stmt->bindParam(':Insignia', $insignia_id);
  20. } else {
  21. $stmt = $pdo->prepare('Select * from alu_fs_calendarioevento(:Insignia, :Evento, :Fecha_inicio, :Fecha_fin)');
  22. $stmt->bindParam(':Insignia', $insignia_id);
  23. $stmt->bindParam(':Evento', $evento_id);
  24. }
  25. }
  26. }
  27. $stmt->bindParam(":Fecha_inicio", $fecha_min);
  28. $stmt->bindParam(":Fecha_fin", $fecha_max);
  29. } else {
  30. if (is_null($insignia_id) && is_null($evento_id))
  31. $stmt = $pdo->prepare('Select * from alu_fs_calendarioevento(null, null, null, null)');
  32. else {
  33. if (is_null($insignia_id)){
  34. $stmt = $pdo->prepare('Select * from alu_fs_calendarioevento(null, :Evento, null, null)');
  35. $stmt->bindParam(':Evento', $evento_id);
  36. } else {
  37. if (is_null($evento_id)) {
  38. $stmt = $pdo->prepare('Select * from alu_fs_calendarioevento(:Insignia, null, null, null)');
  39. $stmt->bindParam(':Insignia', $insignia_id);
  40. } else {
  41. $stmt = $pdo->prepare('Select * from alu_fs_calendarioevento(:Insignia, :Evento, null, null)');
  42. $stmt->bindParam(':Insignia', $insignia_id);
  43. $stmt->bindParam(':Evento', $evento_id);
  44. }
  45. }
  46. }
  47. }
  48. if(!$stmt->execute())
  49. print_r($stmt->errorInfo());
  50. else {
  51. $eventos = $stmt->fetchAll();
  52. $stmt->closeCursor();
  53. $stmt = null;
  54. }
  55. return $eventos;
  56. }
  57. public static function getDatosEventos($pdo, $mes, $anio, $insignia_id = null, $evento_id = null) {
  58. $eventos_display = array();//eventos con todas las fechas para pintarse. Arreglo de Objeto evento
  59. $eventos = self::getEventos($pdo, $mes, $anio, $insignia_id, $evento_id);
  60. foreach($eventos as $evento){
  61. $eventoObj = new Evento($evento['evento_id'], $evento['periodo_id'],$evento['insignia_id'], $evento['titulo'], $evento['detalle'], $evento['shortname'], $evento['fecha_inicio'], $evento['hora_inicio'], $evento['hora_fin'] ,$evento['inscripciones']);
  62. //-- Calcular posibles fechas e insertar fechas en objeto --
  63. $fecha = Fechas::validaFormatoFecha($evento['fecha_inicio']);
  64. switch($evento['repeticion_id']){
  65. case 1: //diario
  66. $cont = 0;
  67. while (strtotime($fecha) <= strtotime($evento['reglas_fecha_final'])) {
  68. $eventoObj->addFecha($fecha);
  69. $fecha = date ("Y-m-d", strtotime("+1 day", strtotime($fecha)));
  70. $cont++;
  71. }
  72. if ($cont == 2)
  73. $eventoObj->periodicidad = 'ULTIMOS';
  74. else
  75. $eventoObj->periodicidad = 'RANGO';
  76. break;
  77. case 2: //semanal
  78. $diasArr = explode(",", $evento['reglas_dias']);
  79. while (strtotime($fecha) <= strtotime($evento['reglas_fecha_final'])) {
  80. if(in_array(date("w", strtotime($fecha)), $diasArr) ){//si es el día que quiero
  81. $eventoObj->addFecha($fecha);
  82. }
  83. $fecha = date ("Y-m-d", strtotime("+1 day", strtotime($fecha)));
  84. }
  85. $eventoObj->periodicidad = 'ALTERNADOS';
  86. break;
  87. case 3://mensual
  88. $diasArr = explode(",", $evento['reglas_dias']);
  89. //reglas
  90. $weekTxt = array(1=>"first", 2=>"second", 3=>"third", 4=>"fourth", -1=>"last");
  91. $dayname = array("sun", "mon", "tue", "wed", "thu", "fri", "sat");
  92. $fecha = date ("Y-m-01", strtotime($fecha));//empieza a revisar en el primer día del mes
  93. $semana = $evento['reglas_semana'];
  94. while (strtotime($fecha) <= strtotime($evento['reglas_fecha_final'])) {
  95. foreach($diasArr as $d){
  96. //echo "-->". intval(date("w", strtotime($fecha)))." == ". intval($d)."[".$fecha."]";
  97. if($semana == 1 && intval(date("w", strtotime($fecha))) == intval($d) ){//si el día actual es el que quiero lo guarda
  98. $eventoObj->addFecha($fecha);
  99. }else{//si no calcula siguiente día
  100. if(intval(date("w", strtotime($fecha))) == intval($d)){
  101. $fechaTmp = date ("Y-m-d", strtotime($weekTxt[$semana-1]." ".$dayname[$d], strtotime($fecha)));
  102. }else{
  103. $fechaTmp = date ("Y-m-d", strtotime($weekTxt[$semana]." ".$dayname[$d], strtotime($fecha)));
  104. }
  105. $eventoObj->addFecha($fechaTmp);
  106. }
  107. }
  108. $fecha = date ("Y-m-01", strtotime("+1 month", strtotime($fecha)));//siguiente mes primer día
  109. }
  110. $eventoObj->periodicidad = 'ALTERNADOS';
  111. break;
  112. default: //no se repite
  113. $eventoObj->addFecha($fecha);
  114. $eventoObj->periodicidad = 'UNICO';
  115. break;
  116. }
  117. $stmt = $pdo->prepare('Select * from fs_calendarioeventoeditado(:Evento)');
  118. $stmt->bindParam(":Evento", $eventoObj->evento_id);
  119. if(!$stmt->execute())
  120. print_r($stmt->errorInfo());
  121. else {
  122. //modifica fechas
  123. $eventosEditados_rs = $stmt->fetchAll();
  124. foreach($eventosEditados_rs as $modif){
  125. $eventoObj->replaceFecha($modif["CalendarioEventoEditado_fecha_origen"], $modif["CalendarioEventoEditado_fecha_nueva"], $modif["CalendarioEventoEditado_hora_inicial"], $modif["CalendarioEventoEditado_hora_final"]);
  126. }
  127. //Quitar fechas de excepción
  128. if (!empty($evento['excepcion'])){
  129. foreach(explode(",", $evento['excepcion']) as $ex){
  130. $eventoObj->removeFecha($ex);
  131. }
  132. $eventoObj->periodicidad = 'ALTERNADOS';
  133. }
  134. //Descompone el objeto en un arreglo con todas las fechas y agrega a lista al total del calendario
  135. $alta = false;
  136. if (!is_null($mes)){
  137. $cont = 0;
  138. $fechas = array_column($eventoObj->fechasArr, 'fecha');
  139. while (!$alta && $cont < count($fechas)) {
  140. if (($anio . '-' . Fechas::dosDigitos($mes)) == substr($fechas[$cont], 0,-3))
  141. $alta = true;
  142. $cont++;
  143. }
  144. } else
  145. $alta = true;
  146. if ($alta) {
  147. $eventoObj->dia = self::primerDiaMes(array_column($eventoObj->fechasArr,'fecha'), $anio . '-' . Fechas::dosDigitos($mes));
  148. array_push($eventos_display, $eventoObj->getEventList($mes, $anio));
  149. }
  150. }
  151. $stmt->closeCursor();
  152. $stmt = null;
  153. }
  154. return $eventos_display;
  155. }
  156. public static function getDatosInsignia($pdo, $evento){
  157. $insignia = array();
  158. $stmt = $pdo->prepare('SELECT * FROM fs_insigniageneral(:Evento_id)');
  159. $stmt->bindParam(':Evento_id', $evento);
  160. if(!$stmt->execute())
  161. print_r($stmt->errorInfo());
  162. else
  163. $insignia = $stmt->fetch();
  164. $stmt->closeCursor();
  165. $stmt = null;
  166. return $insignia;
  167. }
  168. public static function getFechasRomanizadas($fechasArr, $tipo, $fecha, $horas = false, $anio = false){
  169. $fechas = array('texto' => '', 'horario' => '', 'horarioTemp' => '');
  170. if (count($fechas) > 0){
  171. switch ($tipo){
  172. case 'UNICO':
  173. if ($horas)
  174. $fechas['horario'] = Fechas::horas($fechasArr[0]['hora_ini'], $fechasArr[0]['hora_fin'], '-');
  175. else
  176. $fechas['horario'] = '';
  177. $fechas['horarioTemp'] = $fechas['horario'];
  178. break;
  179. case 'RANGO':
  180. case 'ALTERNADOS':
  181. case 'ULTIMOS':
  182. $hora_ini = array_unique(array_column($fechasArr, 'hora_ini'));
  183. $hora_fin = array_unique(array_column($fechasArr, 'hora_fin'));
  184. if(count($hora_ini) == 1 && count($hora_fin) == 1 || !$horas) {
  185. if ($horas)
  186. $fechas['horario'] = Fechas::horas($fechasArr[0]['hora_ini'], $fechasArr[0]['hora_fin'], '-');
  187. else
  188. $fechas['horario'] = '';
  189. switch ($tipo){
  190. case 'ULTIMOS':
  191. $fechas['texto'] = Fechas::estandarizaDia($fechasArr[0]['fecha'], $fechasArr[count($fechasArr) - 1]['fecha'], $tipo, $anio);
  192. break;
  193. case 'ALTERNADOS':
  194. $temp = array_column($fechasArr, 'fecha');
  195. $fechas['texto'] = '';
  196. for ($cont = 0; $cont < (count($temp) - 1); $cont++) {
  197. if ($cont < count($temp) - 2)
  198. $fechas['texto'] .= Fechas::estandarizaDia($temp[$cont], $temp[$cont + 1], 'ALTERNADOS', $anio);
  199. else
  200. $fechas['texto'] .= Fechas::estandarizaDia($temp[$cont], $temp[$cont + 1], 'ULTIMOS', $anio);
  201. }
  202. break;
  203. case 'RANGO':
  204. $fechas['texto'] = Fechas::estandarizaDia($fechasArr[0]['fecha'], $fechasArr[count($fechasArr) - 1]['fecha'], $tipo, $anio);
  205. break;
  206. }
  207. $fechas['horarioTemp'] = $fechas['horario'];
  208. } else {
  209. $encontrado = false;
  210. $fechas['texto'] = '';
  211. for ($cont = 0; $cont < count($fechasArr); $cont++) {
  212. $horario = Fechas::horas($fechasArr[$cont]['hora_ini'], $fechasArr[$cont]['hora_fin'], '-');
  213. $fechas['texto'] .= Fechas::romanizaFecha($fechasArr[$cont]['fecha'], $anio) . ' | ' . $horario;
  214. if ($cont < (count($fechasArr) - 1))
  215. $fechas['texto'] .= '<br />';
  216. if (!is_null($fecha)){
  217. if ($fecha == substr($fechasArr[$cont]['fecha'],0, -3) && !$encontrado)
  218. $fechas['horarioTemp'] = $horario;
  219. }
  220. }
  221. }
  222. break;
  223. }
  224. }
  225. return $fechas;
  226. }
  227. public static function getRegistroEvento($pdo, $usuario, $insignia = null, $fechaIni = null, $fechaFin = null){
  228. $registro = array();
  229. if (is_null($fechaIni))
  230. $fechas = ', null, null)';
  231. else
  232. $fechas = ',:Inicio, :Fin)';
  233. if (!is_null($insignia)){
  234. $stmt = $pdo->prepare('SELECT * FROM alu_fs_registroevento(:Insignia_id, :Usuario_id' . $fechas);
  235. $stmt->bindParam(':Insignia_id', $insignia);
  236. if (!is_null($fechaIni)){
  237. $stmt->bindParam(':Inicio', $fechaIni);
  238. $stmt->bindParam(':Fin', $fechaFin);
  239. }
  240. } else {
  241. $stmt = $pdo->prepare('SELECT * FROM alu_fs_registroevento(null, :Usuario_id' . $fechas);
  242. if (!is_null($fechaIni)){
  243. $stmt->bindParam(':Inicio', $fechaIni);
  244. $stmt->bindParam(':Fin', $fechaFin);
  245. }
  246. }
  247. $stmt->bindParam(':Usuario_id', $usuario);
  248. if(!$stmt->execute())
  249. print_r($stmt->errorInfo());
  250. else
  251. $registro = $stmt->fetchAll();
  252. $stmt->closeCursor();
  253. $stmt = null;
  254. return $registro;
  255. }
  256. private static function primerDiaMes($fechas, $fecha) {
  257. $encontrado = false;
  258. $dia = '';
  259. while (($temp = current($fechas)) && !$encontrado) {
  260. if ($fecha == substr($temp, 0,-3)) {
  261. $dia = substr($temp, -2);
  262. $encontrado = true;
  263. }
  264. next($fechas);
  265. }
  266. return $dia;
  267. }
  268. }