-
Tim Steiner authored
Also contains new code for inheritting Database Row Models.
Tim Steiner authoredAlso contains new code for inheritting Database Row Models.
CourseadminController.php 19.20 KiB
<?php
class CourseAdminController extends Nmc_Controller_Action
{
public function __construct()
{
$this->_registerPlugin(new Nmc_Controller_Action_Plugin_Authorize());
}
public function authorize()
{
if(!Nmc_Registry_Session::getInstance()->userName) {
return false;
}
return true;
}
public function indexAction()
{
$in = new Zend_Filter_Input($this->_action->getParams());
$uriParams = $this->_action->getParams();
$uriParams = $uriParams['URI_PARAMS'];
$out = new Nmc_View_Unl();
if(Zend_Filter::isInt($uriParams[0])) {
$course = CourseGenerations::getInstance()->find($uriParams[0]);
$crosslisting = $course->crosslistings[0];
} else {
$crosslisting = CourseCrosslistings::fetchBySubjectNumberLetter($uriParams[0], $uriParams[1], $uriParams[2]);
}
if(!$crosslisting) {
$course = CourseGenerations::getInstance()->fetchNew();
} else {
$course = $crosslisting->getParentCourse();
if($nextCourse = $crosslisting->getNextAlphabetically()) {
$nextCourse = $nextCourse->getParentCourse();
$nextCourseLink = '/' . $nextCourse->subject . '/' . $nextCourse->courseNumber . '/' . $nextCourse->courseLetter;
if($nextCourse->subject != $course->subject || $nextCourse->getPrimaryKey() == $course->getPrimaryKey()) {
foreach($nextCourse->crosslistings as $crosslisting) {
if($crosslisting->subject == $course->subject) {
$nextCourseLink = '/' . $crosslisting->subject . '/' . $crosslisting->courseNumber . '/' . $crosslisting->courseLetter;
break;
}
}
}
}
if($prevCourse = $crosslisting->getPreviousAlphabetically()) {
$prevCourse = $prevCourse->getParentCourse();
$prevCourseLink = '/' . $prevCourse->subject . '/' . $prevCourse->courseNumber . '/' . $prevCourse->courseLetter;
if($prevCourse->subject != $course->subject || $prevCourse->getPrimaryKey() == $course->getPrimaryKey()) {
foreach($prevCourse->crosslistings as $crosslisting) {
if($crosslisting->subject == $course->subject) {
$prevCourseLink = '/' . $crosslisting->subject . '/' . $crosslisting->courseNumber . '/' . $crosslisting->courseLetter;
break;
}
}
}
}
$out->assign('prevCourseLink', $prevCourseLink);
$out->assign('nextCourseLink', $nextCourseLink);
}
$creditsSingleValues = array();
foreach($course->credits as $credit) {
if($credit->type == 1) {
$creditsSingleValues[] = $credit->hours;
} else if($credit->type == 2) {
$out->creditsRangeMin = $credit->hours;
} else if($credit->type == 3) {
$out->creditsRangeMax = $credit->hours;
} else if($credit->type == 4) {
$out->creditsMaxPerSemester = $credit->hours;
} else if($credit->type == 5) {
$out->creditsMaxPerDegree = $credit->hours;
}
}
$out->creditsSingleValues = implode(' ', $creditsSingleValues);
$out->assign('page', 'edit_course');
$out->assign('course', $course);
$out->assign('uriParams', $uriParams);
echo $out->render();
}
public function updateCourseAction() {
$uriParams = $this->_action->getParams();
$uriParams = $uriParams['URI_PARAMS'];
$course = CourseGenerations::getInstance()->find($_POST['courseId']);
if(!$course) {
$course = CourseGenerations::getInstance()->fetchNew();
$course->type = 'official';
}
foreach($_POST as $key => $val) {
if(!is_array($val) && $key != 'courseId' /*&& $val != ''*/) {
//echo '$course->' . $key . ' = ' . $val . "<br />\n";
$course->$key = $val;
}
}
foreach($_POST as $key => $val) {
if($key == 'credits') {
$credits = $course->credits;
foreach($credits as $key => $credit) {
unset($credits[$key]);
}
foreach($val as $type => $hours) {
if($type == 1) {
foreach(explode(' ', $hours) as $singeValue) {
if(Zend_Filter::getDigits($hours) != '') {
$newAttribute = CourseCredits::getInstance()->fetchNew();
$newAttribute->type = $type;
$newAttribute->hours = $singeValue;
$credits[] = $newAttribute;
}
}
continue;
}
if(Zend_Filter::getDigits($hours) != '') {
$newAttribute = CourseCredits::getInstance()->fetchNew();
$newAttribute->type = $type;
$newAttribute->hours = $hours;
$credits[] = $newAttribute;
}
}
} else if(in_array($key, array('termsOffered','deliveryMethods','campuses'))) {
$course->$key = $val;
//echo '$course->' . $key . ' = '; print_r($val); echo "<br />\n";
} else if($key == 'gradTieIn') {
if(!$course->gradTieIn instanceof CourseGradTieIn) {
$course->gradTieIn = CourseGradTieIns::getInstance()->fetchNew();
}
foreach($val as $key2 => $val2) {
$course->gradTieIn->$key2 = $val2;
//echo '$course->gradTieIn->' . $key2 . ' = ' . $val2 . "\n";
}
} else if(is_array($val)) {
$array = $course->$key;
foreach($array as $key2 => $row) {
unset($array[$key2]);
}
foreach($val as $key2 => $val2) {
if($val2['delete'] == 'yes') {
continue;
}
$attributeTable = call_user_func(array('Course' . $key, 'getInstance'));
$currentAttribute = $attributeTable->fetchNew();
unset($val2['delete']);
//loop over each attribute and set
foreach($val2 as $key3 => $val3) {
if($val3 == '') {
$val3 = null;
}
//echo '$currentAttribute->' . $key3 . ' = ' . $val3 . "<br />\n";
$currentAttribute->$key3 = $val3;
}
$array[] = $currentAttribute;
//save changes
}
//print_r($array);
}
}
$course->save();
$out = new Nmc_View();
$out->refresh = '/courseadmin/index/' . $course->getPrimaryKey();
echo $out->render();
}
public function deleteCourseAction()
{
$in = $this->_action->getParams();
$courseId = Zend_Filter::getInt($in['URI_PARAMS'][0]);
if($courseId > 0) {
$course = CourseGenerations::getInstance()->find($courseId);
$course->delete();
}
$out = new Nmc_View();
$out->refresh = '/courseadmin/index/' . $courseId;
echo $out->render();
}
public function otherAction()
{
echo "DO NOT RUN!!!";
return;
header('Content-type: text/plain');
$localDB = Zend::registry('db');
$remoteDB = Zend_Db::factory('pdoMysql',
array('username' => 'creq',
'password' => 'UrBJtMyvbEJ8KYzp',
'dbname' => 'ugrad_curriculum_request'));
$sql = 'SELECT * FROM creq_current_courses';
$data = $remoteDB->fetchAll($sql);
foreach($data as $row)
{
$course = array();
$course['activities'] = array();
$course['terms_offered'] = array();
$course['delivery_methods'] = array();
$course['credits'] = array();
$course['campuses'] = array();
$sql = 'SELECT id FROM creq_colleges WHERE description=?';
$course['college_id'] = $localDB->fetchOne($localDB->quoteInto($sql, $row['college']));
$act = explode(',', trim($row['activities']));
$act2 = explode(',', trim($row['course_act']));
if($act[0] != '') {
foreach($act as $a) {
$course['activities'][$a] = '';
}
}
/*
if($act2[0] != '') {
foreach($act2 as $a)
{
preg_match('/([a-zA-Z]*)[^0-9]*([0-9]*)/', $a, $matches);
$key = strtoupper($matches[1]);
if($key != '') {
$course['activities'][$key] = $matches[2];
}
}
}
*/
$terms = explode(',', trim($row['term_taught']));
if($terms[0] != '') {
$course['terms_offered'] = $terms;
}
if($row['df_removal'] != '') {
$course['df_removal'] = $row['df_removal'];
}
if($row['delivery_method'] != '') {
$course['delivery_methods'] = explode(',', $row['delivery_method']);
}
if($row['course_credit'] != '') {
preg_match('/([0-9]*)(-[0-9]*)?[^m]*(max [0-9]*)?/', $row['course_credit'], $matches);
$course['credits'] = array();
if($matches[1] != '' && $matches[2] == '') {
$course['credits']['single'] = $matches[1];
} else if($matches[1] != '' && $matches[2] != '') {
$course['credits']['range_min'] = $matches[1];
}
if($matches[2] != '') {
$course['credits']['range_max'] = substr($matches[2], 1);
}
if($matches[3] != '') {
$course['credits']['max'] = substr($matches[3], 3);
}
}
if($row['course_title'] != '') {
$course['title'] = $row['course_title'];
}
if($row['course_num'] != '') {
$course['course_num'] = $row['course_num'];
preg_match('/([a-zA-Z]*)[^0-9a-zA-Z]*(([0-9]*)([a-zA-Z]*))(\/([0-9]*)([a-zA-Z]*))?/', $row['course_num'], $matches);
if($matches[1] != '') {
$course['subject'] = $matches[1];
}
if($matches[3] != '') {
$course['number'] = $matches[3];
}
if($matches[4] != '') {
$course['letter'] = $matches[4];
}
if($matches[6] != '') {
$course['alt_num'] = $matches[6];
}
if($matches[7] != '') {
$course['alt_letter'] = $matches[7];
}
}
if($row['course_prereq'] != '') {
$course['prerequisite'] = $row['course_prereq'];
}
if($row['course_note'] != '') {
$course['notes'] = $row['course_note'];
}
if($row['course_desc'] != '') {
$course['description'] = $row['course_desc'];
}
if($row['course_inst'] != '') {
$course['campuses'] = explode(',', $row['course_inst']);
}
if($row['parent_xlist'] != '') {
$course['parent_course'] = array();
preg_match('/([a-zA-Z]*)([0-9]*)([a-zA-Z]*)/', $row['parent_xlist'], $matches);
if($matches[1] != '') {
$course['parent_course']['subject'] = $matches[1];
}
if($matches[2] != '') {
$course['parent_course']['number'] = $matches[2];
}
if($matches[3] != '') {
$course['parent_course']['letter'] = $matches[3];
}
if( $course['parent_course']['subject'] == $course['subject']
&& $course['parent_course']['number'] == $course['number']
&& $course['parent_course']['letter'] == $course['letter']) {
$parent_courses[] = $course;
} else {
$crosslisted_courses[] = $course;
}
} else {
$parent_courses[] = $course;
}
}
$localDB->beginTransaction();
foreach($parent_courses as $course) {
try {
try {
$sql = 'INSERT INTO creq_subjects(short_name) VALUES(?)';
$localDB->query($sql, array($course['subject']));
} catch(Exception $e) {
//do nothing
}
$sql = 'INSERT INTO creq_courses() VALUES()';
$localDB->query($sql);
$courseId = $localDB->lastInsertId();
$sql = 'INSERT INTO creq_course_generations(course, title, subject, course_number, course_letter, prerequisite, notes, description) '
. 'VALUES(?, ?, ?, ?, ?, ?, ?, ?)';
$localDB->query($sql, array($courseId,
$course['title'],
$course['subject'],
$course['number'],
$course['letter'],
$course['prerequisite'],
$course['notes'],
$course['description']));
$generationId = $localDB->lastInsertId();
$sql = 'UPDATE creq_courses SET current_generation=? WHERE id=?';
$localDB->query($sql, array($generationId, $courseId));
if($course['alt_num'] != '') {
$sql = 'INSERT INTO creq_course_crosslistings(parent_course, subject, course_number, course_letter) '
. 'VALUES(?, ?, ?, ?)';
$localDB->query($sql, array($generationId,
$course['subject'],
$course['alt_num'],
$course['alt_letter']));
}
foreach($course['activities'] as $activity => $hours) {
$sql = 'INSERT INTO creq_course_activities(course_generation, type, credits) '
. 'VALUES(?, ?, ?)';
$localDB->query($sql, array($generationId,
strtolower($activity),
$hours));
}
foreach($course['campuses'] as $campus) {
$sql = 'INSERT INTO creq_course_campuses(course_generation, campus) '
. 'VALUES(?, ?)';
$localDB->query($sql, array($generationId,
$campus));
}
foreach($course['credits'] as $type => $hours) {
if($type == 'single') {
$type = 1;
} else if($type == 'range_min') {
$type = 2;
} else if($type == 'range_max') {
$type = 3;
} else if($type == 'max') {
$type = 4;
}
$sql = 'INSERT INTO creq_course_credits(course_generation, type, hours) '
. 'VALUES(?, ?, ?)';
$localDB->query($sql, array($generationId,
$type,
$hours));
}
foreach($course['delivery_methods'] as $method) {
if($method == 'classroom') {
$method = 1;
} else if($method == 'web') {
$method = 2;
}
$sql = 'INSERT INTO creq_course_delivery_methods(course_generation, delivery_method) '
. 'VALUES(?, ?)';
$localDB->query($sql, array($generationId,
$method));
}
foreach($course['terms_offered'] as $term) {
if($term == 'I') {
$term = 1;
} else if($term == 'II') {
$term = 2;
} else if($term == 'III') {
$term = 3;
}
$sql = 'INSERT INTO creq_course_terms_offered(course_generation, term) '
. 'VALUES(?, ?)';
$localDB->query($sql, array($generationId,
$term));
}
} catch(Exception $e) {
echo $i++ . ' ' . $e->getMessage() . "\n";
//do nothing
}
}
foreach($crosslisted_courses as $course) {
try {
try {
$sql = 'INSERT INTO creq_subjects(short_name) VALUES(?)';
$localDB->query($sql, array($course['subject']));
} catch(Exception $e) {
//do nothing
}
if(isset($course['parent_course'])) {
$sql = 'SELECT b.id FROM creq_courses AS a JOIN creq_course_generations AS b ON(a.current_generation = b.id) '
. 'WHERE subject=? AND course_number=? AND (course_letter=? OR ISNULL(course_letter))';
$parent_course_id = $localDB->fetchOne($sql, array($course['parent_course']['subject'],
$course['parent_course']['number'],
$course['parent_course']['letter']));
$sql = 'INSERT INTO creq_course_crosslistings(parent_course, subject, course_number, course_letter) '
. 'VALUES(?, ?, ?, ?)';
$localDB->query($sql, array($parent_course_id, $course['subject'], $course['number'], $course['letter']));
if($course['alt_num'] != '') {
$localDB->query($sql, array($parent_course_id, $course['subject'], $course['alt_num'], $course['alt_letter']));
}
}
} catch(Exception $e) {
echo $i++ . ' ' . $e->getMessage() . "\n";
//do nothing
}
}
//$localDB->rollBack();
$localDB->commit();
}
}