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 this.answers
.displayAll
= assessment
.display
== "all";
133 this.answers
.showSolution
= false;
136 if (assessment
.mode
== "open")
137 return initializeStage2();
138 $.ajax("/start/assessment", {
141 number: this.student
.number
,
147 return this.showWarning(s
.errmsg
);
150 // Resuming: receive stored answers + startTime
151 this.student
.password
= s
.paper
.password
;
152 this.answers
.inputs
= s
.paper
.inputs
.map( inp
=> { return inp
.input
; });
156 this.student
.password
= s
.password
;
157 // Got password: students answers locked to this page until potential teacher
158 // action (power failure, computer down, ...)
160 socket
= io
.connect("/" + assessment
.name
, {
161 query: "aid=" + assessment
._id
+ "&number=" + this.student
.number
+ "&password=" + this.student
.password
163 socket
.on(message
.allAnswers
, this.setAnswers
);
164 initializeStage2(s
.questions
, s
.paper
);
169 runTimer: function() {
170 if (assessment
.time
<= 0)
173 setInterval( function() {
174 self
.remainingTime
--;
175 if (self
.remainingTime
<= 0)
178 self
.endAssessment();
184 sendAnswer: function() {
185 const realIndex
= this.answers
.indices
[this.answers
.index
];
186 let gotoNext
= () => {
187 if (this.answers
.index
== assessment
.questions
.length
- 1)
188 this.endAssessment();
190 this.answers
.index
++;
191 this.$children
[0].$forceUpdate(); //TODO: bad HACK, and shouldn't be required...
193 if (assessment
.mode
== "open")
194 return gotoNext(); //only local
197 answer: JSON
.stringify({
198 index: realIndex
.toString(),
199 input: this.answers
.inputs
[realIndex
]
200 .map( (tf
,i
) => { return {val:tf
,idx:i
}; } )
201 .filter( item
=> { return item
.val
; })
202 .map( item
=> { return item
.idx
; })
204 number: this.student
.number
,
205 password: this.student
.password
,
207 $.ajax("/send/answer", {
213 return this.showWarning(ret
.errmsg
);
215 socket
.emit(message
.newAnswer
, answerData
);
219 // stage 2 --> 3 (or 4)
220 // from a message by statements component, or time over
221 endAssessment: function() {
222 // Set endTime, destroy password
223 $("#leftButton, #rightButton").show();
224 if (assessment
.mode
== "open")
227 this.answers
.showSolution
= true;
230 $.ajax("/end/assessment", {
234 number: this.student
.number
,
235 password: this.student
.password
,
240 return this.showWarning(ret
.errmsg
);
241 assessment
.conclusion
= ret
.conclusion
;
243 delete this.student
["password"]; //unable to send new answers now
249 // stage 3 --> 4 (on socket message "feedback")
250 setAnswers: function(m
) {
251 for (let i
=0; i
<m
.answers
.length
; i
++)
252 assessment
.questions
[i
].answer
= m
.answers
[i
];
253 this.answers
.showSolution
= true;