89f2560ca08dbe9ae6517e58b05aa467d1fadc5a
[qomet.git] / models / evaluation.js
1 const CourseModel = require("../models/course");
2 const UserModel = require("../models/user");
3 const ObjectId = require("bson-objectid");
4 const TokenGen = require("../utils/tokenGenerator");
5 const db = require("../utils/database");
6
7 const EvaluationModel =
8 {
9 /*
10 * Structure:
11 * _id: BSON id
12 * cid: course ID
13 * name: varchar
14 * active: boolean
15 * mode: secure | watch | exam | open (decreasing security)
16 * fixed: bool (questions in fixed order; default: false)
17 * display: "one" or "all" (generally "all" for open questions, but...)
18 * time: 0, global (one vaue) or per question (array of integers)
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
27 * active: boolean, is question in current evaluation?
28 * points: points for this question (default 1)
29 * param: parameter (if applicable)
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
43 getById: function(eid, callback)
44 {
45 db.evaluations.findOne(
46 { _id: eid },
47 callback
48 );
49 },
50
51 getByPath: function(cid, name, callback)
52 {
53 db.evaluations.findOne(
54 {
55 cid: cid,
56 name: name,
57 },
58 callback
59 );
60 },
61
62 insert: function(cid, name, callback)
63 {
64 db.evaluations.insert(
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 {
84 db.evaluations.find(
85 { cid: cid },
86 callback
87 );
88 },
89
90 // arg: full evaluation without _id field
91 replace: function(eid, evaluation, cb)
92 {
93 // Should be: (but unsupported by mongojs)
94 // db.evaluations.replaceOne(
95 // { _id: eid },
96 // evaluation,
97 // cb
98 // );
99 // Temporary workaround:
100 db.evaluations.update(
101 { _id: eid },
102 { $set: evaluation },
103 cb
104 );
105 },
106
107 getQuestions: function(eid, callback)
108 {
109 db.evaluations.findOne(
110 {
111 _id: eid,
112 display: "all",
113 },
114 { questions: 1},
115 (err,res) => {
116 callback(err, !!res ? res.questions : null);
117 }
118 );
119 },
120
121 getQuestion: function(eid, index, callback)
122 {
123 db.evaluations.findOne(
124 {
125 _id: eid,
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
140 getPaperByNumber: function(eid, number, callback)
141 {
142 db.evaluations.findOne(
143 {
144 _id: eid,
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
162 addDisco: function(eid, number, deltaTime)
163 {
164 db.evaluations.update(
165 {
166 _id: eid,
167 "papers.number": number,
168 },
169 { $inc: {
170 "papers.$.discoCount": 1,
171 "papers.$.totalDisco": deltaTime,
172 } },
173 { $set: { "papers.$.discoTime": null } }
174 );
175 },
176
177 setDiscoTime: function(eid, number)
178 {
179 db.evaluations.update(
180 {
181 _id: eid,
182 "papers.number": number,
183 },
184 { $set: { "papers.$.discoTime": Date.now() } }
185 );
186 },
187
188 getDiscoTime: function(eid, number, cb)
189 {
190 db.evaluations.findOne(
191 { _id: eid },
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
201 hasInput: function(eid, number, password, idx, cb)
202 {
203 db.evaluations.findOne(
204 {
205 _id: eid,
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
224 setInput: function(eid, number, password, input, callback) //input: index + arrayOfInt (or txt)
225 {
226 db.evaluations.update(
227 {
228 _id: eid,
229 "papers.number": number,
230 "papers.password": password,
231 },
232 { $push: { "papers.$.inputs": input } },
233 callback
234 );
235 },
236
237 endEvaluation: function(eid, number, password, callback)
238 {
239 db.evaluations.update(
240 {
241 _id: eid,
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
253 remove: function(eid, cb)
254 {
255 db.evaluations.remove(
256 { _id: eid },
257 cb
258 );
259 },
260
261 removeGroup: function(cid, cb)
262 {
263 db.evaluations.remove(
264 { cid: cid },
265 cb
266 );
267 },
268
269 /////////////////////
270 // ADVANCED FUNCTIONS
271
272 getByRefs: function(initials, code, name, cb)
273 {
274 UserModel.getByInitials(initials, (err,user) => {
275 if (!!err || !user)
276 return cb(err || {errmsg: "User not found"});
277 CourseModel.getByPath(user._id, code, (err2,course) => {
278 if (!!err2 || !course)
279 return cb(err2 || {errmsg: "Course not found"});
280 EvaluationModel.getByPath(course._id, name, (err3,evaluation) => {
281 if (!!err3 || !evaluation)
282 return cb(err3 || {errmsg: "Evaluation not found"});
283 cb(null,evaluation);
284 });
285 });
286 });
287 },
288
289 checkPassword: function(eid, number, password, cb)
290 {
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; });
295 if (paperIdx === -1)
296 return cb({errmsg: "Paper not found"}, false);
297 cb(null, evaluation.papers[paperIdx].password == password);
298 });
299 },
300
301 add: function(uid, cid, name, cb)
302 {
303 // 1) Check that course is owned by user of ID uid
304 CourseModel.getById(cid, (err,course) => {
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);
309 // 2) Insert new blank evaluation
310 EvaluationModel.insert(cid, name, cb);
311 });
312 },
313
314 update: function(uid, evaluation, cb)
315 {
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) => {
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);
326 // 2) Replace evaluation
327 delete evaluation["_id"];
328 evaluation.cid = ObjectId(evaluation.cid);
329 EvaluationModel.replace(eid, evaluation, cb);
330 });
331 });
332 },
333
334 // Set password in responses collection
335 startSession: function(eid, number, password, cb)
336 {
337 EvaluationModel.getPaperByNumber(eid, number, (err,paper) => {
338 if (!!err)
339 return cb(err,null);
340 if (!paper && !!password)
341 return cb({errmsg: "Cannot start a new exam before finishing current"},null);
342 if (!!paper)
343 {
344 if (!password)
345 return cb({errmsg: "Missing password"});
346 if (paper.password != password)
347 return cb({errmsg: "Wrong password"});
348 }
349 EvaluationModel.getQuestions(eid, (err2,questions) => {
350 if (!!err2)
351 return cb(err2,null);
352 if (!!paper)
353 return cb(null,{paper:paper});
354 const pwd = TokenGen.generate(12); //arbitrary number, 12 seems enough...
355 db.evaluations.update(
356 { _id: eid },
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 );
369 });
370 });
371 },
372
373 newAnswer: function(eid, number, password, input, cb)
374 {
375 // Check that student hasn't already answered
376 EvaluationModel.hasInput(eid, number, password, input.index, (err,ret) => {
377 if (!!err)
378 return cb(err,null);
379 if (!!ret)
380 return cb({errmsg:"Question already answered"},null);
381 EvaluationModel.setInput(eid, number, password, input, (err2,ret2) => {
382 if (!!err2 || !ret2)
383 return cb(err2,ret2);
384 return cb(null,ret2);
385 });
386 });
387 },
388
389 // NOTE: no callbacks for next function, failures are not so important
390 // (because monitored: teachers can see what's going on)
391 newConnection: function(eid, number)
392 {
393 //increment discoCount, reset discoTime to NULL, update totalDisco
394 EvaluationModel.getDiscoTime(eid, number, (err,discoTime) => {
395 if (!!discoTime)
396 EvaluationModel.addDisco(eid, number, Date.now() - discoTime);
397 });
398 },
399 }
400
401 module.exports = EvaluationModel;