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 stage: assessment
.mode
!= "open" ? 0 : 1,
27 remainingTime: 0, //global, in seconds
31 countdown: function() {
32 let seconds
= this.remainingTime
% 60;
33 let minutes
= Math
.floor(this.remainingTime
/ 60);
34 return this.padWithZero(minutes
) + ":" + this.padWithZero(seconds
);
36 showAnswers: function() {
37 return this.stage
== 4;
42 if (assessment
.mode
!= "secure")
44 window
.addEventListener("keydown", e
=> {
45 // Ignore F12 (avoid accidental window resize due to devtools)
46 // NOTE: in Chromium at least, fullscreen mode exit with F11 cannot be prevented.
47 // Workaround: disable key at higher level. Possible xbindkey config:
54 window
.addEventListener("blur", () => {
55 this.trySendCurrentAnswer();
56 document
.location
.href
= "/noblur";
58 window
.addEventListener("resize", e
=> {
59 this.trySendCurrentAnswer();
60 document
.location
.href
= "/fullscreen";
64 // In case of AJAX errors
65 showWarning: function(message
) {
66 this.warnMsg
= message
;
67 $("#warning").modal("open");
69 padWithZero: function(x
) {
74 trySendCurrentAnswer: function() {
79 getStudent: function(cb
) {
80 $.ajax("/get/student", {
83 number: this.student
.number
,
89 return this.showWarning(s
.errmsg
);
91 this.student
= s
.student
;
92 Vue
.nextTick( () => { Materialize
.updateTextFields(); });
99 cancelStudent: function() {
102 // stage 1 --> 2 (get all questions, set password)
103 startAssessment: function() {
104 let initializeStage2
= (questions
,paper
) => {
105 $("#leftButton, #rightButton").hide();
106 if (assessment
.time
> 0)
108 const deltaTime
= !!paper
? Date
.now() - paper
.startTime : 0;
109 this.remainingTime
= assessment
.time
* 60 - Math
.round(deltaTime
/ 1000);
112 // Initialize structured answer(s) based on questions type and nesting (TODO: more general)
114 assessment
.questions
= questions
;
115 this.answers
.inputs
= [ ];
116 for (let q
of assessment
.questions
)
117 this.answers
.inputs
.push( _(q
.options
.length
).times( _
.constant(false) ) );
120 this.answers
.indices
= assessment
.fixed
121 ? _
.range(assessment
.questions
.length
)
122 : _
.shuffle( _
.range(assessment
.questions
.length
) );
127 let indices
= paper
.inputs
.map( input
=> { return input
.index
; });
128 let remainingIndices
= _
.difference( _
.range(assessment
.questions
.length
).map(String
), indices
);
129 this.answers
.indices
= indices
.concat( _
.shuffle(remainingIndices
) );
131 this.answers
.index
= !!paper
? paper
.inputs
.length : 0;
132 Vue
.nextTick(statementsLibsRefresh
);
135 if (assessment
.mode
== "open")
136 return initializeStage2();
137 $.ajax("/start/assessment", {
140 number: this.student
.number
,
146 return this.showWarning(s
.errmsg
);
149 // Resuming: receive stored answers + startTime
150 this.student
.password
= s
.paper
.password
;
151 this.answers
.inputs
= s
.paper
.inputs
.map( inp
=> { return inp
.input
; });
155 this.student
.password
= s
.password
;
156 // Got password: students answers locked to this page until potential teacher
157 // action (power failure, computer down, ...)
159 socket
= io
.connect("/" + assessment
.name
, {
160 query: "aid=" + assessment
._id
+ "&number=" + this.student
.number
+ "&password=" + this.student
.password
162 socket
.on(message
.allAnswers
, this.setAnswers
);
163 initializeStage2(s
.questions
, s
.paper
);
168 runTimer: function() {
169 if (assessment
.time
<= 0)
172 setInterval( function() {
173 self
.remainingTime
--;
174 if (self
.remainingTime
<= 0)
177 self
.endAssessment();
183 sendAnswer: function() {
184 const realIndex
= this.answers
.indices
[this.answers
.index
];
185 let gotoNext
= () => {
186 if (this.answers
.index
== assessment
.questions
.length
- 1)
187 this.endAssessment();
189 this.answers
.index
++;
190 this.$children
[0].$forceUpdate(); //TODO: bad HACK, and shouldn't be required...
192 if (assessment
.mode
== "open")
193 return gotoNext(); //only local
196 answer: JSON
.stringify({
197 index: realIndex
.toString(),
198 input: this.answers
.inputs
[realIndex
]
199 .map( (tf
,i
) => { return {val:tf
,idx:i
}; } )
200 .filter( item
=> { return item
.val
; })
201 .map( item
=> { return item
.idx
; })
203 number: this.student
.number
,
204 password: this.student
.password
,
206 $.ajax("/send/answer", {
212 return this.showWarning(ret
.errmsg
);
214 socket
.emit(message
.newAnswer
, answerData
);
218 // stage 2 --> 3 (or 4)
219 // from a message by statements component, or time over
220 endAssessment: function() {
221 // Set endTime, destroy password
222 $("#leftButton, #rightButton").show();
223 if (assessment
.mode
== "open")
228 $.ajax("/end/assessment", {
232 number: this.student
.number
,
233 password: this.student
.password
,
238 return this.showWarning(ret
.errmsg
);
239 assessment
.conclusion
= ret
.conclusion
;
241 delete this.student
["password"]; //unable to send new answers now
247 // stage 3 --> 4 (on socket message "feedback")
248 setAnswers: function(m
) {
249 for (let i
=0; i
<m
.answers
.length
; i
++)
250 assessment
.questions
[i
].answer
= m
.answers
[i
];