2020-03-04 11:07:12 +00:00
package main
import (
"encoding/json"
"errors"
2021-03-01 16:47:00 +00:00
"fmt"
2020-03-04 11:07:12 +00:00
"net/http"
"strconv"
2022-02-21 07:53:27 +00:00
"strings"
2020-09-13 14:37:30 +00:00
"time"
2020-03-04 11:07:12 +00:00
"github.com/julienschmidt/httprouter"
)
2022-02-28 16:45:30 +00:00
var (
_score_cache = map [ int64 ] map [ int64 ] * float64 { }
)
2020-03-04 11:07:12 +00:00
func init ( ) {
router . GET ( "/api/surveys" , apiAuthHandler (
func ( u * User , _ httprouter . Params , _ [ ] byte ) HTTPResponse {
2020-03-08 01:18:32 +00:00
if u == nil {
2022-02-28 18:00:30 +00:00
return formatApiResponse ( getSurveys ( fmt . Sprintf ( "WHERE (shown = TRUE OR direct IS NOT NULL) AND NOW() > start_availability AND promo = %d ORDER BY start_availability ASC" , currentPromo ) ) )
2020-03-08 01:18:32 +00:00
} else if u . IsAdmin {
2021-03-01 17:50:39 +00:00
return formatApiResponse ( getSurveys ( "ORDER BY promo DESC, start_availability ASC" ) )
2020-03-04 11:07:12 +00:00
} else {
2022-02-28 18:00:30 +00:00
surveys , err := getSurveys ( fmt . Sprintf ( "WHERE (shown = TRUE OR direct IS NOT NULL) AND promo = %d ORDER BY start_availability ASC" , u . Promo ) )
2022-02-21 07:53:27 +00:00
if err != nil {
return APIErrorResponse { err : err }
}
var response [ ] Survey
for _ , s := range surveys {
if s . Group == "" || strings . Contains ( u . Groups , "," + s . Group + "," ) {
response = append ( response , s )
}
}
return formatApiResponse ( response , nil )
2020-03-04 11:07:12 +00:00
}
} ) )
router . POST ( "/api/surveys" , apiHandler ( func ( _ httprouter . Params , body [ ] byte ) HTTPResponse {
var new Survey
if err := json . Unmarshal ( body , & new ) ; err != nil {
2020-09-13 14:37:30 +00:00
return APIErrorResponse { err : err }
2020-03-04 11:07:12 +00:00
}
2021-03-01 16:47:00 +00:00
if new . Promo == 0 {
new . Promo = currentPromo
}
2022-02-28 18:00:30 +00:00
return formatApiResponse ( NewSurvey ( new . Title , new . Promo , new . Group , new . Shown , new . Direct , new . StartAvailability , new . EndAvailability ) )
2020-03-04 11:07:12 +00:00
} , adminRestricted ) )
router . GET ( "/api/surveys/:sid" , apiAuthHandler ( surveyAuthHandler (
func ( s Survey , u * User , _ [ ] byte ) HTTPResponse {
2022-02-21 07:53:27 +00:00
if ( s . Promo == u . Promo && ( s . Group == "" || ( u != nil && strings . Contains ( u . Groups , "," + s . Group + "," ) ) && s . Shown ) ) || ( u != nil && u . IsAdmin ) {
2020-03-04 11:07:12 +00:00
return APIResponse { s }
} else {
return APIErrorResponse {
status : http . StatusForbidden ,
2020-09-13 14:37:30 +00:00
err : errors . New ( "Not accessible" ) ,
2020-03-04 11:07:12 +00:00
}
}
} ) ) )
router . PUT ( "/api/surveys/:sid" , apiHandler ( surveyHandler ( func ( current Survey , body [ ] byte ) HTTPResponse {
var new Survey
if err := json . Unmarshal ( body , & new ) ; err != nil {
2020-09-13 14:37:30 +00:00
return APIErrorResponse { err : err }
2020-03-04 11:07:12 +00:00
}
new . Id = current . Id
2022-02-28 18:00:30 +00:00
if new . Direct != current . Direct {
if new . Direct == nil {
current . WSCloseAll ( "" )
} else if * new . Direct == 0 {
current . WSWriteAll ( WSMessage {
Action : "pause" ,
} )
} else {
current . WSWriteAll ( WSMessage {
Action : "new_question" ,
QuestionId : new . Direct ,
} )
}
}
2020-03-04 11:07:12 +00:00
return formatApiResponse ( new . Update ( ) )
} ) , adminRestricted ) )
router . DELETE ( "/api/surveys/:sid" , apiHandler ( surveyHandler (
func ( s Survey , _ [ ] byte ) HTTPResponse {
return formatApiResponse ( s . Delete ( ) )
} ) , adminRestricted ) )
2020-03-08 00:06:44 +00:00
router . GET ( "/api/surveys/:sid/score" , apiAuthHandler ( surveyAuthHandler (
func ( s Survey , u * User , _ [ ] byte ) HTTPResponse {
2021-03-01 16:47:00 +00:00
if ( s . Promo == u . Promo && s . Shown ) || ( u != nil && u . IsAdmin ) {
2020-09-13 14:37:30 +00:00
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 } }
}
2020-03-08 00:06:44 +00:00
} else {
return APIErrorResponse {
status : http . StatusForbidden ,
2020-09-13 14:37:30 +00:00
err : errors . New ( "Not accessible" ) ,
2020-03-08 00:06:44 +00:00
}
}
} ) , loggedUser ) )
2020-11-20 14:46:52 +00:00
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 {
2021-03-01 16:47:00 +00:00
if uauth != nil && ( ( s . Promo == u . Promo && s . Shown && u . Id == uauth . Id ) || uauth . IsAdmin ) {
2020-11-20 14:46:52 +00:00
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 ) )
2020-03-04 11:07:12 +00:00
}
func surveyHandler ( f func ( Survey , [ ] byte ) HTTPResponse ) func ( httprouter . Params , [ ] byte ) HTTPResponse {
return func ( ps httprouter . Params , body [ ] byte ) HTTPResponse {
if sid , err := strconv . Atoi ( string ( ps . ByName ( "sid" ) ) ) ; err != nil {
2020-09-13 14:37:30 +00:00
return APIErrorResponse { err : err }
2020-03-04 11:07:12 +00:00
} else if survey , err := getSurvey ( sid ) ; err != nil {
2020-09-13 14:37:30 +00:00
return APIErrorResponse { err : err }
2020-03-04 11:07:12 +00:00
} else {
return f ( survey , body )
}
}
}
func surveyAuthHandler ( f func ( Survey , * User , [ ] byte ) HTTPResponse ) func ( * User , httprouter . Params , [ ] byte ) HTTPResponse {
return func ( u * User , ps httprouter . Params , body [ ] byte ) HTTPResponse {
if sid , err := strconv . Atoi ( string ( ps . ByName ( "sid" ) ) ) ; err != nil {
2020-09-13 14:37:30 +00:00
return APIErrorResponse { err : err }
2020-03-04 11:07:12 +00:00
} else if survey , err := getSurvey ( sid ) ; err != nil {
2020-09-13 14:37:30 +00:00
return APIErrorResponse { err : err }
2020-03-04 11:07:12 +00:00
} else {
return f ( survey , u , body )
}
}
}
type Survey struct {
Id int64 ` json:"id" `
Title string ` json:"title" `
2021-03-01 16:47:00 +00:00
Promo uint ` json:"promo" `
2022-02-21 07:53:27 +00:00
Group string ` json:"group" `
2020-03-04 11:07:12 +00:00
Shown bool ` json:"shown" `
2022-02-28 18:00:30 +00:00
Direct * int64 ` json:"direct" `
2020-03-08 00:06:44 +00:00
Corrected bool ` json:"corrected" `
2020-03-04 11:07:12 +00:00
StartAvailability time . Time ` json:"start_availability" `
EndAvailability time . Time ` json:"end_availability" `
}
2020-09-13 14:37:30 +00:00
func getSurveys ( cnd string , param ... interface { } ) ( surveys [ ] Survey , err error ) {
2022-02-28 18:00:30 +00:00
if rows , errr := DBQuery ( "SELECT id_survey, title, promo, grp, shown, direct, corrected, start_availability, end_availability FROM surveys " + cnd , param ... ) ; errr != nil {
2020-03-04 11:07:12 +00:00
return nil , errr
} else {
defer rows . Close ( )
for rows . Next ( ) {
var s Survey
2022-02-28 18:00:30 +00:00
if err = rows . Scan ( & s . Id , & s . Title , & s . Promo , & s . Group , & s . Shown , & s . Direct , & s . Corrected , & s . StartAvailability , & s . EndAvailability ) ; err != nil {
2020-03-04 11:07:12 +00:00
return
}
surveys = append ( surveys , s )
}
if err = rows . Err ( ) ; err != nil {
return
}
return
}
}
func getSurvey ( id int ) ( s Survey , err error ) {
2022-02-28 18:00:30 +00:00
err = DBQueryRow ( "SELECT id_survey, title, promo, grp, shown, direct, corrected, start_availability, end_availability FROM surveys WHERE id_survey=?" , id ) . Scan ( & s . Id , & s . Title , & s . Promo , & s . Group , & s . Shown , & s . Direct , & s . Corrected , & s . StartAvailability , & s . EndAvailability )
2020-03-04 11:07:12 +00:00
return
}
2022-02-28 18:00:30 +00:00
func NewSurvey ( title string , promo uint , group string , shown bool , direct * int64 , startAvailability time . Time , endAvailability time . Time ) ( * Survey , error ) {
if res , err := DBExec ( "INSERT INTO surveys (title, promo, grp, shown, direct, start_availability, end_availability) VALUES (?, ?, ?, ?, ?, ?, ?)" , title , promo , group , shown , direct , startAvailability , endAvailability ) ; err != nil {
2022-02-21 06:13:39 +00:00
return nil , err
2020-03-04 11:07:12 +00:00
} else if sid , err := res . LastInsertId ( ) ; err != nil {
2022-02-21 06:13:39 +00:00
return nil , err
2020-03-04 11:07:12 +00:00
} else {
2022-02-28 18:00:30 +00:00
return & Survey { sid , title , promo , group , shown , direct , false , startAvailability , endAvailability } , nil
2020-03-04 11:07:12 +00:00
}
}
2020-09-13 14:37:30 +00:00
func ( s Survey ) GetScore ( u * User ) ( score * float64 , err error ) {
2022-02-28 16:45:30 +00:00
if _ , ok := _score_cache [ u . Id ] ; ! ok {
_score_cache [ u . Id ] = map [ int64 ] * float64 { }
}
if v , ok := _score_cache [ u . Id ] [ s . Id ] ; ok {
score = v
} else {
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
}
_score_cache [ u . Id ] [ s . Id ] = score
2020-09-13 14:37:30 +00:00
}
2020-11-20 14:46:52 +00:00
return
}
2020-09-13 14:37:30 +00:00
2020-11-20 14:46:52 +00:00
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 ( )
2020-09-13 14:37:30 +00:00
2020-11-20 14:46:52 +00:00
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
}
}
2020-09-13 14:37:30 +00:00
2020-03-08 00:06:44 +00:00
return
}
2022-02-21 06:13:39 +00:00
func ( s * Survey ) Update ( ) ( * Survey , error ) {
2022-02-28 18:00:30 +00:00
if _ , err := DBExec ( "UPDATE surveys SET title = ?, promo = ?, grp = ?, shown = ?, direct = ?, corrected = ?, start_availability = ?, end_availability = ? WHERE id_survey = ?" , s . Title , s . Promo , s . Group , s . Shown , s . Direct , s . Corrected , s . StartAvailability , s . EndAvailability , s . Id ) ; err != nil {
2022-02-21 06:13:39 +00:00
return nil , err
2020-03-04 11:07:12 +00:00
} else {
2022-02-21 06:13:39 +00:00
return s , err
2020-03-04 11:07:12 +00:00
}
}
func ( s Survey ) Delete ( ) ( int64 , error ) {
if res , err := DBExec ( "DELETE FROM surveys WHERE id_survey = ?" , s . Id ) ; err != nil {
return 0 , err
} else if nb , err := res . RowsAffected ( ) ; err != nil {
return 0 , err
} else {
return nb , err
}
}
func ClearSurveys ( ) ( int64 , error ) {
if res , err := DBExec ( "DELETE FROM surveys" ) ; err != nil {
return 0 , err
} else if nb , err := res . RowsAffected ( ) ; err != nil {
return 0 , err
} else {
return nb , err
}
}