forked from codespook/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outcomeset.go
92 lines (80 loc) · 2.43 KB
/
outcomeset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package server
type QuestionType string
const LIKERT QuestionType = "likert"
type Aggregation string
const (
MEAN Aggregation = "mean"
SUM Aggregation = "sum"
)
type Question struct {
ID string `json:"id"`
Question string `json:"question"`
Description string `json:"description"`
Type QuestionType `json:"type"`
Deleted bool `json:"deleted"`
Options map[string]interface{} `json:"options"`
CategoryID string `json:"categoryID" bson:"categoryID"`
}
type Category struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Aggregation Aggregation `json:"aggregation"`
}
type OutcomeSet struct {
ID string `json:"id" bson:"_id"`
OrganisationID string `json:"organisationID" bson:"organisationID"`
Name string `json:"name"`
Description string `json:"description"`
Questions []Question `json:"questions"`
Categories []Category `json:"categories"`
Deleted bool `json:"deleted"`
}
func (os *OutcomeSet) GetCategory(catID string) *Category {
for _, c := range os.Categories {
if c.ID == catID {
return &c
}
}
return nil
}
// GetCategoryQuestions gets questions belonging to the provided category ID
// Does not return archived questions
func (os *OutcomeSet) GetCategoryQuestions(catID string) []Question {
out := make([]Question, 0, len(os.Questions))
for _, q := range os.Questions {
if q.CategoryID == catID && !q.Deleted {
out = append(out, q)
}
}
return out
}
// GetArchivedCategoryQuestions gets archived questions belonging to the provided category ID
func (os *OutcomeSet) GetArchivedCategoryQuestions(catID string) []Question {
out := make([]Question, 0, len(os.Questions))
for _, q := range os.Questions {
if q.CategoryID == catID && q.Deleted {
out = append(out, q)
}
}
return out
}
// GetQuestion returns the question with the provided qID or nil
func (os *OutcomeSet) GetQuestion(qID string) *Question {
for _, q := range os.Questions {
if q.ID == qID {
return &q
}
}
return nil
}
// ActiveQuestions returns only the currently active questions (i.e. not deleted)
func (os *OutcomeSet) ActiveQuestions() []Question {
qs := make([]Question, 0, len(os.Questions))
for _, q := range os.Questions {
if !q.Deleted {
qs = append(qs, q)
}
}
return qs
}