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