horario.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { createApp, reactive } from 'https://unpkg.com/petite-vue?module';
  2. const profesores = reactive({
  3. data: [],
  4. search: null,
  5. fetch: async function () {
  6. const response = await fetch('action/action_profesor.php');
  7. this.data = await response.json();
  8. },
  9. get clave() {
  10. const match = this.search.match(/^\((.+)\)/);
  11. return match ? match[1] : '';
  12. },
  13. get current() {
  14. return this.data.find((profesor) => profesor.profesor_clave === profesores.clave);
  15. },
  16. });
  17. const facultades = reactive({
  18. data: [],
  19. fetch: async function () {
  20. const facultades = await fetch('action/action_facultad.php').then(response => response.json());
  21. const carreras = await fetch(`action/carrera.php`).then(response => response.json());
  22. this.data = await Promise.all(facultades.map(async (facultad) => ({
  23. ...facultad,
  24. carreras: await Promise.all(carreras.filter((carrera) => carrera.facultad_id === facultad.facultad_id).map(async (carrera) => {
  25. const grupos = await fetch(`action/action_grupo.php?carrera_id=${carrera.carrera_id}`).then(response => response.json());
  26. return {
  27. ...carrera,
  28. grupos,
  29. };
  30. })),
  31. })));
  32. this.data = this.data.filter((facultad) => facultad.carreras.length > 0);
  33. }
  34. });
  35. const horarios = reactive({
  36. data: [],
  37. fetch: async function (grupo = null, carrera_id = null) {
  38. if (grupo && carrera_id) {
  39. const response = await fetch(`action/action_horario.php?grupo=${grupo}&carrera_id=${carrera_id}`);
  40. this.data = await response.json();
  41. }
  42. else if (profesores.current) {
  43. const response = await fetch(`action/action_horario.php?profesor_id=${profesores.current.profesor_id}`);
  44. this.data = await response.json();
  45. }
  46. },
  47. get structure() {
  48. if (this.data.length === 0)
  49. return null;
  50. const structure = {
  51. sábado: this.data.some((horario) => horario.horario_dia === 6),
  52. hora_mínima: Math.min(...this.data.map((horario) => parseInt(horario.horario_hora.split(':')[0]))),
  53. hora_máxima: Math.max(...this.data.map((horario) => {
  54. const [hour, minute] = horario.horario_fin.split(':').map(Number);
  55. return hour + Math.ceil(minute / 60);
  56. })),
  57. horas_totales: 0
  58. };
  59. structure.horas_totales = structure.hora_máxima - structure.hora_mínima;
  60. return structure;
  61. },
  62. get blocks() {
  63. if (this.data.length === 0)
  64. return null;
  65. return [...Array(this.structure.horas_totales).keys()].flatMap(hora => {
  66. const baseHour = hora + this.structure.hora_mínima;
  67. return [0, 15, 30, 45].map(block => ({ hour: baseHour, block }));
  68. });
  69. },
  70. getHorarioData(hour, block, día) {
  71. const foundHorario = this.data.find((horario) => parseInt(horario.horario_hora.split(':')[0]) === hour &&
  72. parseInt(horario.horario_hora.split(':')[1]) === block &&
  73. horario.horario_dia === día);
  74. return foundHorario;
  75. },
  76. isOccupied(hora, bloque, day) {
  77. if (this.getHorarioData(hora, bloque, day)) {
  78. return false;
  79. }
  80. const currentTimeInMinutes = hora * 60 + bloque;
  81. for (const item of this.data) {
  82. if (item.horario_dia !== day) {
  83. continue; // Skip items that are not on the specified day
  84. }
  85. // Split the hour and minute from horario_hora
  86. const [startHour, startMinute] = item.horario_hora.split(":").map(Number);
  87. const startTimeInMinutes = startHour * 60 + startMinute;
  88. // Calculate end time using duracion
  89. const [durationHours, durationMinutes] = item.duracion.split(":").map(Number);
  90. const endTimeInMinutes = startTimeInMinutes + (durationHours * 60) + durationMinutes;
  91. if (currentTimeInMinutes >= startTimeInMinutes && currentTimeInMinutes < endTimeInMinutes) {
  92. return true; // The block is occupied
  93. }
  94. }
  95. return false; // The block is not occupied by any class
  96. }
  97. });
  98. const app = createApp({
  99. profesores,
  100. horarios,
  101. facultades,
  102. mounted: async function () {
  103. await profesores.fetch();
  104. await facultades.fetch();
  105. }
  106. }).mount('#app');