Add score retrieval
This commit is contained in:
parent
7201c3d02a
commit
e9c25ddd96
@ -2,8 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"time"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
@ -43,7 +43,7 @@ func init() {
|
|||||||
return APIErrorResponse{err: err}
|
return APIErrorResponse{err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
if new.Score != nil && (current.Score != nil || *new.Score != *current.Score) {
|
if new.Score != nil && (current.Score == nil || *new.Score != *current.Score) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
new.IdCorrector = &u.Id
|
new.IdCorrector = &u.Id
|
||||||
new.TimeScored = &now
|
new.TimeScored = &now
|
||||||
|
24
surveys.go
24
surveys.go
@ -4,8 +4,8 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
@ -56,7 +56,13 @@ func init() {
|
|||||||
router.GET("/api/surveys/:sid/score", apiAuthHandler(surveyAuthHandler(
|
router.GET("/api/surveys/:sid/score", apiAuthHandler(surveyAuthHandler(
|
||||||
func(s Survey, u *User, _ []byte) HTTPResponse {
|
func(s Survey, u *User, _ []byte) HTTPResponse {
|
||||||
if s.Shown || (u != nil && u.IsAdmin) {
|
if s.Shown || (u != nil && u.IsAdmin) {
|
||||||
return formatApiResponse(s.GetScore(u))
|
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 {
|
} else {
|
||||||
return APIErrorResponse{
|
return APIErrorResponse{
|
||||||
status: http.StatusForbidden,
|
status: http.StatusForbidden,
|
||||||
@ -135,8 +141,18 @@ func NewSurvey(title string, shown bool, startAvailability time.Time, endAvailab
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Survey) GetScore(u *User) (score int64, err error) {
|
func (s Survey) GetScore(u *User) (score *float64, err error) {
|
||||||
err = DBQueryRow("SELECT SUM(M.score) FROM (SELECT MAX(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 id_question) M", s.Id, u.Id).Scan(&score)
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
var questions []Question
|
||||||
|
if questions, err = s.GetQuestions(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
*score = *score / (float64(len(questions)) * 5.0)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user