-
Notifications
You must be signed in to change notification settings - Fork 2
/
course.js
120 lines (110 loc) · 2.95 KB
/
course.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var rp = require('request-promise');
var Promise = require('bluebird');
var urlbuilder = require('./urlbuilder');
var constants = require('./constants');
var Board = require('./board');
var Course = function(data, jar) {
this.jar = jar;
this.cname = data.crs_cname;
this.ename = data.crs_ename;
this.sn = data.course_sn;
this.timetable = [];
this.contents = [];
this.grades = [];
this.homeworks = [];
this.bulletin = [];
this.boards = [];
this.ready = false;
};
Course.prototype.add_time = function(course_time) {
this.timetable.push(course_time);
};
Course.prototype.fetch = function() {
var self = this;
return rp({
headers: constants.headers,
jar: self.jar,
json: true,
method: 'GET',
url: urlbuilder.api({
mode: 'course',
course_sn: this.sn
})
}).then(function(data) {
self.update_contents(data.contents, data.content_files);
self.update_grades(data.course_grade);
self.update_homeworks(data.homeworks);
self.update_bulletin(data.bulletin);
if(!data.boards) self.boards = false;
self.ready = true;
return self;
});
};
Course.prototype.update_contents = function(contents, content_files) {
if(typeof(contents) == 'undefined' || typeof(content_files) == 'undefined') return;
var self = this;
var content_map = {};
self.contents = [];
contents.forEach(function(content) {
content_map[content.syl_sn] = self.contents.length;
content.files = [];
self.contents.push(content);
});
content_files.forEach(function(content_file) {
self.contents[content_map[content_file.syl_sn]].files.push(content_file.file_name);
});
};
Course.prototype.update_grades = function(data) {
if(typeof(data) == 'undefined') return;
var self = this;
self.grades = [];
data.forEach(function(row) {
self.grades.push(row);
});
};
Course.prototype.update_homeworks = function(data) {
if(typeof(data) == 'undefined') return;
var self = this;
data.forEach(function(row) {
self.homeworks.push(row);
});
};
Course.prototype.update_bulletin = function(data) {
if(typeof(data) == 'undefined') return;
var self = this;
self.bulletin = [];
data.forEach(function(row) {
if(row.b_link == 'http://') row.b_link = false;
self.bulletin.push(row);
});
};
Course.prototype.fetch_boards = function() {
var self = this;
return rp({
headers: constants.headers,
jar: self.jar,
json: true,
method: 'GET',
url: urlbuilder.api({
mode: 'read_board',
course_sn: this.sn,
board: 0
})
}).then(function(data) {
self.boards = [];
data.forEach(function(board) {
self.boards.push(new Board(self.sn, board.sn, board.caption, self.jar));
});
return self.boards;
});
}
Course.prototype.get_boards = function() {
var self = this;
return new Promise(function(resolve, reject) {
if(self.boards.length != 0) {
return resolve(self.boards);
}
self.fetch_boards().then(resolve);
});
};
module.exports = Course;