Select Git revision
CourseGeneration.php
CourseGeneration.php 7.36 KiB
<?php
/**
*
* @tableClass CourseGenerations
* @foreignKey assetId
*
*/
class CourseGeneration extends Asset
{
/**
* Home crosslisting
*
* @var CourseCrosslisting
*/
protected $_homeCrosslisting;
public function _init()
{
parent::_init();
$this->_registerRelation(
new Nmc_Db_Table_Relation_HasSubset(CourseDetails::getInstance(), $this, 'generation'));
$this->_registerRelation(
new Nmc_Db_Table_Relation_HasMany(CourseActivities::getInstance(), $this, 'generation', 'activities'));
$this->_registerRelation(
new Nmc_Db_Table_Relation_HasMany(CourseCredits::getInstance(), $this, 'generation', 'credits'));
$this->_registerRelation(
new Nmc_Db_Table_Relation_HasMany(CourseCrosslistings::getInstance(), $this, 'generation', 'crosslistings'));
$this->_registerRelation(
new Nmc_Db_Table_Relation_HasMany(CourseDfRemovals::getInstance(), $this, 'generation', 'dfRemovals'));
$this->_registerRelation(
new Nmc_Db_Table_Relation_HasMany(CoursePrerequisites::getInstance(), $this, 'generation', 'prerequisites'));
$this->_registerRelation(
new Nmc_Db_Table_Relation_HasOne(CourseGradTieIns::getInstance(), $this, 'generation', 'gradTieIn'));
$this->_homeCrosslisting = $this->_getHomeCrosslisting();
}
public function _clone()
{
$parentId = $this->getPrimaryKey();
// duplicate parent record
parent::_clone();
$this->_homeCrosslisting = clone $this->_homeCrosslisting;
if($parentId) {
$this->parent = $parentId;
}
}
public function _get($name)
{
switch($name)
{
case 'subject':
case 'courseNumber':
case 'courseLetter':
case 'integratedStudies':
return $this->_homeCrosslisting->$name;
break;
default:
return parent::_get($name);
}
}
public function _set($name, $value)
{ switch($name)
{
case 'subject':
case 'courseNumber':
case 'courseLetter':
case 'integratedStudies':
$this->_homeCrosslisting->$name = $value;
break;
default:
parent::_set($name, $value);
break;
}
}
protected function _save()
{
$db = $this->getTable()->getAdapter();
$parentTransaction = false;
try {
$db->beginTransaction();
$parentTransaction = true;
} catch (PDOException $e) {
if($e->getMessage() != 'There is already an active transaction') {
throw $e;
}
}
$saveCourse = $this->course;
$saveCreationTime = $this->creationTime;
if($this->request instanceof Request) {
$saveRequest = $this->request->toArray();
}
$saveRequestId = $this->_data['request'];
try {
if(!$this->course) {
$course = Courses::getInstance()->fetchNew();
$course->save();
$this->course = $course->getPrimaryKey();
}
if(!$this->creationTime) {
$this->creationTime = time();
}
if($this->request instanceof Request) {
$this->request->save();
$this->_data['request'] = $this->request->getPrimaryKey();
}
$this->_setHomeCrosslisting($this->_homeCrosslisting);
parent::_save();
if($parentTransaction) {
$db->commit();
}
} catch(Exception $e) {
if($parentTransaction) {
$db->rollBack();
}
$this->course = $saveCourse;
$this->creationTime = $saveCreationTime;
if($this->request instanceof Request) {
$this->_data['request'] = $saveRequestId;
$this->request->setFromArray($saveRequest);
}
throw $e;
}
}
public function getAssetId()
{
return $this->assetId;
}
public function getParentGeneration($official = false)
{
if(!$this->parent) {
return null;
}
$parentGeneration = CourseGenerations::getInstance()->findOne($this->parent);
$foo = $parentGeneration->type;
while($official && $parentGeneration && $parentGeneration->type != 'official') {
$parentGeneration = CourseGenerations::getInstance()->findOne($parentGeneration->parent);
}
return $parentGeneration;
}
public function getChildGenerations()
{
return CourseGenerations::getInstance()->fetchWithParentGeneration($this);
}
public function isEssentialStudies($college = null)
{
$courseCode = CourseCodes::getInstance()->findBySubjectNumberAndLetter($this->subject,
$this->courseNumber,
$this->courseLetter);
if (is_null($courseCode)) {
return false;
}
return CourseEsDesignations::getInstance()->isCourseCodeEssentialStudies($courseCode, $college);
}
public function getEssentialStudiesAreas(College $college = null)
{
$courseCode = CourseCodes::getInstance()->findBySubjectNumberAndLetter($this->subject,
$this->courseNumber,
$this->courseLetter);
if (is_null($courseCode)) {
if (is_null($college)) {
return array(array());
} else {
return array();
}
}
return CourseEsDesignations::getInstance()->getEssentialStudiesAreas($courseCode, $college);
}
public function getHomeCollege()
{
$department = $this->getHomeDepartment();
$college = $department->college;
return $college;
}
public function getHomeDepartment()
{
$subject = Subjects::getInstance()->fetchSubject($this->subject);
$department = $subject->department;
return $department;
}
/**
* Returns a reference to the home crosslist *
* @return CourseCrosslisting
*/
protected function _getHomeCrosslisting()
{
$crosslistings = $this->crosslistings;
foreach($crosslistings as $key => $crosslisting) {
if($crosslisting->type == 'home listing') {
$homeCrosslisting = $crosslisting;
}
}
if(!$homeCrosslisting) {
$homeCrosslisting = CourseCrosslistings::getInstance()->fetchNew();
$homeCrosslisting->type = 'home listing';
}
return $homeCrosslisting;
}
protected function _setHomeCrosslisting($home)
{
$crosslistings = $this->crosslistings;
$found = false;
foreach($crosslistings as $key => $crosslisting) {
if($crosslisting->type == 'home listing') {
$crosslistings[$key] = $home;
$found = true;
break;
}
}
if(!$found) {
$crosslistings[] = $home;
}
}
}