Select Git revision
CourseGeneration.php
-
Tim Steiner authoredTim Steiner authored
CourseGeneration.php 5.08 KiB
<?php
class CourseGeneration extends Nmc_Db_Table_Row
{
protected $_homeCrosslisting;
public function __construct($config = array())
{
parent::__construct($config);
$this->_registerRelation(
new Nmc_Db_Table_Relation_Extend(Assets::getInstance(), $this, 'assetId'));
$this->_registerRelation(
new Nmc_Db_Table_Relation_HasOne(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->id;
// 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()
{
$parentTransaction = false;
try {
$this->_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->id;
}
if(!$this->creationTime) {
$this->creationTime = time();
}
if($this->request instanceof Request) {
$this->request->save();
$this->_data['request'] = $this->request->id;
}
$this->_setHomeCrosslisting($this->_homeCrosslisting);
parent::_save();
if($parentTransaction) {
$this->_db->commit();
}
} catch(Exception $e) {
if($parentTransaction) {
$this->_db->rollBack();
}
$this->course = $saveCourse;
$this->creationTime = $saveCreationTime;
if($this->request instanceof Request) {
$this->_data['request'] = $saveRequestId;
$this->request->setFromArray($saveRequest);
}
throw $e;
}
}
/**
* 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;
}
}
}