-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplechoice.go
276 lines (245 loc) · 6.72 KB
/
multiplechoice.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020,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.
package question
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"math/rand"
"strings"
"github.com/Top-Ranger/questiongo/helper"
"github.com/Top-Ranger/questiongo/registry"
)
func init() {
err := registry.RegisterQuestionType(FactoryMultipleChoice, "multiple choice")
if err != nil {
panic(err)
}
}
// FactoryMultipleChoice is the factory for multiple choice questions.
func FactoryMultipleChoice(data []byte, id string, language string) (registry.Question, error) {
var mc multipleChoice
err := json.Unmarshal(data, &mc)
if err != nil {
return nil, err
}
mc.id = id
// Sanity checks
testID := make(map[string]bool)
for i := range mc.Answers {
if len(mc.Answers[i]) != 2 {
return nil, fmt.Errorf("multiplechoice: Answer %d must have exactly 2 values (id, text) (%s)", i, id)
}
if testID[mc.Answers[i][0]] {
return nil, fmt.Errorf("multiplechoice: ID %s found twice (%s)", mc.Answers[i][0], id)
}
testID[mc.Answers[i][0]] = true
}
_, ok := registry.GetFormatType(mc.Format)
if !ok {
return nil, fmt.Errorf("multiplechoice: Unknown format type %s (%s)", mc.Format, id)
}
return &mc, nil
}
var multiplechoiceTemplate = template.Must(template.New("multiplechoiceTemplate").Parse(`{{.Question}}<br>
{{range $i, $e := .Data }}
<input type="checkbox" id="{{$e.QID}}_{{$e.AID}}" name="{{$e.QID}}_{{$e.AID}}"><label for="{{$e.QID}}_{{$e.AID}}">{{$e.Text}}</label><br>
{{end}}`))
var multiplechoiceStatisticsTemplate = template.Must(template.New("multiplechoiceStatisticTemplate").Parse(`{{.Question}}<br>
<table>
<thead>
<tr>
<th>Question</th>
<th>Answer (number)</th>
<th>Answer (percentage)</th>
</tr>
</thead>
<tbody>
{{range $i, $e := .Data }}
<tr>
<td>{{$e.Question}}</td>
<td>{{$e.Result}}</td>
<td>{{printf "%.2f" $e.Percent}}</td>
</tr>
{{end}}
<tr>
<td class="th-cell">[number answers]</td>
<td>{{.Sum}}</td>
</tr>
</tbody>
</table>
<br>
{{.Image}}
`))
type multiplechoiceTemplateStructInner struct {
QID string
AID string
Text template.HTML
}
type multiplechoiceStatisticTemplateStruct struct {
Question template.HTML
Sum int
Data []multiplechoiceStatisticsTemplateStructInner
Image template.HTML
}
type multiplechoiceStatisticsTemplateStructInner struct {
Question template.HTML
Result int
Percent float64
}
type multiplechoiceTemplateStruct struct {
Question template.HTML
Data []multiplechoiceTemplateStructInner
}
type multipleChoice struct {
Random bool
Format string
Question string
Answers [][]string
id string
}
func (mc multipleChoice) GetID() string {
return mc.id
}
func (mc multipleChoice) GetHTML() template.HTML {
f, _ := registry.GetFormatType(mc.Format)
td := multiplechoiceTemplateStruct{
Question: f.Format([]byte(mc.Question)),
Data: make([]multiplechoiceTemplateStructInner, 0, len(mc.Answers)),
}
for i := range mc.Answers {
mcts := multiplechoiceTemplateStructInner{
QID: mc.id,
AID: mc.Answers[i][0],
Text: f.FormatClean([]byte(mc.Answers[i][1])),
}
td.Data = append(td.Data, mcts)
}
if mc.Random {
rand.Shuffle(len(td.Data), func(i, j int) {
td.Data[i], td.Data[j] = td.Data[j], td.Data[i]
})
}
output := bytes.NewBuffer(make([]byte, 0))
err := multiplechoiceTemplate.Execute(output, td)
if err != nil {
log.Printf("multiplechoice: Error executing template (%s)", err.Error())
}
return template.HTML(output.Bytes())
}
func (mc multipleChoice) GetStatisticsHeader() []string {
header := make([]string, len(mc.Answers))
for i := range mc.Answers {
header[i] = fmt.Sprintf("%s_%s", mc.id, mc.Answers[i][0])
}
return header
}
func (mc multipleChoice) GetStatistics(data []string) [][]string {
result := make([][]string, len(data))
for d := range data {
r := make([]string, len(mc.Answers))
errorState := true
if !strings.HasPrefix(data[d], "ERROR") {
boolarray := make([]bool, len(mc.Answers))
err := json.Unmarshal([]byte(data[d]), &boolarray)
if err == nil && len(boolarray) == len(r) {
errorState = false
for i := range boolarray {
if boolarray[i] {
r[i] = "true"
} else {
r[i] = "false"
}
}
}
}
if errorState {
for i := range r {
r[i] = "error"
}
}
result[d] = r
}
return result
}
func (mc multipleChoice) GetStatisticsDisplay(data []string) template.HTML {
count := 0
countAnswer := make([]int, len(mc.Answers))
for d := range data {
boolarray := make([]bool, len(mc.Answers))
err := json.Unmarshal([]byte(data[d]), &boolarray)
if err != nil {
continue
}
if len(boolarray) != len(mc.Answers) {
continue
}
count++
for i := range mc.Answers {
if boolarray[i] {
countAnswer[i]++
}
}
}
f, _ := registry.GetFormatType(mc.Format)
td := multiplechoiceStatisticTemplateStruct{
Question: f.Format([]byte(mc.Question)),
Sum: count,
Data: make([]multiplechoiceStatisticsTemplateStructInner, 0, len(mc.Answers)),
}
v := make([]helper.ChartValue, len(mc.Answers))
for i := range mc.Answers {
question := f.FormatClean([]byte(mc.Answers[i][1]))
v[i].Label = string(question)
v[i].Value = float64(countAnswer[i])
inner := multiplechoiceStatisticsTemplateStructInner{
Question: question,
Result: countAnswer[i],
Percent: float64(countAnswer[i]) / float64(td.Sum),
}
td.Data = append(td.Data, inner)
}
td.Image = helper.BarChart(v, mc.id, string(f.FormatClean([]byte(mc.Question))))
output := bytes.NewBuffer(make([]byte, 0))
err := multiplechoiceStatisticsTemplate.Execute(output, td)
if err != nil {
log.Printf("multiplechoice: Error executing template (%s)", err.Error())
}
return template.HTML(output.Bytes())
}
func (mc multipleChoice) ValidateInput(data map[string][]string) error {
return nil
}
func (mc multipleChoice) IgnoreRecord(data map[string][]string) bool {
return false
}
func (mc multipleChoice) GetDatabaseEntry(data map[string][]string) string {
result := make([]bool, len(mc.Answers))
for i := range mc.Answers {
_, ok := data[fmt.Sprintf("%s_%s", mc.id, mc.Answers[i][0])]
if ok {
result[i] = true
} else {
result[i] = false
}
}
b, err := json.Marshal(result)
if err != nil {
return fmt.Sprintf("ERROR: %s", err.Error())
}
return string(b)
}