horario.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { createApp, reactive } from 'https://unpkg.com/petite-vue?module'
  2. type Profesor = {
  3. profesor_clave: string;
  4. profesor_correo: string;
  5. profesor_grado: null | string;
  6. profesor_id: number;
  7. profesor_nombre: string;
  8. }
  9. type Horario = {
  10. carrera: string;
  11. carrera_id: number;
  12. dia: string;
  13. duracion: string;
  14. duracion_id: number;
  15. facultad: string;
  16. facultad_id: number;
  17. fecha_carga: Date;
  18. horario_dia: number;
  19. horario_fecha_fin: null;
  20. horario_fecha_inicio: Date;
  21. horario_fin: string;
  22. horario_grupo: string;
  23. horario_hora: string;
  24. horario_id: number;
  25. limite: null;
  26. materia: string;
  27. materia_id: number;
  28. nivel: string;
  29. nivel_id: number;
  30. periodo: string;
  31. periodo_fecha_fin: Date;
  32. periodo_fecha_inicio: Date;
  33. periodo_id: number;
  34. profesor_id: number;
  35. salon: string;
  36. salon_id: number;
  37. bloques: number;
  38. }
  39. const profesores = reactive({
  40. data: [] as Profesor[],
  41. search: null as null | string,
  42. fetch: async function () {
  43. const response = await fetch('action/action_profesor.php')
  44. this.data = await response.json() as Profesor[]
  45. },
  46. get clave() {
  47. const match = this.search.match(/^\((.+)\)/)
  48. return match ? match[1] : ''
  49. },
  50. get current() {
  51. return this.data.find((profesor: Profesor) => profesor.profesor_clave === profesores.clave)
  52. },
  53. })
  54. type Structure = {
  55. sábado: boolean;
  56. hora_mínima: number;
  57. hora_máxima: number;
  58. horas_totales: number;
  59. }
  60. const horarios = reactive({
  61. data: [] as Horario[],
  62. fetch: async function () {
  63. if (profesores.current) {
  64. const response = await fetch(`action/action_horario.php?profesor_id=${profesores.current.profesor_id}`)
  65. this.data = await response.json()
  66. }
  67. },
  68. get structure() {
  69. if (this.data.length === 0) return null;
  70. const structure: Structure = {
  71. sábado: this.data.some((horario: Horario) => horario.horario_dia === 6),
  72. hora_mínima: Math.min(...this.data.map((horario: Horario) => parseInt(horario.horario_hora.split(':')[0]))),
  73. hora_máxima: Math.max(...this.data.map((horario: Horario) => {
  74. const [hour, minute] = horario.horario_fin.split(':').map(Number);
  75. return hour + Math.ceil(minute / 60);
  76. })),
  77. horas_totales: 0
  78. };
  79. structure.horas_totales = structure.hora_máxima - structure.hora_mínima;
  80. return structure;
  81. },
  82. get blocks() {
  83. if (this.data.length === 0) return null;
  84. return [...Array(this.structure.horas_totales).keys()].flatMap(hora => {
  85. const baseHour = hora + this.structure.hora_mínima;
  86. return [0, 15, 30, 45].map(block => ({ hour: baseHour, block }));
  87. });
  88. },
  89. getHorarioData(hour: number, block: number, día: number) {
  90. const foundHorario = this.data.find((horario: Horario) =>
  91. parseInt(horario.horario_hora.split(':')[0]) === hour &&
  92. parseInt(horario.horario_hora.split(':')[1]) === block &&
  93. horario.horario_dia === día
  94. );
  95. return foundHorario;
  96. },
  97. isOccupied(hora: number, bloque: number, day: number) {
  98. if (this.getHorarioData(hora, bloque, day)) {
  99. return false;
  100. }
  101. const currentTimeInMinutes = hora * 60 + bloque;
  102. for (const item of this.data) {
  103. if (item.horario_dia !== day) {
  104. continue; // Skip items that are not on the specified day
  105. }
  106. // Split the hour and minute from horario_hora
  107. const [startHour, startMinute] = item.horario_hora.split(":").map(Number);
  108. const startTimeInMinutes = startHour * 60 + startMinute;
  109. // Calculate end time using duracion
  110. const [durationHours, durationMinutes] = item.duracion.split(":").map(Number);
  111. const endTimeInMinutes = startTimeInMinutes + (durationHours * 60) + durationMinutes;
  112. if (currentTimeInMinutes >= startTimeInMinutes && currentTimeInMinutes < endTimeInMinutes) {
  113. return true; // The block is occupied
  114. }
  115. }
  116. return false; // The block is not occupied by any class
  117. }
  118. })
  119. const app = createApp({
  120. profesores,
  121. horarios,
  122. mounted: async function () {
  123. await profesores.fetch()
  124. }
  125. }).mount('#app')