@@ -5,7 +5,9 @@ declare var TuxLog : any;
5
5
declare var SessionCache : any ;
6
6
declare var nconf : any ;
7
7
declare var _ : any ;
8
+ declare var async : any ;
8
9
10
+ import { Roles } from '../../../collections/users.ts' ;
9
11
//import session constructor
10
12
var LabSession = require ( '../api/lab.session.js' ) ;
11
13
@@ -21,12 +23,11 @@ Meteor.methods({
21
23
* implement loading wheel, md fetch, course record create in callback
22
24
*/
23
25
'prepareLab' : function ( labId : string ) {
24
-
25
26
TuxLog . log ( "trace" , "preparing lab" ) ;
26
27
27
- // Meteor.user().sessions.push({labId: labId,started: Date.now()});
28
+ ( < any > Meteor . user ( ) ) . sessions . push ( { labId : labId , started : Date . now ( ) } ) ;
28
29
29
- // Collections .users.update({_id: Meteor.userId()},{$set:{sessions: this.user.sessions}});
30
+ Meteor . users . update ( { _id : Meteor . userId ( ) } , { $set :{ sessions : this . user . sessions } } ) ;
30
31
//get course Id
31
32
var courseId = Collections . labs . findOne ( { _id : labId } ) . course_id ;
32
33
@@ -141,7 +142,7 @@ Meteor.methods({
141
142
} ,
142
143
143
144
'getLastLab' : function ( ) {
144
- var sessions = Collections . users . findOne ( { _id : Meteor . userId ( ) } ) . sessions ;
145
+ var sessions = ( < any > Meteor . user ( ) ) . sessions ;
145
146
146
147
var labId = sessions . reduce ( function ( total , current ) {
147
148
if ( current . started < total ) {
@@ -153,5 +154,86 @@ Meteor.methods({
153
154
var courseId = Collections . labs . findOne ( { _id : labId } ) . course_id ;
154
155
155
156
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
+
156
238
}
157
239
} ) ;
0 commit comments