Skip to content
Snippets Groups Projects
CourseCode.php 3.35 KiB
<?php

class CourseCode extends Nmc_Db_Table_Row
{
    /**
     * Gets the next course code
     *
     * @return CourseCode
     */
    public function getNextAlphabetically()
    {
        $db = $this->_table->getAdapter();

        $order = 'subject, course_number, course_letter';

        $where = array();
        $where[] = $db->quoteInto('subject = ?', $this->subject);
        $where[] = $db->quoteInto('course_number = ?', $this->courseNumber);
        $where[] = $db->quoteInto('course_letter > ?', $this->courseLetter);
        $where = implode(' AND ', $where);

        $row = $this->_table->fetchRow($where, $order);
        if($row->getPrimaryKey()) {
            return $row;
        }

        $where = array();
        $where[] = $db->quoteInto('subject = ?', $this->subject);
        $where[] = $db->quoteInto('course_number > ?', $this->courseNumber);
        $where = implode(' AND ', $where);

        $row = $this->_table->fetchRow($where, $order);
        if($row->getPrimaryKey()) {
            return $row;
        }

        $where = array();
        $where[] = $db->quoteInto('subject > ?', $this->subject);
        $where = implode(' AND ', $where);

        $row = $this->_table->fetchRow($where, $order);
        if($row->getPrimaryKey()) {
            return $row;
        }

        return null;

    }

    /**
     * Returns the previous course code
     *
     * @return CourseCode
     */
    public function getPreviousAlphabetically()
    {
        $db = $this->_table->getAdapter();

        $order = 'subject DESC, course_number DESC, course_letter DESC';

        $where = array();
        $where[] = $db->quoteInto('subject = ?', $this->subject);
        $where[] = $db->quoteInto('course_number = ?', $this->courseNumber);
        $where[] = $db->quoteInto('course_letter < ?', $this->courseLetter);
        $where = implode(' AND ', $where);

        $row = $this->_table->fetchRow($where, $order);
        if($row instanceof Nmc_Db_Table_Row && $row->getPrimaryKey()) {
            return $row;
        }

        $where = array();
        $where[] = $db->quoteInto('subject = ?', $this->subject);
        $where[] = $db->quoteInto('course_number < ?', $this->courseNumber);
        $where = implode(' AND ', $where);

        $row = $this->_table->fetchRow($where, $order);
        if($row->getPrimaryKey()) {
            return $row;
        }

        $where = array();
        $where[] = $db->quoteInto('subject < ?', $this->subject);
        $where = implode(' AND ', $where);

        $row = $this->_table->fetchRow($where, $order);
        if($row->getPrimaryKey()) {
            return $row;
        }

        return null;
    }

    public function _save()
    {
        if(!$this->integratedStudies) {
            $this->integratedStudies = 'no';
        }
        if(!$this->courseLetter) {
            $this->courseLetter = '';
        }
        return parent::_save();
    }

    public function _get($name)
    {
        switch($name) {
            case 'courseNumber':
                $courseNumber = parent::_get('courseNumber');
                if(Zend_Filter::isInt($courseNumber) && $courseNumber > 0) {
                    return str_pad($courseNumber, 3, '0', STR_PAD_LEFT);
                } else {
                    return $courseNumber;
                }
                break;
            default:
                return parent::_get($name);
        }
    }
}