projektwoche-schueler.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <input type="checkbox" bind:checked={nur_irrlaeufer}> Nur unvermittelte Schüler anzeigen.
  2. <div class="columns">
  3. <div class="column is-two-thirds">
  4. {#each Object.entries(schueler) as [klasse, schuelers]}
  5. <h3 class="title">{klasse}</h3>
  6. <h3 class="subtitle has-background-warning">{unterwegs(klasse)}</h3>
  7. <table class="table is-striped">
  8. <thead>
  9. <tr>
  10. <th>Name</th>
  11. <th>Vorname</th>
  12. <th>Projektwahl</th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. {#each schuelers.filter(s=>(nur_irrlaeufer && wahl(s) !== 'success') || (!nur_irrlaeufer && s)) as s}
  17. <tr on:click={() => modalset(s)} style="cursor: pointer">
  18. <td>{s.Name}</td>
  19. <td>{s.Vorname}</td>
  20. <td class={'has-background-'+wahl(s)}>
  21. {`Woche: ${s.woche == null ? '–' : s.woche} Mo/Di: ${s.mo_di == null ? '–' : s.mo_di} Mi/Do ${s.mi_do == null ? '–' : s.mi_do}`}
  22. </td>
  23. </tr>
  24. {/each}
  25. </tbody>
  26. </table>
  27. {/each}
  28. </div>
  29. <div class="column">
  30. <h3 class="title">Teilnehmerzahlen</h3>
  31. Ganze Woche: {anwesend_wo}
  32. <br>Nur Mo/Di: {anwesend_modi}
  33. <br>Nur Mi/Do: {anwesend_mido}
  34. <h3 class="title">Projektplätze</h3>
  35. Ganze Woche: {belegt_wo}/{plaetze_wo}
  36. <br>Mo/Di: {belegt_modi}/{plaetze_modi}
  37. <br>Mi/Do: {belegt_mido}/{plaetze_mido}
  38. <h3 class="title">Klassen</h3>
  39. <table class="table is-striped">
  40. <thead>
  41. <tr>
  42. <th>Klasse</th>
  43. <th>Woche</th>
  44. <th>Mo/Di</th>
  45. <th>Mi/Do</th>
  46. </tr>
  47. </thead>
  48. <tbody>
  49. {#each klassen && Object.entries(klassen) as [klasse, abwesenheit]}
  50. <tr>
  51. <td>{klasse}</td>
  52. <td class={`has-text-${abwesenheit === 0 ? 'danger':'success'}`} style="cursor: pointer;" on:click={() => switcher(klasse,0)}>{abwesenheit === 0 ? '✘':'✔'}</td>
  53. <td class={`has-text-${abwesenheit === 1 ? 'danger':'success'}`} style="cursor: pointer;" on:click={() => switcher(klasse,1)}>{abwesenheit === 1 ? '✘':'✔'}</td>
  54. <td class={`has-text-${abwesenheit === 2 ? 'danger':'success'}`} style="cursor: pointer;" on:click={() => switcher(klasse,2)}>{abwesenheit === 2 ? '✘':'✔'}</td>
  55. </tr>
  56. {/each}
  57. </tbody>
  58. </table>
  59. </div>
  60. </div>
  61. {#if modal_s}
  62. <div class="modal" class:is-active={modal}>
  63. <div class="modal-background"></div>
  64. <div class="modal-card">
  65. <header class="modal-card-head">
  66. <p class="modal-card-title">{modal_s.Vorname} {modal_s.Name}</p>
  67. <button class="delete" aria-label="close" on:click={() => modal = !modal}></button>
  68. </header>
  69. <section class="modal-card-body">
  70. <div class="field is-horizontal">
  71. <div class="field-label is-normal">
  72. <label class="label">Woche</label>
  73. </div>
  74. <div class="field-body">
  75. <div class="field is-narrow">
  76. <div class="control">
  77. <div class="select is-fullwidth">
  78. <select bind:value={selected_woche} on:change={wahl_aendern}>
  79. <option value={null} selected={modal_s.woche === null}>Kein Projekt gewählt</option>
  80. {#each projekte.filter(p => p.woche) as p}
  81. <option value={p.id} selected={modal_s.woche === p.id}>{`${p.titel} – ${p.leitung} (${p.gewaehlt_woche}/${p.max_teilnehmer}`})</option>
  82. {/each}
  83. <option value={0} selected={modal_s.woche === 0}>Krank/Beurlaubt</option>
  84. </select>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. <div class="field is-horizontal">
  91. <div class="field-label is-normal">
  92. <label class="label">Mo/Di</label>
  93. </div>
  94. <div class="field-body">
  95. <div class="field is-narrow">
  96. <div class="control">
  97. <div class="select is-fullwidth">
  98. <select bind:value={selected_mo_di} on:change={wahl_aendern}>
  99. <option value={null}>Kein Projekt gewählt</option>
  100. {#each projekte.filter(p => p.mo_di) as p}
  101. <option value={p.id} selected={modal_s.mo_di === p.id}>{`${p.titel} – ${p.leitung} (${p.gewaehlt_mo_di}/${p.max_teilnehmer}`})</option>
  102. {/each}
  103. <option value={0} selected={modal_s.mo_di === 0}>Krank/Beurlaubt</option>
  104. </select>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. </div>
  110. <div class="field is-horizontal">
  111. <div class="field-label is-normal">
  112. <label class="label">Mi/Do</label>
  113. </div>
  114. <div class="field-body">
  115. <div class="field is-narrow">
  116. <div class="control">
  117. <div class="select is-fullwidth">
  118. <select bind:value={selected_mi_do} on:change={wahl_aendern}>
  119. <option value={null}>Kein Projekt gewählt</option>
  120. {#each projekte.filter(p => p.mi_do) as p}
  121. <option value={p.id} selected={modal_s.mi_do}>
  122. {`${p.titel} – ${p.leitung} (${p.gewaehlt_mi_do}/${p.max_teilnehmer}`})
  123. </option>
  124. {/each}
  125. <option value={0} selected={modal_s.mi_do === 0}>Krank/Beurlaubt</option>
  126. </select>
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. </div>
  132. </section>
  133. <footer class="modal-card-foot">
  134. Änderungen sind sofort wirksam
  135. </footer>
  136. </div>
  137. </div>
  138. {/if}
  139. <script>
  140. export let schueler, privat
  141. let nur_irrlaeufer = true
  142. let projekte = [];
  143. let klassen = {}
  144. Object.keys(schueler).forEach(k => klassen[k] = null)
  145. let modal = false, modal_s, selected_woche, selected_mo_di, selected_mi_do;
  146. function modalset (s) {
  147. modal = true
  148. modal_s=s
  149. }
  150. const { Pool } = R('pg')
  151. const _ = R('lodash')
  152. const postgres = new Pool({ connectionString: privat.mein_bk_db})
  153. function projekte_laden () {
  154. postgres.query(`SELECT id, titel, leitung, woche, mo_di, mi_do, max_teilnehmer,
  155. (SELECT COUNT(*)
  156. FROM wahlen
  157. WHERE projekte.id = wahlen.woche
  158. ) as gewaehlt_woche,
  159. (SELECT COUNT(*)
  160. FROM wahlen
  161. WHERE projekte.id = wahlen.mo_di
  162. ) as gewaehlt_mo_di,
  163. (SELECT COUNT(*)
  164. FROM wahlen
  165. WHERE projekte.id = wahlen.mi_do
  166. ) as gewaehlt_mi_do
  167. FROM projekte`,
  168. (err, resp) => projekte = resp.rows)
  169. }
  170. projekte_laden()
  171. postgres.query(`SELECT * FROM klassen ORDER BY klasse`,
  172. (err, resp) => resp.rows.forEach(k => { klassen[k.klasse] = k.abwesenheit }))
  173. $: wahl = s => {
  174. const abwesenheit = klassen[s.Klasse]
  175. if (abwesenheit === null) {
  176. if (s.woche === s.mo_di && s.woche === s.mi_do) return 'danger'
  177. else if (typeof(s.mo_di) !== typeof(s.mi_do)) return 'warning'
  178. else if ([s.mo_di, s.mi_do, s.woche].includes(0)) return 'info'
  179. else return 'success'
  180. } else if (abwesenheit === 0) {
  181. if (s.woche === s.mo_di && s.woche === s.mi_do) return 'success'
  182. else return 'info'
  183. } else if (abwesenheit === 1) {
  184. if (s.woche === s.mo_di && s.woche === s.mi_do) return 'danger'
  185. else if (typeof(s.woche) === 'number' && (typeof(s.mo_di) === 'number'|| typeof(s.mi_do) === 'number')) return 'info'
  186. else if (typeof(s.mi_do) === 'number') return 'success'
  187. else if (s.mo_di === 0 || s.woche === 0) return 'warning'
  188. else return 'danger'
  189. } else if (abwesenheit === 2) {
  190. if (s.woche === s.mo_di && s.woche === s.mi_do) return 'danger'
  191. else if (typeof(s.woche) === 'number' && (typeof(s.mo_di) === 'number'|| typeof(s.mi_do) === 'number')) return 'info'
  192. else if (typeof(s.mo_di) === 'number') return 'success'
  193. else if (s.mi_do === 0 || s.woche === 0) return 'warning'
  194. else return 'danger'
  195. }
  196. }
  197. async function wahl_aendern () {
  198. const text = `INSERT INTO wahlen(schueler_id, woche, mo_di, mi_do)
  199. VALUES($1, $2, $3, $4)
  200. ON CONFLICT (schueler_id) DO
  201. UPDATE SET woche=$2, mo_di=$3, mi_do=$4
  202. RETURNING *`
  203. const values = [modal_s.id, selected_woche, selected_mo_di, selected_mi_do]
  204. try {
  205. const res = await postgres.query(text, values)
  206. const data = res.rows[0]
  207. projekte_laden()
  208. Object.assign(modal_s, data)
  209. schueler = schueler
  210. } catch(err) {
  211. console.log(err.stack)
  212. }
  213. }
  214. async function switcher(k, art) {
  215. const abwesenheit = klassen[k] === null || klassen[k] !== art ? art : null
  216. const text = `INSERT INTO klassen(klasse, abwesenheit)
  217. VALUES($1, $2)
  218. ON CONFLICT (klasse) DO
  219. UPDATE SET abwesenheit = $2
  220. WHERE klassen.klasse = $1
  221. RETURNING abwesenheit`
  222. const values = [k, abwesenheit]
  223. try {
  224. const res = await postgres.query(text, values)
  225. const data = res.rows[0]
  226. klassen[k] = data.abwesenheit
  227. } catch(err) {
  228. console.log(err.stack)
  229. }
  230. }
  231. $: unterwegs = klasse => {
  232. if (!klassen) return ''
  233. let out
  234. switch (klassen[klasse]) {
  235. case null: return ''; break;
  236. case 0: out = 'Montag bis Freitag'; break;
  237. case 1: out = 'Montag und Dienstag'; break;
  238. case 2: out = 'Mittwoch und Donnerstag'; break;
  239. }
  240. return `Die Klasse ist ${out} unterwegs`
  241. }
  242. let anwesend_wo, anwesend_modi, anwesend_mido
  243. $:{
  244. anwesend_wo = 0, anwesend_modi = 0, anwesend_mido = 0
  245. Object.entries(schueler).forEach(([k,v]) => {
  246. const nr = v.length
  247. switch (klassen[k]) {
  248. case null: anwesend_wo += nr; break;
  249. case 1: anwesend_mido += nr; break;
  250. case 2: anwesend_modi += nr; break;
  251. }
  252. })
  253. }
  254. let plaetze_wo, plaetze_modi, plaetze_mido
  255. let belegt_wo, belegt_modi, belegt_mido
  256. $:{
  257. plaetze_wo = 0, plaetze_modi = 0, plaetze_mido = 0
  258. belegt_wo = 0, belegt_modi = 0, belegt_mido = 0
  259. projekte.forEach(p => {
  260. p.woche && (plaetze_wo += p.max_teilnehmer)
  261. p.mo_di && (plaetze_modi += p.max_teilnehmer)
  262. p.mi_do && (plaetze_mido += p.max_teilnehmer)
  263. belegt_wo += parseInt(p.gewaehlt_woche)
  264. belegt_modi += parseInt(p.gewaehlt_mo_di)
  265. belegt_mido += parseInt(p.gewaehlt_mi_do)
  266. })
  267. }
  268. </script>