2a451e4a19ad8df24be8ae4dcad59d93d0778533
1 let socket
= null; //monitor answers in real time
3 if (assessment
.mode
== "secure" && !checkWindowSize())
4 document
.location
.href
= "/fullscreen";
6 function checkWindowSize()
8 // NOTE: temporarily accept smartphone (security hole: pretend being a smartphone on desktop browser...)
9 if (navigator
.userAgent
.match(/(iPhone|iPod|iPad|Android|BlackBerry)/))
11 // 3 is arbitrary, but a small tolerance is required (e.g. in Firefox)
12 return window
.innerWidth
>= screen
.width
-3 && window
.innerHeight
>= screen
.height
-3;
18 assessment: assessment
,
19 answers: { }, //filled later with answering parameters
20 student: { }, //filled later (name, password)
21 // Stage 0: unauthenticated (number),
22 // 1: authenticated (got a name, unvalidated)
23 // 2: locked: password set, exam started
26 remainingTime: assessment
.time
, //integer or array
27 stage: assessment
.mode
!= "open" ? 0 : 1,
31 countdown: function() {
32 const remainingTime
= assessment
.display
== "one" && _
.isArray(assessment
.time
)
33 ? this.remainingTime
[this.answers
.index
]
35 let seconds
= remainingTime
% 60;
36 let minutes
= Math
.floor(remainingTime
/ 60);
37 return this.padWithZero(minutes
) + ":" + this.padWithZero(seconds
);
42 if (["exam","open"].includes(assessment
.mode
))
44 window
.addEventListener("blur", () => {
47 if (assessment
.mode
== "secure")
50 document
.location
.href
= "/noblur";
53 socket
.emit(message
.studentBlur
, {number:this.student
.number
});
55 if (assessment
.mode
== "watch")
57 window
.addEventListener("focus", () => {
60 socket
.emit(message
.studentFocus
, {number:this.student
.number
});
63 window
.addEventListener("resize", e
=> {
66 if (assessment
.mode
== "secure")
69 document
.location
.href
= "/fullscreen";
73 if (checkWindowSize())
74 socket
.emit(message
.studentFullscreen
, {number:this.student
.number
});
76 socket
.emit(message
.studentResize
, {number:this.student
.number
});
81 // In case of AJAX errors (not blur-ing)
82 showWarning: function(message
) {
83 this.warnMsg
= message
;
84 $("#warning").modal("open");
86 padWithZero: function(x
) {
92 getStudent: function() {
93 $.ajax("/courses/student", {
96 number: this.student
.number
,
102 return this.showWarning(s
.errmsg
);
104 this.student
= s
.student
;
105 Vue
.nextTick( () => { Materialize
.updateTextFields(); });
110 cancelStudent: function() {
113 // stage 1 --> 2 (get all questions, set password)
114 startAssessment: function() {
115 let initializeStage2
= paper
=> {
116 $("#leftButton, #rightButton").hide();
117 // Initialize structured answer(s) based on questions type and nesting (TODO: more general)
119 // if display == "all" getQuestionS
120 // otherwise get first question
124 assessment
.questions
= questions
;
125 this.answers
.inputs
= [ ];
126 for (let q
of assessment
.questions
)
127 this.answers
.inputs
.push( _(q
.options
.length
).times( _
.constant(false) ) );
130 this.answers
.indices
= assessment
.fixed
131 ? _
.range(assessment
.questions
.length
)
132 : _
.shuffle( _
.range(assessment
.questions
.length
) );
137 let indices
= paper
.inputs
.map( input
=> { return input
.index
; });
138 let remainingIndices
= _
.difference( _
.range(assessment
.questions
.length
).map(String
), indices
);
139 this.answers
.indices
= indices
.concat( _
.shuffle(remainingIndices
) );
148 if (assessment
.time
> 0)
151 // TODO: distinguish total exam time AND question time
153 const deltaTime
= !!paper
? Date
.now() - paper
.startTime : 0;
154 this.remainingTime
= assessment
.time
* 60 - Math
.round(deltaTime
/ 1000);
159 this.answers
.index
= !!paper
? paper
.inputs
.length : 0;
160 this.answers
.displayAll
= assessment
.display
== "all";
161 this.answers
.showSolution
= false;
164 if (assessment
.mode
== "open")
165 return initializeStage2();
166 $.ajax("/assessments/start", {
169 number: this.student
.number
,
175 return this.showWarning(s
.errmsg
);
178 // Resuming: receive stored answers + startTime
179 this.student
.password
= s
.paper
.password
;
180 this.answers
.inputs
= s
.paper
.inputs
.map( inp
=> { return inp
.input
; });
184 this.student
.password
= s
.password
;
185 // Got password: students answers locked to this page until potential teacher
186 // action (power failure, computer down, ...)
188 socket
= io
.connect("/", {
189 query: "aid=" + assessment
._id
+ "&number=" + this.student
.number
+ "&password=" + this.student
.password
191 socket
.on(message
.allAnswers
, this.setAnswers
);
192 initializeStage2(s
.questions
, s
.paper
);
199 runGlobalTimer: function() {
200 if (assessment
.time
<= 0)
203 setInterval( function() {
204 self
.remainingTime
--;
205 if (self
.remainingTime
<= 0)
208 self
.endAssessment();
213 runQuestionTimer: function(idx
) {
214 if (assessment
.questions
[idx
].time
<= 0)
216 let self
= this; //TODO: question remaining time
217 setInterval( function() {
218 self
.remainingTime
--;
219 if (self
.remainingTime
<= 0)
222 self
.endAssessment();
228 //TODO: get question after sending answer
231 sendOneAnswer: function() {
232 const realIndex
= this.answers
.indices
[this.answers
.index
];
233 let gotoNext
= () => {
234 if (this.answers
.index
== assessment
.questions
.length
- 1)
235 this.endAssessment();
237 this.answers
.index
++;
238 this.$children
[0].$forceUpdate(); //TODO: bad HACK, and shouldn't be required...
240 if (assessment
.mode
== "open")
241 return gotoNext(); //only local
244 answer: JSON
.stringify({
245 index: realIndex
.toString(),
246 input: this.answers
.inputs
[realIndex
]
247 .map( (tf
,i
) => { return {val:tf
,idx:i
}; } )
248 .filter( item
=> { return item
.val
; })
249 .map( item
=> { return item
.idx
; })
251 number: this.student
.number
,
252 password: this.student
.password
,
254 $.ajax("/assessments/answer", {
260 return this.showWarning(ret
.errmsg
);
262 socket
.emit(message
.newAnswer
, answerData
);
266 // TODO: I don't like that + sending should not be definitive in exam mode with display = all
267 sendAnswer: function() {
268 if (assessment
.display
== "one")
269 this.sendOneAnswer();
271 assessment
.questions
.forEach(this.sendOneAnswer
);
273 // stage 2 --> 3 (or 4)
274 // from a message by statements component, or time over
275 endAssessment: function() {
276 // Set endTime, destroy password
277 $("#leftButton, #rightButton").show();
278 if (assessment
.mode
== "open")
281 this.answers
.showSolution
= true;
282 this.answers
.displayAll
= true;
285 $.ajax("/assessments/end", {
289 number: this.student
.number
,
290 password: this.student
.password
,
295 return this.showWarning(ret
.errmsg
);
297 delete this.student
["password"]; //unable to send new answers now
301 // stage 3 --> 4 (on socket message "feedback")
302 setAnswers: function(m
) {
303 const answers
= JSON
.parse(m
.answers
);
304 for (let i
=0; i
<answers
.length
; i
++)
305 assessment
.questions
[i
].answer
= answers
[i
];
306 this.answers
.showSolution
= true;
307 this.answers
.displayAll
= true;