feat: set scores, confirm in bulk and regrade open answers over MCP
- SetScore (0..points in 0.5 steps, comment shown to the student), ConfirmBulk per quiz or attempt and Regrade through the quiz's grader - close() announces an attempt reviewed while it was open - quizzes_review_set_score, quizzes_review_confirm_bulk, quizzes_review_regrade; 409 already_reviewed and grader_manual
This commit is contained in:
@@ -29,6 +29,10 @@ func writeReview(w http.ResponseWriter, status int, data any, err error) {
|
||||
switch {
|
||||
case errors.Is(err, classes.ErrNoGraderScore):
|
||||
reviewConflict(w, "The answer has no grader score to confirm; set the score instead.", "no_grader_score")
|
||||
case errors.Is(err, classes.ErrAlreadyReviewed):
|
||||
reviewConflict(w, "The answer is already reviewed; set the score instead.", "already_reviewed")
|
||||
case errors.Is(err, classes.ErrGraderManual):
|
||||
reviewConflict(w, "The quiz's grader is manual or not available, so nothing can be graded again.", "grader_manual")
|
||||
case errors.Is(err, classes.ErrNotFound):
|
||||
wire.WriteJSON(w, http.StatusNotFound, map[string]any{"error": true, "message": "Open answer, attempt or quiz not found."})
|
||||
default:
|
||||
@@ -143,3 +147,100 @@ func answerReview(w http.ResponseWriter, d *classes.ReviewDetail, err error) {
|
||||
}
|
||||
writeReview(w, http.StatusOK, reviewDetailJSON(*d), nil)
|
||||
}
|
||||
|
||||
// setScoreBody is the POST /{id}/score body.
|
||||
type setScoreBody struct {
|
||||
Points *float64 `json:"points"`
|
||||
Comment *string `json:"comment"`
|
||||
}
|
||||
|
||||
// ReviewSetScore answers POST /api/v1/quiz-reviews/{id}/score with
|
||||
// {points, comment}: the owner's final score on any status, with an
|
||||
// optional comment the student sees. Points outside 0..question points or
|
||||
// off the 0.5 steps, and a comment over 2000 characters, are a 422.
|
||||
func (h *Handlers) ReviewSetScore(w http.ResponseWriter, r *http.Request) {
|
||||
id, ok := reviewID(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var body setScoreBody
|
||||
if !readManaged(w, r, &body) {
|
||||
return
|
||||
}
|
||||
comment, problems := classes.CheckReviewComment(body.Comment)
|
||||
if body.Points == nil {
|
||||
problems = append([]string{"points is required"}, problems...)
|
||||
}
|
||||
if refuse(w, problems) {
|
||||
return
|
||||
}
|
||||
by, ok := h.reviewer(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
svc, ok := h.service(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
d, err := svc.SetScore(r.Context(), id, *body.Points, comment, by)
|
||||
answerReview(w, d, err)
|
||||
}
|
||||
|
||||
// confirmBulkBody is the POST /confirm body.
|
||||
type confirmBulkBody struct {
|
||||
Quiz string `json:"quiz"`
|
||||
AttemptID uint `json:"attempt_id"`
|
||||
}
|
||||
|
||||
// ReviewConfirmBulk answers POST /api/v1/quiz-reviews/confirm with
|
||||
// exactly one of {quiz} and {attempt_id}: every graded answer in scope is
|
||||
// confirmed; it answers {confirmed: n}.
|
||||
func (h *Handlers) ReviewConfirmBulk(w http.ResponseWriter, r *http.Request) {
|
||||
var body confirmBulkBody
|
||||
if !readManaged(w, r, &body) {
|
||||
return
|
||||
}
|
||||
scope := classes.ReviewScope{AttemptID: body.AttemptID, QuizSlug: strings.TrimSpace(body.Quiz)}
|
||||
if refuse(w, classes.CheckReviewScope(scope, false)) {
|
||||
return
|
||||
}
|
||||
by, ok := h.reviewer(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
svc, ok := h.service(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
n, err := svc.ConfirmBulk(r.Context(), scope, by)
|
||||
writeReview(w, http.StatusOK, map[string]any{"confirmed": n}, err)
|
||||
}
|
||||
|
||||
// regradeBody is the POST /regrade body.
|
||||
type regradeBody struct {
|
||||
AnswerID uint `json:"answer_id"`
|
||||
AttemptID uint `json:"attempt_id"`
|
||||
Quiz string `json:"quiz"`
|
||||
}
|
||||
|
||||
// ReviewRegrade answers POST /api/v1/quiz-reviews/regrade with exactly one
|
||||
// of {answer_id}, {attempt_id} and {quiz}: the unreviewed open answers in
|
||||
// scope go back to the quiz's grader; it answers {regraded: n}. 409
|
||||
// grader_manual when the quiz has no grader, already_reviewed for a single
|
||||
// reviewed answer.
|
||||
func (h *Handlers) ReviewRegrade(w http.ResponseWriter, r *http.Request) {
|
||||
var body regradeBody
|
||||
if !readManaged(w, r, &body) {
|
||||
return
|
||||
}
|
||||
scope := classes.ReviewScope{AnswerID: body.AnswerID, AttemptID: body.AttemptID, QuizSlug: strings.TrimSpace(body.Quiz)}
|
||||
if refuse(w, classes.CheckReviewScope(scope, true)) {
|
||||
return
|
||||
}
|
||||
svc, ok := h.service(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
n, err := svc.Regrade(r.Context(), scope)
|
||||
writeReview(w, http.StatusOK, map[string]any{"regraded": n}, err)
|
||||
}
|
||||
|
||||
@@ -107,10 +107,12 @@ func TestReviewErrorMapping(t *testing.T) {
|
||||
code int
|
||||
key string
|
||||
}{
|
||||
"no grader score": {classes.ErrNoGraderScore, http.StatusConflict, "no_grader_score"},
|
||||
"not found": {classes.ErrNotFound, http.StatusNotFound, ""},
|
||||
"validation": {&classes.ValidationError{Problems: []string{"status must be one of"}}, http.StatusUnprocessableEntity, ""},
|
||||
"other": {errors.New("boom"), http.StatusInternalServerError, ""},
|
||||
"no grader score": {classes.ErrNoGraderScore, http.StatusConflict, "no_grader_score"},
|
||||
"already reviewed": {classes.ErrAlreadyReviewed, http.StatusConflict, "already_reviewed"},
|
||||
"grader manual": {classes.ErrGraderManual, http.StatusConflict, "grader_manual"},
|
||||
"not found": {classes.ErrNotFound, http.StatusNotFound, ""},
|
||||
"validation": {&classes.ValidationError{Problems: []string{"status must be one of"}}, http.StatusUnprocessableEntity, ""},
|
||||
"other": {errors.New("boom"), http.StatusInternalServerError, ""},
|
||||
} {
|
||||
rec := httptest.NewRecorder()
|
||||
writeReview(rec, 0, nil, tc.err)
|
||||
@@ -167,3 +169,47 @@ func TestReviewHandlersBeforeDatabase(t *testing.T) {
|
||||
t.Errorf("confirm without a user = %d, want 401", code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReviewWritesBeforeDatabase: the bodies of set_score, confirm_bulk
|
||||
// and regrade are checked before the database: unknown keys, a missing
|
||||
// points, a long comment and a scope that is not exactly one key are 422s.
|
||||
func TestReviewWritesBeforeDatabase(t *testing.T) {
|
||||
h := New(backpack.New(nil))
|
||||
long := strings.Repeat("x", 2001)
|
||||
id := map[string]string{"id": "3"}
|
||||
for name, tc := range map[string]struct {
|
||||
fn http.HandlerFunc
|
||||
body string
|
||||
want string
|
||||
}{
|
||||
"score empty body": {h.ReviewSetScore, "", "invalid JSON"},
|
||||
"score unknown key": {h.ReviewSetScore, `{"points":1,"status":"reviewed"}`, "invalid JSON"},
|
||||
"score without points": {h.ReviewSetScore, `{"comment":"ok"}`, "points is required"},
|
||||
"score long comment": {h.ReviewSetScore, `{"points":1,"comment":"` + long + `"}`, "comment must be at most 2000 characters"},
|
||||
"bulk none": {h.ReviewConfirmBulk, `{}`, "exactly one of attempt_id and quiz is required"},
|
||||
"bulk two": {h.ReviewConfirmBulk, `{"quiz":"q","attempt_id":4}`, "exactly one of attempt_id and quiz is required"},
|
||||
"bulk answer_id": {h.ReviewConfirmBulk, `{"answer_id":4}`, "invalid JSON"},
|
||||
"regrade none": {h.ReviewRegrade, `{}`, "exactly one of answer_id, attempt_id and quiz is required"},
|
||||
"regrade two": {h.ReviewRegrade, `{"answer_id":3,"quiz":"q"}`, "exactly one of answer_id, attempt_id and quiz is required"},
|
||||
} {
|
||||
code, out := call(t, tc.fn, "POST", "/", tc.body, 7, id)
|
||||
if msg, _ := out["message"].(string); code != http.StatusUnprocessableEntity || !strings.Contains(msg, tc.want) {
|
||||
t.Errorf("%s = %d %v, want 422 with %q", name, code, out, tc.want)
|
||||
}
|
||||
}
|
||||
for name, tc := range map[string]struct {
|
||||
fn http.HandlerFunc
|
||||
body string
|
||||
}{
|
||||
"score": {h.ReviewSetScore, `{"points":1.5,"comment":null}`},
|
||||
"bulk": {h.ReviewConfirmBulk, `{"quiz":"q"}`},
|
||||
"regrade": {h.ReviewRegrade, `{"attempt_id":4}`},
|
||||
} {
|
||||
if code, _ := call(t, tc.fn, "POST", "/", tc.body, 7, id); code != http.StatusServiceUnavailable {
|
||||
t.Errorf("%s without a database = %d, want 503", name, code)
|
||||
}
|
||||
}
|
||||
if code, _ := call(t, h.ReviewSetScore, "POST", "/", `{"points":1}`, 7, map[string]string{"id": "x"}); code != http.StatusNotFound {
|
||||
t.Errorf("score with a bad id = %d, want 404", code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user