Add users and grades display
This commit is contained in:
parent
42b15a1546
commit
c9d64640e2
13 changed files with 432 additions and 17 deletions
52
surveys.go
52
surveys.go
|
@ -70,6 +70,26 @@ func init() {
|
|||
}
|
||||
}
|
||||
}), loggedUser))
|
||||
router.GET("/api/users/:uid/surveys/:sid/score", 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.GetScore(&u); err != nil {
|
||||
return APIErrorResponse{err: err}
|
||||
} else if score == nil {
|
||||
return APIResponse{map[string]string{"score": "N/A"}}
|
||||
} else {
|
||||
return APIResponse{map[string]float64{"score": *score}}
|
||||
}
|
||||
} else {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusForbidden,
|
||||
err: errors.New("Not accessible"),
|
||||
}
|
||||
}
|
||||
})(ps, body)
|
||||
})(uauth, ps, body)
|
||||
}, loggedUser))
|
||||
}
|
||||
|
||||
func surveyHandler(f func(Survey, []byte) HTTPResponse) func(httprouter.Params, []byte) HTTPResponse {
|
||||
|
@ -142,17 +162,35 @@ func NewSurvey(title string, shown bool, startAvailability time.Time, endAvailab
|
|||
}
|
||||
|
||||
func (s Survey) GetScore(u *User) (score *float64, err error) {
|
||||
if err = DBQueryRow("SELECT SUM(M.score) FROM (SELECT MAX(score) AS score FROM survey_responses R INNER JOIN survey_quests Q ON Q.id_question = R.id_question WHERE Q.id_survey=? AND R.id_user=? GROUP BY Q.id_question) M", s.Id, u.Id).Scan(&score); err != nil || score == nil {
|
||||
return
|
||||
err = DBQueryRow("SELECT SUM(score)/COUNT(*) FROM student_scores WHERE id_survey=? AND id_user=?", s.Id, u.Id).Scan(&score)
|
||||
if score != nil {
|
||||
*score = *score / 5.0
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var questions []Question
|
||||
if questions, err = s.GetQuestions(); err != nil {
|
||||
return
|
||||
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); err != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
scores = map[int64]*float64{}
|
||||
|
||||
for rows.Next() {
|
||||
var id_user int64
|
||||
var score *float64
|
||||
|
||||
if err = rows.Scan(&id_user, &score); err != nil {
|
||||
return
|
||||
}
|
||||
scores[id_user] = score
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
*score = *score / (float64(len(questions)) * 5.0)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue