1 const UserModel
= require("../models/user");
2 const AssessmentModel
= require("../models/assessment");
3 const db
= require("../utils/database");
12 * description: varchar
13 * password: monitoring password hash
15 * number: student number
23 getByUser: function(uid
, callback
)
31 getById: function(cid
, callback
)
39 getByPath: function(uid
, code
, callback
)
52 insert: function(uid
, code
, description
, cb
)
58 description: description
,
64 setStudents: function(cid
, students
, cb
)
68 { $set: { students: students
} },
73 // Note: return { students: { ... } }, pointing on the requested row
74 getStudent: function(cid
, number
, cb
)
80 students: { $elemMatch: {number: number
} }
86 setPassword: function(cid
, pwd
, cb
)
90 { $set: { password: pwd
} },
95 remove: function(cid
, cb
)
103 /////////////////////
104 // ADVANCED FUNCTIONS
106 getByInitials: function(initials
, callback
)
108 UserModel
.getByInitials(initials
, (err
,user
) => {
113 CourseModel
.getByUser(user
._id
, (err2
,courseArray
) => {
114 callback(err2
, courseArray
);
120 getByRefs: function(initials
, code
, callback
)
122 UserModel
.getByInitials(initials
, (err
,user
) => {
127 CourseModel
.getByPath(user
._id
, code
, (err2
,course
) => {
128 callback(err2
, course
);
134 importStudents: function(uid
, cid
, students
, cb
)
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"},{});
141 CourseModel
.setStudents(cid
, students
, cb
);
145 setPassword: function(uid
, cid
, pwd
, cb
)
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
);
156 remove: function(uid
, cid
, cb
)
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
) => {
166 // 3) remove course (with its students)
167 CourseModel
.remove(cid
, cb
);
173 module
.exports
= CourseModel
;