/** * This file responds to ajax requests * */ if (!isset($_REQUEST['cmd'])) { echo '{"result" : 0, "message" : "Command Not Set"}'; exit; } $cmd = $_REQUEST['cmd']; switch ($cmd) { case 1: addCourse(); break; case 2: updateCourse(); break; case 3: deleteCourse(); break; case 4: viewAllCourses(); break; default: echo '{"result" : 0, "message" : "Unknown Command"}'; break; } /* * */ function addCourse()
$assignment = $assignments[$assignmentKey]; if ($assignment == null) { $assignment = new SampleAssignment(); $assignment->type = $_GET['assignment_type']; $assignment->number = $_GET['assignment_number']; } if ($_GET['file_type'] == 'assignment') { $assignment->assignmentFileName = $fileName; } else { if ($_GET['file_type'] == 'A') { $assignment->sampleFileNames[0] = $fileName; } else { if ($_GET['file_type'] == 'B') { $assignment->sampleFileNames[1] = $fileName; } else { if ($_GET['file_type'] == 'C') { $assignment->sampleFileNames[2] = $fileName; } } } } $assignments[$assignmentKey] = $assignment; $course->sampleAssignments = $assignments; updateCourse($course); // relative to prototype/course.php $msg = "../data/courses/" . $_GET['course'] . "/" . $fileName; } echo "{"; echo "error: '" . $error . "',\n"; echo "msg: '" . $msg . "'\n"; echo "}";
function addCourse($course, $progID) { global $PROGRAM; $path = $PROGRAM . $progID . '.json'; if (!file_exists($path)) { throw new Exception($path . ' does not exist!'); } $f = fopen($path, 'r+'); if ($f) { flock($f, LOCK_EX); $courseIDs = json_decode(fread($f, filesize($path)), true); if (!is_null($courseIDs)) { if (!in_array($course->courseID, $courseIDs)) { $courseIDs[] = $course->courseID; } else { // Samik: Assuming that any course added to a program will have it required/elective // otherwise you will have to "writeCourse" before returning. return 1; } fseek($f, 0); ftruncate($f, 0); fwrite($f, json_encode($courseIDs)); fclose($f); // if the course already exists, just set reqForProgram $existingCourse = getCourseForID($course->courseID); if (!is_null($existingCourse)) { $existingCourse->reqForProgram[$progID] = $course->reqForProgram[$progID]; // Samik: adding to update course, when course is added to new programs writeCourse($existingCourse); return 2; } // adds course file to courses/<courseID>/<courseID>.json updateCourse($course, true); } } else { throw new Exception('Could not open ' . $path . ' for writing'); } return 0; }