This repository has been archived by the owner on Apr 29, 2018. It is now read-only.
forked from cholten99/unimatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
placesinjson.php
executable file
·204 lines (175 loc) · 7.13 KB
/
placesinjson.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
201
202
203
204
<?php
function GetQuery() {
// grab variables for the querystring
$course = mysql_real_escape_string($_GET['course']);
$group = mysql_real_escape_string($_GET['group']);
$minTut = mysql_real_escape_string($_GET['mintut']);
$maxTut = mysql_real_escape_string($_GET['maxtut']);
$minStaff = mysql_real_escape_string($_GET['minstaff']);
$maxStaff = mysql_real_escape_string($_GET['maxstaff']);
$minCar = mysql_real_escape_string($_GET['mincar']);
$maxCar = mysql_real_escape_string($_GET['maxcar']);
$teach = mysql_real_escape_string($_GET['teach']);
$feedback = mysql_real_escape_string($_GET['feed']);
$overall = mysql_real_escape_string($_GET['over']);
$minPub = mysql_real_escape_string($_GET['minpub']);
$maxPub = mysql_real_escape_string($_GET['maxpub']);
$minClub = mysql_real_escape_string($_GET['minclub']);
$maxClub = mysql_real_escape_string($_GET['maxclub']);
// parts of the SELECT statement - to be concatenated
$startString = "SELECT DISTINCT guardian_combo_complete.* FROM courses INNER JOIN guardian_combo_complete USING (university_code) WHERE courses.course LIKE '%$course%' AND ";
switch ($group) {
case "Russell":
$uniGroupString = "uni_group LIKE '%Russell%'";
break;
case "1994":
$uniGroupString = "uni_group LIKE '%1994%'";
break;
case "Million+":
$uniGroupString = "uni_group LIKE '%Million%'";
break;
case "Alliance":
$uniGroupString = "uni_group LIKE '%Alliance%'";
break;
case "UKADIA":
$uniGroupString = "uni_group LIKE '%UKADIA%'";
break;
default:
$uniGroupString = "(uni_group LIKE '%Russell%' OR uni_group LIKE '%1994%' OR uni_group LIKE '%Million%' OR uni_group LIKE '%Alliance%' OR uni_group LIKE '%UKADIA%')";
}
$tuitionString = "max_tuition >= $minTut AND max_tuition <= $maxTut";
switch (mysql_real_escape_string($_GET['ucas'])) {
case 1:
$ucasString = "entry_tariff >= 0 AND entry_tariff <= 180";
break;
case 2:
$ucasString = "entry_tariff >= 180 AND entry_tariff <= 240";
break;
case 3:
$ucasString = "entry_tariff >= 240 AND entry_tariff <= 300";
break;
case 4:
$ucasString = "entry_tariff >= 300 AND entry_tariff <= 360";
break;
case 5:
$ucasString = "entry_tariff >= 360";
break;
default:
$ucasString = "entry_tariff >= 0";
}
$studentStaffRatioString = "student_staff_ratio >= $minStaff AND student_staff_ratio <= $maxStaff";
$careerProspectsString = "career_prospects >= $minCar AND career_prospects <= $maxCar";
$teacherRatingString = "nss_teaching >= $teach";
$feedbackRatingString = "nss_feedback >= $feedback";
$overallRatingString = "nss_overall >= $overall";
$pubString = "num_of_pubs >= $minPub AND num_of_pubs <= $maxPub";
$clubString = "num_of_clubs >= $minClub AND num_of_clubs <= $maxClub";
switch (mysql_real_escape_string($_GET['sort'])) {
case "staff":
$orderby = "student_staff_ratio";
break;
case "ucas":
$orderby = "entry_tariff";
break;
case "car":
$orderby = "career_prospects DESC";
break;
case "over":
$orderby = "nss_overall DESC";
break;
case "tut":
$orderby = "max_tuition";
break;
default:
$orderby = "institution";
}
$endString = " ORDER BY " . $orderby;
// concatenate each part
return $startString . $uniGroupString . " AND " . $tuitionString . " AND " . $ucasString . " AND " . $studentStaffRatioString . " AND " . $careerProspectsString . " AND " . $teacherRatingString . " AND " . $feedbackRatingString . " AND " . $overallRatingString . " AND " . $pubString . " AND " . $clubString . $endString;
}
function InitDB() {
$user = getenv("DB1_USER");
$pass = getenv("DB1_PASS");
$con = mysql_connect("localhost", $user, $pass);
if (!$con) {
TestLog("DB connect failed with" . mysql_error());
die('Could not connect: ' . mysql_error()); // if can't connect
}
mysql_select_db("bowsy_yrs2011", $con);
return $con;
}
function GetInfoTable($row, $showCourses) { // I know one big string would work, but this prevents sending several kB of tabs...
if ($showCourses) {
$courses = "<b>Courses:</b>"."<div id=\"courses\">Loading...</div>";
}
return "<table cellpadding=5>".
"<tr>".
"<td valign=top>".
"<a href=\"".$row['website']."\" target=_blank>[Website]</a><br />".
"<b>2012 rank:</b> ".$row['2012_rank']."<br />".
"<b>Uni group:</b> ".$row['uni_group']."<br />".
"<b>Student/staff ratio:</b> ".$row['student_staff_ratio']."<br />".
"<b>Max tuition:</b> £".$row['max_tuition']."<br />".
"<b>NSS teaching:</b> ".$row['nss_teaching']."%<br />".
"<b>NSS feedback:</b> ".$row['nss_feedback']."%<br />".
"<b>NSS overall:</b> ".$row['nss_overall']."%<br />".
"<b>Career prospects:</b> ".$row['career_prospects']."%<br />".
"<b>Average entry tariff:</b> ".$row['entry_tariff']." UCAS points".
"</td>".
"<td valign=top>".
"<b>Address:</b> ".$row['address']."<br />".
"<b>Pubs:</b> ".$row['num_of_pubs']."<br />".
"<b>Clubs:</b> ".$row['num_of_clubs']."<br />".
$courses.
"</td>".
"</tr>".
"</table>";
}
// Begin Main ---------------------------------------------------------------
// Mustn't forget this!
header("Content-Type: application/json");
// Figure out if the browser supports gzip
if ($gzip = substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
header("Content-Encoding: gzip");
}
// Load in the database
$con = InitDB();
// Load a flat-file database of colours
$colours = file('colours.txt');
// Assemble a Query String from the data we've been sent
if ($all = (mysql_real_escape_string($_GET['display']) == "all")) { // Display All Mode
$queryString = "SELECT * FROM guardian_combo_complete ORDER BY institution";
} else { // Normal Mode
$queryString = GetQuery();
}
// Create something in which to store the output
$json = array();
// We send this over to make a neat callback
$course = mysql_real_escape_string($_GET['course']);
// Add stars if we're storting by something for a particular course
$star = !($all || mysql_real_escape_string($_GET['sort']) == "alpha");
// Put in that Query
$result = mysql_query($queryString);
// Work out how many stars we should put on
$starCount = ceil(mysql_num_rows($result) / 10);
// Add an array element for each result
$i = 0;
while($row = mysql_fetch_array($result)){
$json[] = array(
'name' => $row['institution'],
'code' => $row['university_code'],
'lat' => $row['latitude'],
'lng' => $row['longitude'],
'star' => $star && ($i < $starCount), // Maybe star the first 10%
'course' => $all ? "NONE" : $course, // If we're getting all, course is irrelevant.
'colour' => $colours[$i],
'info' => GetInfoTable($row, !$all) // Include the 'courses' title if we're not getting all.
); $i++;
}
// And echo the result!
if ($gzip) {
echo(gzencode(json_encode($json)));
} else {
echo(json_encode($json));
}
?>