2
0

consulta_horarios.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // initial state
  2. const días = ["lunes", "martes", "miércoles", "jueves", "viernes", "sábado"];
  3. const horas_estándar = /* range from 7 to 22 */ Array.from(Array(22 - 7 + 1).keys()).map(x => x + 7);
  4. // fill the table with empty cells
  5. for (let i = 0; i < horas_estándar.length - 1; i++) {
  6. const hora = horas_estándar[i];
  7. const tr = document.createElement("tr");
  8. tr.id = `hora-${hora}-00`;
  9. tr.classList.add(hora > 13 ? "tarde" : "mañana");
  10. const th = document.createElement("th");
  11. th.classList.add("text-center");
  12. th.scope = "row";
  13. th.rowSpan = 4;
  14. th.innerText = `${hora}:00`;
  15. th.style.verticalAlign = "middle";
  16. tr.appendChild(th);
  17. for (let j = 0; j < días.length; j++) {
  18. const día = días[j];
  19. const td = document.createElement("td");
  20. td.id = `hora-${hora}-00-${día}`;
  21. tr.appendChild(td);
  22. }
  23. document.querySelector("tbody#horario")?.appendChild(tr);
  24. // add 7 rows for each hour
  25. const hours = [15, 30, 45];
  26. for (let j = 1; j < 4; j++) {
  27. const tr = document.createElement("tr");
  28. tr.id = `hora-${hora}-${hours[j - 1]}`;
  29. tr.classList.add(hora > 13 ? "tarde" : "mañana");
  30. for (let k = 0; k < días.length; k++) {
  31. const día = días[k];
  32. const td = document.createElement("td");
  33. td.id = `hora-${hora}-${hours[j - 1]}-${día}`;
  34. // td.innerText = `hora-${hora}-${hours[j - 1]}-${día}`;
  35. tr.appendChild(td);
  36. }
  37. document.querySelector("tbody#horario")?.appendChild(tr);
  38. }
  39. }
  40. // add an inital height to the table cells
  41. const tds = document.querySelectorAll("tbody#horario td");
  42. tds.forEach(td => td.style.height = "2rem");
  43. var table = document.querySelector("table");
  44. var empty_table = table?.innerHTML || "";
  45. // hide the table
  46. table.style.display = "none";
  47. document.getElementById('dlProfesor')?.addEventListener('input', function () {
  48. var input = document.getElementById('dlProfesor');
  49. var value = input.value;
  50. var option = document.querySelector(`option[value="${value}"]`);
  51. if (option) {
  52. var id = option.getAttribute('data-id');
  53. const input_profesor = document.getElementById('editor_profesor');
  54. input_profesor.value = id;
  55. // remove is invalid class
  56. input.classList.remove("is-invalid");
  57. // add is valid class
  58. input.classList.add("is-valid");
  59. }
  60. else {
  61. const input_profesor = document.getElementById('editor_profesor');
  62. input_profesor.value = "";
  63. // remove is valid class
  64. input.classList.remove("is-valid");
  65. // add is invalid class
  66. input.classList.add("is-invalid");
  67. }
  68. });
  69. /**
  70. * Functions and Methods
  71. **/
  72. const buscarGrupo = async () => {
  73. // Add loading animation in the button
  74. const btn = document.querySelector("#btn-buscar");
  75. btn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Cargando...';
  76. btn.disabled = true;
  77. const carrera = document.querySelector("#filter_carrera")?.value;
  78. const grupo = document.querySelector("#filter_grupo")?.value;
  79. console.log(`Carrera: ${carrera}, Grupo: ${grupo}`);
  80. if (carrera == "" || grupo == "") {
  81. triggerMessage("El nombre del grupo y la carrera son requeridos", "Faltan campos");
  82. // Remove loading animation in the button
  83. btn.innerHTML = '<i class="ing-buscar ing"></i> Buscar';
  84. btn.disabled = false;
  85. return;
  86. }
  87. const formData = new FormData();
  88. formData.append("carrera", carrera);
  89. formData.append("grupo", grupo);
  90. formData.append("periodo", document.querySelector("#periodo")?.value);
  91. const thisScript = document.currentScript;
  92. const facultad = thisScript.getAttribute("data-facultad");
  93. formData.append('facultad', facultad);
  94. try {
  95. const response = await fetch("action/action_horario.php", {
  96. method: "POST",
  97. body: formData
  98. }).then(res => res.json());
  99. if (response.status == "success") {
  100. let limits = {
  101. min: 22,
  102. max: 7
  103. };
  104. let sábado = false;
  105. const horario = response.horario;
  106. // show the table
  107. table.style.display = "table";
  108. // clear the table
  109. table.innerHTML = empty_table;
  110. // fill the table
  111. for (let i = 0; i < horario.length; i++) {
  112. const dia = horario[i].dia;
  113. if (dia == "sábado")
  114. sábado = true;
  115. const { hora, minutos } = {
  116. hora: parseInt(horario[i].hora.split(":")[0]),
  117. minutos: horario[i].hora.split(":")[1]
  118. };
  119. // update the limits
  120. if (hora < limits.min) {
  121. limits.min = hora;
  122. }
  123. if (hora > limits.max) {
  124. limits.max = hora;
  125. }
  126. const materia = horario[i].materia;
  127. const profesor = horario[i].profesor;
  128. const salon = horario[i].salon;
  129. const id = horario[i].horario_id;
  130. const prof_id = horario[i].profesor_id;
  131. const cell = document.querySelector(`#hora-${hora}-${minutos}-${dia}`);
  132. cell.innerHTML =
  133. `
  134. <div>
  135. <small class="text-gray">${hora}:${minutos}</small>
  136. <b class="title">${materia}</b> <br>
  137. <br><span>Salón: </span>${salon} <br>
  138. <span class="ing ing-formacion mx-1"></span>${profesor}
  139. </div>
  140. `;
  141. const html = `<div class="menu-flotante p-2">
  142. <a
  143. class="mx-2"
  144. href="#"
  145. data-toggle="modal"
  146. data-target="#modal-editar"
  147. data-dia="${dia}"
  148. data-hora="${hora}:${minutos}"
  149. data-materia="${materia}"
  150. data-profesor="${prof_id}"
  151. data-salon="${salon}"
  152. data-id="${id}"
  153. >
  154. <i class="ing-editar ing"></i>
  155. </a>
  156. <a
  157. class="mx-2"
  158. href="#"
  159. data-toggle="modal"
  160. data-target="#modal-borrar"
  161. data-hoario_id="${id}"
  162. >
  163. <i class="ing-basura ing"></i>
  164. </a>
  165. </div>`;
  166. const td = cell.closest("td");
  167. td.innerHTML += html;
  168. td.classList.add("position-relative");
  169. // this cell spans 4 rows
  170. cell.rowSpan = 6;
  171. cell.classList.add("bloque-clase", "overflow");
  172. for (let j = 1; j < 6; j++) {
  173. const minute = (parseInt(minutos) + j * 15);
  174. const next_minute = (minute % 60).toString().padStart(2, "0");
  175. const next_hour = (hora + Math.floor(minute / 60));
  176. const next_cell = document.querySelector(`#hora-${next_hour}-${next_minute}-${dia}`);
  177. next_cell.remove();
  178. }
  179. }
  180. // remove the elements that are not in the limits
  181. const horas = document.querySelectorAll("tbody#horario tr");
  182. for (let i = 0; i < horas.length; i++) {
  183. const hora = horas[i];
  184. const hora_id = parseInt(hora.id.split("-")[1]);
  185. if (hora_id < limits.min || hora_id > limits.max) {
  186. hora.remove();
  187. }
  188. }
  189. // if there is no sábado, remove the column
  190. if (!sábado) {
  191. document.querySelectorAll("tbody#horario td").forEach(td => {
  192. if (td.id.split("-")[3] == "sábado") {
  193. td.remove();
  194. }
  195. });
  196. // remove the header (the last)
  197. const headers = document.querySelector("#headers");
  198. headers.lastElementChild?.remove();
  199. }
  200. // adjust width
  201. const ths = document.querySelectorAll("tr#headers th");
  202. const width = 95 / (ths.length - 1);
  203. ths.forEach((th, key) => th.style.width = (key == 0) ? "5%" : `${width}%`);
  204. // search item animation
  205. const menúFlontantes = document.querySelectorAll(".menu-flotante");
  206. menúFlontantes.forEach((element) => {
  207. element.classList.add("d-none");
  208. element.parentElement?.addEventListener("mouseover", () => {
  209. element.classList.remove("d-none");
  210. });
  211. element.parentElement?.addEventListener("mouseout", () => {
  212. element.classList.add("d-none");
  213. });
  214. });
  215. }
  216. else {
  217. triggerMessage(response.message, "Error");
  218. // Remove loading animation in the button
  219. btn.innerHTML = '<i class="ing-buscar ing"></i> Buscar';
  220. btn.disabled = false;
  221. }
  222. }
  223. catch (error) {
  224. triggerMessage("Error al cargar el horario", "Error");
  225. triggerMessage(error, "Error");
  226. }
  227. // Remove loading animation in the button
  228. btn.innerHTML = '<i class="ing-buscar ing"></i> Buscar';
  229. btn.disabled = false;
  230. };
  231. async function guardar(id) {
  232. const btn = document.querySelector("#btn-guardar");
  233. const clone = btn.cloneNode(true);
  234. btn.innerHTML = '<i class="ing-cargando ing"></i> Guardando...';
  235. btn.disabled = true;
  236. const data = {
  237. hora: document.querySelector("#editor_hora"),
  238. dia: document.querySelector("#editor_dia"),
  239. salon: document.querySelector("#editor_salón"),
  240. profesor: document.querySelector("#editor_profesor"),
  241. };
  242. const hora = data.hora.value; // h:mm
  243. const { compareHours } = await import('./date_functions');
  244. const hora_antes = compareHours(hora, "07:15") < 0;
  245. const hora_después = compareHours(hora, "21:30") > 0;
  246. if (hora_antes || hora_después) {
  247. alert(`La hora ${hora} no es válida`);
  248. triggerMessage("Selecciona una hora", "Error");
  249. btn.innerHTML = clone.innerHTML;
  250. btn.disabled = false;
  251. data.hora.focus();
  252. data.hora.classList.add("is-invalid");
  253. return;
  254. }
  255. const dia = data.dia.value;
  256. const salon = data.salon.value;
  257. const profesor = data.profesor.value;
  258. const formData = new FormData();
  259. formData.append("id", id);
  260. formData.append("hora", hora);
  261. formData.append("dia", dia);
  262. formData.append("salon", salon);
  263. formData.append("profesor", profesor);
  264. const response = await fetch("action/action_horario_update.php", {
  265. method: "POST",
  266. body: formData
  267. }).then(res => res.json());
  268. if (response.status == "success") {
  269. triggerMessage(response.message, "Éxito", "success");
  270. btn.innerHTML = '<i class="ing-aceptar ing"></i> Guardado';
  271. btn.classList.add("btn-success");
  272. btn.classList.remove("btn-primary");
  273. // return to the initial state
  274. setTimeout(() => {
  275. buscarGrupo();
  276. btn.replaceWith(clone);
  277. $("#modal-editar").modal("hide");
  278. }, 1000);
  279. }
  280. else {
  281. triggerMessage(response.message, "Error");
  282. btn.replaceWith(clone);
  283. $("#modal-editar").modal("hide");
  284. }
  285. }
  286. function triggerMessage(message, header, colour = "danger") {
  287. throw new Error('Function not implemented.');
  288. }