New form to change grade

This commit is contained in:
nemunaire 2023-03-07 03:19:28 +01:00
parent b604e98f64
commit 7642a23947
3 changed files with 80 additions and 2 deletions

View File

@ -1,6 +1,7 @@
<script>
import { createEventDispatcher } from 'svelte';
import ScoreBadge from '$lib/components/ScoreBadge.svelte';
import { ToastsStore } from '$lib/stores/toasts';
const dispatch = createEventDispatcher();
@ -9,6 +10,8 @@
let gradationStatus = {};
let stats = {"mean": 0, "min": 999, "max": 0};
let chgrade = {grade: null, modal: null};
$: refresh_grades(work);
function refresh_grades(w) {
@ -126,9 +129,9 @@
<button
class="btn btn-sm mr-1"
class:btn-success={status.status == "success"}
class:btn-danger={status.status == "failure"}
class:btn-danger={status.status == "failure"}
class:btn-outline-danger={status.status == "killed"}
class:btn-outline-warning={status.status == "pending" || status.status == "running"}
class:btn-outline-warning={status.status == "pending" || status.status == "running"}
title="Relancer la notation"
on:click={() => { grade.redoGradation(); gradationStatus[grade.id] = null; }}
>
@ -136,6 +139,13 @@
</button>
{/await}
{/if}
<button
class="btn btn-sm btn-primary mr-1"
title="Changer la note"
on:click={() => { chgrade = { grade, modal: new bootstrap.Modal(document.getElementById('chgradeModal'))}; chgrade.modal.show(); }}
>
<i class="bi bi-pencil"></i>
</button>
<button
class="btn btn-sm btn-danger mr-1"
title="Supprimer la note"
@ -151,3 +161,28 @@
</table>
{/await}
</div>
<div class="modal fade" tabindex="-1" id="chgradeModal">
<div class="modal-dialog">
<form class="modal-content" on:submit|preventDefault={() => {chgrade.modal.hide(); try { chgrade.grade.save().then(() => refresh_grades(work)); } catch(err) { ToastsStore.addToast({color: "danger", title: "Impossible de changer la note", msg: err}) };}}>
<div class="modal-header">
<h5 class="modal-title">Changer la note {#if chgrade.grade}de {chgrade.grade.login}{/if}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="form-group row mb-2">
<label class="col-2 col-form-label" for="new-grade">Note</label>
<!-- svelte-ignore a11y-autofocus -->
{#if chgrade.grade}
<input type="number" class="form-control col" id="new-grade" autofocus placeholder="15" bind:value={chgrade.grade.score}>
{/if}
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">
Changer la note
</button>
</div>
</form>
</div>
</div>

View File

@ -15,6 +15,21 @@ export class Grade {
this.comment = comment;
}
async save() {
const res = await fetch(`api/works/${this.id_work}/grades/${this.id}`, {
method: 'PUT',
headers: {'Accept': 'application/json'},
body: JSON.stringify(this),
});
if (res.status == 200) {
const data = await res.json()
this.update(data);
return data;
} else {
throw new Error((await res.json()).errmsg);
}
}
async delete() {
if (this.id) {
const res = await fetch(`api/works/${this.id_work}/grades/${this.id}`, {

View File

@ -251,6 +251,26 @@ func declareAPIAdminWorksRoutes(router *gin.RouterGroup) {
gradesRoutes := worksRoutes.Group("/grades/:gid")
gradesRoutes.Use(gradeHandler)
gradesRoutes.PUT("", func(c *gin.Context) {
current := c.MustGet("grade").(*WorkGrade)
var new WorkGrade
if err := c.ShouldBindJSON(&new); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
new.Id = current.Id
grade, err := new.Update()
if err != nil {
log.Println("Unable to Update grade:", err)
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during grade update."})
return
}
c.JSON(http.StatusOK, grade)
})
gradesRoutes.DELETE("", func(c *gin.Context) {
g := c.MustGet("grade").(*WorkGrade)
@ -654,6 +674,14 @@ type WorkGrade struct {
Comment string `json:"comment,omitempty"`
}
func (g *WorkGrade) Update() (*WorkGrade, error) {
if _, err := DBExec("UPDATE user_work_grades SET id_user = ?, id_work = ?, date = ?, grade = ?, comment = ? WHERE id_gradation = ?", g.IdUser, g.IdWork, g.Date, g.Grade, g.Comment, g.Id); err != nil {
return nil, err
} else {
return g, err
}
}
func (g WorkGrade) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM user_work_grades WHERE id_gradation = ?", g.Id); err != nil {
return 0, err