menu.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <main class="container mt-5">
  2. <div class="modal" tabindex="-1" :class="{ show: option.modal }" style="display: block;" aria-modal="true"
  3. role="dialog" v-if="option.modal" @vue:mounted="store.showModal($el); " @vue:unmounted="store.hideModal($el)"
  4. data-bs-backdrop="static" data-bs-keyboard="false">
  5. <div class="modal-dialog">
  6. <div class="modal-content">
  7. <div class="modal-header">
  8. <h5 class="modal-title">{{ option.modal }}</h5>
  9. <button type="button" class="btn-close" aria-label="Close" @click="closeModal"></button>
  10. </div>
  11. <div class="modal-body">
  12. <form>
  13. <div v-for="item in currentOptions" class="mb-3">
  14. <label :for="item.value" :class="item.label_class">
  15. {{ item.label }}</label>
  16. <input :type="item.type" :name="item.name" :value="item.value" :list="item.name"
  17. :id="item.value" v-model="item.value" :class="item.input_class"
  18. :class="{ 'form-control': item.type !== 'radio' }">
  19. <datalist v-if="item.datalist?.length" :id="item.name">
  20. <option v-for="option in item.datalist" :value="option.id">{{ option.nombre }}</option>
  21. </datalist>
  22. </div>
  23. <div class="d-grid gap-2 d-md-flex justify-content-md-end">
  24. <button type="reset" class="btn btn-danger btn-sm">
  25. <i class="fas fa-eraser"></i>
  26. Limpiar
  27. </button>
  28. <button type="submit" class="btn btn-primary btn-sm" @click="submitModal"
  29. :disabled="currentOptions?.some(item => !item.value)">
  30. <i class="fas fa-check"></i>
  31. Exportar
  32. </button>
  33. </div>
  34. </form>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. <!-- STORE ALERT -->
  40. <div class="alert alert-dismissible fade show" :class="`alert-${store.alert.type}`" v-if="store.alert">
  41. <button type="button" class="btn-close" @click="store.alert = null"></button>
  42. <strong>{{ store.alert.type === 'danger' ? 'Error' : 'Éxito' }}:</strong>
  43. {{ store.alert.message }}
  44. </div>
  45. <!-- Buttons outside the modal/dialog -->
  46. <div class="container" @vue:mounted="lastSnapshot">
  47. <div class="d-block text-center mb-3" v-if="last_snapshot">
  48. Último snapshot:
  49. <code>{{ last_snapshot }}</code>
  50. </div>
  51. <div v-else class="d-block text-center mb-3">
  52. No hay snapshots
  53. </div>
  54. <div class="gap-2 d-flex justify-content-md-center flex-wrap">
  55. <button type="button" class="btn col-5" v-for="item in menu" @click="item.click"
  56. :class="`btn-${item.color ?? 'outline-secondary'}`" :aria-label="`Activate ${item.name}`">
  57. <i :class="item.icon" aria-hidden="true"></i>
  58. <span class="ms-2">{{ item.name }}</span>
  59. </button>
  60. </div>
  61. </div>
  62. </main>
  63. <script>
  64. const option = PetiteVue.reactive({ modal: null });
  65. function createDownloadLink({ url, filename, postData }) {
  66. return async () => {
  67. store.loading = true;
  68. const response = await fetch(url, { method: 'POST', body: JSON.stringify(postData) });
  69. const blob = await response.blob();
  70. const downloadUrl = window.URL.createObjectURL(blob);
  71. const anchor = document.createElement('a');
  72. anchor.href = downloadUrl;
  73. anchor.download = filename;
  74. anchor.charset = "windows-1252"; // Set the charset to ANSI for compatibility
  75. anchor.click();
  76. anchor.remove();
  77. store.loading = false;
  78. };
  79. }
  80. async function fetchOptions(url, optionName) {
  81. const response = await fetch(url);
  82. const data = await response.json();
  83. return data.map(item => ({ id: item[optionName.id], nombre: item[optionName.nombre] }));
  84. }
  85. PetiteVue.createApp({
  86. option,
  87. modal: false,
  88. menu: [
  89. {
  90. name: 'Construcción de Calificación',
  91. icon: 'fas fa-sliders',
  92. color: 'warning',
  93. url: '/export/excel.php',
  94. filename: 'calificacion.csv',
  95. click: createDownloadLink({ url: '/export/excel.php', filename: 'calificacion.csv', postData: { query: 'c-calif' } })
  96. },
  97. {
  98. name: 'Calificaciones Brutas',
  99. icon: 'fas fa-xmark',
  100. url: '/export/excel.php',
  101. filename: 'calificacion.csv',
  102. click: createDownloadLink({ url: '/export/excel.php', filename: 'calificaciones.csv', postData: { query: 'n-c-brutas' } })
  103. },
  104. {
  105. name: 'Calificaciones Netas',
  106. icon: 'fas fa-tarp',
  107. url: '/export/excel.php',
  108. filename: 'calificacion.csv',
  109. click: createDownloadLink({ url: '/export/excel.php', filename: 'calificaciones_netas.csv', postData: { query: 'c-net' } })
  110. },
  111. {
  112. name: 'Calificaciones Netas por Curso',
  113. icon: 'fas fa-tarp-droplet',
  114. url: '/export/excel.php',
  115. filename: 'calificacion.csv',
  116. click: createDownloadLink({ url: '/export/excel.php', filename: 'calificaciones_netas_curso.csv', postData: { query: 'c-net-cur' } })
  117. },
  118. {
  119. name: 'Calificaciones Finales',
  120. icon: 'fas fa-chart-simple',
  121. color: 'info',
  122. url: '/export/excel.php',
  123. filename: 'calificacion.csv',
  124. click: createDownloadLink({ url: '/export/excel.php', filename: 'calificaciones_finales.csv', postData: { query: 'c-fin' } })
  125. },
  126. {
  127. name: 'Gráfica de Alumnos',
  128. icon: 'fas fa-chart-bar',
  129. color: 'dark',
  130. url: '/',
  131. click: () => {
  132. // Redirect but with a post (form data page=graph)
  133. const form = document.createElement('form');
  134. form.method = 'POST';
  135. form.action = '/';
  136. const input = document.createElement('input');
  137. input.type = 'hidden';
  138. input.name = 'page';
  139. input.value = 'graph';
  140. form.appendChild(input);
  141. document.body.appendChild(form);
  142. form.submit();
  143. }
  144. },
  145. {
  146. name: 'Guardar Snapshot',
  147. icon: 'fas fa-save',
  148. color: 'success',
  149. url: '/action/snapshot.php',
  150. click: async () => {
  151. store.loading = true;
  152. const response = await fetch('/action/snapshot.php', { method: 'POST' });
  153. const data = await response.json();
  154. store.loading = false;
  155. if (data.success) {
  156. store.alert = { type: 'success', message: data.message };
  157. } else {
  158. store.alert = { type: 'danger', message: data.message };
  159. }
  160. }
  161. },
  162. {
  163. name: 'Usuarios Registrados',
  164. icon: 'fas fa-users',
  165. url: '/export/excel.php',
  166. filename: 'usuarios.csv',
  167. opciones: [
  168. // radio button
  169. { type: 'radio', input_class: "form-check-input", label_class: "form-check-label", name: 'query', label: 'Alumno', value: 'al' },
  170. { type: 'radio', input_class: "form-check-input", label_class: "form-check-label", name: 'query', label: 'Usuarios temporales', value: 'usr-temp' },
  171. { type: 'radio', input_class: "form-check-input", label_class: "form-check-label", name: 'query', label: 'Todos los usuarios', value: 'usr' },
  172. ],
  173. click: async function () {
  174. option.modal = this.name;
  175. try {
  176. await new Promise((resolve, reject) => {
  177. option.closeModal = () => {
  178. option.modal = false;
  179. reject(new Error("Modal closed by user"));
  180. };
  181. option.submitModal = resolve;
  182. });
  183. store.loading = true;
  184. option.modal = false;
  185. let formData = { query: 'usuarios' };
  186. document.querySelectorAll('input[name="query"]').forEach(input => {
  187. if (input.checked) formData.query = input.value;
  188. });
  189. await createDownloadLink({ url: this.url, filename: this.filename, postData: formData })();
  190. } catch (error) {
  191. console.error(error);
  192. } finally {
  193. store.loading = false;
  194. }
  195. }
  196. },
  197. {
  198. name: 'Reporte Syllabus Plan de Cátedra',
  199. icon: 'fas fa-file-invoice',
  200. url: '/export/gema.php',
  201. filename: 'syllabus_plan_catedra.csv',
  202. opciones: [
  203. { type: 'list', name: 'periodo_id', label: 'Periodo de GEMA', value: null, datalist: [] },
  204. ],
  205. mounted: async function () {
  206. this.opciones.find(item => item.name === 'periodo_id').datalist = await fetchOptions('/fetch/periodos.php', { nombre: 'Periodo_desc', id: 'Periodo_id' });
  207. },
  208. click: async function () {
  209. option.modal = this.name;
  210. await this.mounted();
  211. try {
  212. await new Promise((resolve, reject) => {
  213. option.closeModal = () => {
  214. option.modal = false;
  215. reject(new Error("Modal closed by user"));
  216. };
  217. option.submitModal = resolve;
  218. });
  219. store.loading = true;
  220. option.modal = false;
  221. let formData = { query: 'cursos' };
  222. this.opciones.forEach(opt => formData[opt.name] = opt.value);
  223. await createDownloadLink({ url: this.url, filename: this.filename, postData: formData })();
  224. } catch (error) {
  225. console.error(error);
  226. } finally {
  227. store.loading = false;
  228. }
  229. }
  230. },
  231. ],
  232. get currentOptions() {
  233. const currentItem = this.menu.find(item => item.name === this.option.modal);
  234. return currentItem ? currentItem.opciones : [];
  235. },
  236. closeModal: function () {
  237. if (typeof option.closeModal === 'function') option.closeModal();
  238. },
  239. submitModal: function () {
  240. if (typeof option.submitModal === 'function') option.submitModal();
  241. },
  242. async lastSnapshot() {
  243. try {
  244. const response = await fetch('/postgrest/snapshot_calificaciones?limit=1&order=created_at.desc');
  245. const data = await response.json();
  246. this.last_snapshot = data[0]?.created_at;
  247. } catch (error) {
  248. console.error(error);
  249. }
  250. }
  251. }).mount();
  252. </script>