748ab6c33f7c32ea62728b824a33012dc2fb79d3
1 const UserModel
= require("../models/user");
2 const db
= require("../utils/database");
11 * description: varchar
12 * password: monitoring password hash
14 * number: student number
22 getByUser: function(uid
, callback
)
30 getById: function(cid
, callback
)
38 getByPath: function(uid
, code
, callback
)
51 insert: function(uid
, code
, description
, cb
)
57 description: description
,
63 setStudents: function(cid
, students
, cb
)
67 { $set: { students: students
} },
72 // Note: return { students: { ... } }, pointing on the requested row
73 getStudent: function(cid
, number
, cb
)
79 students: { $elemMatch: {number: number
} }
85 setPassword: function(cid
, pwd
, cb
)
89 { $set: { password: pwd
} },
94 remove: function(cid
, cb
)
102 /////////////////////
103 // ADVANCED FUNCTIONS
105 getByInitials: function(initials
, callback
)
107 UserModel
.getByInitials(initials
, (err
,user
) => {
112 CourseModel
.getByUser(user
._id
, (err2
,courseArray
) => {
113 callback(err2
, courseArray
);
119 getByRefs: function(initials
, code
, callback
)
121 UserModel
.getByInitials(initials
, (err
,user
) => {
126 CourseModel
.getByPath(user
._id
, code
, (err2
,course
) => {
127 callback(err2
, course
);
133 importStudents: function(uid
, cid
, students
, cb
)
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"},{});
140 CourseModel
.setStudents(cid
, students
, cb
);
144 setPassword: function(uid
, cid
, pwd
, cb
)
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
);
155 remove: function(uid
, cid
, cb
)
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(
167 // 3) remove course (with its students)
168 CourseModel
.remove(cid
, cb
);
175 module
.exports
= CourseModel
;