-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary.php
executable file
·298 lines (253 loc) · 6.09 KB
/
library.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
<?php
// File: library.php
// Description: Library of utility routines used in the Plan of Study code.
// Author: mapq
//
// Function: endsWith
// Description: simple string check, borrowed from stack overflow
// Params:
function endsWith($haystack, $needle)
{
// search forward starting from end minus needle length characters
if ($needle === "")
return true;
else {
$temp = strlen($haystack) - strlen($needle);
return ($temp >= 0) && (strpos($haystack, $needle, $temp) !== FALSE);
}
}
// Function: get_course_title
// Description: Given a course number, return its title
function get_course_title($area, $course)
{
global $cognates, $areas, $senior;
if ($area == "fivethousand")
return $areas[$course]['title'];
else if ($area == "sixthousand")
return $areas[$course]['title'];
else if ($area == "cognate")
return $cognates[$course];
else if ($area == "senior")
return $senior[$course];
else
return "--";
}
// Function: normalize_course
// Description: Normalize the course number to the format of CS4588
// Params: $course
function normalize_course($course)
{
// strip anything other than letters and numbers resulting in
// a course in the format of CS4588
return strtoupper(preg_replace("/([^a-zA-Z0-9])/", "", trim($course)));
}
// Function: normalize_term
// Description:
// Params:
function normalize_term($term)
{
// strip spaces and leave the term and the year... summer must be
// Summer not Summer I or II
$term = ucwords(strtolower(trim(strtok($term, " \t"))));
$year = strtok(" \t");
if (!$year) {
// possibly there is no space between term and year
preg_match("/([a-zA-Z]+)[ ]*([0-9]+)/", trim($term), $match);
$term = ucwords(strtolower($match[1]));
$year = $match[2];
// echo "$term<br>";//print_r($match);
return trim($term." ".$year);
}
else if (strlen($term.$year) > 0)
return trim($term." ".$year);
else
return "";
}
// Function: valid_studentid
// Description:
// Params:
function valid_studentid($id)
{
// strip anything other than numbers
$id = preg_replace("/([^0-9])/", "", $id);
// strlen=9 -- must be is_numeric()
return (strlen($id) == 9);
}
// Function: valid_coursename
// Description:
// Params:
function valid_coursename($c)
{
if (strlen(trim($c)) < 5) // must be at least 5 chars
return false;
preg_match("/^([a-zA-Z]+)([0-9]+)$/", trim($c), $match);
$dept = strtoupper($match[1]);
$number = $match[2];
return (ctype_alpha($dept) && is_numeric($number));
}
// Function: valid_coursexx
// Description:
// Params:
function valid_coursexx($c)
{
global $cognates, $areas, $senior;
if (strlen(trim($c)) < 6) // must be at least 6 chars
return false;
preg_match("/^([a-zA-Z]+)([0-9]+)$/", trim($c), $match);
$dept = strtoupper($match[1]);
$number = $match[2];
$name = $dept.$number;
// check to see if it is in array
if (isset($areas[$name]))
return true;
else if (isset($senior[$name]))
return true;
else if (isset($cognates[$name]))
return true;
else
return false;
}
// Function: valid_credithours
// Description:
// Params:
function valid_credithours($h)
{
if (is_numeric($h) && (intval($h)>0))
return true;
else
return false;
}
// Function: valid_term
// Description:
// Params:
function valid_term($t)
{
if (strlen(trim($t)) < strlen("fall 9999")) // must be at least this long
return false;
$term = strtolower(trim(strtok($t, " \t")));
$year = strtok(" \t");
if ((strcmp($term, "fall") == 0) ||
(strcmp($term, "spring") == 0) ||
(strcmp($term, "summer") == 0))
if (is_numeric($year) && (strlen($year) == 4))
return true;
return false;
}
// Function: valid_transfers
// Description:
// Params:
function valid_transfers($courses, $limit)
{
$total = 0;
foreach($courses as $course) {
if ($course['section'] == "transfer") {
$total += $course['credits'];
}
}
return $total <= $limit;
}
// Function: valid_indepedent_study_hrs
// Description:
// Params:
function valid_indepedent_study_hrs($courses, $min, $max)
{
$total = 0;
foreach($courses as $course) {
if ($course['section'] == "research")
$total += $course['credits'];
}
return $total >= $min && $total <= $max;
}
// Function: valid_research_hrs
// Description:
// Params:
function valid_research_hrs($courses, $min)
{
$total = 0;
foreach($courses as $course) {
if ($course['section'] == "research")
$total += $course['credits'];
}
return $total >= $min; // && $total <= $max;
}
// Function: valid_cognates
// Description:
// Params:
function valid_cognates($courses, $min, $max)
{
$total = 0;
foreach($courses as $course) {
if ($course['section'] == "cognate") {
$total += $course['credits'];
}
}
return $total >= $min && $total <= $max;
}
// Function: valid_cognates_courses
// Description:
// Params:
function valid_cognates_courses($courses)
{
global $cognates;
foreach($courses as $course) {
if ($course['section'] == "cognates") {
// check that the cognate is in the valid list
if (!isset($cognates[$course['course']]))
return false;
}
}
return true;
}
// Function: valid_senior
// Description:
// Params:
function valid_senior($courses, $limit)
{
$total = 0;
foreach($courses as $course) {
if ($course['section'] == "senior")
$total += $course['credits'];
}
return $total <= $limit;
}
// Function: valid_senior_course
// Description:
// Params:
function valid_senior_course($courses)
{
global $senior;
$total = 0;
foreach($courses as $course) {
if ($course['section'] == "senior")
// check that the course is in the valid list of senior courses
if (!isset($senior[$course['course']]))
return false;
}
return true;
}
// Function: only_one_5974
// Description:
// Params:
function only_one_5974($courses)
{
$totalcr = 0;
foreach($courses as $course) {
if (($course['section'] == "fivethousand") &&
($course['course']) == "CS5974")
$totalcr += $course['credits'];
}
return $totalcr <= 3;
}
// Function: valid_sixthousand
// Description:
// Params:
function valid_sixthousand($courses, $min)
{
$totalcr = 0;
foreach($courses as $course) {
if (strtolower($course['section']) == "sixthousand")
$totalcr += $course['credits'];
}
return $totalcr >= $min;
}
?>