forked from bsgreenb/Open-Textbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
200 lines (167 loc) · 8.57 KB
/
api.php
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
ini_set('memory_limit', '-1');
//An API for the course-book data. Give it sequential urls, like this:
// api.php?campus=N
// api.php?campus=N&term=N
// api.php?campus=N&term=N&division=N
// api.php?campus=N&term=N&division=N&dept=N
// api.php?campus=N&term=N&division=N&dept=N&course=N
// api.php?campus=N&term=N&division=N&dept=N&course=N§ion=N
//
//..where N is an ID. Note that the division always has to be provided. even if the system in question doesn't have divisions, the bookstore_functions code creates a placeholder
require('includes/autoloads.php');
date_default_timezone_set('GMT');
$first_start = microtime(true);
$user_token = $_SERVER['REMOTE_ADDR']; //globalled in url_functions
$json = array();
$json['data'] = array();
if (!$conn = connect()) {
$json['status'] = 'Error: DB Connect failure';
} else if (!isset($_GET['campus'])) { /* campus list */
$query = 'SELECT Campuses.Campus_ID, Campus_Names.Campus_Name
FROM Campuses
INNER JOIN Campus_Names ON Campuses.Campus_ID = Campus_Names.Campus_ID
INNER JOIN Bookstores ON Bookstores.Bookstore_ID = Campuses.Bookstore_ID
WHERE Campus_Names.Is_Primary = "Y"
ORDER BY Campus_Names.Campus_Name ASC';
if (!$result = mysql_query($query)) {
$json['status'] = 'Error: SQL query for all campuses yielded error: ' . mysql_error();
} else if (mysql_num_rows($result) == 0) {
$json['status'] = 'Error: SQL query for all campuses yielded no results';
} else { //all good, show them the campuses
$json['status'] = 'ok';
while ($row = mysql_fetch_assoc($result)) {
$json['data'][] = array('id' => $row['Campus_ID'], 'name' => $row['Campus_Name']);
}
}
} else if (!isset($_GET['section'])) { /* dropdowns.. */
$dd_array = array('campus', 'term', 'division', 'dept', 'course', 'section');
$ids_array = array('Campus_ID', 'Term_ID', 'Division_ID', 'Department_ID', 'Course_ID', 'Class_ID'); //yes, i know I should've called this Section_ID..
$names_array = array('Campus_Name', 'Term_Name', 'Division_Name', 'Department_Code', 'Course_Code', 'Class_Code');
//determine which dropdown they sent based on what they sent..
$sent_dd = 0;
foreach ($dd_array as $dd) {
if (isset($_GET[$dd])) {
$sent_dd++;
} else {
break;
}
}
$sent_dd--; //lazy hack
$sent_name = $dd_array[$sent_dd];
$sent_val = $_GET[$sent_name];
if (!valid_ID($sent_val)) {
$json['status'] = 'Error: Invalid ' . $sent_name . ' id = ' . $sent_val;
} else {
//begin by checking if they're in the DB already
$start = microtime(true);
$query = next_dropdowns_query(array($sent_name => $sent_val));
$end = microtime(true);
$json['first_query_time'] = round($end - $start, 3) * 1000;
if (!$result = mysql_query($query)) {
$json['status'] = 'Error: SQL query based on ' . $sent_name . '=' . $sent_val . ' yielded error: ' . mysql_error();
} else if (mysql_num_rows($result) == 0) {
$json['status'] = 'Error: SQL query based on ' . $sent_name . '=' . $sent_val . ' yielded no results';
} else {
$next_name = $names_array[$sent_dd + 1];
$next_id = $ids_array[$sent_dd + 1];
$row = mysql_fetch_assoc($result);
if (!$row[$next_id]) {
$start = microtime(true);
//mysql_close($conn);
update_classes_from_bookstore($row); //make bookstore requests to update db cache with what we want.. note that this attempts 3 times..
if (!$conn = connect()) {
$json['status'] = mysql_error();
die(json_encode($json));
}
$end = microtime(true);
$json['scrape_and_cache_update_time'] = round($end - $start, 3) * 1000;
//now we try to the query again..
$start = microtime(true);
if (!$result = mysql_query($query)) {
$json['status'] = 'Error: SQL query based on ' . $sent_name . '=' . $sent_val . ' yielded error: ' . mysql_error();
} else if (mysql_num_rows($result) == 0) {
$json['status'] = 'Error: SQL query based on ' . $sent_name . '=' . $sent_val . ' yielded no results';
}
$end = microtime(true);
$json['second_query_time'] = round($end - $start, 3) * 1000;
} else {
mysql_data_seek($result, 0); //rewind
}
$row = mysql_fetch_assoc($result);
if (!$row[$next_id]) {
$json['status'] = 'Warning: No entities returned from bookstore with ' . $sent_name . '=' . $sent_val . '. Could be scraping error, or there could genuinely be no corresponding entities.';
} else { //everything worked out; time to return ata
$json['status'] = 'ok';
mysql_data_seek($result, 0); //rewind
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[$row[$next_id]] = array('id' => $row[$next_id], 'name' => $row[$next_name]);
if (isset($row['Instructor']) && $row['Instructor']) {
$data[$row[$next_id]]['instructor'] = $row['Instructor'];
}
}
$json['data'] = array_values($data);
}
}
}
} else { /* class-books */
$section_id = $_GET['section'];
$campus_id = $_GET['campus'];
if (!valid_ID($campus_id)) {
$json['status'] = 'Error: invalid campus id: ' . $campus_id;
} else if (!valid_ID($section_id)) {
$json['status'] = 'Error: invalid section id: ' . $section_id;
} else {
$Books = array();
$query = class_items_query(array($section_id), $campus_id);
if (!$result = mysql_query($query)) {
$json['status'] = 'Error: Class-Items SQL query failed with ' . mysql_error();
} else if (mysql_num_rows($result) == 0) {
$json['status'] = 'Error: Class-Items SQL query yielded no results';
} else {
$row = mysql_fetch_assoc($result);
if ($row['no_class_item']) { //note that this is Class_Items_Cache.Class_ID, NOT Classes_Cache.Class_ID which must have been set from before (or else you'd get the 0 rows error above)
//mysql_close($conn);
update_class_items_from_bookstore(array($row)); //this function updates the DB with the class-item data
if (!$conn = connect()) {
$json['status'] = mysql_error();
die(json_encode($json));
}
if (!$result = mysql_query($query)) {
$json['status'] = 'Error: Class-Items SQL query failed with ' . mysql_error();
} else if (mysql_num_rows($result) == 0) {
$json['status'] = 'Error: Class-Items SQL query yielded no results';
}
} else {
mysql_data_seek($result, 0); //rewind
}
if (mysql_num_rows($result)) { //everything has worked..
$json['status'] = 'ok';
$json['data']['items'] = array();
$row = mysql_fetch_assoc($result);
if ($row['Item_ID']) { //it has at least one item
//So, loop the books into the array
while ($row = mysql_fetch_assoc($result)) {
$Books = load_books_from_row($Books, $row);
}
//Load our ish into JSON and output...
$data = array('items' => array());
mysql_data_seek($result, 0);
$previous_item = false; //keeps track of whether we've run into a new time
while ($row = mysql_fetch_assoc($result)) {
if ($row['Item_ID'] != $previous_item) {
$previous_item = $row['Item_ID'];
$item = array('id' => $row['Item_ID'], 'necessity' => $row['Necessity'], 'comment' => $row['Comments'], 'isbn' => $row['ISBN'], 'title' => $row['Title'], 'edition' => $row['Edition'], 'authors' => $row['Authors'], 'year' => $row['Year'], 'publisher' => $row['Publisher']);
$data['items'][] = $item;
}
}
$json['data'] = $data;
}
}
}
}
}
$json['total_time'] = round(microtime(true) - $first_start, 3) * 1000;
header('Content-Type: application/json');
echo json_encode($json);