|
15 | 15 |
|
16 | 16 | // Declare Collections |
17 | 17 | declare var Collections: any; |
| 18 | + declare var _: any; |
18 | 19 |
|
19 | 20 | // Inject current user into class |
20 | 21 | @InjectUser("user") |
|
35 | 36 | user: Meteor.User; |
36 | 37 | courseId: string; |
37 | 38 | userId: string = Meteor.userId(); |
38 | | - labs: Array<Object> = []; |
39 | 39 | courseRecord: any; |
40 | | - cur_user: boolean; |
| 40 | + |
| 41 | + // Test |
| 42 | + allLabs: Array<Object>; |
| 43 | + partialLabs: Array<Object>; |
41 | 44 |
|
42 | 45 | // Progress Bar Value |
43 | 46 | public determinateValue: number = 0; |
44 | 47 |
|
45 | 48 | constructor(private route: ActivatedRoute, private router: Router) { |
46 | 49 | super(); |
47 | | - } |
48 | 50 |
|
49 | | - getCourseRecords() { |
50 | | - // Get from course_records |
| 51 | + // Get labs in course_records |
51 | 52 | this.subscribe('course-records', () => { |
52 | 53 | 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); |
| 54 | + var record = Collections.course_records.findOne({ course_id: this.courseId }); |
| 55 | + this.partialLabs = record.labs; |
| 56 | + }, true); |
| 57 | + }); |
| 58 | + |
| 59 | + // Get all labs of this course |
| 60 | + this.subscribe('labs', () => { |
| 61 | + this.autorun(() => { |
| 62 | + this.allLabs = Collections.labs.find({ course_id: this.courseId }).fetch(); |
| 63 | + }, true); |
| 64 | + }); |
71 | 65 | } |
72 | 66 |
|
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++; |
| 67 | + getLabs() { |
| 68 | + var finalLabs = []; // Return this, an array of formatted labs |
| 69 | + if(typeof this.partialLabs !== "undefined" && typeof this.allLabs !== "undefined") { |
| 70 | + // All labs from course database |
| 71 | + finalLabs = this.allLabs; |
| 72 | + // Get Lab Ids and compare with partial labs from course_records |
| 73 | + var finalLabIds = _.map(finalLabs, function(lb) { return lb._id; }); |
| 74 | + for(let i = 0; i < finalLabIds.length; i++) { |
| 75 | + let currentLabId = finalLabIds[i]; |
| 76 | + let numTasks = finalLabs[i].tasks.length; |
| 77 | + // Set default completed in case it is not in course_records |
| 78 | + finalLabs[i].completed = "0/" + numTasks; |
| 79 | + for(let j = 0; j < this.partialLabs.length; j++) { |
| 80 | + if((<any>(this.partialLabs[j]))._id.str === currentLabId.str) { |
| 81 | + finalLabs[i].completed = this.compTasks(this.partialLabs[j]) + "/" + numTasks; |
86 | 82 | } |
87 | 83 | } |
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 | 84 | } |
97 | | - this.determinateValue = (totalCompleted * 100.0) / totalNumTasks; |
98 | 85 | } |
| 86 | + return finalLabs; |
| 87 | + } |
| 88 | + |
| 89 | + compTasks(lab) { |
| 90 | + let comp = 0; |
| 91 | + for(let i = 0; i < lab.tasks.length; i++) { |
| 92 | + if(lab.tasks[i].status === "COMPLETED") { |
| 93 | + comp++; |
| 94 | + } |
| 95 | + } |
| 96 | + return comp; |
99 | 97 | } |
100 | 98 |
|
101 | 99 | ngOnInit(){ |
102 | 100 | this.userId = this.router.routerState.parent(this.route).snapshot.params['userid']; |
103 | 101 | 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 | 102 | } |
107 | 103 |
|
108 | 104 | } |
0 commit comments