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