-
Notifications
You must be signed in to change notification settings - Fork 0
/
Student.php
executable file
·280 lines (259 loc) · 8.93 KB
/
Student.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
<?php
/**
* Handles processing of the main page for student schedules.
*
* @author Bion Oren
* @version 2.0
*/
class Student extends Main {
/** ARRAY List of common classes in the schedule. */
public static $common = array();
/** ARRAY Array of filters to keep classes - of the form keepFilter[classID] = [sectionNumber]. */
protected static $keepFilter = array();
/** ARRAY Sorted array of the form classes[dept][classID][] = [Course]. */
protected $classes = array();
/** MIXED Numeric array of array of course objects for the currently selected courses, or an error string. */
protected $courses = array();
/** ARRAY List of department names. */
protected $departments = array();
/** ARRAY Array of error messages for classes keyed by the class' order of selection. */
protected $errors = array();
/** INTEGER The total number of hours for the selected classes. */
protected $hours = 0;
/** ARRAY Associative array of selected courses (DEPT####). */
protected $selectedChoices = array();
/**
* Initializes all the class variables.
*/
public function __construct() {
parent::__construct();
if(Main::haveSelections()) {
if(!empty($_REQUEST["choice"]) && !is_array($_REQUEST["choice"])) {
$_REQUEST["choice"] = array($_REQUEST["choice"]);
}
foreach($_REQUEST["choice"] as $course) {
if(!empty($course)) {
$this->selectedChoices[] = $course;
}
}
if(empty($this->selectedChoices)) {
unset($_REQUEST["choice"]);
}
}
$this->process();
}
/**
* Checks if the given course is valid in at least one available schedule.
*
* @param ARRAY $sections List of sections (a section list is a list of Course objects).
* @return MIXED False if no errors, error string otherwise.
*/
function checkValidClass(array $sections) {
if($this->hasNoErrors() && !in_array($sections[0]->courseID, $this->selectedChoices)) {
$courses = $this->courses;
$courses[] = $sections;
$conflict = $this->findSchedules($courses);
if(is_array($conflict)) {
return false;
} else {
if($conflict) {
$ret = array();
foreach($sections as $section) {
$ret[] = $section->getPrintQS();
}
return implode("~", $ret);
} else {
return false;
}
}
}
return false;
}
/**
* Used to validate classes in a dropdown list
*
* @param ARRAY $courses List of sections.
* @return MIXED A conflict message if there was a conflict, null if there wasn't a conflict.
*/
function findSchedules(array $courses) {
$numCourses = count($courses);
$indexes = array_fill(0, $numCourses, 0);
$classes = array();
$courseCounts = array();
foreach($courses as $arr) {
$courseCounts[] = count($arr);
}
while(true) {
for($i = 0; $i < $numCourses; $i++) {
$classes[$i] = $courses[$i][$indexes[$i]];
}
if(isValidSchedule($classes)) {
return null;
}
//for each course, if the index for this course is less than the max section index, shift it
//also handles rollover for previous indicies
for($i = 0; ++$indexes[$i] == $courseCounts[$i];) {
$indexes[$i++] = 0;
//this exits the loop
if($i == $numCourses) break 2;
}
}
$conflict = findConflicts($courses, true);
return implode("<br>", $conflict);
}
/**
* Returns an array of the selected (via filter) classes.
*
* @return ARRAY Classes that should be selected.
*/
protected static function getChosenClasses() {
$classFilter = array();
if(isset($_REQUEST["cf"])) {
foreach($_REQUEST["cf"] as $req) {
$classFilter[substr($req, 0, 9)] = $req;
}
}
return $classFilter;
}
/**
* Returns a JSON list of class names for the given department.
*
* @param STRING $dept 4 letter department code.
* @return STRING JSON encoded class list.
*/
public function getCourseJSON($dept) {
$ret = array();
$tmp = array();
foreach($this->classes[$dept] as $key=>$sections) {
$tmp["class"] = $sections[0]->getLabel();
$tmp["error"] = $this->checkValidClass($sections);
$ret[$key] = $tmp;
}
return json_encode($ret);
}
/**
* Returns a JSON list of department names.
*
* @return STRING JSON encoded department list.
*/
public function getDepartmentJSON() {
return json_encode($this->departments);
}
public function getPrintExtra() {
$extra = array();
foreach(Student::$keepFilter as $uid) {
$dept = substr($uid, 0, 4);
$id = substr($uid, 0, 9);
$extra[] = $this->classes[$dept][$id][array_search($uid, $this->classes[$dept][$id])]->getPrintQS();
}
$extra = implode("~", $extra);
if(!empty($extra)) {
$extra = "~".$extra;
}
return $extra;
}
/**
* Returns the querystring used to generate a picture of the given classes.
*
* @param ARRAY $classes List of classes to display.
* @return STRING Querystring for display.
* @see print.php
*/
public static function getPrintQS(array $classes) {
$trad = (Main::isTraditional())?"trad":"non";
$ret = 'sem='.Main::getSemester().'&trad='.$trad.'&classes=';
$tmp = array();
foreach($classes as $class) {
$tmp[] = $class->getPrintQS();
}
$ret .= implode("~", $tmp);
//return $ret;
return htmlspecialchars($ret);
}
/**
* Returns true if the given input class caused an error.
*
* @param STRING $index Class ID.
* @return BOOLEAN True on error.
*/
protected function hasError($index) {
return isset($this->errors[$index]);
}
/**
* Returns true if there were no fatal errors generating schedules.
*
* @return BOOLEAN True if no errors.
*/
protected function hasNoErrors() {
return empty($errors) && is_array($this->courses);
}
/**
* Returns true if the given class is marked (by filters) to be kept for consideration in schedules.
*
* @param COURSE $class Class to evaluate.
* @return BOOLEAN True if kept.
*/
public static function isKept(Course $class) {
return false;
//return !isset(Student::$keepFilter[$class->courseID]) || Student::$keepFilter[$class->courseID] == $class->uid;
}
/**
* Sets up static environment variables.
*
* @return VOID
*/
public static function init() {
Student::$keepFilter = Student::getChosenClasses();
}
/**
* Initilizes internal class arrays. Also fetches all valid schedules for the given input.
*
* @return VOID
*/
protected function process() {
$classData = getClassData(Main::getSemester(), Main::isTraditional());
Main::$CAMPUS_MASK = array_pop($classData);
$this->setCampusMask();
//generate select option values for display later
$courseTitleNumbers = array();
foreach($classData as $class) {
if(!($class->getCampus() & $this->campusMask)) {
continue;
}
$id = $class->courseID;
$dept = substr($id, 0, 4);
$this->departments[$dept] = $dept;
$this->classes[$dept][$id][] = $class;
$courseTitleNumbers[$id][] = $class;
}
//alphabetize the class list
array_multisort($this->classes);
if(Main::haveSelections()) {
//gather input data
foreach($this->selectedChoices as $choices) {
$choiceSections = array();
$partOfSet = count($choices) > 1;
foreach($choices as $key) {
$choiceSections = array_merge($choiceSections, $courseTitleNumbers[$key]);
if($partOfSet) {
foreach($courseTitleNumbers[$key] as $section) {
$section->partOfSet = true;
}
}
}
$this->courses[] = $choiceSections;
}
//find possible schedules
$this->courses = findSchedules($this->courses);
if(!is_array($this->courses)) {
$this->errors = true;
}
foreach($this->courses as $sections) {
if(count($sections) == 1) {
Student::$common[] = $sections[0];
}
}
}
}
}
?>