Include works in allGrades

This commit is contained in:
nemunaire 2023-03-08 04:40:03 +01:00
commit a9bb758e99
5 changed files with 31 additions and 21 deletions

View file

@ -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()