horario.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 horarios = reactive({
  18. data: [],
  19. fetch: async function () {
  20. if (profesores.current) {
  21. const response = await fetch(`action/action_horario.php?profesor_id=${profesores.current.profesor_id}`);
  22. this.data = await response.json();
  23. }
  24. },
  25. get structure() {
  26. if (this.data.length === 0)
  27. return null;
  28. const structure = {
  29. sábado: this.data.some((horario) => horario.horario_dia === 6),
  30. hora_mínima: Math.min(...this.data.map((horario) => parseInt(horario.horario_hora.split(':')[0]))),
  31. hora_máxima: Math.max(...this.data.map((horario) => {
  32. const [hour, minute] = horario.horario_fin.split(':').map(Number);
  33. return hour + Math.ceil(minute / 60);
  34. })),
  35. horas_totales: 0
  36. };
  37. structure.horas_totales = structure.hora_máxima - structure.hora_mínima;
  38. return structure;
  39. },
  40. get blocks() {
  41. if (this.data.length === 0)
  42. return null;
  43. return [...Array(this.structure.horas_totales).keys()].flatMap(hora => {
  44. const baseHour = hora + this.structure.hora_mínima;
  45. return [0, 15, 30, 45].map(block => ({ hour: baseHour, block }));
  46. });
  47. },
  48. getHorarioData(hour, block, día) {
  49. const foundHorario = this.data.find((horario) => parseInt(horario.horario_hora.split(':')[0]) === hour &&
  50. parseInt(horario.horario_hora.split(':')[1]) === block &&
  51. horario.horario_dia === día);
  52. return foundHorario;
  53. },
  54. isOccupied(hora, bloque, day) {
  55. if (this.getHorarioData(hora, bloque, day)) {
  56. return false;
  57. }
  58. const currentTimeInMinutes = hora * 60 + bloque;
  59. for (const item of this.data) {
  60. if (item.horario_dia !== day) {
  61. continue; // Skip items that are not on the specified day
  62. }
  63. // Split the hour and minute from horario_hora
  64. const [startHour, startMinute] = item.horario_hora.split(":").map(Number);
  65. const startTimeInMinutes = startHour * 60 + startMinute;
  66. // Calculate end time using duracion
  67. const [durationHours, durationMinutes] = item.duracion.split(":").map(Number);
  68. const endTimeInMinutes = startTimeInMinutes + (durationHours * 60) + durationMinutes;
  69. if (currentTimeInMinutes >= startTimeInMinutes && currentTimeInMinutes < endTimeInMinutes) {
  70. return true; // The block is occupied
  71. }
  72. }
  73. return false; // The block is not occupied by any class
  74. }
  75. });
  76. const app = createApp({
  77. profesores,
  78. horarios,
  79. mounted: async function () {
  80. await profesores.fetch();
  81. }
  82. }).mount('#app');