<?php
include_once '../session/admin-session.php';
include_once '../classes/class_admin.php';
include_once '../classes/validation.php';
include_once '../classes/common.php';
include_once 'read-configration.php';
ob_start();

$ObjDB = new class_db();
$log = new common();
$objadm = new class_admin();
$val = new validation();

$objadm->sem_num_greater = "27";
$SemListArr = $objadm->GetSemList();

$datetime = DATE('Y-m-d H:i:s');
$admin_roll = $_SESSION['user_sesroll'];

if ($_POST['newg']) {
    $this_grade = $val->ValidateString($_REQUEST['this_grade']);
    $this_roll = $val->ValidateString($_REQUEST['this_roll']);
    $this_sub = $val->ValidateString($_REQUEST['this_sub']);
    $this_file = $val->ValidateString($_REQUEST['this_file']);

    if ($this_grade == 'O' or $this_grade == 'A+' or $this_grade == 'A' or $this_grade == 'B+' or $this_grade == 'B' or $this_grade == 'C+' or $this_grade == 'C' or $this_grade == 'D' or $this_grade == 'P' or $this_grade == 'F' or $this_grade == 'I') {
        $query = "UPDATE $this_file SET grade = '$this_grade', rs_flag = 'C' WHERE stu_roll = '$this_roll' and sub_code = '$this_sub'";
        $ObjDB->sproc_name = $query;
        $ObjDB->ExecuteQuery();
        $GRADMSG = "Grade of $this_roll in $this_sub has been changed to $this_grade";

        $activity = "Grade of $this_roll in $this_sub changed to $this_grade";
        $query1 = "INSERT INTO actions (roll, activity, datetime) VALUES('" . $admin_roll . "','" . $activity . "','" . $datetime . "')";
        $ObjDB->sproc_name = $query1;
        $ObjDB->ExecuteQuery();
        
        $this_grade = '';
        $ths_roll = '';
        $this_sub = '';
        $this_file = '';
        $success_msg = $GRADMSG;
    } else {
        $GRADMSG = "Grade of $this_roll in $this_sub has not been changed.";
        $success_msg = $GRADMSG;
    }
}

if ($_POST['semester_btn']) {
    $num_sem = $val->ValidateString($_REQUEST['num_sem']);
    $sel_course_code = $val->ValidateString($_REQUEST['sel_course_code']);
    $subject = $val->ValidateString($sel_course_code);
    $subb = explode("$", $subject);

    $sub_list2 = $subb[0];
    $sub_code2 = $subb[1];
    $sub_title2 = $subb[2];
    $stu_year2 = $subb[3];

    $bref = explode(",", $stu_year2);
    $bnum = count($bref);

    $view_table = "";
    $view_table .= '<table class="table table-bordered align-middle">';

    $heading_val = "$sub_list2 : $sub_code2 : $sub_title2 : ($stu_year2)<BR>";
    $view_table .= "<tr><td>" . $heading_val . "</td></tr>";

    for ($bb = 1; $bb < 14; $bb++) {
        if (in_array($bb, $bref)) {
            $batch = 16 - round($bb / 2);
            if ($bb < 11) {
                $reg_file = 'isr_reg_b' . $batch . "_tbl";
            } else {
                $reg_file = 'isr_reg_r' . $batch . "_tbl";
            }

            $view_table .= "<tr><td>FILE: " . $reg_file . "</td></tr>";

            $query2 = "SELECT a.*, b.name FROM $reg_file a, isr_stu_main_tbl b where a.stu_roll = b.roll and a.sub_code = '$sub_code2' and a.sub_flag = 'Y' and a.b1_sem = '$num_sem' order by a.stu_roll";
            $ObjDB->sproc_name = $query2;
            $res_query2 = $ObjDB->SelectQuery();

            $found = mysqli_num_rows($res_query2);
            if ($found == 0) {
                $view_table .= "<tr><td>No Records Found!</td></tr>";
            } else {
                $view_table .= "<tr><td>";

                $view_table .= '<table class="table table-bordered align-middle">';

                $i2 = 1;
                while ($row2 = mysqli_fetch_array($res_query2)) {
                    $roll = $row2["stu_roll"];
                    $name = $row2["name"];
                    $grade = $row2["grade"];
                    $rs_flag = $row2["rs_flag"];

                    $view_table .= '<tr>
                            <td>' . $i2 . '</td>
                            <td>' . $roll . '</td>
                            <td>' . $name . '</td>
                            <td>' . $grade . '</td>';
                    if ($rs_flag == 'N') {
                        $view_table .= '<td>
                                <form method="post" action="">
                                    <input type="text" name="this_grade" maxlength="2" size="2"class="form-control input-sm">
                                    <input type="hidden" name="this_roll" value="' . $roll . '">
                                    <input type="hidden" name="this_sub" value="' . $sub_code2 . '">
                                    <input type="hidden" name="this_file" value="' . $reg_file . '">
                                    <input type="hidden" name="finder" value="Submit">
                                    <input type="hidden" name="subject" value="' . $subject . '">
                                    <input type="submit" name="newg" value="Save"class="btn btn-danger btn-sm">
                                </form>
                            </td>';
                    } else {
                        $view_table .= '<td>&nbsp;</td>';
                    }
                    $view_table .= '</tr>';

                    $i2++;
                }


                $view_table .= '</table>';
            }
        }
    }



    $view_table .= '</table>';
}
?>
<div class="container-fluid">
    <div class="row">
        <div class="col-xl-12 col-xxl-12 col-sm-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title">Semester Grade Change</h5>
                </div>
                <div class="text-success"><?php echo $success_msg; ?></div>
                <div class="card-body">
                    <form method="POST" action="">
                        <div class="row">
                            <div class="col-lg-5">
                                <label for="num_sem">Semester</label>
                                <select id="num_sem" name="num_sem" required="" onchange="FunGetCourseBySem(this.value)">
                                    <option value="">-- - - Select - - - --</option>
                                    <?php while ($rsem = mysqli_fetch_array($SemListArr)) {
                                        ?>
                                        <option value="<?php echo $rsem['sem_num']; ?>"><?php echo $rsem['new_name']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                            <div class="col-lg-5">
                                <label for="sel_course_code">Subject List</label>
                                <select name="sel_course_code" id="sel_course_code" required="">
                                    <option value="">Select Subject</option>
                                </select>
                            </div>
                            <div class="col-lg-2">
                                <input type="submit" name="semester_btn" id="semester_btn" value="Submit" class="btn btn-danger"/>
                            </div>
                        </div>
                    </form>

                    <div class="row">
                        <div class="col-lg-12">
                            <div class="table-responsive">
                                <?php echo $view_table; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
$pagemaincontent = ob_get_contents();
ob_clean();
$PAGENAME = 'control-bulk-change';
$HTMLTITLE = '';
$HTMLDESCRIPTION = '';
$HTMLKEYWORDS = '';
include("admin-master.php");
?>
<script type="text/javascript">
    function FunGetCourseBySem(SemVal) {
        $.ajax({
            type: "POST",
            url: "action.php",
            data: "action=GetCourseBySem&SemVal=" + SemVal,
            beforeSend: function () {

            },
            success: function (data) {

                $("#sel_course_code").append(data);
                // Refresh bootstrap-select after modifying options
                $('#sel_course_code').selectpicker('refresh');
            }
        });
    }
</script>