-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaoauto.js
366 lines (347 loc) · 12.9 KB
/
taoauto.js
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
var currentRow=0;
var response={}
var codeScript
var warnings=""
function init() {
if(typeof responses!="undefined") {
if(typeof data.script!="undefined") var script=data.script
else {
var script=''
for(var i=0; i<Object.keys(responses[0].response).length;i++) {
script+='\rvar resp'+(i+1)+'=response["'+Object.keys(responses[0].response)[i]+'"];';
}
script+='\rreturn {'
if($(".itemvalue").length>0) {
script+=$(".itemvalue").map(function(i) {return '\r '+$(this).data("item_name")+':response["'+Object.keys(responses[0].response)[i]+'"]=="correct"?1:0';
}).get().join(",")
} else script+='\r item1:resp1=="correct"?1:0'
script+='\r}'
}
quill.setContents([
{ insert: script+'\n'}
]);
quill.formatLine(0,quill.getLength(),"code-block",true)
codeScript=setScript()
$(".nextautoresponse").click(function() {
currentRow+=($(this).data("next")==">"?1:-1)
if(currentRow<0) currentRow=responses.length
if(currentRow>responses.length) currentRow=0
codeScript=setScript()
codeCurrentRow()
updatestats()
})
$("#rescoreThisBtn").click(function() {
codeScript=setScript()
codeCurrentRow()
updatestats()
})
$("#rescoreAllBtn").click(function() {
codeScript=setScript()
warnings=""
showWait(true)
setTimeout(function() {
for(currentRow=responses.length-1;currentRow>=0;currentRow--) {
codeCurrentRow();
}
showWait(false)
updatestats()
if(warnings.length>0) showWarning(warnings,10000)
},100)
})
codeCurrentRow();
}
}
function setScript() {
try {
var newScript=new Function ('response','"use strict";'+quill.getText(0))
return newScript
} catch (e) { alert("Error:"+e.message);}
}
function codeCurrentRow() {
$("#response_id").val(responses[currentRow].response_id)
response=responses[currentRow].response
for(task_name in response) { $('[data-task_name="'+task_name+'"]').html(response[task_name]) }
responses[currentRow] = Object.assign(responses[currentRow],codeScript(response))
fillItems()
}
function fillItems() {
$(".itemvalue").each(function() {
$(this).val(responses[currentRow][$(this).data("item_name")])
})
}
function save() {
var script=quill.getText(0)
data={script:script}
}
///////////////////////
// MC-functions
function makeMC(resp) {
// MC is in a format that is simlar to JSON, but we need to convert it a little...
resp=(resp==""?"[]":resp.replace(/'/g,"\"").replace(/;/g,","))
try {
return JSON.parse(resp)
} catch(e) {
var error=_("Error in JSON. response_id: <b>{0}</b> resp: <pre style=\"overflow:scroll;max-height:30px\">{1}</pre>. Message: {2}<br>",$("#response_id").val(),resp,e.message)
warnings+=error
return {}
}
}
function scoreMC(resp,correct=[]) {
if(typeof resp !== 'array') resp=makeMC(resp)
return (resp.filter(value => correct.includes(value))?1:0)
}
function scoreSC(resp,correct=[]) {
return (correct.indexOf(resp)>-1?1:0)
}
///////////////////////
// Match-functions
function makeMatch(resp, variablesInColumns=false) {
// Match is in a format that is simlar to JSON, but we need to convert it a little...
resp=(resp==""?"{}":resp.replace(/; /g,",").replace(/ /g,":").replace("[","{").replace("]","}").replace(/([0-9a-z_]+)/gi,'"$1"'))
if(variablesInColumns) {
resp=resp.replace(/(".*?"):(".*?")/g,"$2:$1")
}
try {
return JSON.parse(resp)
} catch(e) {
var error=_("Error in JSON. response_id: <b>{0}</b> resp: <pre style=\"overflow:scroll;max-height:30px\">{1}</pre>. Message: {2}<br>",$("#response_id").val(),resp,e.message)
warnings+=error
return {}
}
}
function scoreMatch(resp,identifier,correct=[],variablesInColumns = false) {
if(typeof resp !== 'object') resp=makeMatch(resp,variablesInColumns)
return (correct.indexOf(resp[identifier])>-1?1:0)
}
///////////////////////
// GapMatch-functions
function makeTextGapMatch(resp) {
// GapMatch is in a format that is simlar to JSON, but we need to convert it a little...
resp=(resp==""?"{}":resp)//.replace(/'/g,"\"").replace(/;/g,","))
try {
return JSON.parse(resp)
} catch(e) {
var error=_("Error in JSON. response_id: <b>{0}</b> resp: <pre style=\"overflow:scroll;max-height:30px\">{1}</pre>. Message: {2}<br>",$("#response_id").val(),resp,e.message)
warnings+=error
return {}
}
}
function textInGap(resp,text,gaps,noWhereElse=true) {
if(typeof resp !== 'object') resp=makeTextGapMatch(resp)
var isIn=false
for(gap of gaps) {
if(typeof resp[gap]!="undefined" && resp[gap].indexOf(text)>-1) {
isIn=true
break
}
}
if(isIn && noWhereElse) {
var gaps=Object.keys(resp).filter(x => !gaps.includes(x));
for(gap of gaps) {
if(typeof resp[gap]!="undefined" && resp[gap].indexOf(text)>-1) {
isIn=false
break
}
}
}
return isIn?1:0;
}
///////////////////////
// Gantt
function makeGantt(gantt,names=[],timespan=30,timeFormat="d/M H:mm") { // https://moment.github.io/luxon////parsing?id=table-of-tokens
//Creaate object
var ganttobj={gantt:{},names:names,timespan:timespan,timeFormat:timeFormat}
// Extract intervals from JSON
ganttarr=(gantt.length>0?JSON.parse(gantt.replace(/'/g,'"')).response:"")
//Eksplode to list of lists
ganttarr=ganttarr.split(";")
//Give rows names and split rows
for(var i=0;i<ganttarr.length;i++) {
ganttobj.gantt[names[i]]=ganttarr[i].split(", ").map(function(x) {return x.split(" - ").map(function (t) {return DateTime.fromFormat(t,timeFormat)}).flat()}).flat()
}
return ganttobj
}
function isBefore(gantt,a=[],b=[],whichA="all",whichB="all",orEqual=false,strict=true,strictAfter=true) {
//strictAfter is only used internally to control strict from isAfter
if(whichA=="last") whichA="all" //Just to help the designer
if(whichB=="first") whichB="all" //do.
if(typeof b!="object") b=[b]
if(typeof a!="object") a=[a]
var res=1
for(var i of a) {
for(var j of b) {
if(typeof gantt.gantt[i]!="undefined" && typeof gantt.gantt[j]!="undefined") {
var aval=(whichA=="all"?gantt.gantt[i][gantt.gantt[i].length-1]:(whichA=="first"?gantt.gantt[i][0]:gantt.gantt[i][whichA])) // All, first or Given number
var bval=(whichB=="all"?gantt.gantt[j][gantt.gantt[j].length-1]:(whichB=="last"?gantt.gantt[j][0]:gantt.gantt[j][whichB])) // All, last or Given number
if(!strict & typeof bval=="undefined") bval=Infinity
if(!strictAfter & typeof aval=="undefined") aval=0
var thisres=orEqual?aval <= bval:aval < bval
res=typeof thisres=="undefined"?0:res & thisres
}
}
}
return(res)
}
function isFirst(gantt,a="",which="all",orEqual=false,strict=true) {
var res=1
for(var i of gantt.names) {
if(a!=i && !isBefore(gantt,a,i,which,"all",orEqual,strict)) {
res=0
break
}
}
return(res)
}
function isAfter(gantt,a=[],b=[],whichA="all",whichB="all",orEqual=false,strict=true) {
return isBefore(gantt,b,a,whichB,whichA,orEqual,true,strict)
}
function isLast(gantt,a=[],which="all",orEqual=false,strict=true) {
var res=1
for(i of gantt.names) {
if(a!=i) res=(res & isAfter(gantt,a,i,which,"all",orEqual,strict))
}
return(res)
}
// @param whichA/whichB for isOverlap: any: one or more a/b elements overlap, all: all a/b elements need to overlap, first/first: First a/b needs to overlap, last/last: Last a/b needs to overlap, or use number (or sequence)
// @rdname isBefore
// @export
function isOverlap(gantt,a="",b=[],whichA="any",whichB="any") {
if(typeof b!="object") b=[b]
var res=whichA=="all"
var aval=[]
switch(whichA) {
case "all":
case "any":
aval=gantt.gantt[a] // Test all elements
break
case "first":
aval=gantt.gantt[a][1] // First
break
case "last":
aval=gantt.gantt[a][gantt.gantt[a].length-1] // Last
break
default:
aval=gantt.gantt[a][whichA] // Given number (or sequence)
}
for(i of b) {
var ival=[]
switch(whichB) {
case "all":
case "any":
ival=gantt.gantt[i] // Test all elements
break
case "first":
ival=gantt.gantt[i][1] // First
break
case "last":
ival=gantt.gantt[i][gantt.gantt[i].length-1] // Last
break
default:
ival=gantt.gantt[i][whichB] // Given number (or sequence)
}
var AinI=aval.filter(x=>ival.includes(x))
res=(whichA=="any" || whichB=="any")?
res | AinI.length>0:
whichA=="all"?
res & AinI.length==ival.length:
res | AinI.length==ival.length
}
return(res?1:0)
}
//noOverlap only if there is actually elements that could have overlapped.
//b: character or vector of characters
// @rdname isBefore
// @export
function noOverlap(gantt,a=[],b=[]) {
if(typeof b!="object") b=[b]
var res=1
for(i of b) {
res=res & (gantt.gantt[a].length>0 & gantt.gantt[i].length>0?
gantt.gantt[a].filter(x=>gantt.gantt[i].includes(x)).length>0:
0)
}
return (res)
}
// numSlots
//
// @param gantt a gantt object (created by makeGantt)
// @param a an element
//
// @return Returns number of time slots - also counting non-connected slots
// @export
//
// @examples
// response="{'response':'1/8 10:30 - 1/8 12:30;1/8 13:00 - 1/8 14:00;1/8 11:30 - 1/8 12:30'}"
// gantt=makeGantt(response,names=c("waitress","actor","pianist"),timespan=30,timeFormat="%d/%m %H:%M")
// numSlots(gantt,"actor")
function numSlots(gantt,a=[]) {
gantt.gantt[a].length
}
// Get start or end time of an element
//
// @param gantt A gantt object (created by makeGantt)
// @param a an element
// @param which start, end
// @param humanReadable If true, time is given in human readable format
//
// @return
// @export
//
// @examples
// response="{'response':'1/8 10:30;1/8 13:00 - 1/8 14:00;1/8 11:30 - 1/8 12:30'}"
// gantt=makeGantt(response,names=c("waitress","actor","pianist"),timespan=30,timeFormat="%d/%m %H:%M")
// getTime(gantt,"waitress",which="end",humanReadable=true)
// getTime(gantt,"actor",which="end",humanReadable=true)
function getTime(gantt,a=[],which="start",humanReadable=false) {
if(typeof a!="object") a=[a]
if(typeof gantt.gantt[a]=="undefined") return(NaN)
var time=(which=="start"?gantt.gantt[a][0].valueOf():gantt.gantt[a][gantt.gantt[a].length-1].valueOf()+gantt.timespan*60)
return (humanReadable?time.toFormat(gantt.timeFormat):time)
}
// Get the minimum/maximum start/end time among elements
//
// @param gantt A gantt object (created by makeGantt)
// @param a a vector of elements to compare
// @param which Which time to compare ("start" or "end")
// @param strict If true and an element among the compared is not present, NA is returned.
//
// @return Returns minimum/maximum time in seconds of the earliest/latest element starting/ending
// @export
//
// @examples
// response=matrix(c("{'response':'1/8 17:00;;1/8 17:00 - 1/8 18:00;1/8 18:00, 1/8 20:00 - 1/8 21:00;1/8 20:30;1/8 20:00 - 1/8 20:30;1/8 19:30 - 1/8 20:30'}"))
// gantt=makeGantt(response,names=c("waitress","actor","pianist","bartender","cleaning","ticketer","musician"),timespan=30,timeFormat="%d/%m %H:%M")
// getMinTime(gantt,c("pianist","bartender"),which="start")
// getMaxTime(gantt,c("actor","bartender"),which="end",strict=false)
// getMaxTime(gantt,"actor",which="end")
// as.difftime(getMaxTime(gantt,c("pianist","bartender"),which="end")-getMinTime(gantt,c("pianist","bartender"),which="start"),units = "secs")
//
function getMinTime(gantt,a,which,strict=true) {
if(typeof a!="object") a=[a]
return a.map(i=>getTime(gantt,i,which)).reduce((x,y)=>Math.min(x,y),Infinity)
}
// a is a vector of names
// @rdname getMinTime
// @export
function getMaxTime(gantt,a,which,strict=true) {
if(typeof a!="object") a=[a]
return a.map(i=>getTime(gantt,i,which)).reduce((x,y)=>Math.max(x,y),0)
}
// Get duration
//
// @param gantt A gantt object (created by makeGantt)
// @param a An element
//
// @return Returns duration in seconds
// @export
//
// @examples
// response=matrix(c("{'response':'1/8 17:00;1/8 17:00 - 1/8 17:30;1/8 17:00 - 1/8 18:00;1/8 17:00, 1/8 18:00, 1/8 19:00, 1/8 20:00;1/8 20:30;1/8 20:00 - 1/8 20:30;1/8 19:30 - 1/8 20:30'}"))
// gantt=makeGantt(response2,names=c("waitress","actor","pianist","bartender","cleaning","ticketer","musician"),timespan=30,timeFormat="%d/%m %H:%M")
// getDuration(gantt,"actor")
// getDuration(gantt,"waitress")
// getDuration(gantt,"bartender")
// getDuration(gantt,"cleaning")
function getDuration(gantt,a) {
return numSlots(gantt,a)*gantt.timespan*60
}