From a9bb758e99ed7c2497b7476403807a1d44a79f07 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Wed, 8 Mar 2023 04:40:03 +0100 Subject: [PATCH] Include works in allGrades --- db.go | 2 +- grades.go | 20 ++++++++++-------- surveys.go | 4 ++-- ui/src/lib/components/StudentGrades.svelte | 24 ++++++++++++++-------- ui/src/routes/grades/[promo]/+page.js | 2 +- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/db.go b/db.go index d67d311..c1a1e0c 100644 --- a/db.go +++ b/db.go @@ -273,7 +273,7 @@ CREATE TABLE IF NOT EXISTS survey_shared( return err } if _, err := db.Exec(` -CREATE VIEW IF NOT EXISTS student_scores AS SELECT T.id_user, T.id_survey, Q.id_question, MAX(R.score) AS score FROM (SELECT DISTINCT R.id_user, S.id_survey FROM survey_responses R INNER JOIN survey_quests Q ON R.id_question = Q.id_question INNER JOIN surveys S ON Q.id_survey = S.id_survey) T LEFT OUTER JOIN survey_quests Q ON T.id_survey = Q.id_survey LEFT OUTER JOIN survey_responses R ON R.id_user = T.id_user AND Q.id_question = R.id_question GROUP BY id_user, id_survey, id_question; +CREATE VIEW IF NOT EXISTS student_scores AS SELECT "survey" AS kind, T.id_user, T.id_survey AS id, Q.id_question, MAX(R.score) AS score FROM (SELECT DISTINCT R.id_user, S.id_survey FROM survey_responses R INNER JOIN survey_quests Q ON R.id_question = Q.id_question INNER JOIN surveys S ON Q.id_survey = S.id_survey) T LEFT OUTER JOIN survey_quests Q ON T.id_survey = Q.id_survey LEFT OUTER JOIN survey_responses R ON R.id_user = T.id_user AND Q.id_question = R.id_question GROUP BY id_user, kind, id, id_question UNION SELECT "work" AS kind, G.id_user, G.id_work AS id, 0 AS id_question, G.grade AS score FROM works W RIGHT OUTER JOIN user_work_grades G ON G.id_work = W.id_work GROUP BY id_user, kind, id, id_question; `); err != nil { return err } diff --git a/grades.go b/grades.go index 891ba21..4c1132b 100644 --- a/grades.go +++ b/grades.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "net/http" "strconv" @@ -81,28 +82,29 @@ func gradeHandler(c *gin.Context) { } } -func GetAllGrades() (scores map[int64]map[int64]*float64, err error) { - if rows, errr := DBQuery("SELECT id_user, id_survey, SUM(score)/COUNT(*) FROM student_scores GROUP BY id_user, id_survey"); errr != nil { +func GetAllGrades() (scores map[int64]map[string]*float64, err error) { + if rows, errr := DBQuery("SELECT id_user, kind, id, SUM(score)/COUNT(*) FROM student_scores GROUP BY id_user, kind, id"); errr != nil { return nil, errr } else { defer rows.Close() - scores = map[int64]map[int64]*float64{} + scores = map[int64]map[string]*float64{} for rows.Next() { var id_user int64 - var id_survey int64 + var kind string + var id int64 var score *float64 - if err = rows.Scan(&id_user, &id_survey, &score); err != nil { + if err = rows.Scan(&id_user, &kind, &id, &score); err != nil { return } if scores[id_user] == nil { - scores[id_user] = map[int64]*float64{} + scores[id_user] = map[string]*float64{} } - scores[id_user][id_survey] = score + scores[id_user][fmt.Sprintf("%c.%d", kind[0], id)] = score } if err = rows.Err(); err != nil { return @@ -113,7 +115,7 @@ func GetAllGrades() (scores map[int64]map[int64]*float64, err error) { } func (s Survey) GetGrades() (scores map[int64]*float64, err error) { - if rows, errr := DBQuery("SELECT id_question, SUM(score)/COUNT(*) FROM student_scores WHERE id_survey=? GROUP BY id_question", s.Id); errr != nil { + if rows, errr := DBQuery("SELECT id_question, SUM(score)/COUNT(*) FROM student_scores WHERE kind = 'survey' AND id=? GROUP BY id_question", s.Id); errr != nil { return nil, errr } else { defer rows.Close() @@ -138,7 +140,7 @@ func (s Survey) GetGrades() (scores map[int64]*float64, err error) { } func (s Survey) GetUserGrades(u *User) (scores map[int64]*float64, err error) { - if rows, errr := DBQuery("SELECT id_question, MAX(score) FROM student_scores WHERE id_survey=? AND id_user = ? GROUP BY id_question", s.Id, u.Id); errr != nil { + if rows, errr := DBQuery("SELECT id_question, MAX(score) FROM student_scores WHERE kind = 'survey' AND id=? AND id_user = ? GROUP BY id_question", s.Id, u.Id); errr != nil { return nil, errr } else { defer rows.Close() diff --git a/surveys.go b/surveys.go index 822703f..4859423 100644 --- a/surveys.go +++ b/surveys.go @@ -342,7 +342,7 @@ func (s Survey) GetScore(u *User) (score *float64, err error) { if ok { score = v } else { - err = DBQueryRow("SELECT SUM(score)/COUNT(*) FROM student_scores WHERE id_survey=? AND id_user=?", s.Id, u.Id).Scan(&score) + err = DBQueryRow("SELECT SUM(score)/COUNT(*) FROM student_scores WHERE kind = 'survey' AND id=? AND id_user=?", s.Id, u.Id).Scan(&score) if score != nil { *score = *score / 5.0 } @@ -355,7 +355,7 @@ func (s Survey) GetScore(u *User) (score *float64, err error) { } func (s Survey) GetScores() (scores map[int64]*float64, err error) { - if rows, errr := DBQuery("SELECT id_user, SUM(score)/COUNT(*) FROM student_scores WHERE id_survey=? GROUP BY id_user", s.Id); errr != nil { + if rows, errr := DBQuery("SELECT id_user, SUM(score)/COUNT(*) FROM student_scores WHERE kind = 'survey' AND id_survey=? GROUP BY id_user", s.Id); errr != nil { return nil, errr } else { defer rows.Close() diff --git a/ui/src/lib/components/StudentGrades.svelte b/ui/src/lib/components/StudentGrades.svelte index 7843c5f..16cd4b4 100644 --- a/ui/src/lib/components/StudentGrades.svelte +++ b/ui/src/lib/components/StudentGrades.svelte @@ -20,7 +20,7 @@ Notes -{#await getSurveys()} +{#await getSurveys(true)}
Chargement des questionnaires corrigés… @@ -38,9 +38,15 @@ ID Login - {#each surveys as survey (survey.id)} - {#if survey.corrected && (promo === null || survey.promo == promo)} - {survey.title} + {#each surveys as survey} + {#if survey.corrected && (!promo || survey.promo == promo)} + + {#if survey.kind == "survey"} + {survey.title} + {:else} + {survey.title} + {/if} + {/if} {/each} @@ -57,13 +63,15 @@ {:then users} {#each users as user (user.id)} - {#if promo === null || user.promo === promo} + {#if !promo || user.promo == promo} {user.id} {user.login} - {#each surveys as survey (survey.id)} - {#if survey.corrected && (promo === null || survey.promo == promo)} - {grades[user.id] && grades[user.id][survey.id]?grades[user.id][survey.id]:""} + {#each surveys as survey} + {#if survey.corrected && (!promo || survey.promo == promo)} + + {grades[user.id] && grades[user.id][survey.kind + "." + survey.id]?grades[user.id][survey.kind + "." + survey.id]:""} + {/if} {/each} diff --git a/ui/src/routes/grades/[promo]/+page.js b/ui/src/routes/grades/[promo]/+page.js index eab131c..9612920 100644 --- a/ui/src/routes/grades/[promo]/+page.js +++ b/ui/src/routes/grades/[promo]/+page.js @@ -1,5 +1,5 @@ export async function load({ params }) { return { - promo: params.promo, + promo: parseInt(params.promo), }; }