Display survey mean to admin

This commit is contained in:
nemunaire 2022-11-18 16:11:24 +01:00
parent d6f620bc0d
commit d7f679ce84

View File

@ -77,11 +77,12 @@ func declareAPIAuthSurveysRoutes(router *gin.RouterGroup) {
surveysRoutes.Use(surveyHandler)
surveysRoutes.GET("/score", func(c *gin.Context) {
loggedUser := c.MustGet("LoggedUser").(*User)
var u *User
if user, ok := c.Get("user"); ok {
u = user.(*User)
} else {
u = c.MustGet("LoggedUser").(*User)
u = loggedUser
}
s := c.MustGet("survey").(*Survey)
@ -93,10 +94,29 @@ func declareAPIAuthSurveysRoutes(router *gin.RouterGroup) {
return
}
if score == nil {
c.JSON(http.StatusOK, map[string]string{"score": "N/A"})
} else {
if score != nil {
c.JSON(http.StatusOK, map[string]float64{"score": *score})
} else if _, ok := c.Get("user"); !ok && loggedUser.IsAdmin {
// Admin retrieve mean score
scores, err := s.GetGrades()
if err != nil {
log.Printf("Unable to GetGrades(sid=%d): %s", s.Id, err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs when trying to retrieve grades."})
return
}
*score = 0
nbGrades := 0
for _, s := range scores {
*score += *s
nbGrades += 1
}
*score /= float64(nbGrades)
c.JSON(http.StatusOK, map[string]float64{"score": *score})
} else {
c.JSON(http.StatusOK, map[string]string{"score": "N/A"})
}
} else {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Not accessible"})