-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestionnaire.go
559 lines (490 loc) · 16.2 KB
/
questionnaire.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020,2021,2022 Marcus Soll
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// The main package holds the actual server of QuestionGo!
package main
import (
"archive/zip"
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"html/template"
"io"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"github.com/Top-Ranger/questiongo/helper"
"github.com/Top-Ranger/questiongo/registry"
"github.com/Top-Ranger/questiongo/translation"
)
// ErrValidation represents an error related to validating answer input
type ErrValidation error
var questionnaireTemplate *template.Template
var questionnaireStartTemplate *template.Template
func init() {
var err error
questionnaireTemplate, err = template.New("questionnaire").Funcs(evenOddFuncMap).ParseFS(templateFiles, "template/questionnaire.html")
if err != nil {
panic(err)
}
questionnaireStartTemplate, err = template.ParseFS(templateFiles, "template/start.html")
if err != nil {
panic(err)
}
}
// QuestionnairePage represents a single page on the questionnaire.
type QuestionnairePage struct {
RandomOrderQuestions bool
Questions [][]string
questions []registry.Question
}
// Questionnaire represents a questionnaire.
// It provides useful methods to handle the questionnaire.
// It must not be created on its own, but retrieved from LoadQuestionnaire or LoadAllQuestionnaires.
// A questionnaire is expected to hold all information in a single directory.
type Questionnaire struct {
Password string
PasswordMethod string
Open bool
Language string
Start string
StartFormat string
End string
EndFormat string
Contact string
RandomOrderPages bool
DoNotRandomiseFirstNPages int
DoNotRandomiseLastNPages int
ShowProgress bool
AllowBack bool
Pages []QuestionnairePage
startCache []byte
endCache []byte
id string
allQuestions []registry.Question
}
type questionnaireTemplatePageStruct struct {
QuestionData []template.HTML
First bool
Last bool
NextID string
PrevID string
ID string
}
type questionnaireTemplateStruct struct {
Pages []questionnaireTemplatePageStruct
ShowProgress bool
AllowBack bool
ID string
Translation translation.Translation
ServerPath string
}
type questionnaireStartTemplateStruct struct {
Text template.HTML
Key string
Contact string
Translation translation.Translation
ServerPath string
}
// GetStart returns the questionnaire start page.
func (q Questionnaire) GetStart() []byte {
return q.startCache
}
// GetEnd returns the questionnaire end page.
func (q Questionnaire) GetEnd() []byte {
return q.endCache
}
// WriteQuestions writes a html page containing the actual questionnaire to the writer.
// Since the questionnaite might contain random elements, it should be called seperately for each user instead of caching the result.
func (q Questionnaire) WriteQuestions(w io.Writer) {
translationStruct, err := translation.GetTranslation(q.Language)
if err != nil {
w.Write([]byte(fmt.Sprintf("can not get translation for language '%s'", q.Language)))
}
t := questionnaireTemplateStruct{
Pages: make([]questionnaireTemplatePageStruct, len(q.Pages)),
ID: q.id,
ShowProgress: q.ShowProgress,
AllowBack: q.AllowBack,
Translation: translationStruct,
ServerPath: config.ServerPath,
}
for p := range q.Pages {
questionData := make([]template.HTML, len(q.Pages[p].questions))
for i := range q.Pages[p].questions {
questionData[i] = q.Pages[p].questions[i].GetHTML()
}
if q.Pages[p].RandomOrderQuestions {
rand.Shuffle(len(questionData), func(i, j int) {
questionData[i], questionData[j] = questionData[j], questionData[i]
})
}
t.Pages[p].QuestionData = questionData
}
if q.RandomOrderPages {
rand.Shuffle(len(t.Pages)-q.DoNotRandomiseFirstNPages-q.DoNotRandomiseLastNPages, func(i, j int) {
t.Pages[i+q.DoNotRandomiseFirstNPages], t.Pages[j+q.DoNotRandomiseFirstNPages] = t.Pages[j+q.DoNotRandomiseFirstNPages], t.Pages[i+q.DoNotRandomiseFirstNPages]
})
}
for p := range t.Pages {
t.Pages[p].ID = fmt.Sprintf("__page_%d", p)
t.Pages[p].NextID = fmt.Sprintf("__page_%d", p+1)
t.Pages[p].PrevID = fmt.Sprintf("__page_%d", p-1)
if p == 0 {
t.Pages[p].First = true
}
if p == len(t.Pages)-1 {
t.Pages[p].Last = true
}
}
err = questionnaireTemplate.ExecuteTemplate(w, "questionnaire.html", t)
if err != nil {
fmt.Println(err.Error())
}
}
// GetResults returns a save html fragment containing the results of a question for each question.
func (q Questionnaire) GetResults() ([]template.HTML, error) {
safe, ok := registry.GetDataSafe(config.DataSafe)
if !ok {
return nil, fmt.Errorf("can not get datasafe %s", config.DataSafe)
}
ids := make([]string, len(q.allQuestions))
for i := range q.allQuestions {
ids[i] = q.allQuestions[i].GetID()
}
data, err := safe.GetData(q.id, ids)
if len(data) != len(ids) {
return nil, fmt.Errorf("datasafe returned %d question data, expected was %d", len(data), len(ids))
}
if err != nil {
return nil, err
}
result := make([]template.HTML, 0, len(q.allQuestions))
for i := range q.allQuestions {
result = append(result, q.allQuestions[i].GetStatisticsDisplay(data[i]))
}
return result, nil
}
// WriteZip writes a zip file containing one result file per question to the writer.
func (q Questionnaire) WriteZip(w io.Writer) error {
safe, ok := registry.GetDataSafe(config.DataSafe)
if !ok {
return fmt.Errorf("can not get datasafe %s", config.DataSafe)
}
ids := make([]string, len(q.allQuestions))
for i := range q.allQuestions {
ids[i] = q.allQuestions[i].GetID()
}
data, err := safe.GetData(q.id, ids)
if len(data) != len(ids) {
return fmt.Errorf("datasafe returned %d question data, expected was %d", len(data), len(ids))
}
if err != nil {
return err
}
result := zip.NewWriter(w)
for i := range q.allQuestions {
f, err := result.Create(strings.Join([]string{q.allQuestions[i].GetID(), "csv"}, "."))
if err != nil {
return err
}
csv := csv.NewWriter(f)
err = csv.Write(q.allQuestions[i].GetStatisticsHeader())
if err != nil {
return err
}
if err != nil {
return err
}
r := q.allQuestions[i].GetStatistics(data[i])
err = csv.WriteAll(r)
if err != nil {
return csv.Error()
}
}
return result.Close()
}
// WriteCSV writes a single csv file containing the current combined results of all questions.
func (q Questionnaire) WriteCSV(w io.Writer) error {
safe, ok := registry.GetDataSafe(config.DataSafe)
if !ok {
return fmt.Errorf("can not get datasafe %s", config.DataSafe)
}
ids := make([]string, len(q.allQuestions))
for i := range q.allQuestions {
ids[i] = q.allQuestions[i].GetID()
}
data, err := safe.GetData(q.id, ids)
if len(data) != len(ids) {
return fmt.Errorf("datasafe returned %d question data, expected was %d", len(data), len(ids))
}
if err != nil {
return err
}
csv := csv.NewWriter(w)
header := make([]string, 0)
result := make([][][]string, len(q.allQuestions))
maxLength := 0
for i := range q.allQuestions {
header = append(header, q.allQuestions[i].GetStatisticsHeader()...)
result[i] = q.allQuestions[i].GetStatistics(data[i])
if len(result[i]) > maxLength {
maxLength = len(result[i])
}
}
err = csv.Write(helper.EscapeCSVLine(header))
if err != nil {
return err
}
showError := sync.Once{}
errorList := make([]string, 0)
for data := 0; data < maxLength; data++ {
write := make([]string, 0, len(header))
for i := range result {
if len(result[i]) > data {
write = append(write, result[i][data]...)
} else {
// Sone question has less result
// This should not happen
// Let's still catch this by filling it with empty data
showError.Do(func() {
t := translation.GetDefaultTranslation()
log.Printf("csv export (%s): %s", q.id, t.ErrorAnswersDifferentAmount)
errorList = append(errorList, t.ErrorAnswersDifferentAmount)
})
write = append(write, make([]string, len(q.allQuestions[i].GetStatisticsHeader()))...)
}
}
csv.Write(helper.EscapeCSVLine(write))
}
csv.Flush()
for i := range errorList {
t := translation.GetDefaultTranslation()
w.Write([]byte("\n#"))
w.Write([]byte(t.AnErrorOccured))
w.Write([]byte(": "))
w.Write([]byte(errorList[i]))
}
return csv.Error()
}
// SaveData stores the questionnaire results contained in the http.Request permanently.
func (q Questionnaire) SaveData(r *http.Request) error {
results := make(map[string]map[string][]string)
safe, ok := registry.GetDataSafe(config.DataSafe)
if !ok {
return fmt.Errorf("can not get datasafe %s", config.DataSafe)
}
r.ParseForm()
for k := range r.Form {
split := strings.Split(k, "_")
if len(split) == 0 {
continue
}
ok := false
for i := range q.allQuestions {
if split[0] == q.allQuestions[i].GetID() {
ok = true
break
}
}
if !ok {
continue
}
m, ok := results[split[0]]
if !ok {
m = make(map[string][]string)
results[split[0]] = m
}
m[k] = r.Form[k]
}
// Validate input first
for i := range q.allQuestions {
m, ok := results[q.allQuestions[i].GetID()]
if !ok {
m = make(map[string][]string)
}
err := q.allQuestions[i].ValidateInput(m)
if err != nil {
return ErrValidation(fmt.Errorf("save data: Validation failed for '%s - %s': %s", q.id, q.allQuestions[i].GetID(), err.Error()))
}
}
// See if we need to drop the data
for i := range q.allQuestions {
m, ok := results[q.allQuestions[i].GetID()]
if !ok {
m = make(map[string][]string)
}
if q.allQuestions[i].IgnoreRecord(m) {
// Silently drop out and ignore the record
return nil
}
}
questionID := make([]string, len(q.allQuestions))
data := make([]string, len(q.allQuestions))
for i := range q.allQuestions {
m, ok := results[q.allQuestions[i].GetID()]
if !ok {
m = make(map[string][]string)
}
questionID[i] = q.allQuestions[i].GetID()
data[i] = q.allQuestions[i].GetDatabaseEntry(m)
}
err := safe.SaveData(q.id, questionID, data)
if err != nil {
log.Printf("save data: Can not save questionnaire data for '%s': %s", q.id, err.Error())
}
return err
}
// LoadQuestionnaire loads a single questionnaire from a file.
// path must contain the path to the questionnaire folder.
// file must contain the path to the actual questionnaire json.
// key holds the key of the questionnaire (usually path).
func LoadQuestionnaire(path, file, key string) (Questionnaire, error) {
// Load config
var q Questionnaire
b, err := os.ReadFile(file)
if err != nil {
return Questionnaire{}, err
}
err = json.Unmarshal(b, &q)
if err != nil {
return Questionnaire{}, err
}
translationStruct, err := translation.GetTranslation(q.Language)
if err != nil {
return Questionnaire{}, fmt.Errorf("can not get translation for language '%s'", q.Language)
}
// Check password method
ok := registry.PasswordMethodExists(q.PasswordMethod)
if !ok {
return Questionnaire{}, fmt.Errorf("unknown password method '%s'", q.PasswordMethod)
}
// Load Questions
testID := make(map[string]bool)
q.allQuestions = make([]registry.Question, 0)
for p := range q.Pages {
q.Pages[p].questions = make([]registry.Question, 0, len(q.Pages[p].Questions))
for i := range q.Pages[p].Questions {
if len(q.Pages[p].Questions[i]) != 3 {
return Questionnaire{}, fmt.Errorf("question %d-%d arguments have wrong length (%s)", p, i, file)
}
if strings.Contains(q.Pages[p].Questions[i][0], "_") {
return Questionnaire{}, fmt.Errorf("ID %s must not have '_' (%s)", q.Pages[p].Questions[i][0], file)
}
if testID[q.Pages[p].Questions[i][0]] {
return Questionnaire{}, fmt.Errorf("ID %s found twice (%s)", q.Pages[p].Questions[i][0], file)
}
testID[q.Pages[p].Questions[i][0]] = true
pathQ := filepath.Join(path, q.Pages[p].Questions[i][2])
b, err = os.ReadFile(pathQ)
if err != nil {
return Questionnaire{}, fmt.Errorf("can not read file %s: %w (%s)", pathQ, err, file)
}
factory, ok := registry.GetQuestionType(q.Pages[p].Questions[i][1])
if !ok {
return Questionnaire{}, fmt.Errorf("unknown question type %s (%s)", q.Pages[p].Questions[i][1], file)
}
newQuestion, err := factory(b, q.Pages[p].Questions[i][0], q.Language)
if err != nil {
return Questionnaire{}, fmt.Errorf("can not create question %d-%d: %w (%s)", p, i, err, file)
}
q.Pages[p].questions = append(q.Pages[p].questions, newQuestion)
q.allQuestions = append(q.allQuestions, newQuestion)
}
}
// Fill cache
pathQ := filepath.Join(path, q.Start)
b, err = os.ReadFile(pathQ)
if err != nil {
return Questionnaire{}, fmt.Errorf("can not read file %s: %w (%s)", pathQ, err, file)
}
f, ok := registry.GetFormatType(q.StartFormat)
if !ok {
return Questionnaire{}, fmt.Errorf("can not format start: Unknown type %s (%s)", q.StartFormat, file)
}
td := questionnaireStartTemplateStruct{
Text: f.Format(b),
Key: key,
Contact: q.Contact,
Translation: translationStruct,
ServerPath: config.ServerPath,
}
output := bytes.NewBuffer(make([]byte, 0, len(td.Text)+len(td.Contact)+5000))
questionnaireStartTemplate.Execute(output, td)
q.startCache = output.Bytes()
pathQ = filepath.Join(path, q.End)
b, err = os.ReadFile(pathQ)
if err != nil {
return Questionnaire{}, fmt.Errorf("can not read file %s: %w (%s)", pathQ, err, file)
}
f, ok = registry.GetFormatType(q.EndFormat)
if !ok {
return Questionnaire{}, fmt.Errorf("can not format end: Unknown type %s (%s)", q.StartFormat, file)
}
text := textTemplateStruct{f.Format(b), translationStruct, config.ServerPath}
output = bytes.NewBuffer(make([]byte, 0, len(text.Text)*2))
textTemplate.Execute(output, text)
q.endCache = output.Bytes()
// Check random order
if q.RandomOrderPages {
if q.DoNotRandomiseFirstNPages < 0 {
return Questionnaire{}, fmt.Errorf("value DoNotRandomiseFirstNPages must be positive, is %d (%s)", q.DoNotRandomiseFirstNPages, file)
}
if q.DoNotRandomiseLastNPages < 0 {
return Questionnaire{}, fmt.Errorf("value DoNotRandomiseLastNPages must be positive, is %d (%s)", q.DoNotRandomiseLastNPages, file)
}
if q.DoNotRandomiseFirstNPages+q.DoNotRandomiseLastNPages > len(q.Pages) {
return Questionnaire{}, fmt.Errorf("DoNotRandomiseFirstNPages + DoNotRandomiseLastNPages must not be larger than number of pages, currently %d + %d = %d > %d (%s)", q.DoNotRandomiseFirstNPages, q.DoNotRandomiseLastNPages, q.DoNotRandomiseFirstNPages+q.DoNotRandomiseLastNPages, len(q.Pages), file)
}
}
// ID
q.id = key
return q, nil
}
// LoadAllQuestionnaires loads all questionnaires from a folder.
// It expects to have each questionnaire in a direct subfolder.
// The questionnaire definition is in that subfolder in the file 'questionnaire.json'.
func LoadAllQuestionnaires(dataPath string) (map[string]Questionnaire, error) {
questionnaires := make(map[string]Questionnaire)
dirs, err := os.ReadDir(config.DataFolder)
if err != nil {
return nil, err
}
for i := range dirs {
if !dirs[i].IsDir() {
continue
}
content, err := os.ReadDir(filepath.Join(config.DataFolder, dirs[i].Name()))
if err != nil {
continue
}
for j := range content {
if content[j].Name() == "questionnaire.json" {
q, err := LoadQuestionnaire(filepath.Join(config.DataFolder, dirs[i].Name()), filepath.Join(config.DataFolder, dirs[i].Name(), content[j].Name()), dirs[i].Name())
if err != nil {
log.Printf("load all questionnaire: Can not load %s: %s", filepath.Join(config.DataFolder, dirs[i].Name(), content[j].Name()), err.Error())
break
}
questionnaires[dirs[i].Name()] = q
break
}
}
}
return questionnaires, nil
}