Add users and grades display
This commit is contained in:
parent
42b15a1546
commit
c9d64640e2
13 changed files with 432 additions and 17 deletions
128
grades.go
Normal file
128
grades.go
Normal file
|
@ -0,0 +1,128 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/users/:uid/surveys/:sid/grades", apiAuthHandler(func(uauth *User, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
return surveyAuthHandler(func(s Survey, uauth *User, _ []byte) HTTPResponse {
|
||||
return userHandler(func(u User, _ []byte) HTTPResponse {
|
||||
if uauth != nil && ((s.Shown && u.Id == uauth.Id) || uauth.IsAdmin) {
|
||||
if score, err := s.GetUserGrades(&u); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if score == nil {
|
||||
return APIResponse{"N/A"}
|
||||
} else {
|
||||
return APIResponse{score}
|
||||
}
|
||||
} else {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusForbidden,
|
||||
err: errors.New("Not accessible"),
|
||||
}
|
||||
}
|
||||
})(ps, body)
|
||||
})(uauth, ps, body)
|
||||
}, loggedUser))
|
||||
router.GET("/api/grades", apiAuthHandler(func(uauth *User, ps httprouter.Params, body []byte) HTTPResponse {
|
||||
if uauth != nil && uauth.IsAdmin {
|
||||
if score, err := GetAllGrades(); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if score == nil {
|
||||
return APIResponse{"N/A"}
|
||||
} else {
|
||||
return APIResponse{score}
|
||||
}
|
||||
} else {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusForbidden,
|
||||
err: errors.New("Not accessible"),
|
||||
}
|
||||
}
|
||||
}, adminRestricted))
|
||||
}
|
||||
|
||||
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"); err != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
scores = map[int64]map[int64]*float64{}
|
||||
|
||||
for rows.Next() {
|
||||
var id_user int64
|
||||
var id_survey int64
|
||||
var score *float64
|
||||
|
||||
if err = rows.Scan(&id_user, &id_survey, &score); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if scores[id_user] == nil {
|
||||
scores[id_user] = map[int64]*float64{}
|
||||
}
|
||||
|
||||
scores[id_user][id_survey] = score
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
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); err != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
scores = map[int64]*float64{}
|
||||
|
||||
for rows.Next() {
|
||||
var id_question int64
|
||||
var score *float64
|
||||
|
||||
if err = rows.Scan(&id_question, &score); err != nil {
|
||||
return
|
||||
}
|
||||
scores[id_question] = score
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
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); err != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
scores = map[int64]*float64{}
|
||||
|
||||
for rows.Next() {
|
||||
var id_question int64
|
||||
var score *float64
|
||||
|
||||
if err = rows.Scan(&id_question, &score); err != nil {
|
||||
return
|
||||
}
|
||||
scores[id_question] = score
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in a new issue