host.html 7.6 KB

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