feat: add admin controllers, permissions and navigation for quizzes and attempts
This commit is contained in:
14
models/answers/columns.yaml
Normal file
14
models/answers/columns.yaml
Normal file
@@ -0,0 +1,14 @@
|
||||
columns:
|
||||
question:
|
||||
label: golem15.quizzes::lang.answers.question
|
||||
relation: question
|
||||
select: text
|
||||
searchable: true
|
||||
text:
|
||||
label: golem15.quizzes::lang.answers.text
|
||||
searchable: true
|
||||
is_correct:
|
||||
label: golem15.quizzes::lang.answers.is_correct
|
||||
type: switch
|
||||
sort_order:
|
||||
label: golem15.quizzes::lang.common.sort_order
|
||||
23
models/answers/fields.yaml
Normal file
23
models/answers/fields.yaml
Normal file
@@ -0,0 +1,23 @@
|
||||
fields:
|
||||
question:
|
||||
label: golem15.quizzes::lang.answers.question
|
||||
type: relation
|
||||
nameFrom: text
|
||||
required: true
|
||||
text:
|
||||
label: golem15.quizzes::lang.answers.text
|
||||
type: textarea
|
||||
size: tiny
|
||||
required: true
|
||||
is_correct:
|
||||
label: golem15.quizzes::lang.answers.is_correct
|
||||
type: switch
|
||||
span: left
|
||||
sort_order:
|
||||
label: golem15.quizzes::lang.common.sort_order
|
||||
type: number
|
||||
span: right
|
||||
explanation:
|
||||
label: golem15.quizzes::lang.answers.explanation
|
||||
type: textarea
|
||||
size: small
|
||||
@@ -2,24 +2,29 @@
|
||||
|
||||
package models
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
usermodels "git.golem15.com/golem15/sm-user-plugin/models"
|
||||
)
|
||||
|
||||
// Attempt is one user's run through a quiz, or through one category of its
|
||||
// questions when Category is set. The score columns are set when the attempt
|
||||
// is finished.
|
||||
type Attempt struct {
|
||||
ID uint `gorm:"column:id;primaryKey"`
|
||||
QuizID uint `gorm:"column:quiz_id"`
|
||||
UserID uint `gorm:"column:user_id"`
|
||||
Category *string `gorm:"column:category"`
|
||||
StartedAt time.Time `gorm:"column:started_at"`
|
||||
CompletedAt *time.Time `gorm:"column:completed_at"`
|
||||
ScorePoints float64 `gorm:"column:score_points"`
|
||||
MaxPoints int `gorm:"column:max_points"`
|
||||
ScorePercent *float64 `gorm:"column:score_percent"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
Quiz *Quiz `gorm:"foreignKey:QuizID"`
|
||||
ID uint `gorm:"column:id;primaryKey"`
|
||||
QuizID uint `gorm:"column:quiz_id"`
|
||||
UserID uint `gorm:"column:user_id"`
|
||||
Category *string `gorm:"column:category"`
|
||||
StartedAt time.Time `gorm:"column:started_at"`
|
||||
CompletedAt *time.Time `gorm:"column:completed_at"`
|
||||
ScorePoints float64 `gorm:"column:score_points"`
|
||||
MaxPoints int `gorm:"column:max_points"`
|
||||
ScorePercent *float64 `gorm:"column:score_percent"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
Quiz *Quiz `gorm:"foreignKey:QuizID"`
|
||||
User *usermodels.User `gorm:"foreignKey:UserID"`
|
||||
}
|
||||
|
||||
// TableName is the plugin-prefixed table.
|
||||
|
||||
25
models/attempts/columns.yaml
Normal file
25
models/attempts/columns.yaml
Normal file
@@ -0,0 +1,25 @@
|
||||
columns:
|
||||
quiz:
|
||||
label: golem15.quizzes::lang.attempts.quiz
|
||||
relation: quiz
|
||||
select: title
|
||||
searchable: true
|
||||
user:
|
||||
label: golem15.quizzes::lang.attempts.user
|
||||
relation: user
|
||||
select: email
|
||||
searchable: true
|
||||
category:
|
||||
label: golem15.quizzes::lang.questions.category
|
||||
started_at:
|
||||
label: golem15.quizzes::lang.attempts.started_at
|
||||
type: datetime
|
||||
completed_at:
|
||||
label: golem15.quizzes::lang.attempts.completed_at
|
||||
type: datetime
|
||||
score_points:
|
||||
label: golem15.quizzes::lang.attempts.score_points
|
||||
max_points:
|
||||
label: golem15.quizzes::lang.attempts.max_points
|
||||
score_percent:
|
||||
label: golem15.quizzes::lang.attempts.score_percent
|
||||
226
models/filters.go
Normal file
226
models/filters.go
Normal file
@@ -0,0 +1,226 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// FilterDB returns the database the admin filter choices are read from. The
|
||||
// plugin sets it at boot.
|
||||
//
|
||||
// TODO: requires SummerCMS change. pact.FilterOptions receives neither a
|
||||
// context nor a database handle, so a model whose filter choices come from
|
||||
// the database needs this package-level hook.
|
||||
var FilterDB func() *gorm.DB
|
||||
|
||||
// Filter scopes of the admin lists.
|
||||
const (
|
||||
ScopeByQuiz = "filterByQuiz"
|
||||
ScopeByCategory = "filterByCategory"
|
||||
ScopeByType = "filterByType"
|
||||
)
|
||||
|
||||
var (
|
||||
_ pact.FilterScope = Question{}
|
||||
_ pact.FilterOptions = Question{}
|
||||
_ pact.FilterScope = Answer{}
|
||||
_ pact.FilterOptions = Answer{}
|
||||
_ pact.FilterScope = Attempt{}
|
||||
_ pact.FilterOptions = Attempt{}
|
||||
)
|
||||
|
||||
// FilterScopes lists the admin list scopes of questions.
|
||||
func (Question) FilterScopes() []string { return []string{ScopeByQuiz, ScopeByCategory, ScopeByType} }
|
||||
|
||||
// FilterScope narrows the questions list to the chosen quizzes, categories
|
||||
// or types.
|
||||
func (Question) 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_questions.quiz_id IN ?", ids(values))
|
||||
case ScopeByCategory:
|
||||
cats := make([]string, 0, len(values))
|
||||
for _, v := range values {
|
||||
if c, ok := DecodeCategoryValue(v); ok {
|
||||
cats = append(cats, c)
|
||||
}
|
||||
}
|
||||
if len(cats) == 0 {
|
||||
return db.Where("1 = 0")
|
||||
}
|
||||
return db.Where("golem15_quizzes_questions.category IN ?", cats)
|
||||
case ScopeByType:
|
||||
return db.Where("golem15_quizzes_questions.type IN ?", values)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// FilterOptions serves the quizzes, the distinct categories and the types.
|
||||
func (Question) FilterOptions(scope string) []pact.Option {
|
||||
switch scope {
|
||||
case ScopeByQuiz:
|
||||
return quizOptions()
|
||||
case ScopeByCategory:
|
||||
return categoryOptions()
|
||||
case ScopeByType:
|
||||
return []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"},
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FilterScopes lists the admin list scopes of answers.
|
||||
func (Answer) FilterScopes() []string { return []string{ScopeByQuiz} }
|
||||
|
||||
// FilterScope narrows the answers list to the questions of the chosen
|
||||
// quizzes.
|
||||
func (Answer) FilterScope(name string, db *gorm.DB, value any) *gorm.DB {
|
||||
values := filterValues(value)
|
||||
if db == nil || len(values) == 0 || name != ScopeByQuiz {
|
||||
return db
|
||||
}
|
||||
return db.Where("golem15_quizzes_answers.question_id IN (SELECT id FROM golem15_quizzes_questions WHERE quiz_id IN ?)", ids(values))
|
||||
}
|
||||
|
||||
// FilterOptions serves the quizzes.
|
||||
func (Answer) FilterOptions(scope string) []pact.Option {
|
||||
if scope == ScopeByQuiz {
|
||||
return quizOptions()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FilterScopes lists the admin list scopes of attempts.
|
||||
func (Attempt) FilterScopes() []string { return []string{ScopeByQuiz} }
|
||||
|
||||
// FilterScope narrows the attempts list to the chosen quizzes.
|
||||
func (Attempt) FilterScope(name string, db *gorm.DB, value any) *gorm.DB {
|
||||
values := filterValues(value)
|
||||
if db == nil || len(values) == 0 || name != ScopeByQuiz {
|
||||
return db
|
||||
}
|
||||
return db.Where("golem15_quizzes_attempts.quiz_id IN ?", ids(values))
|
||||
}
|
||||
|
||||
// FilterOptions serves the quizzes.
|
||||
func (Attempt) FilterOptions(scope string) []pact.Option {
|
||||
if scope == ScopeByQuiz {
|
||||
return quizOptions()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func filterDB() *gorm.DB {
|
||||
if FilterDB == nil {
|
||||
return nil
|
||||
}
|
||||
return FilterDB()
|
||||
}
|
||||
|
||||
func quizOptions() []pact.Option {
|
||||
db := filterDB()
|
||||
if db == nil {
|
||||
return nil
|
||||
}
|
||||
var quizzes []Quiz
|
||||
if err := db.Select("id", "title").Order("title").Find(&quizzes).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]pact.Option, 0, len(quizzes))
|
||||
for _, q := range quizzes {
|
||||
out = append(out, pact.Option{Value: strconv.FormatUint(uint64(q.ID), 10), Label: q.Title})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func categoryOptions() []pact.Option {
|
||||
db := filterDB()
|
||||
if db == nil {
|
||||
return nil
|
||||
}
|
||||
var cats []string
|
||||
if err := db.Model(&Question{}).Where("category IS NOT NULL AND category <> ''").
|
||||
Distinct("category").Order("category").Pluck("category", &cats).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]pact.Option, 0, len(cats))
|
||||
for _, c := range cats {
|
||||
out = append(out, pact.Option{Value: EncodeCategoryValue(c), Label: c})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// EncodeCategoryValue turns a category into a filter value the admin list
|
||||
// accepts.
|
||||
//
|
||||
// TODO: requires SummerCMS change. cabana passes a scope filter value only
|
||||
// when it is an integer or an ASCII identifier, so a category with spaces or
|
||||
// diacritics ("Teoria stosunków") travels hex-encoded as "c_<hex>".
|
||||
func EncodeCategoryValue(category string) string {
|
||||
return "c_" + hex.EncodeToString([]byte(category))
|
||||
}
|
||||
|
||||
// DecodeCategoryValue reverses EncodeCategoryValue.
|
||||
func DecodeCategoryValue(v string) (string, bool) {
|
||||
raw, ok := strings.CutPrefix(v, "c_")
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
b, err := hex.DecodeString(raw)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return string(b), true
|
||||
}
|
||||
|
||||
func filterValues(value any) []string {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
if v == "" {
|
||||
return nil
|
||||
}
|
||||
return []string{v}
|
||||
case []string:
|
||||
return v
|
||||
case int64:
|
||||
return []string{strconv.FormatInt(v, 10)}
|
||||
case int:
|
||||
return []string{strconv.Itoa(v)}
|
||||
case []any:
|
||||
out := make([]string, 0, len(v))
|
||||
for _, x := range v {
|
||||
switch s := x.(type) {
|
||||
case string:
|
||||
if s != "" {
|
||||
out = append(out, s)
|
||||
}
|
||||
case float64:
|
||||
out = append(out, strconv.FormatFloat(s, 'f', -1, 64))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ids keeps the numeric values; an empty result matches nothing.
|
||||
func ids(values []string) []int64 {
|
||||
out := []int64{0}
|
||||
for _, v := range values {
|
||||
if n, err := strconv.ParseInt(v, 10, 64); err == nil && n > 0 {
|
||||
out = append(out, n)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -2,12 +2,7 @@
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
import "time"
|
||||
|
||||
// Question types.
|
||||
const (
|
||||
@@ -56,91 +51,3 @@ func (Question) Rules() map[string]string {
|
||||
"points": "required|integer|min:0",
|
||||
}
|
||||
}
|
||||
|
||||
// FilterDB returns the database the admin filter choices are read from. The
|
||||
// plugin sets it at boot.
|
||||
//
|
||||
// TODO: requires SummerCMS change. pact.FilterOptions receives neither a
|
||||
// context nor a database handle, so a model whose filter choices come from
|
||||
// the database needs this package-level hook.
|
||||
var FilterDB func() *gorm.DB
|
||||
|
||||
// Filter scopes of the questions admin list.
|
||||
const (
|
||||
ScopeByQuiz = "filterByQuiz"
|
||||
ScopeByCategory = "filterByCategory"
|
||||
)
|
||||
|
||||
// FilterScopes lists the admin list scopes the model answers.
|
||||
func (Question) FilterScopes() []string { return []string{ScopeByQuiz, ScopeByCategory} }
|
||||
|
||||
// FilterScope narrows the admin list to the chosen quizzes or categories.
|
||||
func (Question) FilterScope(name string, db *gorm.DB, value any) *gorm.DB {
|
||||
if db == nil {
|
||||
return db
|
||||
}
|
||||
values := filterValues(value)
|
||||
if len(values) == 0 {
|
||||
return db
|
||||
}
|
||||
switch name {
|
||||
case ScopeByQuiz:
|
||||
return db.Where("golem15_quizzes_questions.quiz_id IN ?", values)
|
||||
case ScopeByCategory:
|
||||
return db.Where("golem15_quizzes_questions.category IN ?", values)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// FilterOptions serves the quizzes and the distinct categories as choices.
|
||||
func (Question) FilterOptions(scope string) []pact.Option {
|
||||
if FilterDB == nil {
|
||||
return nil
|
||||
}
|
||||
db := FilterDB()
|
||||
if db == nil {
|
||||
return nil
|
||||
}
|
||||
var out []pact.Option
|
||||
switch scope {
|
||||
case ScopeByQuiz:
|
||||
var quizzes []Quiz
|
||||
if err := db.Select("id", "title").Order("title").Find(&quizzes).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
for _, q := range quizzes {
|
||||
out = append(out, pact.Option{Value: uintString(q.ID), Label: q.Title})
|
||||
}
|
||||
case ScopeByCategory:
|
||||
var cats []string
|
||||
if err := db.Model(&Question{}).Where("category IS NOT NULL AND category <> ''").
|
||||
Distinct("category").Order("category").Pluck("category", &cats).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
for _, c := range cats {
|
||||
out = append(out, pact.Option{Value: c, Label: c})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func filterValues(value any) []string {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
if v == "" {
|
||||
return nil
|
||||
}
|
||||
return []string{v}
|
||||
case []string:
|
||||
return v
|
||||
case []any:
|
||||
out := make([]string, 0, len(v))
|
||||
for _, x := range v {
|
||||
if s, ok := x.(string); ok && s != "" {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
18
models/questions/columns.yaml
Normal file
18
models/questions/columns.yaml
Normal file
@@ -0,0 +1,18 @@
|
||||
columns:
|
||||
quiz:
|
||||
label: golem15.quizzes::lang.questions.quiz
|
||||
relation: quiz
|
||||
select: title
|
||||
searchable: true
|
||||
text:
|
||||
label: golem15.quizzes::lang.questions.text
|
||||
searchable: true
|
||||
type:
|
||||
label: golem15.quizzes::lang.questions.type
|
||||
category:
|
||||
label: golem15.quizzes::lang.questions.category
|
||||
searchable: true
|
||||
points:
|
||||
label: golem15.quizzes::lang.questions.points
|
||||
sort_order:
|
||||
label: golem15.quizzes::lang.common.sort_order
|
||||
45
models/questions/fields.yaml
Normal file
45
models/questions/fields.yaml
Normal file
@@ -0,0 +1,45 @@
|
||||
fields:
|
||||
quiz:
|
||||
label: golem15.quizzes::lang.questions.quiz
|
||||
type: relation
|
||||
nameFrom: title
|
||||
span: left
|
||||
required: true
|
||||
type:
|
||||
label: golem15.quizzes::lang.questions.type
|
||||
type: dropdown
|
||||
span: right
|
||||
required: true
|
||||
default: single
|
||||
options:
|
||||
single: golem15.quizzes::lang.questions.type_single
|
||||
multiple: golem15.quizzes::lang.questions.type_multiple
|
||||
self_check: golem15.quizzes::lang.questions.type_self_check
|
||||
text:
|
||||
label: golem15.quizzes::lang.questions.text
|
||||
type: textarea
|
||||
size: small
|
||||
required: true
|
||||
category:
|
||||
label: golem15.quizzes::lang.questions.category
|
||||
type: text
|
||||
span: left
|
||||
points:
|
||||
label: golem15.quizzes::lang.questions.points
|
||||
type: number
|
||||
span: right
|
||||
default: 1
|
||||
required: true
|
||||
answer_text:
|
||||
label: golem15.quizzes::lang.questions.answer_text
|
||||
type: textarea
|
||||
size: small
|
||||
comment: golem15.quizzes::lang.questions.answer_text_comment
|
||||
explanation:
|
||||
label: golem15.quizzes::lang.questions.explanation
|
||||
type: textarea
|
||||
size: small
|
||||
sort_order:
|
||||
label: golem15.quizzes::lang.common.sort_order
|
||||
type: number
|
||||
span: left
|
||||
16
models/quizzes/columns.yaml
Normal file
16
models/quizzes/columns.yaml
Normal file
@@ -0,0 +1,16 @@
|
||||
columns:
|
||||
title:
|
||||
label: golem15.quizzes::lang.quizzes.title_field
|
||||
searchable: true
|
||||
slug:
|
||||
label: golem15.quizzes::lang.quizzes.slug
|
||||
searchable: true
|
||||
context:
|
||||
label: golem15.quizzes::lang.quizzes.context
|
||||
searchable: true
|
||||
is_published:
|
||||
label: golem15.quizzes::lang.quizzes.is_published
|
||||
type: switch
|
||||
updated_at:
|
||||
label: golem15.quizzes::lang.common.updated_at
|
||||
type: datetime
|
||||
44
models/quizzes/fields.yaml
Normal file
44
models/quizzes/fields.yaml
Normal file
@@ -0,0 +1,44 @@
|
||||
fields:
|
||||
title:
|
||||
label: golem15.quizzes::lang.quizzes.title_field
|
||||
type: text
|
||||
span: left
|
||||
required: true
|
||||
slug:
|
||||
label: golem15.quizzes::lang.quizzes.slug
|
||||
type: text
|
||||
span: right
|
||||
comment: golem15.quizzes::lang.quizzes.slug_comment
|
||||
description:
|
||||
label: golem15.quizzes::lang.quizzes.description
|
||||
type: textarea
|
||||
size: small
|
||||
context:
|
||||
label: golem15.quizzes::lang.quizzes.context
|
||||
type: text
|
||||
span: left
|
||||
comment: golem15.quizzes::lang.quizzes.context_comment
|
||||
is_published:
|
||||
label: golem15.quizzes::lang.quizzes.is_published
|
||||
type: switch
|
||||
span: right
|
||||
passing_score:
|
||||
label: golem15.quizzes::lang.quizzes.passing_score
|
||||
type: number
|
||||
span: left
|
||||
default: 50
|
||||
required: true
|
||||
time_limit_seconds:
|
||||
label: golem15.quizzes::lang.quizzes.time_limit_seconds
|
||||
type: number
|
||||
span: right
|
||||
comment: golem15.quizzes::lang.quizzes.time_limit_comment
|
||||
round_size:
|
||||
label: golem15.quizzes::lang.quizzes.round_size
|
||||
type: number
|
||||
span: left
|
||||
comment: golem15.quizzes::lang.quizzes.round_size_comment
|
||||
shuffle_questions:
|
||||
label: golem15.quizzes::lang.quizzes.shuffle_questions
|
||||
type: switch
|
||||
span: right
|
||||
@@ -1,5 +0,0 @@
|
||||
package models
|
||||
|
||||
import "strconv"
|
||||
|
||||
func uintString(n uint) string { return strconv.FormatUint(uint64(n), 10) }
|
||||
Reference in New Issue
Block a user