/
exam.go
217 lines (178 loc) · 4.82 KB
/
exam.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
package main
import (
"encoding/json"
"fmt"
"github.com/dreadl0ck/cryptoutils"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
func genExam(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().UnixNano())
var (
examQuestions string
examSolutions string
count int
)
id, err := cryptoutils.RandomString(10)
if err != nil {
log.Fatal(err)
}
id = strings.TrimSuffix(id, "==")
courseID := strings.ToLower(r.FormValue("course"))
cou, ok := data.Courses[courseID]
if !ok {
http.Error(w, "invalid course id", http.StatusBadRequest)
return
}
// write header
examQuestions += "# " + strings.ToUpper(courseID) + " Test Exam" + "\n"
examQuestions += "\n> ID: " + id + "\n\n"
examSolutions += "# " + strings.ToUpper(courseID) + " Test Exam Solution" + "\n"
examSolutions += "\n> ID: " + id + "\n\n"
err = r.ParseForm()
if err != nil {
fmt.Println(err)
}
fmt.Println("mkexam", r.RemoteAddr, r.UserAgent())
for c, n := range cou.Info.Exam {
var (
category = cou.Categories[strings.ToLower(c)]
current int
done []int
val = r.FormValue(c)
flagged []string
)
if len(category) == 0 {
continue
}
if val != "" && val != "[]" {
err = json.Unmarshal([]byte(val), &flagged)
if err != nil {
fmt.Println(err)
}
fmt.Println("will only include", flagged, "for category", c)
flagged = shuffle(flagged)
}
// write category
examQuestions += "\n### " + strings.ToUpper(c) + "\n"
examSolutions += "\n### " + strings.ToUpper(c) + "\n"
if c == "history" {
count++
// always add a quote for history part
for q, author := range cou.Info.Quotes {
// skip CIA joke for exam questions
if strings.HasPrefix(q, "CIA") {
continue
}
examQuestions += strconv.Itoa(count) + ") Name the author of the following quote and explain his intentions: \n"
examQuestions += " \n"
examQuestions += " " + q + "\n"
examQuestions += " \n\n"
examSolutions += strconv.Itoa(count) + ") Name the author of the following quote and explain his intentions: \n"
examSolutions += " \n"
examSolutions += " " + q + "\n"
examSolutions += " \n"
examSolutions += " " + author
examSolutions += " \n"
break
}
}
var (
numFlagged = len(flagged)
)
// write questions
for i := 0; i < n; i++ {
if numFlagged > 0 {
if i >= numFlagged {
break
}
n, err := strconv.Atoi(flagged[i])
if err == nil {
current = n
} else {
fmt.Println("invalid integer in flagged values", flagged[i])
continue
}
} else {
// pick a random one
for {
current = rand.Intn(len(category))
// exclude light bulb jokes
if !hasBeenAsked(current, done) && !strings.Contains(category[current].Question, "light bulb") {
break
}
}
}
count++
examQuestions += strconv.Itoa(count) + ") " + fixLinks(category[current].Question)
examQuestions += " \n"
examSolutions += strconv.Itoa(count) + ") " + fixLinks(category[current].Question)
examSolutions += " \n"
var res string
for _, line := range strings.Split(category[current].Answer, "\n") {
res = fixLinks(line)
if strings.HasPrefix(res, "!") {
examSolutions += fixLinks(line)
} else {
examSolutions += " " + fixLinks(line)
}
}
done = append(done, current)
if len(done) == len(category) {
break
}
}
}
var base string
if *tls {
// TODO: dont hardcode paths
base = filepath.Join("/etc/quizsrv", "files", id)
} else {
base = filepath.Join(*configFolder, "files", id)
}
_ = os.MkdirAll(base, 0777)
fq, err := os.Create(filepath.Join(base, "examQuestions.md"))
if err != nil {
log.Fatal(err)
}
fq.WriteString(examQuestions)
fs, err := os.Create(filepath.Join(base, "examSolutions.md"))
if err != nil {
log.Fatal(err)
}
fs.WriteString(examSolutions)
err = fq.Close()
if err != nil {
log.Fatal(err)
}
err = fs.Close()
if err != nil {
log.Fatal(err)
}
var e string
if *tls {
e = "/usr/bin/mdtopdf"
} else {
e = "mdtopdf"
}
examName := strings.ToUpper(courseID) + "-Exam-"
err = exec.Command(e, "-i", filepath.Join(base, "examSolutions.md"), "-o", filepath.Join(base, "/" + examName + "Solutions.pdf")).Run()
if err != nil {
log.Fatal(err)
}
err = exec.Command(e, "-i", filepath.Join(base, "examQuestions.md"), "-o", filepath.Join(base, "/" + examName + "Questions.pdf")).Run()
if err != nil {
log.Fatal(err)
}
dir, _ := os.Getwd()
fmt.Println("done! generated exam", id, "to", base, "pwd:", dir)
w.WriteHeader(http.StatusOK)
w.Write([]byte("<a target='_blank' href='/files/" + id + "/" + examName + "Questions.pdf'>" + examName + "Questions.pdf</a><br><a target='_blank' href='/files/" + id + "/"+examName + "Solutions.pdf'>" + examName + "Solutions.pdf</a><br>"))
}