feat: review open answers in the admin and pick a grader per quiz

- Do oceny admin screen (golem15.quizzes.reviews) over the open answers,
  with a read-only context partial, score and comment virtual fields saved
  through SetScore, confirm and regrade record actions, bulk confirm
- open question type, rubric and max_length in the question form; grader
  dropdown on the quiz form from the grader registry
- Review model with quiz, status and student filters
This commit is contained in:
Jakub Zych
2026-11-09 18:31:19 +01:00
parent 1ce1ebee16
commit 2ad4e8b1ec
20 changed files with 1244 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package models
import (
"encoding/hex"
"slices"
"strconv"
"strings"
@@ -22,6 +23,8 @@ const (
ScopeByQuiz = "filterByQuiz"
ScopeByCategory = "filterByCategory"
ScopeByType = "filterByType"
ScopeByStatus = "filterByStatus"
ScopeByStudent = "filterByStudent"
)
var (
@@ -31,6 +34,8 @@ var (
_ pact.FilterOptions = Answer{}
_ pact.FilterScope = Attempt{}
_ pact.FilterOptions = Attempt{}
_ pact.FilterScope = Review{}
_ pact.FilterOptions = Review{}
)
// FilterScopes lists the admin list scopes of questions.
@@ -75,6 +80,7 @@ func (Question) FilterOptions(scope string) []pact.Option {
{Value: TypeSingle, Label: "golem15.quizzes::lang.questions.type_single"},
{Value: TypeMultiple, Label: "golem15.quizzes::lang.questions.type_multiple"},
{Value: TypeSelfCheck, Label: "golem15.quizzes::lang.questions.type_self_check"},
{Value: TypeOpen, Label: "golem15.quizzes::lang.questions.type_open"},
}
}
return nil
@@ -121,6 +127,54 @@ func (Attempt) FilterOptions(scope string) []pact.Option {
return nil
}
// FilterScopes lists the admin list scopes of the review screen.
func (Review) FilterScopes() []string { return []string{ScopeByQuiz, ScopeByStatus, ScopeByStudent} }
// FilterScope narrows the review list to the open answers of the chosen
// quizzes, grading statuses or students.
func (Review) FilterScope(name string, db *gorm.DB, value any) *gorm.DB {
values := filterValues(value)
if db == nil || len(values) == 0 {
return db
}
switch name {
case ScopeByQuiz:
return db.Where("golem15_quizzes_attempt_answers.question_id IN (SELECT id FROM golem15_quizzes_questions WHERE quiz_id IN ?)", ids(values))
case ScopeByStatus:
statuses := make([]string, 0, len(values))
for _, v := range values {
if slices.Contains(GradingStatuses, v) {
statuses = append(statuses, v)
}
}
if len(statuses) == 0 {
return db.Where("1 = 0")
}
return db.Where("golem15_quizzes_attempt_answers.grading_status IN ?", statuses)
case ScopeByStudent:
return db.Where("golem15_quizzes_attempt_answers.attempt_id IN (SELECT id FROM golem15_quizzes_attempts WHERE user_id IN ?)", ids(values))
}
return db
}
// FilterOptions serves the quizzes, the grading statuses and the students
// who have open answers.
func (Review) FilterOptions(scope string) []pact.Option {
switch scope {
case ScopeByQuiz:
return quizOptions()
case ScopeByStatus:
out := make([]pact.Option, 0, len(GradingStatuses))
for _, status := range GradingStatuses {
out = append(out, pact.Option{Value: status, Label: "golem15.quizzes::lang.reviews.status_" + status})
}
return out
case ScopeByStudent:
return studentOptions()
}
return nil
}
func filterDB() *gorm.DB {
if FilterDB == nil {
return nil
@@ -144,6 +198,32 @@ func quizOptions() []pact.Option {
return out
}
// studentOptions lists the users with at least one open answer, by email.
func studentOptions() []pact.Option {
db := filterDB()
if db == nil {
return nil
}
var rows []struct {
ID uint
Email string
}
err := db.Raw(`SELECT DISTINCT u.id AS id, COALESCE(u.email, '') AS email
FROM users u
JOIN golem15_quizzes_attempts a ON a.user_id = u.id
JOIN golem15_quizzes_attempt_answers aa ON aa.attempt_id = a.id
WHERE aa.grading_status IS NOT NULL
ORDER BY email, id`).Scan(&rows).Error
if err != nil {
return nil
}
out := make([]pact.Option, 0, len(rows))
for _, r := range rows {
out = append(out, pact.Option{Value: strconv.FormatUint(uint64(r.ID), 10), Label: r.Email})
}
return out
}
func categoryOptions() []pact.Option {
db := filterDB()
if db == nil {