thoughts about time, refactor statements component
[qomet.git] / models / course.js
CommitLineData
43828378
BA
1const UserModel = require("../models/user");
2const AssessmentModel = require("../models/assessment");
3const db = require("../utils/database");
e99c53fb
BA
4
5const CourseModel =
6{
43828378
BA
7 /*
8 * Structure:
9 * _id: BSON id
10 * uid: prof ID
11 * code: varchar
12 * description: varchar
13 * password: monitoring password hash
14 * students: array of
15 * number: student number
16 * name: varchar
17 * group: integer
18 */
19
20 //////////////////
21 // BASIC FUNCTIONS
22
23 getByUser: function(uid, callback)
24 {
25 db.courses.find(
26 { uid: uid },
27 callback
28 );
29 },
30
31 getById: function(cid, callback)
32 {
33 db.courses.findOne(
34 { _id: cid },
35 callback
36 );
37 },
38
39 getByPath: function(uid, code, callback)
40 {
41 db.courses.findOne(
42 {
43 $and: [
44 { uid: uid },
45 { code: code },
46 ]
47 },
48 callback
49 );
50 },
51
52 insert: function(uid, code, description, cb)
53 {
54 db.courses.insert(
55 {
56 uid: uid,
57 code: code,
58 description: description,
59 students: [ ],
60 },
61 cb);
62 },
63
64 setStudents: function(cid, students, cb)
65 {
66 db.courses.update(
67 { _id: cid },
68 { $set: { students: students } },
69 cb
70 );
71 },
72
73 // Note: return { students: { ... } }, pointing on the requested row
74 getStudent: function(cid, number, cb)
75 {
76 db.courses.findOne(
77 { _id: cid },
78 {
79 _id: 0,
80 students: { $elemMatch: {number: number} }
81 },
82 cb
83 );
84 },
85
86 setPassword: function(cid, pwd, cb)
87 {
88 db.courses.update(
89 { _id: cid },
90 { $set: { password: pwd } },
91 cb
92 );
93 },
94
95 remove: function(cid, cb)
96 {
97 db.courses.remove(
98 { _id: cid },
99 cb
100 );
101 },
102
103 /////////////////////
104 // ADVANCED FUNCTIONS
105
e99c53fb
BA
106 getByInitials: function(initials, callback)
107 {
43828378 108 UserModel.getByInitials(initials, (err,user) => {
e99c53fb
BA
109 if (!!err || !user)
110 callback(err, []);
111 else
112 {
43828378 113 CourseModel.getByUser(user._id, (err2,courseArray) => {
e99c53fb
BA
114 callback(err2, courseArray);
115 });
116 }
117 });
118 },
119
120 getByRefs: function(initials, code, callback)
121 {
43828378 122 UserModel.getByInitials(initials, (err,user) => {
e99c53fb
BA
123 if (!!err || !user)
124 callback(err, []);
125 else
126 {
43828378 127 CourseModel.getByPath(user._id, code, (err2,course) => {
e99c53fb
BA
128 callback(err2, course);
129 });
130 }
131 });
132 },
133
134 importStudents: function(uid, cid, students, cb)
135 {
136 // 1) check if uid == course uid
43828378 137 CourseModel.getById(cid, (err,course) => {
e99c53fb
BA
138 if (!!err || !course || !course.uid.equals(uid))
139 return cb({errmsg:"Not your course"},{});
140 // 2) Set students
43828378 141 CourseModel.setStudents(cid, students, cb);
e99c53fb
BA
142 });
143 },
144
145 setPassword: function(uid, cid, pwd, cb)
146 {
147 // 1) check if uid == course uid
43828378 148 CourseModel.getById(cid, (err,course) => {
e99c53fb
BA
149 if (!!err || !course || !course.uid.equals(uid))
150 return cb({errmsg:"Not your course"},{});
151 // 2) Insert new student (overwrite if number already exists)
43828378 152 CourseModel.setPassword(cid, pwd, cb);
e99c53fb
BA
153 });
154 },
155
156 remove: function(uid, cid, cb)
157 {
158 // 1) check if uid == course uid
43828378 159 CourseModel.getById(cid, (err,course) => {
e99c53fb
BA
160 if (!!err || !course || !course.uid.equals(uid))
161 return cb({errmsg:"Not your course"},{});
162 // 2) remove all associated assessments
43828378 163 AssessmentModel.removeGroup(cid, (err2,ret) => {
e99c53fb
BA
164 if (!!err)
165 return cb(err,{});
166 // 3) remove course (with its students)
43828378 167 CourseModel.remove(cid, cb);
e99c53fb
BA
168 });
169 });
170 },
171}
172
173module.exports = CourseModel;