| 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 | ///////////////////// |
| 264 | // ADVANCED FUNCTIONS |
| 265 | |
| 266 | getByRefs: function(initials, code, name, cb) |
| 267 | { |
| 268 | UserModel.getByInitials(initials, (err,user) => { |
| 269 | if (!!err || !user) |
| 270 | return cb(err || {errmsg: "User not found"}); |
| 271 | CourseModel.getByPath(user._id, code, (err2,course) => { |
| 272 | if (!!err2 || !course) |
| 273 | return cb(err2 || {errmsg: "Course not found"}); |
| 274 | EvaluationModel.getByPath(course._id, name, (err3,evaluation) => { |
| 275 | if (!!err3 || !evaluation) |
| 276 | return cb(err3 || {errmsg: "Evaluation not found"}); |
| 277 | cb(null,evaluation); |
| 278 | }); |
| 279 | }); |
| 280 | }); |
| 281 | }, |
| 282 | |
| 283 | checkPassword: function(eid, number, password, cb) |
| 284 | { |
| 285 | EvaluationModel.getById(eid, (err,evaluation) => { |
| 286 | if (!!err || !evaluation) |
| 287 | return cb(err, evaluation); |
| 288 | const paperIdx = evaluation.papers.findIndex( item => { return item.number == number; }); |
| 289 | if (paperIdx === -1) |
| 290 | return cb({errmsg: "Paper not found"}, false); |
| 291 | cb(null, evaluation.papers[paperIdx].password == password); |
| 292 | }); |
| 293 | }, |
| 294 | |
| 295 | add: function(uid, cid, name, cb) |
| 296 | { |
| 297 | // 1) Check that course is owned by user of ID uid |
| 298 | CourseModel.getById(cid, (err,course) => { |
| 299 | if (!!err || !course) |
| 300 | return cb({errmsg: "Course retrieval failure"}); |
| 301 | if (!course.uid.equals(uid)) |
| 302 | return cb({errmsg:"Not your course"},undefined); |
| 303 | // 2) Insert new blank evaluation |
| 304 | EvaluationModel.insert(cid, name, cb); |
| 305 | }); |
| 306 | }, |
| 307 | |
| 308 | update: function(uid, evaluation, cb) |
| 309 | { |
| 310 | const eid = ObjectId(evaluation._id); |
| 311 | // 1) Check that evaluation is owned by user of ID uid |
| 312 | EvaluationModel.getById(eid, (err,evaluationOld) => { |
| 313 | if (!!err || !evaluationOld) |
| 314 | return cb({errmsg: "Evaluation retrieval failure"}); |
| 315 | CourseModel.getById(ObjectId(evaluationOld.cid), (err2,course) => { |
| 316 | if (!!err2 || !course) |
| 317 | return cb({errmsg: "Course retrieval failure"}); |
| 318 | if (!course.uid.equals(uid)) |
| 319 | return cb({errmsg:"Not your course"},undefined); |
| 320 | // 2) Replace evaluation |
| 321 | delete evaluation["_id"]; |
| 322 | evaluation.cid = ObjectId(evaluation.cid); |
| 323 | EvaluationModel.replace(eid, evaluation, cb); |
| 324 | }); |
| 325 | }); |
| 326 | }, |
| 327 | |
| 328 | // Set password in responses collection |
| 329 | startSession: function(eid, number, password, cb) |
| 330 | { |
| 331 | EvaluationModel.getPaperByNumber(eid, number, (err,paper) => { |
| 332 | if (!!err) |
| 333 | return cb(err,null); |
| 334 | if (!paper && !!password) |
| 335 | return cb({errmsg: "Cannot start a new exam before finishing current"},null); |
| 336 | if (!!paper) |
| 337 | { |
| 338 | if (!password) |
| 339 | return cb({errmsg: "Missing password"}); |
| 340 | if (paper.password != password) |
| 341 | return cb({errmsg: "Wrong password"}); |
| 342 | } |
| 343 | EvaluationModel.getQuestions(eid, (err2,questions) => { |
| 344 | if (!!err2) |
| 345 | return cb(err2,null); |
| 346 | if (!!paper) |
| 347 | return cb(null,{paper:paper}); |
| 348 | const pwd = TokenGen.generate(12); //arbitrary number, 12 seems enough... |
| 349 | db.evaluations.update( |
| 350 | { _id: eid }, |
| 351 | { $push: { papers: { |
| 352 | number: number, |
| 353 | startTime: Date.now(), |
| 354 | password: password, |
| 355 | totalDisco: 0, |
| 356 | discoCount: 0, |
| 357 | inputs: [ ], //TODO: this is stage 1, stack indexed answers. |
| 358 | // then build JSON tree for easier access / correct |
| 359 | }}}, |
| 360 | (err3,ret) => { cb(err3,{password:password}); } |
| 361 | ); |
| 362 | }); |
| 363 | }); |
| 364 | }, |
| 365 | |
| 366 | newAnswer: function(eid, number, password, input, cb) |
| 367 | { |
| 368 | // Check that student hasn't already answered |
| 369 | EvaluationModel.hasInput(eid, number, password, input.index, (err,ret) => { |
| 370 | if (!!err) |
| 371 | return cb(err,null); |
| 372 | if (!!ret) |
| 373 | return cb({errmsg:"Question already answered"},null); |
| 374 | EvaluationModel.setInput(eid, number, password, input, (err2,ret2) => { |
| 375 | if (!!err2 || !ret2) |
| 376 | return cb(err2,ret2); |
| 377 | return cb(null,ret2); |
| 378 | }); |
| 379 | }); |
| 380 | }, |
| 381 | |
| 382 | // NOTE: no callbacks for next function, failures are not so important |
| 383 | // (because monitored: teachers can see what's going on) |
| 384 | newConnection: function(eid, number) |
| 385 | { |
| 386 | //increment discoCount, reset discoTime to NULL, update totalDisco |
| 387 | EvaluationModel.getDiscoTime(eid, number, (err,discoTime) => { |
| 388 | if (!!discoTime) |
| 389 | EvaluationModel.addDisco(eid, number, Date.now() - discoTime); |
| 390 | }); |
| 391 | }, |
| 392 | } |
| 393 | |
| 394 | module.exports = EvaluationModel; |