Skip to content

Commit

Permalink
Got Lab Adding to Work
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekTBrown committed Aug 10, 2017
1 parent af38149 commit ea68715
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 85 deletions.
34 changes: 4 additions & 30 deletions both/collections/course.collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import { CourseSchema } from '../schemas/course.schema';
import { Course } from '../models/course.model';
import { Role } from '../models/user.model';
import { Lab } from '../models/lab.model';

import { Role } from '../models/user.model';
import { Users } from '../collections/user.collection';

import { Lab, LabFileImportOpts } from '../models/lab.model';
import { Labs } from '../collections/lab.collection';

// Array of Fields that can be Updated
Expand Down Expand Up @@ -50,41 +51,14 @@
});
}

public addInstructor(course_id : string, user_id : string){
super.update({ _id: course_id }, { '$addToSet' : { instructors : user_id}});
}

public removeInstructor(course_id : string, user_id : string){
super.update({ _id: course_id }, { '$pull' : { instructors : user_id}});
}

public addLab(course_id : string, lab_id : string){
super.update({ _id: course_id }, { '$addToSet' : { labs : lab_id }});
}

public removeLab(course_id : string, lab_id : string){
super.update({ _id: course_id }, { '$pull' : { instructors : lab_id}});
}

public getLabs(course_id : string){
var course;

if(typeof (course = super.findOne({ _id: course_id })) !== "undefined"){
return Labs.observable.find({ _id : { '$in' : course.labs }});
}
}

public reorderLabs(course_id : string, labs : string[]) : Promise<any>{
return new Promise((resolve, reject) => {
super.rawCollection().findAndModify({ _id : course_id, labs : { $all : labs }},{},{ $set : { labs : labs } }, (err, res) => {
if(err){
reject(err);
} else {
resolve();
}
});
})
}


}
export const Courses = new CourseCollection();
15 changes: 6 additions & 9 deletions both/collections/course_record.collection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
IMPORTS
**/
import * as _ from 'lodash';
import { Mongo } from 'meteor/mongo';
import { MongoObservable } from 'meteor-rxjs';

Expand All @@ -24,16 +25,12 @@
// Create Observable
this.observable = new MongoObservable.Collection(this);

// Set Editing Permissions
// Permissions
const allowed_fields = [];
super.allow({
insert: function(user_id, course) {
return Users.getRoleFor(user_id, course._id) >= Role.instructor;
},
update: function(user_id, course, fields) {
return Users.getRoleFor(user_id, course._id) >= Role.instructor;
},
remove: function(user_id, course) {
return Users.getRoleFor(user_id, course._id) >= Role.instructor;
update: function(user_id, course_record : CourseRecord, fields) {
return _.intersection(fields, allowed_fields).length === 0 &&
Users.getRoleFor(user_id, course_record.course_id) >= Role.instructor;
},
fetch: []
});
Expand Down
15 changes: 7 additions & 8 deletions both/collections/lab.collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
IMPORTS
**/
import * as _ from 'lodash';
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo'
import { MongoObservable } from 'meteor-rxjs';
Expand All @@ -24,15 +25,13 @@
// Create Observable
this.observable = new MongoObservable.Collection(this);

// Set Editing Permissions
let isAuthorized = function(user_id : string, lab : Lab){
return Users.getRoleFor(lab.course_id, user_id) >= Role.course_admin;
}

// Permissions
const allowed_fields = ['name', 'description', 'status'];
super.allow({
insert: isAuthorized,
update: isAuthorized,
remove: isAuthorized,
update: (user_id : string, lab : Lab, fields : string[]) => {
return _.intersection(fields, allowed_fields).length === 0 &&
Users.getRoleFor(user_id, lab.course_id) >= Role.instructor;
},
fetch: ["course_id"]
});
}
Expand Down
7 changes: 7 additions & 0 deletions both/models/lab.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
md: string;
}

/* LAB FILE IMPORT OPTS */
export interface LabFileImportOpts{
_id?: string;
course_id: string;
file: string;
}

/* LAB MODEL */
export interface Lab {
_id?: string;
Expand Down
2 changes: 1 addition & 1 deletion client/imports/course/course_view.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h2> Labs </h2>
<!-- Add Lab -->
<section class="add_lab">
<div fxLayout="row" fxLayoutAlign="center center" class="file_drop"
(drop)="onDrop($event)" (dragover)="onDragOver($event)" (dragenter)="dragActive = true" (dragleave)="dragActive = false" (dragend)="dragActive = false"
(drop)="onDrop($event)" (dragover)="onDragOver($event)" (dragenter)="dragActive = true" (dragleave)="dragActive = false"
[class.active]="dragActive">

<md-icon>file_upload</md-icon>
Expand Down
32 changes: 17 additions & 15 deletions client/imports/course/course_view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,8 @@

this.labs = this.course
.mergeMap((course) => {
return Observable.fromPromise(
new Promise((resolve, reject) => {
Meteor.subscribe('labs.course', course._id, () => {
var labs = Labs.find({ course_id : course._id });
if(_.isNull(labs)){
reject("Course Record Not Found");
} else {
resolve(labs.fetch());
}
});
})
);
Meteor.subscribe('labs.course', course._id);
return Labs.observable.find({ course_id : course._id });
});
}

Expand All @@ -134,13 +124,25 @@
private dragActive = false;
@HostListener('drop', ['$event'])
onDrop(event) {
event.stopPropagation();
event.preventDefault();
this.dragActive = false;

// Get File
var fileReader = new FileReader();
_.each(event.dataTransfer.files, (file) => {
var lab_file = fileReader.readAsText(file);
_.each(event.dataTransfer.files, (fileTarget) => {
var fileReader = new FileReader();
fileReader.onload = (fileEvent) => {
var lab_file = ((<any>fileEvent.target).result);
Meteor.call('Courses.createLab',{ course_id : this.route.snapshot.params['course_id'], lab_file : lab_file }, (err, res) => {
if(err){
console.error("COULD NOT UPLOAD LAB");
console.error(err);
} else {

}
})
}
fileReader.readAsText(fileTarget,"UTF-8");
});
}

Expand Down
4 changes: 2 additions & 2 deletions client/imports/course/course_view_lab.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@
}

private delete(){
Labs.remove(this.lab._id, (err, res) => {
Meteor.call('Courses.removeLab', this.lab._id, this.lab.course_id, (err, res) => {
if(err){
console.error(err);
}
});
})
}

}
7 changes: 4 additions & 3 deletions imports/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

import { Course, ContentPermissions, EnrollPermissions } from '../../both/models/course.model';
import { Courses } from '../../both/collections/course.collection';
import { createLab } from '../../server/methods/course.methods';

import { Lab, LabStatus } from '../../both/models/lab.model';
import { Labs } from '../../both/collections/lab.collection';
import { Example1 } from "./example_labs";

import { User, Role } from '../../both/models/user.model';
import { Users } from '../../both/collections/user.collection';

import { Example1 } from "./example_labs";

/*
* cleanupDatabase
Expand Down Expand Up @@ -174,8 +175,8 @@
]
})
};
Courses.addLab(this.courses['gpi'], this.labs['gpi/git']);
Courses.addLab(this.courses['gpi'], this.labs['gpi/apache']);
createLab(this.courses['gpi'], this.labs['gpi/git']);
createLab(this.courses['gpi'], this.labs['gpi/apache']);

}

Expand Down
4 changes: 2 additions & 2 deletions imports/test/server/api/lab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import { DefaultFixtures } from '../../../fixtures';
import { Example1 } from '../../../fixtures/example_labs';

import { Lab } from '../../../../both/models/lab.model';
import { Lab, LabFileImportOpts } from '../../../../both/models/lab.model';
import { Labs } from '../../../../both/collections/lab.collection';
import { LabRuntime, LabFileImportOpts } from '../../../../server/imports/runtime/lab_runtime';
import { LabRuntime } from '../../../../server/imports/runtime/lab_runtime';

export function LabRuntimeTests(){
describe('Lab Runtime', function(){
Expand Down
12 changes: 1 addition & 11 deletions server/imports/runtime/lab_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { Cache } from '../service/cache';
import { log } from '../service/log';

import { Lab as LabModel, Task as TaskModel, LabStatus } from '../../../both/models/lab.model';
import { Lab as LabModel, Task as TaskModel, LabStatus, LabFileImportOpts } from '../../../both/models/lab.model';
import { Labs } from '../../../both/collections/lab.collection';

import { VMConfig, VMResolveConfig, VMConfigCustom } from '../api/vmconfig';
Expand All @@ -30,16 +30,6 @@
console: console
}

/*
labFileImportOpts
Sets options for creating a labfile
*/
export interface LabFileImportOpts{
_id?: string;
course_id: string;
file: string;
}

export class LabRuntime extends Cache implements LabModel {
// LabCache Elements
protected static _TTL : number = Meteor.settings['private']['labvm']['labruntime_idle_timeout'];
Expand Down
109 changes: 105 additions & 4 deletions server/methods/course.methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import { Course, ContentPermissions } from '../../both/models/course.model';
import { Courses } from '../../both/collections/course.collection';

import { Lab, LabFileImportOpts } from '../../both/models/lab.model';
import { LabRuntime } from '../../server/imports/runtime/lab_runtime';
import { Labs } from '../../both/collections/lab.collection';

import { Role } from '../../both/models/user.model';
import { Users } from '../../both/collections/user.collection';

Expand Down Expand Up @@ -47,12 +52,108 @@
Meteor.publish('courses.id', coursesId);

/* METHODS */
export function addInstructor(course_id : string, user_id : string) {
Courses.update({ _id: course_id }, { '$addToSet' : { instructors : user_id}});
}

export function removeInstructor(course_id : string, user_id : string){
Courses.update({ _id: course_id }, { '$pull' : { instructors : user_id}});
}

/* CREATE LAB */
export function createLab(course_id : string, lab_file : string) : Promise<any>{

// Create Lab Runtime
return LabRuntime.createLabRuntime({
course_id : course_id,
file: lab_file
})

// Create Lab Record
.then((labRuntime : LabRuntime) => {
return labRuntime._id;

// Insert into Course Record
}).then((lab_id) => {
return new Promise((resolve, reject) => {
Courses.update({
_id: course_id
}, {
'$addToSet' : { labs : lab_id}
}, (err, res) => {
if(err){
reject(err);
} else {
resolve(res);
}
});
});
})
}

/* REMOVE LAB */
export function removeLab(course_id : string, lab_id : string) : Promise<any>{
// Remove Lab
return new Promise((resolve, reject) => {
Labs.remove({
_id : lab_id
}, (err, res) => {
if(err){
reject(err);
} else {
resolve(res);
}
});

// Remove from Course Record
}).then(() => {
return new Promise((resolve, reject) => {
Courses.update({
_id: course_id
}, {
'$pull' : { instructors : lab_id}
},(err, res) => {
if(err){
reject(err);
} else {
resolve(res);
}
});
});
});
}

export function reorderLabs(course_id : string, labs : string[]) : Promise<any>{
return new Promise((resolve, reject) => {
Courses.rawCollection().findAndModify({ _id : course_id, labs : { $all : labs }},{},{ $set : { labs : labs } }, (err, res) => {
if(err){
reject(err);
} else {
resolve();
}
});
})
}

Meteor.methods({
'Courses.reorderLabs'(course_id : string, labs : string[]){
return Courses.reorderLabs(course_id, labs)
'Courses.createLab'({course_id, lab_file}){
return createLab(course_id, lab_file)
.catch((err) => {
throw new Meteor.Error("Could not add lab.")
})
},

'Courses.removeLab'({course_id, lab_id}){
return removeLab(course_id, lab_id)
.catch((err) => {
throw new Meteor.Error("Could not remove lab.")
})
},

'Courses.reorderLabs'({course_id, labs}){
return reorderLabs(course_id, labs)
.catch((err) => {
console.error(err);
throw new Meteor.Error("Redorder Conflict");
throw new Meteor.Error("Could not reorder labs.");
});
}
})

0 comments on commit ea68715

Please sign in to comment.