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