$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
 $objReader = PHPExcel_IOFactory::createReader($inputFileType);
 $objPHPExcel = $objReader->load($inputFileName);
 $objPHPExcel->setActiveSheetIndex(0);
 $objWorksheet = $objPHPExcel->getActiveSheet();
 $highestRow = $objWorksheet->getHighestRow();
 // COLUMNS
 $SUBJECT = 0;
 $STUDENT_ID = 1;
 $NAME = 2;
 $GRADE = 3;
 //SUBJECT
 $subject_code = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, 2)->getValue();
 $subject = Subject::get_by_sql("SELECT * FROM tbl_subjects WHERE code = '" . $subject_code . "'")[0];
 if ($subject) {
     $subject_id = Subject::get_by_sql("SELECT * FROM tbl_subjects WHERE code = '" . $subject_code . "'")[0]->id;
     for ($row = 2; $row <= $highestRow; $row++) {
         $student_id = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($STUDENT_ID, $row)->getValue();
         $grade = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($GRADE, $row)->getValue();
         $student = Student::get_by_id($student_id);
         if ($student) {
             $grades = Grade::get_by_sql("SELECT * FROM tbl_student_grades WHERE subject_id = '" . $subject_id . "' AND student_id = " . $student_id)[0];
             if ($grades) {
                 $grades->grade = $grade;
                 $grades->update();
             } else {
                 $newgrades = new Grade();
                 $newgrades->grade = $grade;
                 $newgrades->student_id = $student_id;
                 $newgrades->subject_id = $subject_id;
                 $newgrades->create();
}
if (!$sidx) {
    $sidx = 1;
}
$first = $_GET['first'];
if ($first == "true") {
    $future_sem = 1;
} else {
    if ($first == "2nd") {
        $future_sem = 2;
    } else {
        $future_sem = $student->semester == 1 ? 2 : 1;
    }
}
//$student_subjects = Grade::get_by_sql("SELECT * FROM " . T_STUDENT_GRADES . " WHERE ".C_GRADE_STUDENT_ID."=".$student->id);
$future_subjects = Subject::get_by_sql("SELECT * FROM " . T_SUBJECTS . " WHERE " . C_SUBJECT_COURSE_ID . "=" . $student->course_id . " AND " . C_SUBJECT_SEMESTER . " = " . $future_sem . " ORDER BY {$sidx} {$sord} LIMIT {$start} , {$limit}");
// $student_current_subjects = array();
// foreach($student_subjects as $student_subject)
// {
//     array_push($student_current_subjects, Subject::get_by_id($student_subject->subject_id));
// }
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>";
if (count($future_subjects) > 0) {
    foreach ($future_subjects as $final_subject) {
        $s .= "<row id='" . $final_subject->id . "'>";
        $s .= "<cell>" . $final_subject->id . "</cell>";
require_once "../includes/initialize.php";
global $session;
if (!$session->is_logged_in()) {
    redirect_to("index.php");
}
if ($_POST['oper'] == 'del') {
    Subject::get_by_id($_POST['id'])->delete();
}
if ($_POST['prereq_subject_code'] == "NOTHING" || $_POST['prereq_subject_code'] == "") {
    $prereq_subject_id = 0;
} else {
    $prereq_subject_id = Subject::get_by_sql("SELECT * FROM " . T_SUBJECTS . " WHERE " . C_SUBJECT_CODE . "='" . trim($_POST['prereq_subject_code']) . "'")[0]->id;
}
$course_id = Course::get_by_sql("SELECT * FROM " . T_COURSES . " WHERE " . C_COURSE_CODE . "='" . trim($_POST['course_code']) . "'")[0]->id;
$rows = Subject::get_by_sql("SELECT * FROM " . T_SUBJECTS . " WHERE " . C_SUBJECT_CODE . "='" . $_POST['code'] . "' AND " . C_SUBJECT_COURSE_ID . "=" . $course_id);
if ($_POST['oper'] == 'add') {
    if (count($rows) > 0) {
        die("exists");
    }
    $subject = new Subject();
    $subject->code = $_POST['code'];
    $subject->description = $_POST['description'];
    $subject->units = $_POST['units'];
    $subject->prereq_subject_id = $prereq_subject_id;
    $subject->course_id = $course_id;
    $subject->year = $_POST['year'];
    $subject->semester = $_POST['semester'];
    $subject->curriculum_id = $_POST['curriculum_id'];
    $subject->create();
} else {
if ($page > $total_pages) {
    $page = $total_pages;
}
$start = $limit * $page - $limit;
if ($start < 0) {
    $start = 0;
}
if (!$sidx) {
    $sidx = 1;
}
if (isset($_GET['searchString']) && isset($_GET['searchField'])) {
    $searchString = $_GET['searchString'];
    $searchField = $_GET['searchField'];
    $subjects = Subject::get_by_sql("SELECT * FROM " . T_SUBJECTS . " WHERE " . $searchField . " LIKE '%" . $searchString . "%' ORDER BY {$sidx} {$sord} LIMIT {$start} , {$limit}");
} else {
    $subjects = Subject::get_by_sql("SELECT * FROM " . T_SUBJECTS . " ORDER BY {$sidx} {$sord} LIMIT {$start} , {$limit}");
}
if ($isteacher) {
    $subjects = TeacherSubject::get_by_teacher_id($session->user_id);
}
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);
    }