-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexams2json.R
231 lines (206 loc) · 7.95 KB
/
exams2json.R
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
exams2json <- function(file, n = 1L, dir = ".",
name = "R/exams", sname = NULL, qname = NULL,
driver = list(
sweave = list(quiet = quiet, pdf = FALSE, png = !svg, svg = svg, resolution = resolution, width = width, height = height, encoding = encoding),
read = NULL,
transform = NULL,
write = NULL),
quiet = TRUE, resolution = 100, width = 4, height = 4, svg = FALSE, encoding = "",
url = "https://arsnova.eu/api", sessionkey = NULL, jsessionid = NULL,
active = TRUE, votingdisabled = FALSE, showstatistic = FALSE, showanswer = FALSE, abstention = TRUE,
variant = "lecture", ssl.verifypeer = TRUE, fix_choice = FALSE, ...)
{
## names (session, questions)
if(is.null(sname)) sname <- name
if(length(sname) != 2L) sname <- rep(sname, length.out = 2L)
sname[1L] <- abbreviate(sname[1L], 50L)
sname[2L] <- abbreviate(sname[2L], 8L)
if(is.null(qname)) qname <- name
if(length(qname) == 1L && length(file) > 1L) qname <- paste0(qname, "/",
formatC(1:length(file), width = floor(log10(length(file))) + 1L, flag = "0"))
if(length(qname) != length(file)) stop("length of 'file' and 'qname' do not match")
## Markdown transformer (Github flavor for \( rather than $ math mode)
mdtransform <- make_exercise_transform_pandoc(to = "markdown_github+tex_math_single_backslash")
## create JSON/RCurl write with custom options
arsnovawrite <- make_exams_write_arsnova(url = url, sessionkey = sessionkey, jsessionid = jsessionid,
name = name, sname = sname, qname = qname,
active = active, votingdisabled = votingdisabled, showstatistic = showstatistic,
showanswer = showanswer, abstention = abstention, variant = variant,
ssl.verifypeer = ssl.verifypeer, fix_choice = fix_choice)
if(is.null(driver$transform)){
driver$transform = mdtransform;
}
if(is.null(driver$write)){
driver$write = arsnovawrite;
}
## generate xexams
rval <- xexams(file, n = n, dir = dir,
driver = list(
sweave = driver$sweave,
read = driver$read,
transform = driver$transform,
write = driver$write),
...)
## return xexams object invisibly
invisible(rval)
}
make_exams_write_arsnova <- function(url = "https://arsnova.eu/api", sessionkey = NULL, jsessionid = NULL,
name = "R/exams", sname = NULL, qname = NULL, active = TRUE, votingdisabled = FALSE, showstatistic = FALSE,
showanswer = FALSE, abstention = TRUE, variant = "lecture",
ssl.verifypeer = TRUE, fix_choice = FALSE)
{
## question list template
qtemp <- list(
subject = "name",
question = NULL,
questionlist = list(),
solution = list(),
solutionslist = list(),
metainfo = list(),
supplements =list()
#
# # type = "skill_question",
# questionType = "abcd",
# # questionVariant = match.arg(variant, c("lecture", "preparation")),
# subject = "name",
# text = NULL,
# # active = active,
# # releasedFor = "all",
# questionlist = list(),
# solution = list(),
# solutionslist = list(),
# metainfo = list()
# solution = FALSE
# sessionKeyword = sessionkey,
# votingDisabled = votingdisabled,
# showStatistic = showstatistic,
# showAnswer = showanswer,
# abstention = abstention
)
## session list template
stemp <- list(
exportData = list(
session = list(
name = if(is.null(sname)) name else sname[1L],
shortName = if(is.null(sname)) name else sname[2L],
# active = active,
publicPool = list(
# ppAuthorName = NULL,
# ppAuthorMail = NULL,
# ppUniversity = NULL,
# ppLogo = NULL,
# ppSubject = NULL,
# ppLicense = NULL,
# ppLevel = NULL,
# ppDescription = NULL,
# ppFaculty = NULL,
name = if(is.null(sname)) name else sname[1L],
shortName = if(is.null(sname)) name else sname[2L]
)
),
questions = list(),
feedbackQuestions = list()
)
)
fix_choice <- if(fix_choice) {
function(x) {
x <- gsub("\\(", "", x, fixed = TRUE)
x <- gsub("\\)", "", x, fixed = TRUE)
x <- gsub("\\%", "%", x, fixed = TRUE)
x
}
} else {
function(x) x
}
## set up actual write function
function(exm, dir, info)
{
## basic indexes
id <- info$id
n <- info$n
m <- length(exm)
# ## check whether all exercises are schoice/mchoice
# wrong_type <- sapply(1L:m, function(n) exm[[n]]$metainfo$file)[
# !sapply(1L:m, function(n) exm[[n]]$metainfo$type %in% c("schoice", "mchoice", "num", "string"))]
# if(length(wrong_type) > 0) {
# stop(paste("the following exercises are not supported:",
# paste(wrong_type, collapse = ", ")))
# }
## placeholder in session template
stemp$exportData$questions <- vector(mode = "list", length = m)
for(j in 1L:m) {
## copy question template and adapt subject
json <- qtemp
json$subject <- if(is.null(qname)) name else qname[j]
## collapse question text
json$question <- paste(exm[[j]]$question, collapse = "\n")
json$questionlist <- exm[[j]]$questionlist
json$solution <- exm[[j]]$solution
json$solutionlist <- exm[[j]]$solutionlist
json$metainfo <- exm[[j]]$metainfo
json$supplements <- exm[[j]]$supplements
# json$solution <- exm[[j]]$metainfo$solution
# ## questionType and possibleAnswers
# if(exm[[j]]$metainfo$type %in% c("schoice", "mchoice")) {
# json$possibleAnswers <- lapply(seq_along(exm[[j]]$questionlist),
# function(i) list(text = fix_choice(as.vector(exm[[j]]$questionlist)[i]), correct = exm[[j]]$metainfo$solution[i]))
# }
# if(exm[[j]]$metainfo$type == "mchoice") {
# json$questionType <- "mchoice"
# json$noCorrect <- sum(exm[[j]]$metainfo$solution) > 0
# }
# if(exm[[j]]$metainfo$type %in% c("num", "string")) json$questionType <- "freetext"
#
# ## if there is an existing session, just add questions
stemp$exportData$questions[[j]] <- json
}
## collect json file for entire session (rather than individual questions only)
## assign names for output files
fil <- gsub("/", "", name, fixed = TRUE)
fil <- gsub(" ", "-", fil, fixed = TRUE)
fil <- paste0(fil, "-", formatC(id, width = floor(log10(n)) + 1L, flag = "0"))
## json string for whole session
json <- RJSONIO::toJSON(stemp)
## create and copy output json
fil <- paste0(fil, ".json")
writeLines(json, fil)
file.copy(fil, dir, overwrite = TRUE)
# if(is.null(sessionkey)) {
# ## json string for whole session
# json <- RJSONIO::toJSON(stemp)
# ## create and copy output json
# fil <- paste0(fil, ".json")
# writeLines(json, fil)
# file.copy(fil, dir, overwrite = TRUE)
# } else {
# ans <- function(x, i) if(i > length(x$possibleAnswers)) "" else x$possibleAnswers[[i]]$text
# df <- sapply(stemp$exportData$questions, function (x) {
# c(
# "questionType" = x$questionType,
# "questionSubject" = x$subject,
# "question" = x$text,
# "answer1" = ans(x, 1L),
# "answer2" = ans(x, 2L),
# "answer3" = ans(x, 3L),
# "answer4" = ans(x, 4L),
# "answer5" = ans(x, 5L),
# "answer6" = ans(x, 6L),
# "answer7" = ans(x, 7L),
# "answer8" = ans(x, 8L),
# "correctAnswer" = if(x$questionType == "freetext") "" else {
# paste0(which(sapply(x$possibleAnswers, "[[", "correct")), collapse = ",")
# },
# "abstention" = ifelse(x$abstention, "y", "n"),
# "hint" = "",
# "solution" = ""
# )
# })
# df <- as.data.frame(t(df), stringsAsFactors = FALSE)
# ## create and copy output json
# fil <- paste0(fil, ".csv")
# write.table(df, fil, quote = TRUE, col.names = TRUE, row.names = FALSE, sep = ",")
# file.copy(fil, dir, overwrite = TRUE)
#
# }
}
}