<?php

require_once "../includes/initialize.php";
global $session;
if (!$session->is_logged_in()) {
    redirect_to("index.php");
}
if ($_POST['oper'] == 'add') {
    $curriculum = new Curriculum();
    $curriculum->curriculum = $_POST['curriculum'];
    $curriculum->create();
} else {
    if ($_POST['oper'] == 'edit') {
        $curriculum = Curriculum::get_by_id($_POST['id']);
        $curriculum->curriculum = $_POST['curriculum'];
        $curriculum->update();
    } else {
        if ($_POST['oper'] == 'del') {
            Curriculum::get_by_id($_POST['id'])->delete();
        }
    }
}
}
header("Content-type: text/xml;charset=utf-8");
$s = "<?xml version='1.0' encoding='utf-8'?>";
$s .= "<rows>";
$s .= "<page>" . $page . "</page>";
$s .= "<total>" . $total_pages . "</total>";
$s .= "<records>" . $count . "</records>";
foreach ($subjects as $subjectobject) {
    $subject = $subjectobject;
    if ($isteacher) {
        $subject = Subject::get_by_id($subjectobject->subject_id);
    }
    $s .= "<row id='" . $subject->id . "'>";
    $s .= "<cell></cell>";
    $s .= "<cell>" . $subject->id . "</cell>";
    $s .= "<cell>" . $subject->code . "</cell>";
    $s .= "<cell>" . $subject->description . "</cell>";
    $s .= "<cell>" . $subject->units . "</cell>";
    $subject_code = $subject->prereq_subject_id == 0 ? "NONE" : Subject::get_by_id($subject->prereq_subject_id)->code;
    $s .= "<cell>" . $subject_code . "</cell>";
    $s .= "<cell>" . Course::get_by_id($subject->course_id)->code . "</cell>";
    $s .= "<cell>" . $subject->year . "</cell>";
    $s .= "<cell>" . $subject->semester . "</cell>";
    $failed_grades = Grade::get_by_sql("SELECT * FROM " . T_STUDENT_GRADES . " WHERE " . C_GRADE_SUBJECT_ID . "=" . $subject->id . " AND grade < 75");
    $s .= "<cell>" . count($failed_grades) . "</cell>";
    $s .= "<cell>" . Curriculum::get_by_id($subject->curriculum_id)->curriculum . "</cell>";
    $s .= "<cell>" . $subject->curriculum_id . "</cell>";
    $s .= "</row>";
}
$s .= "</rows>";
echo $s;
    $start = 0;
}
if (!$sidx) {
    $sidx = 1;
}
$courses = Course::get_by_sql("SELECT * FROM " . T_COURSES . " ORDER BY {$sidx} {$sord} LIMIT {$start} , {$limit}");
if (isset($_GET['searchString']) && isset($_GET['searchField'])) {
    $searchString = $_GET['searchString'];
    $searchField = $_GET['searchField'];
    $courses = Course::get_by_sql("SELECT * FROM " . T_COURSES . " WHERE " . $searchField . " LIKE '%" . $searchString . "%' ORDER BY {$sidx} {$sord} LIMIT {$start} , {$limit}");
} else {
    $courses = Course::get_by_sql("SELECT * FROM " . T_COURSES . " ORDER BY {$sidx} {$sord} LIMIT {$start} , {$limit}");
}
header("Content-type: text/xml;charset=utf-8");
$s = "<?xml version='1.0' encoding='utf-8'?>";
$s .= "<rows>";
$s .= "<page>" . $page . "</page>";
$s .= "<total>" . $total_pages . "</total>";
$s .= "<records>" . $count . "</records>";
foreach ($courses as $course) {
    $s .= "<row id='" . $course->id . "'>";
    $s .= "<cell></cell>";
    $s .= "<cell>" . $course->id . "</cell>";
    $s .= "<cell>" . $course->code . "</cell>";
    $s .= "<cell>" . $course->description . "</cell>";
    $s .= "<cell>" . Curriculum::get_by_id($course->curriculum_id)->curriculum . "</cell>";
    $s .= "<cell>" . $course->curriculum_id . "</cell>";
    $s .= "</row>";
}
$s .= "</rows>";
echo $s;
				<td>CODE</td>
				<td>DESCRIPTION</td>
				<td>UNITS</td>
				<td>PREREQUISITE</td>
				<td>COURSE</td>
				<td>YEAR</td>
				<td>SEMESTER</td>
				<td>CURRICULUM</td>
			</tr>
		</thead>
		<?php 
$subjects = Subject::getByCurriculum($curriculumid);
if (count($subjects) > 0) {
    foreach ($subjects as $subject) {
        $course = Course::get_by_id($subject->course_id);
        $curriculum = Curriculum::get_by_id($subject->curriculum_id);
        if ($subject->prereq_subject_id != "") {
            $prerequisitesubject = Subject::get_by_id($subject->prereq_subject_id);
            if (!$prerequisitesubject) {
                $prerequisitesubject = new Subject();
                $prerequisitesubject->code = "NONE";
            }
        } else {
            $prerequisitesubject = new Subject();
            $prerequisitesubject->code = "NONE";
        }
        if ($subject->semester == 1) {
            $subject->semester = "First";
        } else {
            if ($subject->semester == 2) {
                $subject->semester = "Second";