reposiciones.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import { type } from "os";
  2. declare function triggerMessage(message: string, title: string, type?: string): void;
  3. declare const write: boolean;
  4. declare const moment: any;
  5. // from this 'horario_id', 'fecha', 'hora', 'duracion_id', 'descripcion', 'profesor_id', 'salon', 'unidad', 'periodo_id', 'fecha_clase' make a type of ReposicionParams
  6. export interface ReposicionParams {
  7. horario_id: number;
  8. fecha: string;
  9. hora: string;
  10. duracion_id: number;
  11. descripcion: string;
  12. profesor_id: number;
  13. salon: string;
  14. unidad: number;
  15. periodo_id: number;
  16. fecha_clase: string;
  17. }
  18. type Horario = {
  19. id: number;
  20. carrera_id: number;
  21. materia_id: number;
  22. grupo: string;
  23. profesores: Profesor[];
  24. dia: string;
  25. hora: string;
  26. hora_final: string;
  27. salon: string;
  28. fecha_inicio: string;
  29. fecha_final: string;
  30. fecha_carga: string;
  31. nivel_id: number;
  32. periodo_id: number;
  33. facultad_id: number;
  34. materia: string;
  35. horas: number;
  36. minutos: number;
  37. duracion: number;
  38. retardo: boolean;
  39. original_id: number;
  40. last: boolean;
  41. bloques: number;
  42. };
  43. type Profesor = {
  44. id: number;
  45. clave: string;
  46. grado: string;
  47. profesor: string;
  48. nombre: string;
  49. facultad_id: number;
  50. };
  51. // Get references to the HTML elements
  52. const form = document.getElementById('form') as HTMLFormElement;
  53. const steps = Array.from(form.querySelectorAll('.step')) as HTMLElement[];
  54. const nextButton = document.getElementById('next-button') as HTMLButtonElement;
  55. const prevButton = document.getElementById('prev-button') as HTMLButtonElement;
  56. let currentStep = 0;
  57. // #clave_profesor on change => show step 2
  58. const clave_profesor = document.getElementById('clave_profesor') as HTMLInputElement;
  59. const horario_reponer = document.getElementById('horario_reponer') as HTMLInputElement;
  60. const fechas_clase = document.getElementById('fechas_clase') as HTMLInputElement;
  61. const fecha_reponer = $('#fecha_reponer') as JQuery<HTMLElement>;
  62. const hora_reponer = $('#hora_reponer') as JQuery<HTMLElement>;
  63. const minutos_reponer = $('#minutos_reponer') as JQuery<HTMLElement>;
  64. clave_profesor.addEventListener('change', async () => {
  65. const step2 = document.getElementById('step-2') as HTMLElement;
  66. clave_profesor.disabled = true;
  67. // get option which value is the same as clave_profesor.value
  68. const option = document.querySelector(`option[value="${clave_profesor.value}"]`) as HTMLOptionElement;
  69. // make a form data with #form
  70. const profesor_id = document.getElementById('profesor_id') as HTMLInputElement;
  71. profesor_id.value = option.dataset.id;
  72. const formData = new FormData(form);
  73. const response = await fetch(`./action/action_horario_profesor.php`, {
  74. method: 'POST',
  75. body: formData
  76. });
  77. const data = await response.json();
  78. if (data['success'] === false) {
  79. const message = "Hubo un error al obtener los horarios del profesor."
  80. const title = 'Error';
  81. const color = 'danger';
  82. triggerMessage(message, title, color);
  83. return;
  84. }
  85. const horarios = data.data as Horario[];
  86. const initial = document.createElement('option');
  87. initial.value = '';
  88. initial.textContent = 'Seleccione un horario';
  89. initial.selected = true;
  90. initial.disabled = true;
  91. horario_reponer.innerHTML = '';
  92. horario_reponer.appendChild(initial);
  93. horarios.forEach((horario) => {
  94. const dias = ['Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado', 'Domingo'];
  95. const option = document.createElement('option');
  96. option.value = `${horario.id}`;
  97. // materia máx 25 caracteres, if materia.length > 25 then slice(0, 20)
  98. const max = 25;
  99. option.textContent = `${horario.materia.slice(0, max) + (horario.materia.length > max ? '...' : '')} - Grupo: ${horario.grupo} - ${horario.hora.slice(0, 5)}-${horario.hora_final.slice(0, 5)} - Salon: ${horario.salon} - ${horario.dia}`;
  100. option.dataset.materia = `${horario.materia}`;
  101. option.dataset.grupo = `${horario.grupo}`;
  102. option.dataset.hora = `${horario.hora.slice(0, 5)}`; // slice(0, 5) => HH:MM
  103. option.dataset.hora_final = `${horario.hora_final.slice(0, 5)}`;
  104. option.dataset.salon = `${horario.salon}`;
  105. option.dataset.dia = `${horario.dia}`;
  106. option.dataset.id = `${horario.id}`;
  107. horario_reponer.appendChild(option);
  108. });
  109. currentStep = 1;
  110. step2.style.display = 'block';
  111. prevButton.disabled = false;
  112. });
  113. // disable clave_profesor
  114. // from second step to first step
  115. prevButton.addEventListener('click', () => {
  116. const inputs = [clave_profesor, horario_reponer, fechas_clase, fecha_reponer, hora_reponer] as HTMLInputElement[];
  117. switch (currentStep) {
  118. case 1:
  119. case 2:
  120. case 3:
  121. const step = document.getElementById(`step-${currentStep + 1}`) as HTMLElement;
  122. step.style.display = 'none';
  123. inputs[currentStep - 1].disabled = false;
  124. inputs[currentStep - 1].value = '';
  125. if (--currentStep === 0) {
  126. prevButton.disabled = true;
  127. }
  128. break;
  129. case 4:
  130. const step5 = document.getElementById('step-5') as HTMLElement;
  131. step5.style.display = 'none';
  132. fecha_reponer.prop('disabled', false);
  133. fecha_reponer.val('');
  134. hora_reponer.parent().removeClass('disabled');
  135. hora_reponer.siblings('.datalist-input').text('hh');
  136. hora_reponer.val('');
  137. minutos_reponer.parent().removeClass('disabled');
  138. minutos_reponer.siblings('.datalist-input').text('mm');
  139. minutos_reponer.val('');
  140. currentStep--;
  141. break;
  142. }
  143. nextButton.disabled = true;
  144. });
  145. // #horario_reponer on change => show step 3
  146. horario_reponer.addEventListener('change', async () => {
  147. const selected = horario_reponer.querySelector(`option[value="${horario_reponer.value}"]`) as HTMLOptionElement;
  148. horario_reponer.title = `Materia: ${selected.dataset.materia} - Grupo: ${selected.dataset.grupo} - Horario: ${selected.dataset.hora}-${selected.dataset.hora_final} - Salon: ${selected.dataset.salon} - Día: ${selected.dataset.dia}`;
  149. const step3 = document.getElementById('step-3') as HTMLElement;
  150. horario_reponer.disabled = true;
  151. // make a form data with #form
  152. const response = await fetch(`./action/action_fechas_clase.php?horario_id=${horario_reponer.value}`, {
  153. method: 'GET',
  154. });
  155. const data = await response.json();
  156. if (data['success'] === false) {
  157. const message = "Hubo un error al obtener las fechas de clase."
  158. const title = 'Error';
  159. const color = 'danger';
  160. triggerMessage(message, title, color);
  161. return;
  162. }
  163. type Fecha = {
  164. fecha: string;
  165. dia_mes: number;
  166. day: number;
  167. month: number;
  168. year: number;
  169. }
  170. const meses = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
  171. const fechas = data.data as Fecha[];
  172. const initial = document.createElement('option');
  173. initial.value = '';
  174. initial.textContent = 'Seleccione la fecha de la falta';
  175. initial.selected = true;
  176. initial.disabled = true;
  177. fechas_clase.innerHTML = '';
  178. fechas_clase.appendChild(initial);
  179. fechas_clase.title = 'Seleccione la fecha de la falta';
  180. fechas.forEach((fecha) => {
  181. const option = document.createElement('option');
  182. option.value = `${fecha}`;
  183. option.textContent = `${fecha.dia_mes} de ${meses[fecha.month - 1]} de ${fecha.year}`;
  184. fechas_clase.appendChild(option);
  185. });
  186. step3.style.display = 'block';
  187. currentStep = 2;
  188. });
  189. // #fechas_clase on change => show step 4
  190. fechas_clase.addEventListener('change', () => {
  191. const step4 = document.getElementById('step-4') as HTMLElement;
  192. step4.style.display = 'block';
  193. fechas_clase.disabled = true;
  194. currentStep = 3;
  195. });
  196. // when both #fecha_reponer and #hora_reponer are selected => show step 5
  197. const lastStep = () => {
  198. // timeout to wait for the value to be set
  199. setTimeout(() => {
  200. if (fecha_reponer.val() !== '' && hora_reponer.val() !== '' && minutos_reponer.val() !== '') {
  201. const step5 = document.getElementById('step-5') as HTMLElement;
  202. step5.style.display = 'block';
  203. // disable both
  204. fecha_reponer.prop('disabled', true);
  205. hora_reponer.parent().addClass('disabled');
  206. minutos_reponer.parent().addClass('disabled');
  207. const nextButton = document.getElementById('next-button') as HTMLButtonElement;
  208. // remove property disabled
  209. nextButton.removeAttribute('disabled');
  210. currentStep = 4;
  211. }
  212. }, 100);
  213. }
  214. fecha_reponer.on('change', lastStep);
  215. // on click on the sibling ul>li of #hora_reponer and #minutos_reponer
  216. hora_reponer.siblings('ul').children('li').on('click', lastStep);
  217. minutos_reponer.siblings('ul').children('li').on('click', lastStep);
  218. // Initialize the form
  219. hideSteps();
  220. showCurrentStep();
  221. function hideSteps() {
  222. steps.forEach((step) => {
  223. step.style.display = 'none';
  224. });
  225. }
  226. function showCurrentStep() {
  227. steps[currentStep].style.display = 'block';
  228. prevButton.disabled = currentStep === 0;
  229. }
  230. function handleSubmit(event: Event) {
  231. event.preventDefault();
  232. // Handle form submission
  233. // You can access the form data using the FormData API or serialize it manually
  234. }