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;
}
$TeacherSubjects = TeacherSubject::get_by_sql("SELECT * FROM " . T_TEACHER_SUBJECTS . " 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 ($TeacherSubjects as $TeacherSubject) {
    $subject = Subject::get_by_id($TeacherSubject->subject_id);
    $teacher = User::get_by_id($TeacherSubject->teacher_id);
    $s .= "<row id='" . $TeacherSubject->id . "'>";
    $s .= "<cell></cell>";
    $s .= "<cell>" . $TeacherSubject->id . "</cell>";
    $s .= "<cell>" . $teacher->id . "</cell>";
    $s .= "<cell>" . $teacher->name . "</cell>";
    $s .= "<cell>" . $subject->id . "</cell>";
require_once "../includes/initialize.php";
global $session;
if (!$session->is_logged_in()) {
    redirect_to("index.php");
}
$id = $_POST['id'];
$teacher_id = $_POST['teacher_id'];
$subject_id = $_POST['subject_id'];
$operator = $_POST['oper'];
if ($operator == 'add') {
    $object = new TeacherSubject();
    $object->teacher_id = $teacher_id;
    $object->subject_id = $subject_id;
    if (Subject::get_by_id($object->subject_id) != null && User::get_by_id($object->teacher_id) != null) {
        $object->create();
    }
} else {
    if ($operator == 'edit') {
        $object = TeacherSubject::get_by_id($id);
        $object->teacher_id = $teacher_id;
        $object->subject_id = $subject_id;
        if (Subject::get_by_id($object->subject_id) != null && User::get_by_id($object->teacher_id) != null) {
            $object->update();
        }
    } else {
        if ($operator == 'del') {
            TeacherSubject::get_by_id($id)->delete();
        }
    }
}
$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);
    }
    $s .= "<row id='" . $subject->id . "'>";
    $s .= "<cell></cell>";
    $s .= "<cell>" . $subject->id . "</cell>";