admin: Display student needing help on home page

This commit is contained in:
nemunaire 2022-05-01 11:18:08 +02:00
parent 052dacae5b
commit 5fbd5af348
3 changed files with 54 additions and 0 deletions

24
help.go
View File

@ -7,6 +7,9 @@ import (
) )
func init() { func init() {
router.GET("/api/help", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse {
return formatApiResponse(getNeedHelps())
}, adminRestricted))
router.POST("/api/help", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse { router.POST("/api/help", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse {
return formatApiResponse(u.NewNeedHelp()) return formatApiResponse(u.NewNeedHelp())
}, loggedUser)) }, loggedUser))
@ -20,6 +23,27 @@ type NeedHelp struct {
DateTreated *time.Time `json:"treated,omitempty"` DateTreated *time.Time `json:"treated,omitempty"`
} }
func getNeedHelps() (nh []NeedHelp, err error) {
if rows, errr := DBQuery("SELECT id_need_help, id_user, date, comment, date_treated FROM user_need_help"); errr != nil {
return nil, errr
} else {
defer rows.Close()
for rows.Next() {
var n NeedHelp
if err = rows.Scan(&n.Id, &n.IdUser, &n.Date, &n.Comment, &n.DateTreated); err != nil {
return
}
nh = append(nh, n)
}
if err = rows.Err(); err != nil {
return
}
return
}
}
func (u *User) NewNeedHelp() (NeedHelp, error) { func (u *User) NewNeedHelp() (NeedHelp, error) {
if res, err := DBExec("INSERT INTO user_need_help (id_user, comment) VALUES (?, ?)", u.Id, ""); err != nil { if res, err := DBExec("INSERT INTO user_need_help (id_user, comment) VALUES (?, ?)", u.Id, ""); err != nil {
return NeedHelp{}, err return NeedHelp{}, err

View File

@ -51,6 +51,15 @@ export async function getUserScore(uid, survey) {
} }
} }
export async function getUserNeedingHelp() {
const res = await fetch(`api/help`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return await res.json();
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getScore(survey) { export async function getScore(survey) {
const res = await fetch(`api/surveys/${survey.id}/score`, {headers: {'Accept': 'application/json'}}) const res = await fetch(`api/surveys/${survey.id}/score`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) { if (res.status == 200) {

View File

@ -1,5 +1,6 @@
<script lang="ts"> <script lang="ts">
import { user } from '../stores/user'; import { user } from '../stores/user';
import { getUser, getUserNeedingHelp } from '../lib/users';
import SurveyList from '../components/SurveyList.svelte'; import SurveyList from '../components/SurveyList.svelte';
import ValidateSubmissions from '../components/ValidateSubmissions.svelte'; import ValidateSubmissions from '../components/ValidateSubmissions.svelte';
@ -37,6 +38,26 @@
<ValidateSubmissions /> <ValidateSubmissions />
{#if $user.is_admin}
<p class="lead">Demande d'aide&nbsp;:</p>
{#await getUserNeedingHelp()}
<span class="spinner-border spinner-border" role="status" aria-hidden="true"></span>
{:then nhs}
<ul style="columns: 2">
{#each nhs as user (user.id)}
<li>
{#await getUser(nhs.id_user)}
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
{:then u}
<a href="users/{u.id}">{u.login}</a>
{/await}
({user.date})
</li>
{/each}
</ul>
{/await}
{/if}
<p class="lead">Voici la liste des questionnaires&nbsp;:</p> <p class="lead">Voici la liste des questionnaires&nbsp;:</p>
{:else} {:else}
<p class="card-text lead"> <p class="card-text lead">