@@ -5,7 +5,9 @@ declare var TuxLog : any;
55declare var SessionCache : any ;
66declare var nconf : any ;
77declare var _ : any ;
8+ declare var async : any ;
89
10+ import { Roles } from '../../../collections/users.ts' ;
911//import session constructor
1012var LabSession = require ( '../api/lab.session.js' ) ;
1113
@@ -21,12 +23,11 @@ Meteor.methods({
2123 * implement loading wheel, md fetch, course record create in callback
2224 */
2325 'prepareLab' : function ( labId : string ) {
24-
2526 TuxLog . log ( "trace" , "preparing lab" ) ;
2627
27- // Meteor.user().sessions.push({labId: labId,started: Date.now()});
28+ ( < any > Meteor . user ( ) ) . sessions . push ( { labId : labId , started : Date . now ( ) } ) ;
2829
29- // Collections .users.update({_id: Meteor.userId()},{$set:{sessions: this.user.sessions}});
30+ Meteor . users . update ( { _id : Meteor . userId ( ) } , { $set :{ sessions : this . user . sessions } } ) ;
3031 //get course Id
3132 var courseId = Collections . labs . findOne ( { _id : labId } ) . course_id ;
3233
@@ -141,7 +142,7 @@ Meteor.methods({
141142 } ,
142143
143144 'getLastLab' : function ( ) {
144- var sessions = Collections . users . findOne ( { _id : Meteor . userId ( ) } ) . sessions ;
145+ var sessions = ( < any > Meteor . user ( ) ) . sessions ;
145146
146147 var labId = sessions . reduce ( function ( total , current ) {
147148 if ( current . started < total ) {
@@ -153,5 +154,86 @@ Meteor.methods({
153154 var courseId = Collections . labs . findOne ( { _id : labId } ) . course_id ;
154155
155156 return { labId : labId , courseId : courseId } ;
157+ } ,
158+
159+ 'addInstructor' : function ( course_id , instructor_id ) {
160+
161+ if ( ! ( Roles . isAdministratorFor ( course_id , Meteor . userId ( ) ) || Roles . isInstructorFor ( course_id , instructor_id ) ) ) {
162+ throw new Meteor . Error ( "only administrators or instructors can modify instructors" ) ;
163+ }
164+ else {
165+ var inst : any = Meteor . users . findOne ( { _id : instructor_id } ) ;
166+
167+ if ( ! inst ) {
168+ throw new Meteor . Error ( "no user found with given id" ) ;
169+ }
170+ else {
171+ inst . roles . instructor . push ( course_id ) ;
172+
173+ Meteor . users . update ( { id : instructor_id } , { $set : { roles : inst . roles } } ) ;
174+
175+ var instructor = {
176+ name : inst . profile . first_name + " " + inst . profile . last_name ,
177+ id : instructor_id
178+ } ;
179+
180+ Collections . courses . update ( { _id : course_id } , { $push :{ instructors : instructor } } ) ;
181+ }
182+ }
183+ } ,
184+
185+ 'removeInstructor' : function ( course_id , instructor_id ) {
186+
187+ if ( ! ( Roles . isAdministratorFor ( course_id , Meteor . userId ( ) ) || Roles . isInstructorFor ( course_id , instructor_id ) ) ) {
188+ throw new Meteor . Error ( "only administrators or instructors can modify instructors" ) ;
189+ }
190+ else {
191+ var inst : any = Meteor . users . findOne ( { _id : instructor_id } ) ;
192+
193+ if ( ! inst ) {
194+ throw new Meteor . Error ( "no user found with given id" ) ;
195+ }
196+ else {
197+ Collections . courses . update ( { _id : course_id } , { $pull :{ instructors : { id : instructor_id } } } ) ;
198+
199+ inst . roles . instructor . delete ( inst . roles . instructor . indexOf ( course_id ) ) ;
200+ Meteor . users . update ( { _id : instructor_id } , { $set : { roles : inst . roles } } ) ;
201+ }
202+ }
203+ } ,
204+
205+ 'createCourse' : function ( course_name , userId , course_number ) {
206+ if ( ! Roles . isGlobalAdministrator ( Meteor . userId ( ) ) ) {
207+ throw new Meteor . Error ( "only administrators can create courses" )
208+ }
209+ else {
210+ var user = Meteor . users . findOne ( { _id : userId } ) ;
211+ if ( ! user ) {
212+ throw new Meteor . Error ( "the instructor id does not match that of a registered user" ) ;
213+ }
214+ else {
215+ if ( ! course_number ) {
216+ course_number = "" ;
217+ }
218+ var course = {
219+ course_number : course_number ,
220+ course_name : course_name ,
221+ }
222+ var courseId = Collections . courses . insert ( course ) ;
223+
224+ Meteor . call ( 'addInstructor' , courseId , userId ) ;
225+ }
226+ }
227+ } ,
228+
229+ 'deleteCourse' : function ( course_id ) {
230+ var course = Collections . courses . findOne ( { _id : course_id } ) ;
231+
232+ var instructors = course . instructors ;
233+
234+ async . map ( instructors , function ( instructor ) {
235+ Meteor . call ( 'removeInstructor' , course_id , instructor . id ) ;
236+ } ) ;
237+
156238 }
157239} ) ;
0 commit comments