-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourse.php
executable file
·241 lines (225 loc) · 9.49 KB
/
Course.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
<?php
/*
* Copyright 2010 Bion Oren
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once($path."Object.php");
/**
* Manages interactions with an individual class.
*
* @author Bion Oren
* @property-read Course $completeClass A Course object which was taken to complete this course.
* @property-read STRING $department The 4 letter department code for this class.
* @property-read STRING $departmentlinkid The magic code used to link back to this department in the LETU catalog.
* @property-read INTEGER $hours The number of credit hours this course is worth.
* @property-read INTEGER $ID The primary key for this class in the database.
* @property-read STRING $linkid The magic code linking this course back to the LETU catalog.
* @property-read INTEGER $number The number for this course.
* @property-read INTEGER $offered Status of semesters this course is offered.
* @property-read STRING $title Course title.
* @property-read INTEGER $years Status of years this course is offered.
*/
class Course extends Object {
/** STRING Cached query used to fetch class information from the database. */
const fetchSQL = "SELECT classes.*,
departments.department, departments.linkid AS deptlinkid
FROM classes
LEFT OUTER JOIN departments ON classes.departmentID = departments.ID ";
/** INTEGER Indicates a prerequisite dependency. */
const PREREQ = 1;
/** INTEGER Indicates a corequisite dependency. */
const COREQ = 2;
/** INTEGER Indicates a either a prerequisite or a corequisite dependency. */
const EITHER = 3;
/** Course A Course object which was taken to complete this course. */
protected $completeClass = null;
/** STRING The 4 letter department code for this class. */
protected $department;
/** STRING The magic code used to link back to this department in the LETU catalog. */
protected $departmentlinkid;
/** ARRAY List of course dependencies, keyed by the dependency constants in this class. */
protected $dependencies;
/** INTEGER The number of credit hours this course is worth. */
protected $hours;
/** INTEGER The primary key for this class in the database. */
protected $ID;
/** True if this class is being used as a substitute for another class. */
public $isSubstitute = false;
/** STRING The magic code linking this course back to the LETU catalog. */
protected $linkid;
/** INTEGER The ID of a note associated with this class. */
public $noteID = null;
/** INTEGER The number for this course. */
protected $number;
/**
* INTEGER Status of semesters this course is offered.
* @see dbinit.php
*/
protected $offered;
/** STRING Course title. */
protected $title;
/** INTEGER Psuedo-unique number assigned to this course instance. */
protected $uid;
/**
* INTEGER Status of years this course is offered.
* @see dbinit.php
*/
protected $years;
/**
* Conctructs a new course from database information.
*
* @param ARRAY $row Associative array of database info.
* @see fetchSQL
*/
protected function __construct(array $row) {
$this->uid = mt_rand();
$this->ID = intval($row["ID"]);
$this->department = $row["department"];
$this->departmentlinkid = $row["deptlinkid"];
$this->number = $row["number"];
$this->title = $row["title"];
$this->linkid = $row["linkid"];
$this->hours = $row["hours"];
$this->offered = $row["offered"];
$this->years = $row["years"];
$this->setupDependencies();
}
/**
* Returns true if the provided class is effectively equal to this one.
*
* @param Course $class Class to compare to.
* @return BOOLEAN True if they are essentially equal.
*/
public function equals(Course $class) {
return $class->ID == $this->ID && $class->title == $this->title;
}
/**
* Returns a list of this class' corequisites.
*
* @return MIXED Dependency string or something falsy if their are no corequisites.
* @warning The return value in the no coreq case is only guaranteed to evaluate to false, not to be some particular false value.
*/
public function getCorequisites() {
return $this->dependencies[Course::COREQ];
}
/**
* Returns a new Course object generated from a database row.
*
* @param INTEGER $id The primary key of the class.
* @return Course Class corresponding to the provided key.
*/
public static function getFromID($id) {
$db = SQLiteManager::getInstance();
$sql = Course::fetchSQL."WHERE classes.ID=".$id;
$result = $db->query($sql);
return new Course($result->fetchArray(SQLITE3_ASSOC));
}
/**
* Returns a new Course object that has the provided department and number.
*
* @param STRING $dept Department code.
* @param INTEGER $num Course number.
* @param STRING $title Optional title to provide for the new class.
* @return Course New Course object with the given department and number (and optionally title), or null if no such class exists.
*/
public static function getFromDepartmentNumber($dept, $num, $title="") {
$db = SQLiteManager::getInstance();
//try to get the class from our year if we can
$sql = Course::fetchSQL."WHERE departments.department='".$dept."' AND ".$num." BETWEEN classes.number AND classes.endNumber";
$result = $db->query($sql);
$ret = $result->fetchArray(SQLITE3_ASSOC);
if($ret === false) {
return null;
}
if(!empty($title)) {
$ret["title"] = $title;
$ret["number"] = $num;
}
return new Course($ret);
}
/**
* Returns this classes' department and number together.
*
* @return STRING DepartmentNumber course identifier.
* @see getDepartment()
* @see getNumber()
*/
public function getLabel() {
return $this->department.$this->number;
}
/**
* Returns a list of this class' dependencies that can be either prerequisites or corequisites.
*
* @return MIXED Dependency string or something falsy if their are no applicable dependencies.
* @warning The return value in the no dependencies case is only guaranteed to evaluate to false, not to be some particular false value.
*/
public function getPreOrCorequisites() {
return $this->dependencies[Course::EITHER];
}
/**
* Returns a list of this class' prerequisites.
*
* @return MIXED Dependency string or something falsy if their are no prerequisites.
* @warning The return value in the no prereq case is only guaranteed to evaluate to false, not to be some particular false value.
*/
public function getPrerequisites() {
return $this->dependencies[Course::PREREQ];
}
/**
* Returns a unique ID for this class instance.
*
* @return STRING UID.
* @see uid
*/
public function getUID() {
return $this->ID."_".$this->uid;
}
/**
* Returns true if this class has been completed.
*
* @return BOOLEAN True if the class has been completed.
*/
public function isComplete() {
return $this->completeClass !== null;
}
/**
* Completes this class.
*
* @param Course $class Class completing this one.
* @return VOID
*/
public function setComplete(Course $class) {
$this->completeClass = $class;
$class->isSubstitute = true;
}
/**
* Sets up course dependencies for this class.
*
* @return VOID
*/
protected function setupDependencies() {
$this->dependencies = array();
$result = SQLiteManager::getInstance()->query("SELECT type, data FROM classDependencyMap WHERE classID = '".$this->ID."'");
while($row = $result->fetchArray(SQLITE3_ASSOC)) {
$this->dependencies[$row["type"]] = $row["data"];
}
}
/**
* Returns a simple string represenation of this class.
*
* @return STRING Debug string.
*/
public function __toString() {
return $this->department.$this->number." - ".$this->title;
}
}
?>