Select Git revision
CourseEsDesignations.php
-
Tim Steiner authoredTim Steiner authored
CourseEsDesignations.php 3.65 KiB
<?php
class CourseEsDesignations extends Nmc_Db_Table
{
protected $_primary = 'courseEsDesignationId';
protected $_rowClass = 'CourseEsDesignation';
/**
* The one true instance
*
* @var CourseEsDesignations
*/
static protected $_instance;
/**
* Return the one true instance
*
* @return CourseEsDesignations
*/
static public function getInstance($config = array())
{
if (!self::$_instance) {
self::$_instance = new CourseEsDesignations($config);
}
return self::$_instance;
}
/**
* Returns boolean indicating whether or not the course is ES for the
* specified college, or any college if none is given.
*
* @param CourseCode $course
* @param College $college
* @return bool
*/
public function isCourseCodeEssentialStudies(CourseCode $course, College $college = null)
{
$db = $this->getAdapter();
$where = array();
$where[] = $db->quoteInto('courseCode = ?', $course->getPrimaryKey());
if ($college) {
$where[] = $db->quoteInto('college = ?', $college->getPrimaryKey());
}
$where = implode(' AND ', $where);
$result = $this->fetchAll($where);
if ($result->count() > 0) {
return true;
} else {
return false;
}
}
public function getEssentialStudiesAreas(CourseCode $course, College $college = null)
{
$db = $this->getAdapter();
$where = array();
$where[] = $db->quoteInto('courseCode = ?', $course->getPrimaryKey());
if ($college) {
$where[] = $db->quoteInto('college = ?', $college->getPrimaryKey());
}
$where = implode(' AND ', $where);
$results = $this->fetchAll($where);
$areas = $this->getEmptyEssentialStudiesArray($college);
foreach ($results as $row) {
if ($college) {
$areas[$row->area] = true;
} else {
$areas[$row->college->name][$row->area] = true;
}
}
return $areas;
}
public function setEssentialStudiesAreas(CourseCode $course, College $college, $areas)
{
$db = $this->getAdapter();
foreach ($areas as $areaName => $areaValue) {
$where = array();
$where[] = $db->quoteInto('courseCode = ?', $course->getPrimaryKey());
$where[] = $db->quoteInto('college = ?', $college->getPrimaryKey());
$where[] = $db->quoteInto('area = ?', $areaName);
$where = implode(' AND ', $where);
$row = $this->fetchRow($where);
if ($row && $areaValue == 'no') {
$row->delete();
} else if (!$row && $areaValue == 'yes') {
$row = $this->fetchNew();
$row->courseCode = $course->getPrimaryKey();
$row->college = $college;
$row->area = $areaName;
$row->save();
}
}
}
public function getEmptyEssentialStudiesArray(College $college = null)
{
$areaCodes = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H');
$colleges = Colleges::getInstance()->fetchAll();
$areas = array();
if ($college) {
foreach ($areaCodes as $areaCode) {
$areas[$areaCode] = false;
}
} else {
foreach ($colleges as $collegeRow) {
$areas[$collegeRow->name] = array();
foreach ($areaCodes as $areaCode) {
$areas[$collegeRow->name][$areaCode] = false;
}
}
}
return $areas;
}
}