diff --git a/application/modules/courses/models/CourseModel.php b/application/modules/courses/models/CourseModel.php index 480be6d8eec0a93335ab2c196839e7577606c37b..c30da5e8902bfffbc7fe47c4a5a3aa114149f410 100644 --- a/application/modules/courses/models/CourseModel.php +++ b/application/modules/courses/models/CourseModel.php @@ -1527,6 +1527,10 @@ class Courses_CourseModel extends Unl_Model public function setCourseCode($subject, $courseNumber, $courseLetter = null) { + if (!self::isSubjectValid($subject)) { + throw new Zend_Exception('The subject code ' . $subject . ' is invalid.'); + } + $homeIndex = null; foreach ($this->_data['crosslistings'] as $index => $crosslisting) { if ($crosslisting['type'] == 'home listing') { @@ -1631,6 +1635,10 @@ class Courses_CourseModel extends Unl_Model public function addCrosslisting($type, $subject, $courseNumber, $courseLetter) { + if (!self::isSubjectValid($subject)) { + throw new Zend_Exception('The subject code ' . $subject . ' is invalid.'); + } + $lowestId = min(array_keys($this->_data['crosslistings'])); if (!($lowestId < 0)) { $lowestId = 0; @@ -1648,6 +1656,10 @@ class Courses_CourseModel extends Unl_Model public function editCrosslisting($id, $type, $subject, $courseNumber, $courseLetter) { + if (!self::isSubjectValid($subject)) { + throw new Zend_Exception('The subject code ' . $subject . ' is invalid.'); + } + if (!$this->_data['crosslistings'][$id]) { throw new Exception('Attempt to edit a non-existant crosslisting'); } @@ -2149,5 +2161,33 @@ class Courses_CourseModel extends Unl_Model } return true; } + + static $_subjectList; + + static protected function _loadSubjectList() + { + if (self::$_subjectList) { + return self::$_subjectList; + } + + $db = Zend_Registry::get('db'); + $select = new Zend_Db_Select($db); + + $select->from('creqSubjects', array('name')); + $records = $select->query()->fetchAll(); + self::$_subjectList = array(); + foreach ($records as $record) { + self::$_subjectList[] = $record['name']; + } + + return self::$_subjectList; + } + + static public function isSubjectValid($subject) + { + self::_loadSubjectList(); + + return in_array($subject, self::$_subjectList); + } }