var courseId = req.params.id;
Course.findById( courseId, function( err, course ) {
- if( course ) {
+ if( course && !course.deleted ) {
req.course = course;
// If a course is found, the user is checked to see if they are
var lectureId = req.params.id;
Lecture.findById( lectureId, function( err, lecture ) {
- if( lecture ) {
+ if( lecture && !lecture.deleted ) {
req.lecture = lecture;
// If a lecture is found, the user is checked to see if they are
Note.findById( noteId, function( err, note ) {
// If a note is found, and user is set, check if
// user is authorized to interact with that note.
- if( note && user ) {
+ if( note && user && !note.deleted ) {
note.authorize( user, function( auth ) {
if( auth ) {
// If authorzied, then set req.note to be used later
res.redirect( '/' );
}
})
- } else if ( note && note.public ) {
+ } else if ( note && note.public && !note.deleted ) {
// If note is found, but user is not set because they are not
// logged in, and the note is public, set the note to read only
// and store the note for later.
req.RO = true;
next();
- } else if ( note && !note.public ) {
+ } else if ( note && !note.public && !note.deleted ) {
// If the note is found, but user is not logged in and the note is
// not public, then ask them to login to view the note. Once logged
// in they will be redirected to the note, at which time authorization
// No note was found
req.flash( 'error', 'Invalid note specified!' );
- res.redirect( '/login' );
+ res.redirect( '/schools' );
}
});
}
// If any courses are found, set them to the appropriate school, otherwise
// leave empty.
if( courses.length > 0 ) {
- school.courses = courses;
+ school.courses = courses.filter(function(course) {
+ if (!course.deleted) return course;
+ });
} else {
school.courses = [];
}
});
});
+// Edit Course
+app.get( '/course/:id/edit', loadUser, loadCourse, function( req, res) {
+ var course = req.course;
+ var user = req.user;
+
+ if ( user.admin ) {
+ res.render( 'course/new', {course: course} )
+ } else {
+ req.flash( 'error', 'You don\'t have permission to do that' )
+ res.redirect( '/schools' );
+ }
+})
+
+// Recieve Course Edit Form
+app.post( '/course/:id/edit', loadUser, loadCourse, function( req, res ) {
+ var course = req.course;
+ var user = req.user;
+
+ if (user.admin) {
+ var courseChanges = req.body;
+ course.number = courseChanges.number;
+ course.name = courseChanges.name;
+ course.description = courseChanges.description;
+ course.department = courseChanges.department;
+
+ course.save(function(err) {
+ if (err) {
+ req.flash( 'error', 'There was an error saving the course' );
+ }
+ res.redirect( '/course/'+ course._id.toString());
+ })
+ } else {
+ req.flash( 'error', 'You don\'t have permission to do that' )
+ res.redirect( '/schools' );
+ }
+});
+
// Delete Course
-// XXX Non functioning
-// Will be used as apart of a larger admin interface for managing courses and
-// lectures on the site.
app.get( '/course/:id/delete', loadUser, loadCourse, function( req, res) {
var course = req.course;
var user = req.user;
if ( user.admin ) {
course.delete(function( err ) {
if ( err ) req.flash( 'info', 'There was a problem removing course: ' + err )
- else req.flash( 'info', 'Successfully removed course' )
- res.redirect( '/schools' );
+ else req.flash( 'info', 'Successfully removed course' )
+ res.redirect( '/schools' );
});
} else {
req.flash( 'error', 'You don\'t have permission to do that' )
// XXX: room for additional resources
created : { type : Date, default : Date.now },
creator : ObjectId,
+ deleted : Boolean,
// many users may subscribe to a course
users : Array
var id = this._id;
Course.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
- callback( err );
+ if (callback) callback( err );
+ Lecture.find( { course: id }, function( err, lectures) {
+ if (lectures.length > 0) {
+ lectures.forEach(function(lecture) {
+ lecture.delete();
+ })
+ }
+ })
})
});
date : { type : Date, default: Date.now },
live : Boolean,
creator : ObjectId,
+ deleted : Boolean,
course : ObjectId
});
});
});
+LectureSchema.method( 'delete', function( callback ) {
+ var id = this._id;
+
+ Lecture.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+ if (callback) callback( err );
+ Note.find( { lecture : id }, function(err, notes) {
+ notes.forEach(function(note) {
+ note.delete();
+ })
+ })
+ Post.find( { lecture : id }, function(err, posts) {
+ posts.forEach(function(post) {
+ post.delete();
+ })
+ })
+ })
+});
+
var Lecture = mongoose.model( 'Lecture', LectureSchema );
// notes
visits : Number,
created : { type : Date, default : Date.now },
creator : ObjectId,
+ deleted : Boolean,
lecture : ObjectId,
Note.collection.update( { '_id' : id }, { '$inc' : { 'visits' : 1 } } );
});
+NoteSchema.method( 'delete', function( callback ) {
+ var id = this._id;
+
+ Note.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+ if (callback) callback( err );
+ })
+});
+
var Note = mongoose.model( 'Note', NoteSchema );
// comments
comments : Array,
- lecture : String // ObjectId
+ lecture : String, // ObjectId
+ deleted : Boolean
})
-mongoose.model( 'Post', PostSchema );
-
-
-// Deleted documents
-
-var DeletedCourse = new Schema( {
- name : { type : String, required : true },
- number : String,
- description : String,
- instructor : ObjectId,
- // courses are tied to one school
- school : ObjectId,
-
- // XXX: room for additional resources
- created : { type : Date, default : Date.now },
- creator : ObjectId,
-
- // many users may subscribe to a course
- users : Array
-});
-
-
-mongoose.model( 'DeletedCourse', DeletedCourse )
-
-var DeletedLecture = new Schema( {
- name : { type : String, required : true },
- date : { type : Date, default: Date.now },
- live : Boolean,
- creator : ObjectId,
-
- course : ObjectId
-});
-
-
-mongoose.model( 'DeletedLecture', DeletedLecture )
-
-var DeletedNote = new Schema( {
- name : { type : String, required : true },
- path : String,
- public : Boolean,
- roID : String,
- visits : Number,
- created : { type : Date, default : Date.now },
- creator : ObjectId,
-
- lecture : ObjectId,
+PostSchema.method( 'delete', function( callback ) {
+ var id = this._id;
- collaborators : [String]
+ Post.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+ if (callback) callback( err );
+ })
});
-
-mongoose.model( 'DeletedNote', DeletedNote )
-
-var DeletedPost = new Schema({
- date : { type : Date, default : Date.now },
- body : String,
- votes : [String],
- reports : [String],
- public : Boolean,
-
- userid : String, // ObjectId,
- userName : String,
- userAffil : String,
-
- comments : Array,
-
- lecture : String // ObjectId
-})
-
-
-mongoose.model( 'DeletedPost', DeletedPost )
-
+mongoose.model( 'Post', PostSchema );
var ArchivedCourse = new Schema({
id: Number,
div.content
div.container1
form( method = 'POST' )
- h1 Create New Course
- p
- | Use this form to create a new course.
- | After creating the course, you can create a lecture with in it.
+ - var course = course || false;
+ - if (course)
+ h1 Edit Course
+ p
+ | Use this form to edit a course.
- div.row
- div.label Course number
- div.field
- input( type = 'text', name = 'number' )
+ div.row
+ div.label Course number
+ div.field
+ input( type = 'text', name = 'number', value = course.number )
- div.row
- div.label Course name
- div.field
- input( type = 'text', name = 'name' )
- div.row
- div.label Course subject
- div.field
- input( type = 'text', name = 'subject' )
- div.row
- div.label Course department
- div.field
- input( type = 'text', name = 'department' )
- div.row
- div.label Instructor's Name
- div.field
- input( type = 'text', name = 'instructorName' )
- div.row
- div.label Instructor's Email
- div.field
- input( type = 'text', name = 'email' )
- div.row
- div( class = 'label')
-
- div( class = 'field')
- button Create Course
+ div.row
+ div.label Course name
+ div.field
+ input( type = 'text', name = 'name', value = course.name )
+ div.row
+ div.label Course subject
+ div.field
+ input( type = 'text', name = 'subject', value = course.subject )
+ div.row
+ div.label Course department
+ div.field
+ input( type = 'text', name = 'department', value = course.department )
+ //
+ div.row
+ div.label Instructor's Name
+ div.field
+ input( type = 'text', name = 'instructorName' )
+ div.row
+ div.label Instructor's Email
+ div.field
+ input( type = 'text', name = 'email' )
+ div.row
+ div( class = 'label')
+
+ div( class = 'field')
+ button Save Course
+ - else
+ h1 Create New Course
+ p
+ | Use this form to create a new course.
+ | After creating the course, you can create a lecture with in it.
+
+ div.row
+ div.label Course number
+ div.field
+ input( type = 'text', name = 'number' )
+
+ div.row
+ div.label Course name
+ div.field
+ input( type = 'text', name = 'name' )
+ div.row
+ div.label Course subject
+ div.field
+ input( type = 'text', name = 'subject' )
+ div.row
+ div.label Course department
+ div.field
+ input( type = 'text', name = 'department' )
+ div.row
+ div.label Instructor's Name
+ div.field
+ input( type = 'text', name = 'instructorName' )
+ div.row
+ div.label Instructor's Email
+ div.field
+ input( type = 'text', name = 'email' )
+ div.row
+ div( class = 'label')
+
+ div( class = 'field')
+ button Create Course
div
span(class='sub_menu')
a( href = '/course/#{ course._id }/lecture/new' ) New Lecture
+ - if ( user.admin )
+ div
+ span(class='sub_menu')
+ a( href = '/course/#{ course._id }/edit' ) Edit Course
+ div
+ span(class='sub_menu')
+ a( href = '/course/#{ course._id }/delete' ) Delete Course