|
8 | 8 | // Angular Material Imports
|
9 | 9 | import { MATERIAL_DIRECTIVES, MATERIAL_PROVIDERS } from 'ng2-material';
|
10 | 10 | import { MdProgressBar } from '@angular2-material/progress-bar';
|
| 11 | + import { OVERLAY_PROVIDERS } from '@angular2-material/core/overlay/overlay'; |
11 | 12 |
|
12 | 13 | // Angular Meteor Imports
|
13 | 14 | import { MeteorComponent } from 'angular2-meteor';
|
14 | 15 | import { InjectUser } from 'angular2-meteor-accounts-ui';
|
15 | 16 |
|
| 17 | +// Roles |
| 18 | + import { Roles } from '../../../../../collections/users.ts'; |
| 19 | + |
16 | 20 | // Declare Collections
|
17 | 21 | declare var Collections: any;
|
| 22 | + declare var _: any; |
18 | 23 |
|
19 | 24 | // Inject current user into class
|
20 | 25 | @InjectUser("user")
|
|
28 | 33 | ROUTER_DIRECTIVES,
|
29 | 34 | MATERIAL_DIRECTIVES
|
30 | 35 | ],
|
31 |
| - providers: [ MATERIAL_PROVIDERS ] |
| 36 | + providers: [ MATERIAL_PROVIDERS, OVERLAY_PROVIDERS ] |
32 | 37 | })
|
33 | 38 |
|
34 | 39 | export class LabList extends MeteorComponent {
|
35 | 40 | user: Meteor.User;
|
36 | 41 | courseId: string;
|
37 | 42 | userId: string = Meteor.userId();
|
38 |
| - labs: Array<Object> = []; |
39 | 43 | courseRecord: any;
|
40 |
| - cur_user: boolean; |
| 44 | + exportData: string = ""; |
| 45 | + |
| 46 | + // Test |
| 47 | + allLabs: Array<Object>; |
| 48 | + partialLabs: Array<Object>; |
41 | 49 |
|
42 | 50 | // Progress Bar Value
|
43 | 51 | public determinateValue: number = 0;
|
44 | 52 |
|
45 | 53 | constructor(private route: ActivatedRoute, private router: Router) {
|
46 | 54 | super();
|
47 |
| - } |
48 | 55 |
|
49 |
| - getCourseRecords() { |
50 |
| - // Get from course_records |
| 56 | + // Get labs in course_records |
51 | 57 | this.subscribe('course-records', () => {
|
52 | 58 | this.autorun(() => {
|
53 |
| - if(this.cur_user) { |
54 |
| - // Student |
55 |
| - this.courseRecord = Collections.course_records.findOne({ course_id: this.courseId, user_id: Meteor.userId() }); |
56 |
| - } |
57 |
| - else { |
58 |
| - var localCourseRecord = Collections.course_records.findOne({ course_id: this.courseId, user_id: this.userId }); |
59 |
| - if(localCourseRecord === null || typeof localCourseRecord === "undefined") { |
60 |
| - // Admin |
61 |
| - this.courseRecord = Meteor.call('getUserCourseRecord', this.courseId, this.userId); |
62 |
| - } |
63 |
| - else { |
64 |
| - // Instructor |
65 |
| - this.courseRecord = localCourseRecord; |
66 |
| - } |
67 |
| - } |
68 |
| - this.setLabs(); |
69 |
| - }); |
70 |
| - }, true); |
| 59 | + var record = Collections.course_records.findOne({ course_id: this.courseId }); |
| 60 | + this.partialLabs = record.labs; |
| 61 | + }, true); |
| 62 | + }); |
| 63 | + |
| 64 | + // Get all labs of this course |
| 65 | + this.subscribe('labs', () => { |
| 66 | + this.autorun(() => { |
| 67 | + this.allLabs = Collections.labs.find({ course_id: this.courseId }).fetch(); |
| 68 | + }, true); |
| 69 | + }); |
71 | 70 | }
|
72 | 71 |
|
73 |
| - setLabs() { |
74 |
| - if(typeof this.courseRecord !== "undefined" && this.courseRecord !== null) { |
75 |
| - let labs = this.courseRecord.labs; |
76 |
| - let totalCompleted = 0; |
77 |
| - let totalNumTasks = 0; |
78 |
| - for (let i = 0; i < labs.length; i++) { |
79 |
| - let lab = labs[i]; |
80 |
| - let tasksCompleted = 0; |
81 |
| - let tasks = lab.tasks; |
82 |
| - for (let j = 0; j < tasks.length; j++) { |
83 |
| - let task = tasks[j]; |
84 |
| - if (task.status === 'COMPLETED') { |
85 |
| - tasksCompleted++; |
| 72 | + getLabs() { |
| 73 | + var finalLabs = []; // Return this, an array of formatted labs |
| 74 | + if(typeof this.partialLabs !== "undefined" && typeof this.allLabs !== "undefined") { |
| 75 | + // All labs from course database |
| 76 | + finalLabs = this.allLabs; |
| 77 | + // Get Lab Ids and compare with partial labs from course_records |
| 78 | + var finalLabIds = _.map(finalLabs, function(lb) { return lb._id; }); |
| 79 | + for(let i = 0; i < finalLabIds.length; i++) { |
| 80 | + let currentLabId = finalLabIds[i]; |
| 81 | + let numTasks = finalLabs[i].tasks.length; |
| 82 | + // Set default completed in case it is not in course_records |
| 83 | + finalLabs[i].completed = "0/" + numTasks; |
| 84 | + for(let j = 0; j < this.partialLabs.length; j++) { |
| 85 | + if((<any>(this.partialLabs[j]))._id.str === currentLabId.str) { |
| 86 | + finalLabs[i].completed = this.compTasks(this.partialLabs[j]) + "/" + numTasks; |
86 | 87 | }
|
87 | 88 | }
|
88 |
| - this.labs.push({ |
89 |
| - 'id': lab._id, |
90 |
| - 'name': 'Lab ' + (i + 1).toString(), |
91 |
| - 'completed': tasksCompleted.toString() + '/' + tasks.length.toString(), |
92 |
| - 'date': 'soon' |
93 |
| - }); |
94 |
| - totalCompleted += tasksCompleted; |
95 |
| - totalNumTasks += tasks.length; |
96 | 89 | }
|
97 |
| - this.determinateValue = (totalCompleted * 100.0) / totalNumTasks; |
98 | 90 | }
|
| 91 | + return finalLabs; |
| 92 | + } |
| 93 | + |
| 94 | + compTasks(lab) { |
| 95 | + let comp = 0; |
| 96 | + for(let i = 0; i < lab.tasks.length; i++) { |
| 97 | + if(lab.tasks[i].status === "COMPLETED") { |
| 98 | + comp++; |
| 99 | + } |
| 100 | + } |
| 101 | + return comp; |
99 | 102 | }
|
100 | 103 |
|
101 | 104 | ngOnInit(){
|
102 | 105 | this.userId = this.router.routerState.parent(this.route).snapshot.params['userid'];
|
103 | 106 | this.courseId = this.router.routerState.parent(this.route).snapshot.params['courseid'];
|
104 |
| - this.cur_user = (typeof this.userId === "undefined" || this.userId === null); |
105 |
| - this.getCourseRecords(); |
106 | 107 | }
|
107 | 108 |
|
| 109 | + isInstruct() { |
| 110 | + if(typeof this.courseId !== "undefined") { |
| 111 | + return Roles.isInstructorFor(this.courseId); |
| 112 | + } |
| 113 | + else { |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + exportLab(lab_id) { |
| 118 | + var self = this; |
| 119 | + Meteor.call('exportLab', lab_id, function(err, res) { |
| 120 | + if(err) { |
| 121 | + self.exportData = "Error getting data"; |
| 122 | + } |
| 123 | + else { |
| 124 | + self.exportData = res; |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
108 | 128 | }
|
0 commit comments