'update'
[qomet.git] / models / course.js
1 const UserModel = require("../models/user");
2 const db = require("../utils/database");
3
4 const CourseModel =
5 {
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
105 getByInitials: function(initials, callback)
106 {
107 UserModel.getByInitials(initials, (err,user) => {
108 if (!!err || !user)
109 callback(err, []);
110 else
111 {
112 CourseModel.getByUser(user._id, (err2,courseArray) => {
113 callback(err2, courseArray);
114 });
115 }
116 });
117 },
118
119 getByRefs: function(initials, code, callback)
120 {
121 UserModel.getByInitials(initials, (err,user) => {
122 if (!!err || !user)
123 callback(err, []);
124 else
125 {
126 CourseModel.getByPath(user._id, code, (err2,course) => {
127 callback(err2, course);
128 });
129 }
130 });
131 },
132
133 importStudents: function(uid, cid, students, cb)
134 {
135 // 1) check if uid == course uid
136 CourseModel.getById(cid, (err,course) => {
137 if (!!err || !course || !course.uid.equals(uid))
138 return cb({errmsg:"Not your course"},{});
139 // 2) Set students
140 CourseModel.setStudents(cid, students, cb);
141 });
142 },
143
144 setPassword: function(uid, cid, pwd, cb)
145 {
146 // 1) check if uid == course uid
147 CourseModel.getById(cid, (err,course) => {
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)
151 CourseModel.setPassword(cid, pwd, cb);
152 });
153 },
154
155 remove: function(uid, cid, cb)
156 {
157 // 1) check if uid == course uid
158 CourseModel.getById(cid, (err,course) => {
159 if (!!err || !course || !course.uid.equals(uid))
160 return cb({errmsg:"Not your course"},{});
161 // 2) remove all associated evaluations
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 );
171 });
172 },
173 }
174
175 module.exports = CourseModel;