| 1 | const CourseEntity = require("../entities/course"); |
| 2 | const UserEntity = require("../entities/user"); |
| 3 | const AssessmentEntity = require("../entities/assessment"); |
| 4 | |
| 5 | const CourseModel = |
| 6 | { |
| 7 | getByInitials: function(initials, callback) |
| 8 | { |
| 9 | UserEntity.getByInitials(initials, (err,user) => { |
| 10 | if (!!err || !user) |
| 11 | callback(err, []); |
| 12 | else |
| 13 | { |
| 14 | CourseEntity.getByUser(user._id, (err2,courseArray) => { |
| 15 | callback(err2, courseArray); |
| 16 | }); |
| 17 | } |
| 18 | }); |
| 19 | }, |
| 20 | |
| 21 | getByRefs: function(initials, code, callback) |
| 22 | { |
| 23 | UserEntity.getByInitials(initials, (err,user) => { |
| 24 | if (!!err || !user) |
| 25 | callback(err, []); |
| 26 | else |
| 27 | { |
| 28 | CourseEntity.getByPath(user._id, code, (err2,course) => { |
| 29 | callback(err2, course); |
| 30 | }); |
| 31 | } |
| 32 | }); |
| 33 | }, |
| 34 | |
| 35 | importStudents: function(uid, cid, students, cb) |
| 36 | { |
| 37 | // 1) check if uid == course uid |
| 38 | CourseEntity.getById(cid, (err,course) => { |
| 39 | if (!!err || !course || !course.uid.equals(uid)) |
| 40 | return cb({errmsg:"Not your course"},{}); |
| 41 | // 2) Set students |
| 42 | CourseEntity.setStudents(cid, students, cb); |
| 43 | }); |
| 44 | }, |
| 45 | |
| 46 | setPassword: function(uid, cid, pwd, cb) |
| 47 | { |
| 48 | // 1) check if uid == course uid |
| 49 | CourseEntity.getById(cid, (err,course) => { |
| 50 | if (!!err || !course || !course.uid.equals(uid)) |
| 51 | return cb({errmsg:"Not your course"},{}); |
| 52 | // 2) Insert new student (overwrite if number already exists) |
| 53 | CourseEntity.setPassword(cid, pwd, cb); |
| 54 | }); |
| 55 | }, |
| 56 | |
| 57 | remove: function(uid, cid, cb) |
| 58 | { |
| 59 | // 1) check if uid == course uid |
| 60 | CourseEntity.getById(cid, (err,course) => { |
| 61 | if (!!err || !course || !course.uid.equals(uid)) |
| 62 | return cb({errmsg:"Not your course"},{}); |
| 63 | // 2) remove all associated assessments |
| 64 | AssessmentEntity.removeGroup(cid, (err2,ret) => { |
| 65 | if (!!err) |
| 66 | return cb(err,{}); |
| 67 | // 3) remove course (with its students) |
| 68 | CourseEntity.remove(cid, cb); |
| 69 | }); |
| 70 | }); |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | module.exports = CourseModel; |