-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.php
executable file
·338 lines (315 loc) · 11.8 KB
/
utils.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/***************************************************************************\
This file is part of RoomAllocation.
RoomAllocation is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RoomAllocation is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RoomAllocation. If not, see <http://www.gnu.org/licenses/>.
\***************************************************************************/
?>
<?php
require_once 'config.php';
require_once 'WorldRegions.php';
require_once 'models/Group_Model.php';
require_once 'models/Allocation_Model.php';
require_once 'models/Person_Model.php';
$Group_Model = new Group_Model();
$Allocation_Model = new Allocation_Model();
$Person_Model = new Person_Model();
/**
* @brief Applies input sanitizing functions for every value in the array, recursively
* @param {array} &$arr
* @returns {array}
*/
function recursive_escape( array &$arr ){
foreach( $arr as $k => $v ){
if( is_array( $v ) ){
recursive_escape($arr[$k]);
} else {
$arr[$k] = addslashes( $v );
}
}
return $arr;
}
/**
* Get all roommates of a person from a group
* @param {string} $eid
* @param {string} $group_id
*/
function get_roommates( $eid, $group_id ){
global $Group_Model;
global $Person_Model;
$roommates = $Group_Model->get_group_members_eid($eid);
$result = array();
foreach ($roommates as $member_eid) {
if ($member_eid == $eid) {
continue;
}
if ($member_eid == FRESHMAN_EID) {
$result[] = array(
'id' => -1,
'eid' => 0,
'account' => 'i-have-a-bo$$-account',
'fname' => 'Freshman',
'lname' => 'Cage',
'country' => 'Freshmania',
'college' => 'Everywhere',
'email' => '[email protected]',
'year' => date('y') + 3,
'status' => 'undergrad',
'major' => 'Freshman Studies',
'isTall' => 0,
'group_id' => $group_id
);
continue;
}
$result[] = $Person_Model->get($member_eid);
}
// $q = "SELECT p.* FROM ".TABLE_PEOPLE." p, ".TABLE_IN_GROUP." i
// WHERE i.group_id='$group_id'
// AND p.eid=i.eid AND i.eid<>'$eid' ";
// $roommates = sqlToArray( mysql_query( $q ) );
return $result;
}
/**
* @brief Extracts only one field from a 2-d array.
* Typically used when you want to get one column from sqlToArray
* @param {string} $column
* @param {array} $array
* @returns {array}
*/
function extract_column( $column, array $array ){
foreach( $array as $key => $row ){
$array[$key] = $row[$column];
}
return $array;
}
/**
* @brief Appends a value to multiple keys in a 2-d array
* @param {mixed} $class
* @param {array} $keys
* @param {array} &$target
*/
function add_class( $class, array $keys, array &$target ){
foreach( $keys as $room ){
if( !isset( $target[$room] ) ){
$target[$room] = array();
}
$target[$room][] = $class;
}
return $target;
}
function add_to_group ($eid, $group_id = null) {
global $Group_Model;
$group_id = $Group_Model->add_to_group($eid, $group_id);
$q_people = "SELECT p.* FROM ".TABLE_IN_GROUP." i, ".TABLE_PEOPLE." p
WHERE group_id='$group_id' AND i.eid=p.eid";
$people = Model::to_array( mysql_query( $q_people ) );
$points = get_points( $people );
$Group_Model->set_group_score($group_id, $points['total']);
return $group_id;
}
function group_info( $eid ){
$q = "SELECT
i.group_id,
(SELECT COUNT(id) FROM ".TABLE_IN_GROUP." j where j.group_id=i.group_id) AS members
FROM ".TABLE_IN_GROUP." i
WHERE i.eid='$eid';";
return mysql_fetch_assoc( mysql_query( $q ) );
}
function get_points( array $people, $college = null ){
global $WorldRegions;
global $WorldRegions_Inv;
global $Allocation_Model;
$year = ((int)date('Y')) % 100;
$countries = array();
$majors = array();
$individual_points = 0;
$individual = array();
foreach( $people as $v ){
if ($college === null) {
$alloc = $Allocation_Model->get_allocation($v['eid']);
$college = $alloc['college'];
}
if( $v['eid'] != FRESHMAN_EID ){
if( $v['status'] == 'undergrad' )
$p = min(2, max(1, 3-($v['year']-$year) ) );
else
$p = 1;
$countries[$v['country']] = true;
$majors[$v['major']] = true;
$p += ($college == $v['college']) ? 0.5 : 0;
$individual[] = $p;
$individual_points += $p;
} else {
$individual[] = 0;
}
}
$country_points = count($countries) > 1 ? count($countries) : 0;
$major_points = count($majors) > 1 ? count($majors)*0.25 : 0;
$world_regions = array_map(function ($v) use ($WorldRegions_Inv, $countries) {
if (isset($WorldRegions_Inv[$v])) {
return $WorldRegions_Inv[$v];
}
return null;
},
array_keys($countries)
);
$world_regions = array_unique( $world_regions );
$world_regions = count( $world_regions ) * 0.5;
$world_regions = $world_regions > 0.5 ? $world_regions : 0;
$total = $individual_points + $country_points + $major_points + $world_regions;
return array(
'people' => $individual,
'individual' => $individual_points,
'country' => $country_points,
'major' => $major_points,
'world' => $world_regions,
'total' => $total
);
}
function print_score( array $people, $points = null ){
$points = $points ? $points : get_points( $people );
$h = '<table class="points" cellspacing="0" cellpadding="0">';
$h .= '<tr class="individual-points"><td colspan="2" class="section">Individual points</td></tr>';
foreach( $points['people'] as $k => $value ){
$h .= "<tr class=\"individual-points\">
<td>".$people[$k]['fname'].", ".$people[$k]['lname']."</td>
<td class=\"value\">".$value."</td>
</tr>";
}
$h .= '<tr class="bonus-points"><td colspan="2" class="section">Bonus points</td></tr>';
$h .= '<tr class="bonus-points"><td>Nationalities</td><td class="value">'.$points['country'].'</td></tr>';
$h .= '<tr class="bonus-points"><td>World Regions</td><td class="value">'.$points['world'].'</td></tr>';
$h .= '<tr class="bonus-points"><td>Majors</td><td class="value">'.$points['major'].'</td></tr>';
$h .= '<tr class="total-points"><td class="section">Total</td><td class="value">'.$points['total'].'</td></tr>';
$h .= '</table>';
return $h;
}
function getFaceHTML( $info, $append = '', $classes = '' ){
foreach( $info as $k => $v ){ $$k = $v; }
$img = imageUrl( $eid );
$country_flag = flagURL( $country );
$d = 3-((2000+(int)$year)-(int)date("Y"));
$year_of_study = $d."<sup>".($d==1?'st':($d==2?'nd':($d==3?'rd':'th')))."</sup>";
$email = $info['email'];
$short_email = substr($email, 0, strrpos($email, '-'));
return <<<HTML
<table class="face $classes" cellspacing="0" cellpadding="0" id="face-eid-$eid">
<tr>
<td rowspan="4" class="photo"><img src="$img" height="64" /></td>
<td class="name"><b>$fname, $lname</b></td>
<td rowspan="4" class="country-photo">
<img height="64" alt="$country" src="$country_flag">
</td>
</tr>
<tr>
<td class="year">class of 20$year ($year_of_study year)</td>
</tr>
<tr>
<td class="country">$country</td>
</tr>
<tr>
<td class="email"><a href="mailto:$email">$short_email</a></td>
</tr>
$append
</table>
HTML;
}
function getFaceHTML_received( $info, $append = '', $classes = '' ){
$actions = '
<tr class="actions">
<td colspan="3" style="padding:3px;border-top:1px solid #999;background:#fff;text-align:center">
<div class="gh-button-group">
<a href="javascript:void(0)" onclick="sendResponse(\'requestReceived\',\''.$info['eid'].'\',\'yes\')" class="gh-button pill primary safe icon approve">accept</a>
<a href="mailto:'.$info['email'].'" class="gh-button pill icon mail">send email</a>
<a href="javascript:void(0)" onclick="sendResponse(\'requestReceived\',\''.$info['eid'].'\',\'no\')" class="gh-button pill danger icon remove">reject</a>
</div>
</td>
</tr>
';
return getFaceHTML( $info, $actions.$append, $classes );
}
function getFaceHTML_sent( $info, $append = '', $classes = '' ){
$actions = '
<tr class="actions">
<td colspan="3" style="padding:3px;border-top:1px solid #999;background:#fff;text-align:center">
<div class="gh-button-group">
<a href="mailto:'.$info['email'].'" class="gh-button pill icon mail">send email</a>
<a href="javascript:void(0)" onclick="sendResponse(\'requestSent\',\''.$info['eid'].'\',\'no\')" class="gh-button pill danger icon remove">cancel request</a>
</div>
</td>
</tr>
';
return getFaceHTML( $info, $append.$actions, $classes );
}
function is_tall_apartment (array $apartment) {
$tall_room_regexp = '/\s*^([MKC]?[ABCD]-(137|[123]08)|[N]?(B-[1234](78|8[014589]|9[2367])|C-([234](08|1[01689])|222|224|203|303)))\s*$/i';
foreach ($apartment as $room_number) {
if (preg_match($tall_room_regexp, $room_number)) {
return true;
}
}
return false;
}
function include_freshman (array $people) {
if (!isset($people[FRESHMAN_EID])) {
$people[FRESHMAN_EID] = array(
'id' => '-1',
'eid' => FRESHMAN_EID,
'account' => 'freshamn-invalid-account',
'fname' => 'Freshman',
'lname' => 'Cage',
'country' => 'Freshmania',
'college' => null,
'email' => null,
'year' => date('y') + 3,
'status' => 'undergrad',
'major' => null,
'absent' => '1',
'isTall' => '0',
'random_password' => NULL,
'query' => '',
);
}
return $people;
}
function email_template ($content) {
return '<div style="border:1px solid #ccc; border-radius:5px; background:#D1E1F4; max-width:500px; padding:0!important; font-family:tahoma,Verdana,arial; font-size:11pt; overflow:hidden;">
<div style="background:rgba(255,255,255,0.9); border-bottom: 1px solid #ccc; padding:10px; font-size:13pt; font-weight:bold;">
Hello World!
</div>
<div style="padding:10px">
'.$content.'
</div>
<div style="background:rgba(255,255,255,0.9); border-top:1px solid #ccc; padding:10px">
Enjoy! <br />
Cheerio, <br />
Stefan
</div>
</div> ';
}
/**
* Send a HTML email
* @param String $to
* @param String $subject
* @param String $message
* @return String
*/
function send_mail ($to, $subject, $from = '[email protected]', $message = 'Empty <b>HTML</b> mail') {
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from" . "\r\n";
$bcc = explode(',', $to);
for($i=1; $i<count($bcc); ++$i) {
$headers .= "BCC: ".$bcc[$i]." \r\n";
}
return mail($bcc[0], $subject, $message, $headers) ? true : false;
}
?>