Commit | Line | Data |
---|---|---|
43828378 BA |
1 | const CourseModel = require("../models/course"); |
2 | const UserModel = require("../models/user"); | |
e99c53fb | 3 | const ObjectId = require("bson-objectid"); |
e99c53fb | 4 | const TokenGen = require("../utils/tokenGenerator"); |
43828378 | 5 | const db = require("../utils/database"); |
e99c53fb | 6 | |
a3080c33 | 7 | const EvaluationModel = |
e99c53fb | 8 | { |
43828378 BA |
9 | /* |
10 | * Structure: | |
11 | * _id: BSON id | |
12 | * cid: course ID | |
13 | * name: varchar | |
a80c6a3b BA |
14 | * active: boolean |
15 | * mode: secure | watch | exam | open (decreasing security) | |
43828378 BA |
16 | * fixed: bool (questions in fixed order; default: false) |
17 | * display: "one" or "all" (generally "all" for open questions, but...) | |
a80c6a3b | 18 | * time: 0, global (one vaue) or per question (array of integers) |
43828378 BA |
19 | * introduction: "", |
20 | * coefficient: number, default 1 | |
21 | * questions: array of | |
22 | * index: for paper test, like 2.1.a (?!); and quiz: 0, 1, 2, 3... | |
23 | * wording: varchar (HTML) | |
24 | * options: array of varchar --> if present, question type == quiz! | |
25 | * fixed: bool, options in fixed order (default: false) | |
26 | * answer: array of integers (for quiz) or html text (for paper); striped in exam mode | |
a3080c33 | 27 | * active: boolean, is question in current evaluation? |
43828378 | 28 | * points: points for this question (default 1) |
73609d3b | 29 | * param: parameter (if applicable) |
43828378 BA |
30 | * papers : array of |
31 | * number: student number | |
32 | * inputs: array of {index,answer[array of integers or html text],startTime} | |
33 | * current: index of current question (if relevant: display="one") | |
34 | * startTime | |
35 | * discoTime, totalDisco: last disconnect timestamp (if relevant) + total time | |
36 | * discoCount: number of disconnections | |
37 | * password: random string identifying student for exam session TEMPORARY | |
38 | */ | |
39 | ||
40 | ////////////////// | |
41 | // BASIC FUNCTIONS | |
42 | ||
a3080c33 | 43 | getById: function(eid, callback) |
43828378 | 44 | { |
a3080c33 BA |
45 | db.evaluations.findOne( |
46 | { _id: eid }, | |
43828378 BA |
47 | callback |
48 | ); | |
49 | }, | |
50 | ||
51 | getByPath: function(cid, name, callback) | |
52 | { | |
a3080c33 | 53 | db.evaluations.findOne( |
43828378 BA |
54 | { |
55 | cid: cid, | |
56 | name: name, | |
57 | }, | |
58 | callback | |
59 | ); | |
60 | }, | |
61 | ||
62 | insert: function(cid, name, callback) | |
63 | { | |
a3080c33 | 64 | db.evaluations.insert( |
43828378 BA |
65 | { |
66 | name: name, | |
67 | cid: cid, | |
68 | active: false, | |
69 | mode: "exam", | |
70 | fixed: false, | |
71 | display: "one", | |
72 | time: 0, | |
73 | introduction: "", | |
74 | coefficient: 1, | |
75 | questions: [ ], | |
76 | papers: [ ], | |
77 | }, | |
78 | callback | |
79 | ); | |
80 | }, | |
81 | ||
82 | getByCourse: function(cid, callback) | |
83 | { | |
a3080c33 | 84 | db.evaluations.find( |
43828378 BA |
85 | { cid: cid }, |
86 | callback | |
87 | ); | |
88 | }, | |
89 | ||
a3080c33 BA |
90 | // arg: full evaluation without _id field |
91 | replace: function(eid, evaluation, cb) | |
43828378 BA |
92 | { |
93 | // Should be: (but unsupported by mongojs) | |
a3080c33 BA |
94 | // db.evaluations.replaceOne( |
95 | // { _id: eid }, | |
96 | // evaluation, | |
43828378 BA |
97 | // cb |
98 | // ); | |
99 | // Temporary workaround: | |
a3080c33 BA |
100 | db.evaluations.update( |
101 | { _id: eid }, | |
102 | { $set: evaluation }, | |
43828378 BA |
103 | cb |
104 | ); | |
105 | }, | |
106 | ||
a3080c33 | 107 | getQuestions: function(eid, callback) |
43828378 | 108 | { |
a3080c33 | 109 | db.evaluations.findOne( |
43828378 | 110 | { |
a3080c33 | 111 | _id: eid, |
43828378 BA |
112 | display: "all", |
113 | }, | |
114 | { questions: 1}, | |
115 | (err,res) => { | |
116 | callback(err, !!res ? res.questions : null); | |
117 | } | |
118 | ); | |
119 | }, | |
120 | ||
a3080c33 | 121 | getQuestion: function(eid, index, callback) |
43828378 | 122 | { |
a3080c33 | 123 | db.evaluations.findOne( |
43828378 | 124 | { |
a3080c33 | 125 | _id: eid, |
43828378 BA |
126 | display: "one", |
127 | }, | |
128 | { questions: 1}, | |
129 | (err,res) => { | |
130 | if (!!err || !res) | |
131 | return callback(err, res); | |
132 | const qIdx = res.questions.findIndex( item => { return item.index == index; }); | |
133 | if (qIdx === -1) | |
134 | return callback({errmsg: "Question not found"}, null); | |
135 | callback(null, res.questions[qIdx]); | |
136 | } | |
137 | ); | |
138 | }, | |
139 | ||
a3080c33 | 140 | getPaperByNumber: function(eid, number, callback) |
43828378 | 141 | { |
a3080c33 | 142 | db.evaluations.findOne( |
43828378 | 143 | { |
a3080c33 | 144 | _id: eid, |
43828378 BA |
145 | "papers.number": number, |
146 | }, | |
147 | (err,a) => { | |
148 | if (!!err || !a) | |
149 | return callback(err,a); | |
150 | for (let p of a.papers) | |
151 | { | |
152 | if (p.number == number) | |
153 | return callback(null,p); //reached for sure | |
154 | } | |
155 | } | |
156 | ); | |
157 | }, | |
158 | ||
159 | // NOTE: no callbacks for 2 next functions, failures are not so important | |
160 | // (because monitored: teachers can see what's going on) | |
161 | ||
a3080c33 | 162 | addDisco: function(eid, number, deltaTime) |
43828378 | 163 | { |
a3080c33 | 164 | db.evaluations.update( |
43828378 | 165 | { |
a3080c33 | 166 | _id: eid, |
43828378 BA |
167 | "papers.number": number, |
168 | }, | |
169 | { $inc: { | |
170 | "papers.$.discoCount": 1, | |
171 | "papers.$.totalDisco": deltaTime, | |
172 | } }, | |
173 | { $set: { "papers.$.discoTime": null } } | |
174 | ); | |
175 | }, | |
176 | ||
a3080c33 | 177 | setDiscoTime: function(eid, number) |
43828378 | 178 | { |
a3080c33 | 179 | db.evaluations.update( |
43828378 | 180 | { |
a3080c33 | 181 | _id: eid, |
43828378 BA |
182 | "papers.number": number, |
183 | }, | |
184 | { $set: { "papers.$.discoTime": Date.now() } } | |
185 | ); | |
186 | }, | |
187 | ||
a3080c33 | 188 | getDiscoTime: function(eid, number, cb) |
43828378 | 189 | { |
a3080c33 BA |
190 | db.evaluations.findOne( |
191 | { _id: eid }, | |
43828378 BA |
192 | (err,a) => { |
193 | if (!!err) | |
194 | return cb(err, null); | |
195 | const idx = a.papers.findIndex( item => { return item.number == number; }); | |
196 | cb(null, a.papers[idx].discoTime); | |
197 | } | |
198 | ); | |
199 | }, | |
200 | ||
a3080c33 | 201 | hasInput: function(eid, number, password, idx, cb) |
43828378 | 202 | { |
a3080c33 | 203 | db.evaluations.findOne( |
43828378 | 204 | { |
a3080c33 | 205 | _id: eid, |
43828378 BA |
206 | "papers.number": number, |
207 | "papers.password": password, | |
208 | }, | |
209 | (err,a) => { | |
210 | if (!!err || !a) | |
211 | return cb(err,a); | |
212 | let papIdx = a.papers.findIndex( item => { return item.number == number; }); | |
213 | for (let i of a.papers[papIdx].inputs) | |
214 | { | |
215 | if (i.index == idx) | |
216 | return cb(null,true); | |
217 | } | |
218 | cb(null,false); | |
219 | } | |
220 | ); | |
221 | }, | |
222 | ||
223 | // https://stackoverflow.com/questions/27874469/mongodb-push-in-nested-array | |
a3080c33 | 224 | setInput: function(eid, number, password, input, callback) //input: index + arrayOfInt (or txt) |
43828378 | 225 | { |
a3080c33 | 226 | db.evaluations.update( |
43828378 | 227 | { |
a3080c33 | 228 | _id: eid, |
43828378 BA |
229 | "papers.number": number, |
230 | "papers.password": password, | |
231 | }, | |
232 | { $push: { "papers.$.inputs": input } }, | |
233 | callback | |
234 | ); | |
235 | }, | |
236 | ||
a3080c33 | 237 | endEvaluation: function(eid, number, password, callback) |
43828378 | 238 | { |
a3080c33 | 239 | db.evaluations.update( |
43828378 | 240 | { |
a3080c33 | 241 | _id: eid, |
43828378 BA |
242 | "papers.number": number, |
243 | "papers.password": password, | |
244 | }, | |
245 | { $set: { | |
246 | "papers.$.endTime": Date.now(), | |
247 | "papers.$.password": "", | |
248 | } }, | |
249 | callback | |
250 | ); | |
251 | }, | |
252 | ||
a3080c33 | 253 | remove: function(eid, cb) |
43828378 | 254 | { |
a3080c33 BA |
255 | db.evaluations.remove( |
256 | { _id: eid }, | |
43828378 BA |
257 | cb |
258 | ); | |
259 | }, | |
260 | ||
261 | removeGroup: function(cid, cb) | |
262 | { | |
a3080c33 | 263 | db.evaluations.remove( |
43828378 BA |
264 | { cid: cid }, |
265 | cb | |
266 | ); | |
267 | }, | |
268 | ||
269 | ///////////////////// | |
270 | // ADVANCED FUNCTIONS | |
271 | ||
e99c53fb BA |
272 | getByRefs: function(initials, code, name, cb) |
273 | { | |
43828378 | 274 | UserModel.getByInitials(initials, (err,user) => { |
e99c53fb BA |
275 | if (!!err || !user) |
276 | return cb(err || {errmsg: "User not found"}); | |
43828378 | 277 | CourseModel.getByPath(user._id, code, (err2,course) => { |
e99c53fb BA |
278 | if (!!err2 || !course) |
279 | return cb(err2 || {errmsg: "Course not found"}); | |
a3080c33 BA |
280 | EvaluationModel.getByPath(course._id, name, (err3,evaluation) => { |
281 | if (!!err3 || !evaluation) | |
282 | return cb(err3 || {errmsg: "Evaluation not found"}); | |
283 | cb(null,evaluation); | |
e99c53fb BA |
284 | }); |
285 | }); | |
286 | }); | |
287 | }, | |
288 | ||
a3080c33 | 289 | checkPassword: function(eid, number, password, cb) |
71d1ca9c | 290 | { |
a3080c33 BA |
291 | EvaluationModel.getById(eid, (err,evaluation) => { |
292 | if (!!err || !evaluation) | |
293 | return cb(err, evaluation); | |
294 | const paperIdx = evaluation.papers.findIndex( item => { return item.number == number; }); | |
71d1ca9c BA |
295 | if (paperIdx === -1) |
296 | return cb({errmsg: "Paper not found"}, false); | |
a3080c33 | 297 | cb(null, evaluation.papers[paperIdx].password == password); |
71d1ca9c BA |
298 | }); |
299 | }, | |
300 | ||
e99c53fb BA |
301 | add: function(uid, cid, name, cb) |
302 | { | |
303 | // 1) Check that course is owned by user of ID uid | |
43828378 | 304 | CourseModel.getById(cid, (err,course) => { |
e99c53fb BA |
305 | if (!!err || !course) |
306 | return cb({errmsg: "Course retrieval failure"}); | |
307 | if (!course.uid.equals(uid)) | |
308 | return cb({errmsg:"Not your course"},undefined); | |
a3080c33 BA |
309 | // 2) Insert new blank evaluation |
310 | EvaluationModel.insert(cid, name, cb); | |
e99c53fb BA |
311 | }); |
312 | }, | |
313 | ||
a3080c33 | 314 | update: function(uid, evaluation, cb) |
e99c53fb | 315 | { |
a3080c33 BA |
316 | const eid = ObjectId(evaluation._id); |
317 | // 1) Check that evaluation is owned by user of ID uid | |
318 | EvaluationModel.getById(eid, (err,evaluationOld) => { | |
319 | if (!!err || !evaluationOld) | |
320 | return cb({errmsg: "Evaluation retrieval failure"}); | |
321 | CourseModel.getById(ObjectId(evaluationOld.cid), (err2,course) => { | |
e99c53fb BA |
322 | if (!!err2 || !course) |
323 | return cb({errmsg: "Course retrieval failure"}); | |
324 | if (!course.uid.equals(uid)) | |
325 | return cb({errmsg:"Not your course"},undefined); | |
a3080c33 BA |
326 | // 2) Replace evaluation |
327 | delete evaluation["_id"]; | |
328 | evaluation.cid = ObjectId(evaluation.cid); | |
329 | EvaluationModel.replace(eid, evaluation, cb); | |
e99c53fb BA |
330 | }); |
331 | }); | |
332 | }, | |
333 | ||
334 | // Set password in responses collection | |
a3080c33 | 335 | startSession: function(eid, number, password, cb) |
e99c53fb | 336 | { |
a3080c33 | 337 | EvaluationModel.getPaperByNumber(eid, number, (err,paper) => { |
f03a2ad9 BA |
338 | if (!!err) |
339 | return cb(err,null); | |
71d1ca9c BA |
340 | if (!paper && !!password) |
341 | return cb({errmsg: "Cannot start a new exam before finishing current"},null); | |
f03a2ad9 BA |
342 | if (!!paper) |
343 | { | |
344 | if (!password) | |
71d1ca9c | 345 | return cb({errmsg: "Missing password"}); |
f03a2ad9 | 346 | if (paper.password != password) |
71d1ca9c | 347 | return cb({errmsg: "Wrong password"}); |
f03a2ad9 | 348 | } |
a3080c33 | 349 | EvaluationModel.getQuestions(eid, (err2,questions) => { |
43828378 BA |
350 | if (!!err2) |
351 | return cb(err2,null); | |
f03a2ad9 | 352 | if (!!paper) |
43828378 | 353 | return cb(null,{paper:paper}); |
2c545c26 | 354 | const pwd = TokenGen.generate(12); //arbitrary number, 12 seems enough... |
a3080c33 BA |
355 | db.evaluations.update( |
356 | { _id: eid }, | |
43828378 BA |
357 | { $push: { papers: { |
358 | number: number, | |
359 | startTime: Date.now(), | |
360 | endTime: undefined, | |
361 | password: password, | |
362 | totalDisco: 0, | |
363 | discoCount: 0, | |
364 | inputs: [ ], //TODO: this is stage 1, stack indexed answers. | |
365 | // then build JSON tree for easier access / correct | |
366 | }}}, | |
367 | (err3,ret) => { cb(err3,{password:password}); } | |
368 | ); | |
e99c53fb BA |
369 | }); |
370 | }); | |
371 | }, | |
372 | ||
a3080c33 | 373 | newAnswer: function(eid, number, password, input, cb) |
f03a2ad9 | 374 | { |
f03a2ad9 | 375 | // Check that student hasn't already answered |
a3080c33 | 376 | EvaluationModel.hasInput(eid, number, password, input.index, (err,ret) => { |
f03a2ad9 BA |
377 | if (!!err) |
378 | return cb(err,null); | |
379 | if (!!ret) | |
380 | return cb({errmsg:"Question already answered"},null); | |
a3080c33 | 381 | EvaluationModel.setInput(eid, number, password, input, (err2,ret2) => { |
f03a2ad9 BA |
382 | if (!!err2 || !ret2) |
383 | return cb(err2,ret2); | |
384 | return cb(null,ret2); | |
385 | }); | |
386 | }); | |
387 | }, | |
388 | ||
43828378 | 389 | // NOTE: no callbacks for next function, failures are not so important |
29c8b391 | 390 | // (because monitored: teachers can see what's going on) |
a3080c33 | 391 | newConnection: function(eid, number) |
29c8b391 BA |
392 | { |
393 | //increment discoCount, reset discoTime to NULL, update totalDisco | |
a3080c33 | 394 | EvaluationModel.getDiscoTime(eid, number, (err,discoTime) => { |
29c8b391 | 395 | if (!!discoTime) |
a3080c33 | 396 | EvaluationModel.addDisco(eid, number, Date.now() - discoTime); |
29c8b391 BA |
397 | }); |
398 | }, | |
43828378 | 399 | } |
e99c53fb | 400 | |
a3080c33 | 401 | module.exports = EvaluationModel; |