2104193a309f92484ffceefbf61830904aec53f8
[qomet.git] / entities / assessment.js
1 const db = require("../utils/database");
2
3 const AssessmentEntity =
4 {
5 /*
6 * Structure:
7 * _id: BSON id
8 * cid: course ID
9 * name: varchar
10 * active: boolean true/false
11 * mode: secure | exam | open (decreasing security)
12 * fixed: bool (questions in fixed order; default: false)
13 * display: "one" or "all" (generally "all" for open questions, but...)
14 * time: 0, //<=0 means "untimed"; otherwise, time in seconds
15 * introduction: "",
16 * conclusion: "https://www.youtube.com/watch?v=6-9AjToJYuw",
17 * coefficient: number, default 1
18 * questions: array of
19 * index: for paper test, like 2.1.a (?!); and quiz: 0, 1, 2, 3...
20 * wording: varchar (HTML)
21 * options: array of varchar --> if present, question type == quiz!
22 * fixed: bool, options in fixed order (default: false)
23 * answer: array of integers (for quiz) or html text (for paper); striped in exam mode
24 * active: boolean, is question in current assessment? --> striped if inactive!
25 * points: points for this question (default 1)
26 * papers : array of
27 * number: student number
28 * inputs: array of indexed arrays of integers (or html text if not quiz)
29 * startTime, endTime
30 * password: random string identifying student for exam session TEMPORARY
31 */
32
33 getById: function(aid, callback)
34 {
35 db.assessments.findOne(
36 { _id: aid },
37 callback
38 );
39 },
40
41 getByPath: function(cid, name, callback)
42 {
43 db.assessments.findOne(
44 {
45 cid: cid,
46 name: name,
47 },
48 callback
49 );
50 },
51
52 insert: function(cid, name, callback)
53 {
54 db.assessments.insert(
55 {
56 name: name,
57 cid: cid,
58 active: false,
59 mode: "exam",
60 fixed: false,
61 display: "one",
62 time: 0,
63 introduction: "",
64 conclusion: "",
65 coefficient: 1,
66 questions: [ ],
67 papers: [ ],
68 },
69 callback
70 );
71 },
72
73 getByCourse: function(cid, callback)
74 {
75 db.assessments.find(
76 { cid: cid },
77 callback
78 );
79 },
80
81 // arg: full assessment without _id field
82 replace: function(aid, assessment, cb)
83 {
84 // Should be: (but unsupported by mongojs)
85 // db.assessments.replaceOne(
86 // { _id: aid },
87 // assessment,
88 // cb
89 // );
90 // Temporary workaround:
91 db.assessments.update(
92 { _id: aid },
93 { $set: assessment },
94 cb
95 );
96 },
97
98 getQuestions: function(aid, callback)
99 {
100 db.assessments.findOne(
101 { _id: aid },
102 { questions: 1},
103 (err,res) => {
104 callback(err, !!res ? res.questions : null);
105 }
106 );
107 },
108
109 startSession: function(aid, number, password, callback)
110 {
111 // TODO: security, do not re-do tasks if already done
112 db.assessments.update(
113 { _id: aid },
114 { $push: { papers: {
115 number: number,
116 startTime: Date.now(),
117 endTime: undefined,
118 password: password,
119 inputs: [ ], //TODO: this is stage 1, stack indexed answers.
120 // then build JSON tree for easier access / correct
121 }}},
122 callback
123 );
124 },
125
126 // https://stackoverflow.com/questions/27874469/mongodb-push-in-nested-array
127 setInput: function(aid, number, password, input, callback) //input: index + arrayOfInt (or txt)
128 {
129 db.assessments.update(
130 {
131 _id: aid,
132 "papers.number": number,
133 "papers.password": password,
134 },
135 { $push: { "papers.$.inputs": input } },
136 callback
137 );
138 },
139
140 endAssessment: function(aid, number, password, callback)
141 {
142 db.assessments.update(
143 {
144 _id: aid,
145 "papers.number": number,
146 "papers.password": password,
147 },
148 { $set: {
149 "papers.$.endTime": Date.now(),
150 "papers.$.password": "",
151 } },
152 callback
153 );
154 },
155
156 getConclusion: function(aid, callback)
157 {
158 db.assessments.findOne(
159 { _id: aid },
160 { conclusion: 1},
161 (err,res) => {
162 callback(err, !!res ? res.conclusion : null);
163 }
164 );
165 },
166
167 remove: function(aid, cb)
168 {
169 db.assessments.remove(
170 { _id: aid },
171 cb
172 );
173 },
174
175 removeGroup: function(cid, cb)
176 {
177 db.assessments.remove(
178 { cid: cid },
179 cb
180 );
181 },
182 }
183
184 module.exports = AssessmentEntity;