-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation_answers.go
445 lines (373 loc) · 12.9 KB
/
operation_answers.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
package surveygo
import (
"fmt"
"github.com/rendis/devtoolkit"
"github.com/rendis/surveygo/v2/question/types"
"github.com/rendis/surveygo/v2/question/types/choice"
"github.com/rendis/surveygo/v2/reviewer"
)
const groupQuestionTemplate = "group.%d.%s"
// ReviewAnswers verifies if the answers provided are valid for this survey.
// Args:
// * ans: the answers to check.
// Returns:
// * map[string]bool:
// - key: the name id of the missing question
// - value: if the question is required or not
// - error: if an error occurred
func (s *Survey) ReviewAnswers(ans Answers) (*SurveyResume, error) {
var invalidAnswers []*InvalidAnswerError
var correctAnswersCount = make(map[string]int)
var groupsCount = make(map[string]int)
for nameId, values := range ans {
if invalids := s.reviewNameId(nameId, values, correctAnswersCount, groupsCount); invalids != nil {
invalidAnswers = append(invalidAnswers, invalids...)
}
}
if len(invalidAnswers) > 0 {
return &SurveyResume{InvalidAnswers: invalidAnswers}, nil
}
return s.getSurveyResume(correctAnswersCount, groupsCount), nil
}
// TranslateAnswers translates the nameIDs of the answers to the values provided in each question (if any, otherwise the nameID is used).
// Translations:
// * text type: the value is the same passed in the answer
// * simple choice type: the value is the value, if any, of the choice with the same nameID as the answer
func (s *Survey) TranslateAnswers(ans Answers, ignoreUnknown bool) (Answers, error) {
var res = make(Answers, len(ans))
for nameId, answers := range ans {
// if nameId is a question
if s.isQuestion(nameId) {
translations, err := s.translateAnswers(nameId, answers, ignoreUnknown)
if err != nil {
return nil, err
}
res[nameId] = translations
}
// if nameId is a group
if s.isGroup(nameId) {
groupAnswers, err := reviewer.ExtractGroupNestedAnswers(answers)
if err != nil {
return nil, fmt.Errorf("invalid group answers for group '%s'. %s", nameId, err)
}
for i, groupAnswersPack := range groupAnswers {
for questionNameId, answersPack := range groupAnswersPack {
translations, err := s.translateAnswers(questionNameId, answersPack, ignoreUnknown)
if err != nil {
return nil, err
}
key := fmt.Sprintf(groupQuestionTemplate, i, questionNameId)
res[key] = translations
}
}
}
}
return res, nil
}
// GetDisabledQuestions returns a map with the name id of the disabled questions.
// Questions are disabled:
// * if the question is disabled
// * if the group of the question is disabled
func (s *Survey) GetDisabledQuestions() map[string]bool {
var disabledQuestions = make(map[string]bool)
for _, group := range s.Groups {
groupDisabled := group.Disabled
for _, questionNameId := range group.QuestionsIds {
if q, ok := s.Questions[questionNameId]; ok && (groupDisabled || q.Disabled) {
disabledQuestions[questionNameId] = true
}
}
}
return disabledQuestions
}
// GetEnabledQuestions returns a map with the name id of the enabled questions.
// Questions are enabled:
// * if the question is enabled and the group of the question is enabled
func (s *Survey) GetEnabledQuestions() map[string]bool {
var enabledQuestions = make(map[string]bool)
for _, group := range s.Groups {
if group.Disabled {
continue
}
for _, questionNameId := range group.QuestionsIds {
if q, ok := s.Questions[questionNameId]; ok && !q.Disabled {
enabledQuestions[questionNameId] = true
}
}
}
return enabledQuestions
}
// GetRequiredQuestions returns a map with the name id of the required questions.
func (s *Survey) GetRequiredQuestions() map[string]bool {
var requiredQuestions = make(map[string]bool)
for _, group := range s.Groups {
for _, questionNameId := range group.QuestionsIds {
if q, ok := s.Questions[questionNameId]; ok && q.Required {
requiredQuestions[questionNameId] = q.Required
}
}
}
return requiredQuestions
}
// GetOptionalQuestions returns a map with the name id of the optional questions.
func (s *Survey) GetOptionalQuestions() map[string]bool {
var optionalQuestions = make(map[string]bool)
for _, group := range s.Groups {
for _, questionNameId := range group.QuestionsIds {
if q, ok := s.Questions[questionNameId]; ok && !q.Required {
optionalQuestions[questionNameId] = true
}
}
}
return optionalQuestions
}
// GetRequiredAndOptionalQuestions returns a map with the name id of the required and optional questions.
// The value is true if the question is required, false otherwise.
func (s *Survey) GetRequiredAndOptionalQuestions() map[string]bool {
var requiredAndOptionalQuestions = make(map[string]bool)
for _, group := range s.Groups {
for _, questionNameId := range group.QuestionsIds {
if q, ok := s.Questions[questionNameId]; ok {
requiredAndOptionalQuestions[questionNameId] = q.Required
}
}
}
return requiredAndOptionalQuestions
}
// GroupAnswersByType groups the answers by the type of the question.
func (s *Survey) GroupAnswersByType(ans Answers) map[types.QuestionType]Answers {
var res = make(map[types.QuestionType]Answers)
for nameId, answers := range ans {
// if nameId is a question
if s.isQuestion(nameId) {
q := s.Questions[nameId]
if _, ok := res[q.QTyp]; !ok {
res[q.QTyp] = make(Answers)
}
res[q.QTyp][nameId] = answers
}
// if nameId is a group
if s.isGroup(nameId) {
groupAnswers, err := reviewer.ExtractGroupNestedAnswers(answers)
if err != nil {
continue
}
for _, groupAnswersPack := range groupAnswers {
for questionNameId, answersPack := range groupAnswersPack {
q := s.Questions[questionNameId]
if _, ok := res[q.QTyp]; !ok {
res[q.QTyp] = make(Answers)
}
key := fmt.Sprintf(groupQuestionTemplate, 0, questionNameId)
res[q.QTyp][key] = answersPack
}
}
}
}
return res
}
// translateAnswers translates the nameIDs of the answers to the values provided in each question (if any, otherwise the nameID is used).
func (s *Survey) translateAnswers(nameId string, answers []any, ignoreUnknown bool) ([]any, error) {
if len(answers) == 0 {
return answers, nil
}
q, ok := s.Questions[nameId]
if !ok {
if ignoreUnknown {
return answers, nil
}
return nil, fmt.Errorf("question '%s' not found", nameId)
}
// if text type, the value is the same passed in the answer
if types.IsTextType(q.QTyp) {
return answers, nil
}
// if simple choice type the translation is the value
if types.IsSimpleChoiceType(q.QTyp) {
var res []any
c, err := choice.CastToChoice(q.Value)
if err != nil {
return nil, err
}
var optionsMap = make(map[string]*choice.Option)
var optionsValuesMap = make(map[any]*choice.Option)
for _, option := range c.Options {
optionsMap[option.NameId] = option
optionsValuesMap[option.Value] = option
}
for _, answer := range answers {
answeredNameId, ok := answer.(string)
if !ok {
if ignoreUnknown {
continue
}
return nil, fmt.Errorf("invalid type, expected string, got '%T'", answer)
}
var option *choice.Option
if option, ok = optionsMap[answeredNameId]; !ok {
// if the option is not found, try to find it in the options by value
option, ok = optionsValuesMap[answeredNameId]
}
if !ok {
if ignoreUnknown {
res = append(res, answeredNameId)
continue
}
return nil, fmt.Errorf("option not found for question '%s' (searched by nameId and value '%s')", nameId, answeredNameId)
}
// if the option has a value, use it, otherwise use the answered name id
if option.Value != nil {
res = append(res, option.Value)
continue
}
res = append(res, answeredNameId)
}
return res, nil
}
return answers, nil
}
// reviewQuestion verifies if the answer provided is valid for the given question.
func (s *Survey) reviewQuestion(questionNameID string, answers []any, correctAnswersCount map[string]int) *InvalidAnswerError {
q := s.Questions[questionNameID]
reviewerFn, err := reviewer.GetQuestionReviewer(q.QTyp)
if err != nil {
return &InvalidAnswerError{
QuestionNameId: questionNameID,
Answer: answers,
Error: err.Error(),
}
}
if err = reviewerFn(q.Value, answers, q.QTyp); err != nil {
return &InvalidAnswerError{
QuestionNameId: questionNameID,
Answer: answers,
Error: err.Error(),
}
}
// update correct answers count
correctAnswersCount[questionNameID]++
return nil
}
// reviewGroup verifies if the answers provided are valid for the given group.
func (s *Survey) reviewGroup(groupNameID string, nestedAnswers []any, correctAnswersCount map[string]int, groupsCount map[string]int) []*InvalidAnswerError {
groupAnswers, err := reviewer.ExtractGroupNestedAnswers(nestedAnswers)
if err != nil {
return []*InvalidAnswerError{{
QuestionNameId: groupNameID,
Answer: nestedAnswers,
Error: err.Error(),
}}
}
var invalidAnswers []*InvalidAnswerError
for _, groupedAnswers := range groupAnswers {
for questionNameID, answers := range groupedAnswers {
if invalids := s.reviewNameId(questionNameID, answers, correctAnswersCount, groupsCount); invalids != nil {
invalidAnswers = append(invalidAnswers, invalids...)
}
}
}
// update correct answers count
groupsCount[groupNameID] += len(groupAnswers)
return invalidAnswers
}
// reviewNameId verifies if the answers provided are valid for the given nameId.
func (s *Survey) reviewNameId(nameId string, values []any, correctAnswersCount map[string]int, groupsCount map[string]int) []*InvalidAnswerError {
// if nameId is a question
if s.isQuestion(nameId) {
if invalid := s.reviewQuestion(nameId, values, correctAnswersCount); invalid != nil {
return []*InvalidAnswerError{invalid}
}
return nil
}
// if nameId is a group
if s.isGroup(nameId) {
if invalids := s.reviewGroup(nameId, values, correctAnswersCount, groupsCount); len(invalids) > 0 {
return invalids
}
return nil
}
// if nameId is not a question or a group
var invalid = &InvalidAnswerError{
QuestionNameId: nameId,
Answer: values,
Error: fmt.Sprintf("question '%s' not found", nameId),
}
return []*InvalidAnswerError{invalid}
}
// getSurveyResume returns the resume of the survey based on the answers provided.
func (s *Survey) getSurveyResume(correctAnswersCount map[string]int, groupsCount map[string]int) *SurveyResume {
var resume = &SurveyResume{
TotalsResume: TotalsResume{
UnansweredQuestions: make(map[string]bool),
},
GroupsResume: make(map[string]*GroupTotalsResume),
ExternalSurveyIds: make(map[string]string),
}
visibleQuestionWithGroup := s.getVisibleQuestionFromActiveGroups()
for questionId, groupId := range visibleQuestionWithGroup {
q := s.Questions[questionId]
// init group resume if not exists
if _, ok := resume.GroupsResume[groupId]; !ok {
resume.GroupsResume[groupId] = &GroupTotalsResume{
TotalsResume: TotalsResume{
UnansweredQuestions: map[string]bool{},
},
AnswerGroups: groupsCount[groupId],
}
}
questionResponsesCount := correctAnswersCount[questionId]
questionOccurrenceCount := devtoolkit.IfThenElse(groupsCount[groupId] == 0, 1, groupsCount[groupId])
// update totals
resume.TotalQuestions += questionOccurrenceCount
resume.GroupsResume[groupId].TotalQuestions += questionOccurrenceCount
if q.Required {
resume.TotalRequiredQuestions += questionOccurrenceCount
resume.GroupsResume[groupId].TotalRequiredQuestions += questionOccurrenceCount
}
// update answers
resume.TotalQuestionsAnswered += questionResponsesCount
resume.GroupsResume[groupId].TotalQuestionsAnswered += questionResponsesCount
if q.Required {
resume.TotalRequiredQuestionsAnswered += questionResponsesCount
resume.GroupsResume[groupId].TotalRequiredQuestionsAnswered += questionResponsesCount
}
// update unanswered questions
if questionResponsesCount == 0 {
resume.UnansweredQuestions[q.NameId] = q.Required
resume.GroupsResume[groupId].UnansweredQuestions[q.NameId] = q.Required
}
}
// update external survey ids
for _, g := range s.Groups {
if g.IsExternalSurvey {
resume.ExternalSurveyIds[g.NameId] = g.QuestionsIds[0]
}
}
return resume
}
// getVisibleQuestionFromActiveGroups returns a maps with the visible questions within its active groups nameId.
// and active groups are the groups that are visible and enabled.
func (s *Survey) getVisibleQuestionFromActiveGroups() map[string]string {
var questionWithGroup = map[string]string{}
for _, group := range s.Groups {
// skip hidden && disabled groups
if group.Hidden || group.Disabled {
continue
}
for _, questionNameId := range group.QuestionsIds {
// get only visible question
if q, ok := s.Questions[questionNameId]; ok && q.Visible {
questionWithGroup[questionNameId] = group.NameId
}
}
}
return questionWithGroup
}
func (s *Survey) isQuestion(nameId string) bool {
_, ok := s.Questions[nameId]
return ok
}
func (s *Survey) isGroup(nameId string) bool {
_, ok := s.Groups[nameId]
return ok
}