<?php

require_once "../includes/initialize.php";
function has_prerequisite($id, $subjects)
{
    foreach ($subjects as $subject) {
        if ($id == $subject->id) {
            return true;
        }
    }
    return false;
}
$student_id = $_GET['student_id'];
$subject_ids = $_POST['subject_ids'];
$student_grades = Grade::get_by_sql("SELECT * FROM " . T_STUDENT_GRADES . " WHERE " . C_GRADE_STUDENT_ID . "=" . $student_id);
$student_subjects = array();
foreach ($student_grades as $student_subject) {
    array_push($student_subjects, Subject::get_by_id($student_subject->subject_id));
}
$warnings = "";
foreach ($subject_ids as $id) {
    $thestudent = Student::get_by_id($student_id);
    $thestudent->semester = Subject::get_by_id($id)->semester;
    $thestudent->update();
    if (Grade::exists($id, $student_id)) {
        $newgrade = Grade::get_by_subj_stud($id, $student_id);
        $newgrade->delete();
    }
    $newgrade = new Grade();
    $newgrade->subject_id = $id;
    $newgrade->student_id = $student_id;
 // 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();
             }
         }
     }
 } else {
     echo "*********SUBJECT CODE NOT FOUND*********";
 }
<?php

require_once "../includes/initialize.php";
global $session;
if (!$session->is_logged_in()) {
    redirect_to("index.php");
}
$id = $_POST['id'];
$subject_id = $_POST['subject_id'];
$student_id = $_POST['student_id'];
$grades = $_POST['grade'];
$operator = $_POST['oper'];
if ($operator == 'add') {
    $rows = Grade::get_by_sql("SELECT * FROM " . T_STUDENT_GRADES . " WHERE " . C_GRADE_SUBJECT_ID . "=" . $subject_id . " AND " . C_GRADE_STUDENT_ID . "=" . $student_id);
    if (count($rows) > 0) {
        die("exists");
    }
    $grade = new Grade();
    $grade->grade = $grades;
    $grade->subject_id = $subject_id;
    $grade->student_id = $student_id;
    $grade->create();
} else {
    if ($operator == 'edit') {
        $grade = Grade::get_by_id($id);
        $grade->grade = $grades;
        $grade->subject_id = $subject_id;
        $grade->student_id = $student_id;
        $grade->update();
    } else {
        if ($operator == 'del') {
if ($count > 0 && $limit > 0) {
    $total_pages = ceil($count / $limit);
} else {
    $total_pages = 0;
}
if ($page > $total_pages) {
    $page = $total_pages;
}
$start = $limit * $page - $limit;
if ($start < 0) {
    $start = 0;
}
if (!$sidx) {
    $sidx = 1;
}
$grades = Grade::get_by_sql("SELECT * FROM " . T_STUDENT_GRADES . $where . " 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 ($grades as $grade) {
    $student = Student::get_by_id($grade->student_id);
    $s .= "<row id='" . $grade->id . "'>";
    $s .= "<cell></cell>";
    $s .= "<cell>" . $grade->id . "</cell>";
    $s .= "<cell>" . $grade->grade . "</cell>";
    $s .= "<cell>" . $grade->subject_id . "</cell>";
    $s .= "<cell>" . $student->id . "</cell>";
    $s .= "<cell>" . $student->get_full_name() . "</cell>";
}
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;
if ($count > 0 && $limit > 0) {
    $total_pages = ceil($count / $limit);
} else {
    $total_pages = 0;
}
if ($page > $total_pages) {
    $page = $total_pages;
}
$start = $limit * $page - $limit;
if ($start < 0) {
    $start = 0;
}
if (!$sidx) {
    $sidx = 1;
}
$grades = Grade::get_by_sql("SELECT * FROM " . T_STUDENT_GRADES . " WHERE " . C_GRADE_STUDENT_ID . " = " . $student_id . " 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 ($grades as $grade) {
    if (Subject::get_by_id($grade->subject_id)->course_id != $student->course_id) {
        continue;
    }
    if (Subject::get_by_id($grade->subject_id)->semester != $student->semester) {
        //continue;
    }
    $s .= "<row id='" . $grade->id . "'>";
    $s .= "<cell>" . $grade->id . "</cell>";