Able to make corrections

This commit is contained in:
nemunaire 2022-02-28 10:52:27 +01:00
parent df2da4221e
commit 11c49bcb34
22 changed files with 1085 additions and 80 deletions

View file

@ -23,7 +23,7 @@ func init() {
return APIErrorResponse{err: err}
}
return formatApiResponse(q.NewCorrectionTemplate(new.Label, new.Score, new.ScoreExplaination))
return formatApiResponse(q.NewCorrectionTemplate(new.Label, new.RegExp, new.Score, new.ScoreExplaination))
}), adminRestricted))
router.GET("/api/surveys/:sid/questions/:qid/corrections/:cid", apiHandler(correctionHandler(
@ -51,6 +51,48 @@ func init() {
return formatApiResponse(ct.Delete())
}), adminRestricted))
router.GET("/api/questions/:qid/corrections", apiHandler(questionHandler(
func(q Question, _ []byte) HTTPResponse {
if cts, err := q.GetCorrectionTemplates(); err != nil {
return APIErrorResponse{err: err}
} else {
return APIResponse{cts}
}
}), adminRestricted))
router.POST("/api/questions/:qid/corrections", apiHandler(questionHandler(func(q Question, body []byte) HTTPResponse {
var new CorrectionTemplate
if err := json.Unmarshal(body, &new); err != nil {
return APIErrorResponse{err: err}
}
return formatApiResponse(q.NewCorrectionTemplate(new.Label, new.RegExp, new.Score, new.ScoreExplaination))
}), adminRestricted))
router.GET("/api/questions/:qid/corrections/:cid", apiHandler(correctionHandler(
func(ct CorrectionTemplate, _ []byte) HTTPResponse {
if users, err := ct.GetUserCorrected(); err != nil {
return APIErrorResponse{err: err}
} else {
return APIResponse{users}
}
}), adminRestricted))
router.PUT("/api/questions/:qid/corrections/:cid", apiHandler(correctionHandler(func(current CorrectionTemplate, body []byte) HTTPResponse {
var new CorrectionTemplate
if err := json.Unmarshal(body, &new); err != nil {
return APIErrorResponse{err: err}
}
new.Id = current.Id
if err := new.Update(); err != nil {
return APIErrorResponse{err: err}
} else {
return APIResponse{new}
}
}), adminRestricted))
router.DELETE("/api/questions/:qid/corrections/:cid", apiHandler(correctionHandler(func(ct CorrectionTemplate, body []byte) HTTPResponse {
return formatApiResponse(ct.Delete())
}), adminRestricted))
router.GET("/api/users/:uid/questions/:qid", apiAuthHandler(func(u *User, ps httprouter.Params, body []byte) HTTPResponse {
return userHandler(func(u User, _ []byte) HTTPResponse {
if qid, err := strconv.Atoi(string(ps.ByName("qid"))); err != nil {
@ -75,6 +117,14 @@ func init() {
return formatApiResponse(u.NewCorrection(new.IdTemplate))
}), adminRestricted))
router.PUT("/api/users/:uid/corrections", apiHandler(userHandler(func(u User, body []byte) HTTPResponse {
var new map[int64]bool
if err := json.Unmarshal(body, &new); err != nil {
return APIErrorResponse{err: err}
}
return formatApiResponse(u.EraseCorrections(new))
}), adminRestricted))
router.DELETE("/api/users/:uid/corrections/:cid", apiHandler(userCorrectionHandler(func(u User, uc UserCorrection, body []byte) HTTPResponse {
return formatApiResponse(uc.Delete(u))
}), adminRestricted))
@ -112,19 +162,20 @@ type CorrectionTemplate struct {
Id int64 `json:"id"`
IdQuestion int64 `json:"id_question"`
Label string `json:"label"`
RegExp string `json:"regexp"`
Score int `json:"score"`
ScoreExplaination string `json:"score_explaination,omitempty"`
}
func (q *Question) GetCorrectionTemplates() (ct []CorrectionTemplate, err error) {
if rows, errr := DBQuery("SELECT id_template, id_question, label, score, score_explanation FROM correction_templates WHERE id_question=?", q.Id); errr != nil {
if rows, errr := DBQuery("SELECT id_template, id_question, label, re, score, score_explanation FROM correction_templates WHERE id_question=?", q.Id); errr != nil {
return nil, errr
} else {
defer rows.Close()
for rows.Next() {
var c CorrectionTemplate
if err = rows.Scan(&c.Id, &c.IdQuestion, &c.Label, &c.Score, &c.ScoreExplaination); err != nil {
if err = rows.Scan(&c.Id, &c.IdQuestion, &c.Label, &c.RegExp, &c.Score, &c.ScoreExplaination); err != nil {
return
}
ct = append(ct, c)
@ -138,27 +189,27 @@ func (q *Question) GetCorrectionTemplates() (ct []CorrectionTemplate, err error)
}
func (q *Question) GetCorrectionTemplate(id int) (c CorrectionTemplate, err error) {
err = DBQueryRow("SELECT id_template, id_question, label, score, score_explanation FROM correction_templates WHERE id_question=? AND id_template=?", q.Id, id).Scan(&c.Id, &c.IdQuestion, &c.Label, &c.Score, &c.ScoreExplaination)
err = DBQueryRow("SELECT id_template, id_question, label, re, score, score_explanation FROM correction_templates WHERE id_question=? AND id_template=?", q.Id, id).Scan(&c.Id, &c.IdQuestion, &c.Label, &c.RegExp, &c.Score, &c.ScoreExplaination)
return
}
func GetCorrectionTemplate(id int64) (c CorrectionTemplate, err error) {
err = DBQueryRow("SELECT id_template, id_question, label, score, score_explanation FROM correction_templates WHERE id_template=?", id).Scan(&c.Id, &c.IdQuestion, &c.Label, &c.Score, &c.ScoreExplaination)
err = DBQueryRow("SELECT id_template, id_question, label, re, score, score_explanation FROM correction_templates WHERE id_template=?", id).Scan(&c.Id, &c.IdQuestion, &c.Label, &c.RegExp, &c.Score, &c.ScoreExplaination)
return
}
func (q *Question) NewCorrectionTemplate(label string, score int, score_explaination string) (CorrectionTemplate, error) {
if res, err := DBExec("INSERT INTO correction_templates (id_question, label, score, score_explanation) VALUES (?, ?, ?, ?)", q.Id, label, score, score_explaination); err != nil {
func (q *Question) NewCorrectionTemplate(label string, regexp string, score int, score_explaination string) (CorrectionTemplate, error) {
if res, err := DBExec("INSERT INTO correction_templates (id_question, label, re, score, score_explanation) VALUES (?, ?, ?, ?, ?)", q.Id, label, regexp, score, score_explaination); err != nil {
return CorrectionTemplate{}, err
} else if cid, err := res.LastInsertId(); err != nil {
return CorrectionTemplate{}, err
} else {
return CorrectionTemplate{cid, q.Id, label, score, score_explaination}, nil
return CorrectionTemplate{cid, q.Id, label, regexp, score, score_explaination}, nil
}
}
func (t *CorrectionTemplate) Update() error {
_, err := DBExec("UPDATE correction_templates SET id_question = ?, label = ?, score = ?, score_explanation = ? WHERE id_template = ?", t.IdQuestion, t.Label, t.Score, t.ScoreExplaination, t.Id)
_, err := DBExec("UPDATE correction_templates SET id_question = ?, label = ?, re = ?, score = ?, score_explanation = ? WHERE id_template = ?", t.IdQuestion, t.Label, t.RegExp, t.Score, t.ScoreExplaination, t.Id)
return err
}
@ -269,6 +320,26 @@ func (u *User) NewCorrection(id int64) (*UserCorrectionSummary, error) {
}
}
func (u *User) EraseCorrections(ids map[int64]bool) (*UserCorrectionSummary, error) {
var lastid int64
for id, st := range ids {
lastid = id
if st {
DBExec("INSERT INTO student_corrected (id_user, id_template) VALUES (?, ?)", u.Id, id)
} else {
DBExec("DELETE FROM student_corrected WHERE id_user = ? AND id_template = ?", u.Id, id)
}
}
if ucs, err := u.ComputeScoreQuestion(lastid); err != nil {
return nil, err
} else {
return ucs, nil
}
}
func (c *UserCorrection) Delete(u User) (*UserCorrectionSummary, error) {
if res, err := DBExec("DELETE FROM student_corrected WHERE id_correction = ?", c.Id); err != nil {
return nil, err