EventosController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. class EventosController extends Controller
  8. {
  9. public function eventosActivos()
  10. {
  11. $evento = DB::table('calendario_eventos_colegioabc')
  12. ->where('idEscuela', 'COLEGIOABC')
  13. // ->where('estatus', 'Activo')
  14. ->get();
  15. return response()->json($evento, 200);
  16. }
  17. public function crear(Request $request)
  18. {
  19. try {
  20. DB::table('calendario_eventos_colegioabc')->insert([
  21. 'idEscuela' => 'COLEGIOABC',
  22. 'idUsuario' => 'PR011',
  23. 'estatus' => 'Activo',
  24. 'titulo' => $request->titulo,
  25. 'fechaInicio' => $request->fechaInicio,
  26. 'fechaFin' => $request->fechaFin,
  27. // var_dump($request->todoDia),
  28. 'diaCompleto' => $request->todoDia === true ? 'Si' : 'No',
  29. 'colorEvento' => $request->categoria,
  30. 'colorTexto' => '#FFFFFF',
  31. 'descripcion' => $request->contenido,
  32. 'visualizarListaInvitados' => 'No',
  33. 'mostrarConfirmacion' => 'No',
  34. 'solicitarConfirmacionAsistencia' => 'No',
  35. 'jsonInvitados' => '{}',
  36. 'numeroAdjuntos' => '0',
  37. 'jsonAdjuntos' => '{}',
  38. 'videollamada' => 'No',
  39. 'tipoGrabacion' => 'No_Grabar',
  40. 'tipoChat' => 'Publico_Moderador',
  41. 'idUsuarioCc' => 'PR011',
  42. ]);
  43. return response()->json([
  44. 'mensaje' => 'Evento creado exitosamente',
  45. 'idEvento' => DB::getPdo()->lastInsertId()
  46. ], 201);
  47. } catch (\Exception $e) {
  48. return response()->json(['mensaje' => 'Error al actualizar el evento: ' . $e->getMessage()], 500);
  49. }
  50. }
  51. public function savecalendarioUsuarios(Request $request)
  52. {
  53. try {
  54. DB::table('calendario_usuarios_colegioabc')->insert([
  55. 'idEscuela' => 'COLEGIOABC',
  56. 'idEvento' => $request->idEvento,
  57. 'idUsuario' => $request->idUsuario,
  58. 'tipo' => 'Invitado',
  59. 'estatus' => 'Activo',
  60. 'asistencia' => 'Sin Confirmación',
  61. 'idUsuarioCc' => 'PR011',
  62. ]);
  63. return response()->json([
  64. 'mensaje' => 'Evento asignado exitosamente',
  65. ], 201);
  66. } catch (\Exception $e) {
  67. return response()->json(['mensaje' => 'Error al actualizar el evento: ' . $e->getMessage()], 500);
  68. }
  69. }
  70. public function getWeekEvents()
  71. {
  72. // Obtener fechas de la semana actual (lunes a viernes)
  73. $now = Carbon::now();
  74. $monday = $now->startOfWeek(Carbon::MONDAY);
  75. $weekDays = [
  76. 'Lunes' => $monday->copy()->format('Y-m-d'),
  77. 'Martes' => $monday->copy()->addDay()->format('Y-m-d'),
  78. 'Miércoles' => $monday->copy()->addDays(2)->format('Y-m-d'),
  79. 'Jueves' => $monday->copy()->addDays(3)->format('Y-m-d'),
  80. 'Viernes' => $monday->copy()->addDays(4)->format('Y-m-d'),
  81. ];
  82. $result = [];
  83. foreach ($weekDays as $dayName => $date) {
  84. $events = DB::table('calendario_eventos_colegioabc')
  85. ->where('idEscuela', 'COLEGIOABC')
  86. ->where('estatus', 'Activo')
  87. ->whereDate('fechaInicio', '<=', $date)
  88. ->whereDate('fechaFin', '>=', $date)
  89. ->get();
  90. $eventCount = $events->count();
  91. $eventDescription = $this->getEventDescription($eventCount);
  92. // Si hay solo un evento, obtener el título
  93. if ($eventCount === 1) {
  94. $eventDescription = $events->first()->titulo;
  95. }
  96. $result[] = [
  97. 'day' => $dayName,
  98. 'event' => $eventDescription,
  99. 'num' => Carbon::parse($date)->day,
  100. 'completeDate' => $date
  101. ];
  102. }
  103. return $result;
  104. }
  105. protected function getEventDescription($count)
  106. {
  107. if ($count === 0) {
  108. return 'Sin Evento';
  109. } elseif ($count === 1) {
  110. return '1 evento';
  111. } else {
  112. return "+ de 1 evento";
  113. }
  114. }
  115. }