host.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <main class="container mt-5" v-if="page === 'host'" v-scope @vue:mounted="mounted">
  2. <h1 class="mb-4">Conectar un HOST</h1>
  3. <form action="action/conectar_moodle.php" method="post" v-scope="{host: null}" autocomplete="off">
  4. <div v-if="hosts.length > 0">
  5. <div class="input-group mb-3">
  6. <div class="input-group-prepend">
  7. <label for="moodle-host" class="input-group-text">Moodle host</label>
  8. </div>
  9. <input class="form-control" list="moodle-hosts" id="moodle-host" name="moodle-host"
  10. placeholder="Seleccione o ingrese un Moodle host" required v-model="selectedHost">
  11. <datalist id="moodle-hosts">
  12. <option v-for="host in hosts" :value="host.host">{{ host.etiqueta }}</option>
  13. </datalist>
  14. <button id="limpiar-host" type="reset" class="btn btn-secondary" @click="selectedHost = null">
  15. <i class="fas fa-eraser"></i>
  16. </button>
  17. <button id="agregar-host" type="button" class="btn btn-success"
  18. @click="page = 'current_host'; selectedHost = null">
  19. <i class="fas fa-plus"></i>
  20. </button>
  21. <button type="button" class="btn btn-info"
  22. :disabled="hosts.filter(host => host.host === selectedHost).length === 0"
  23. @click="page = 'current_host'">
  24. <i class="fas fa-pencil-alt"></i>
  25. </button>
  26. </div>
  27. </div>
  28. <div v-else class="mb-3">
  29. <p>No hay hosts registrados</p>
  30. </div>
  31. <div class="d-grid gap-2">
  32. <button type="submit" class="btn btn-primary"
  33. :disabled="hosts.filter(host => host.host === selectedHost).length === 0">
  34. <i class="fas fa-plug"></i> Conectar
  35. </button>
  36. </div>
  37. </form>
  38. </main>
  39. <div v-else @vue:mounted="selectHost">
  40. <main class="container mt-5">
  41. <h1>Registrar un nuevo HOST de Moodle</h1>
  42. <button class="btn btn-primary mb-4" @click="page = 'host'">
  43. <i class="fas fa-arrow-left"></i> Regresar
  44. </button>
  45. <form action="/action/new_host.php" method="post" @submit.prevent="newHost" id="new_host">
  46. <div class="mb-3">
  47. <label for="etiqueta" class="form-label">Etiqueta</label>
  48. <input type="text" name="etiqueta" class="form-control" placeholder="Etiqueta para identificar el host"
  49. required v-model="current_host.etiqueta" autocomplete="off">
  50. <div class="form-text">Etiqueta para identificar: <code>Moodle2023A</code></div>
  51. </div>
  52. <div class="mb-3">
  53. <label for="base_datos" class="form-label">Base de datos</label>
  54. <input type="text" name="base_datos" class="form-control" placeholder="Nombre de la base de datos"
  55. required v-model="current_host.postgres_dbname">
  56. <div class="form-text">Ejemplo: <code>moodle42licdb</code></div>
  57. </div>
  58. <div class="mb-3">
  59. <label for="host" class="form-label">Host de Moodle</label>
  60. <input type="text" name="host" class="form-control" placeholder="200.13.89.000" required
  61. v-model="current_host.host">
  62. <div class="form-text">localhost, moodleXYZ.lci.ulsa.mx, 200.13.89.000</div>
  63. </div>
  64. <div class="mb-3">
  65. <label for="puerto" class="form-label">Puerto de la base de datos</label>
  66. <input type="text" name="puerto" class="form-control" placeholder="5432" required pattern="[0-9]+"
  67. v-model="current_host.puerto">
  68. </div>
  69. <div class="mb-3">
  70. <label for="usuario" class="form-label">Usuario de Postgres</label>
  71. <input type="text" name="usuario" class="form-control" placeholder="postgres" required
  72. v-model="current_host.postgres_user">
  73. </div>
  74. <div class="mb-3">
  75. <label for="password" class="form-label">Contraseña de Postgres</label>
  76. <input type="password" name="password" class="form-control"
  77. placeholder="Contraseña del usuario postgres" required v-model="current_host.postgres_password">
  78. </div>
  79. <div class="mb-3">
  80. <label for="periodos" class="form-label">Periodos de GEMA</label>
  81. <select id="periodos" name="periodos" class="form-select" multiple required
  82. v-model="current_host.periodos_gema">
  83. <option v-for="periodo in periodos" :value="periodo.Periodo_id"
  84. :selected="current_host.periodos_gema.includes(periodo.Periodo_id)">
  85. {{ periodo.Periodo_desc }} de {{ periodo.Nivel_desc }}
  86. </option>
  87. </select>
  88. </div>
  89. <div class="d-grid gap-2">
  90. <button type="submit" class="btn btn-primary">
  91. <i class="fas fa-database"></i> Registrar
  92. </button>
  93. </div>
  94. </form>
  95. </main>
  96. </div>
  97. <script>
  98. PetiteVue.createApp({
  99. store,
  100. // state
  101. page: 'host',
  102. // data
  103. hosts: [],
  104. periodos: [],
  105. selectedHost: null,
  106. current_host: {},
  107. reset() {
  108. this.current_host = {
  109. etiqueta: null,
  110. postgres_dbname: null,
  111. host: null,
  112. puerto: 5432,
  113. postgres_user: 'postgres',
  114. periodos_gema: []
  115. }
  116. },
  117. async selectHost() {
  118. if (this.selectedHost) {
  119. const data = await store.fetch(`/postgrest/moodle_host?host=eq.${this.selectedHost}`, {
  120. headers: {
  121. 'Prefer': 'plurality=singular'
  122. }
  123. })
  124. this.current_host = data[0]
  125. this.current_host.postgres_password = '';
  126. }
  127. else {
  128. this.reset()
  129. }
  130. },
  131. async newHost() {
  132. try {
  133. const response = await fetch('/postgrest/rpc/new_host', {
  134. method: 'POST',
  135. headers: {
  136. 'Content-Type': 'application/json',
  137. Authorization: `Bearer ${store.token}`
  138. },
  139. body: JSON.stringify({
  140. etiqueta_param: this.current_host.etiqueta,
  141. host_param: this.current_host.host,
  142. puerto_param: this.current_host.puerto,
  143. postgres_user_param: this.current_host.postgres_user,
  144. postgres_dbname_param: this.current_host.postgres_dbname,
  145. postgres_password_param: this.current_host.postgres_password,
  146. periodos_gema_param: `{${this.current_host.periodos_gema.join(',')}}`,
  147. moodle_host_id_param: this.current_host.moodle_host_id ?? null,
  148. }),
  149. })
  150. } catch (error) {
  151. store.error = {
  152. title: 'Error al registrar el host',
  153. message: 'Ocurrió un error al registrar el host',
  154. avoidable: true,
  155. }
  156. }
  157. store.error = {
  158. title: 'Host registrado',
  159. message: 'El host se ha registrado correctamente',
  160. avoidable: true,
  161. }
  162. this.reset()
  163. },
  164. async mounted() {
  165. this.reset()
  166. this.hosts = await store.fetch('/postgrest/moodle_host')
  167. console.log(this.hosts);
  168. this.periodos = await store.fetch('/fetch/periodos.php?all=true')
  169. }
  170. }).mount()
  171. </script>