-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathitem_analysis.go
More file actions
331 lines (297 loc) · 10.2 KB
/
item_analysis.go
File metadata and controls
331 lines (297 loc) · 10.2 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package httpserver
import (
"encoding/csv"
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
"time"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/lextures/lextures/server/internal/apierr"
"github.com/lextures/lextures/server/internal/repos/course"
repoitemanalysis "github.com/lextures/lextures/server/internal/repos/itemanalysis"
"github.com/lextures/lextures/server/internal/repos/rbac"
svcitemanalysis "github.com/lextures/lextures/server/internal/service/itemanalysis"
)
// itemAnalysisJSON is the API response shape for GET item-analysis.
type itemAnalysisJSON struct {
QuizID string `json:"quizId"`
InsufficientData bool `json:"insufficientData,omitempty"`
NResponses int `json:"nResponses"`
MinimumRequired int `json:"minimumRequired,omitempty"`
StatsPending bool `json:"statsPending,omitempty"`
TestStats *testStatsJSON `json:"testStats,omitempty"`
ItemStats []itemStatJSON `json:"itemStats,omitempty"`
}
type testStatsJSON struct {
NResponses int `json:"nResponses"`
KR20 *float64 `json:"kr20"`
CronbachAlpha *float64 `json:"cronbachAlpha"`
MeanScore *float64 `json:"meanScore"`
StdDev *float64 `json:"stdDev"`
ComputedAt string `json:"computedAt"`
}
type itemStatJSON struct {
QuestionIndex int `json:"questionIndex"`
QuestionText string `json:"questionText"`
NResponses int `json:"nResponses"`
PValue *float64 `json:"pValue"`
RPb *float64 `json:"rPb"`
DistractorFreqs map[string]float64 `json:"distractorFreqs,omitempty"`
Flag *string `json:"flag"`
}
// handleGetItemAnalysis returns pre-computed item stats for a quiz.
// Requires instructor access (course:CODE:item:create permission).
func (d Deps) handleGetItemAnalysis() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet+","+http.MethodOptions)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
courseCode, viewer, itemID, ok := d.requireItemAnalysisAccess(w, r)
if !ok {
return
}
_ = courseCode
ctx := r.Context()
// Count attempts to check for insufficient data early
n, err := repoitemanalysis.CountSubmittedAttempts(ctx, d.Pool, itemID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to count attempts.")
return
}
_ = viewer
if n < svcitemanalysis.MinResponses {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(itemAnalysisJSON{
QuizID: itemID.String(),
InsufficientData: true,
NResponses: n,
MinimumRequired: svcitemanalysis.MinResponses,
})
return
}
testStat, err := repoitemanalysis.GetTestStats(ctx, d.Pool, itemID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to load item analysis.")
return
}
if testStat == nil {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(itemAnalysisJSON{
QuizID: itemID.String(),
NResponses: n,
StatsPending: true,
})
return
}
itemRows, err := repoitemanalysis.GetItemStats(ctx, d.Pool, itemID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to load item stats.")
return
}
out := itemAnalysisJSON{
QuizID: itemID.String(),
NResponses: testStat.NResponses,
TestStats: toTestStatsJSON(testStat),
ItemStats: toItemStatsJSON(itemRows),
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(out)
}
}
// handleComputeItemAnalysis manually triggers CTT computation for a quiz.
func (d Deps) handleComputeItemAnalysis() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost+","+http.MethodOptions)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
courseCode, viewer, itemID, ok := d.requireItemAnalysisAccess(w, r)
if !ok {
return
}
_ = courseCode
_ = viewer
ctx := r.Context()
result, err := svcitemanalysis.Compute(ctx, d.Pool, itemID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to compute item analysis.")
return
}
if result.N < svcitemanalysis.MinResponses {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(itemAnalysisJSON{
QuizID: itemID.String(),
InsufficientData: true,
NResponses: result.N,
MinimumRequired: svcitemanalysis.MinResponses,
})
return
}
out := itemAnalysisJSON{
QuizID: itemID.String(),
NResponses: result.TestStat.NResponses,
TestStats: toTestStatsJSON(&result.TestStat),
ItemStats: toItemStatsJSON(result.ItemStats),
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(out)
}
}
// handleExportItemAnalysisCSV downloads item stats as a CSV file.
func (d Deps) handleExportItemAnalysisCSV() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet+","+http.MethodOptions)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
courseCode, viewer, itemID, ok := d.requireItemAnalysisAccess(w, r)
if !ok {
return
}
_ = courseCode
_ = viewer
ctx := r.Context()
testStat, err := repoitemanalysis.GetTestStats(ctx, d.Pool, itemID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to load item analysis.")
return
}
itemRows, err := repoitemanalysis.GetItemStats(ctx, d.Pool, itemID)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to load item stats.")
return
}
if testStat == nil || len(itemRows) == 0 {
apierr.WriteJSON(w, http.StatusNotFound, apierr.CodeNotFound, "No item analysis available yet.")
return
}
filename := fmt.Sprintf("item-analysis-%s-%s.csv", itemID.String()[:8], time.Now().UTC().Format("20060102"))
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
cw := csv.NewWriter(w)
_ = cw.Write([]string{
"question_index", "question_text", "n",
"p_value", "r_pb",
"distractor_a_pct", "distractor_b_pct", "distractor_c_pct", "distractor_d_pct",
"flag",
})
for _, row := range itemRows {
pval := ""
if row.PValue != nil {
pval = strconv.FormatFloat(*row.PValue, 'f', 4, 64)
}
rpb := ""
if row.RPB != nil {
rpb = strconv.FormatFloat(*row.RPB, 'f', 4, 64)
}
flag := ""
if row.Flag != nil {
flag = *row.Flag
}
dA := fmtDistractor(row.DistractorFreqs, "A")
dB := fmtDistractor(row.DistractorFreqs, "B")
dC := fmtDistractor(row.DistractorFreqs, "C")
dD := fmtDistractor(row.DistractorFreqs, "D")
_ = cw.Write([]string{
strconv.Itoa(row.QuestionIndex),
row.QuestionText,
strconv.Itoa(row.NResponses),
pval, rpb,
dA, dB, dC, dD,
flag,
})
}
cw.Flush()
}
}
// requireItemAnalysisAccess checks authentication and instructor permission, returning
// (courseCode, viewerID, itemID, ok). Writes an error and returns ok=false on failure.
func (d Deps) requireItemAnalysisAccess(w http.ResponseWriter, r *http.Request) (string, uuid.UUID, uuid.UUID, bool) {
courseCode, viewer, ok := d.requireCourseAccess(w, r)
if !ok {
return "", uuid.UUID{}, uuid.UUID{}, false
}
itemID, err := uuid.Parse(chi.URLParam(r, "item_id"))
if err != nil {
apierr.WriteJSON(w, http.StatusBadRequest, apierr.CodeInvalidInput, "Invalid item id.")
return "", uuid.UUID{}, uuid.UUID{}, false
}
ctx := r.Context()
cid, err := course.GetIDByCourseCode(ctx, d.Pool, courseCode)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to load course.")
return "", uuid.UUID{}, uuid.UUID{}, false
}
if cid == nil {
apierr.WriteJSON(w, http.StatusNotFound, apierr.CodeNotFound, "Course not found.")
return "", uuid.UUID{}, uuid.UUID{}, false
}
perm := "course:" + courseCode + ":item:create"
canEdit, err := rbac.UserHasPermission(ctx, d.Pool, viewer, perm)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Failed to verify permissions.")
return "", uuid.UUID{}, uuid.UUID{}, false
}
if !canEdit {
apierr.WriteJSON(w, http.StatusForbidden, apierr.CodeForbidden, "Forbidden.")
return "", uuid.UUID{}, uuid.UUID{}, false
}
return courseCode, viewer, itemID, true
}
func toTestStatsJSON(r *repoitemanalysis.TestStatRow) *testStatsJSON {
if r == nil {
return nil
}
return &testStatsJSON{
NResponses: r.NResponses,
KR20: r.KR20,
CronbachAlpha: r.CronbachAlpha,
MeanScore: r.MeanScore,
StdDev: r.StdDev,
ComputedAt: r.ComputedAt.Format(time.RFC3339),
}
}
func toItemStatsJSON(rows []repoitemanalysis.ItemStatRow) []itemStatJSON {
sort.Slice(rows, func(i, j int) bool { return rows[i].QuestionIndex < rows[j].QuestionIndex })
out := make([]itemStatJSON, len(rows))
for i, r := range rows {
out[i] = itemStatJSON{
QuestionIndex: r.QuestionIndex,
QuestionText: r.QuestionText,
NResponses: r.NResponses,
PValue: r.PValue,
RPb: r.RPB,
DistractorFreqs: r.DistractorFreqs,
Flag: r.Flag,
}
}
return out
}
func fmtDistractor(freqs map[string]float64, key string) string {
if freqs == nil {
return ""
}
if v, ok := freqs[key]; ok {
return strconv.FormatFloat(v*100, 'f', 1, 64) + "%"
}
return ""
}