<?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();

$semb1 = $_REQUEST['sno'];
if (empty($semb1)) {
    $semb1 = $current_sem_num;
}
$sub_list = 'c' . $semb1;

$query = "select sub_code, round(avg(percent)) as aaa from isr_attn_final_tbl where percent > 0 group by sub_code";
$ObjDB->sproc_name = $query;
$res_query = $ObjDB->SelectQuery();
while ($row = mysqli_fetch_array($res_query)) {
    $sub_code = $row['sub_code'];
    $aaa = $row['aaa'];
    if ($aaa > 100)
        $aaa = 100;
    $av_atn[$sub_code] = $aaa;
}
?>
<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">Faculty Semester Feedback</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-lg-2"></div>
                        <div class="col-lg-8">                        
                            <form action="" method="POST">
                                <div class="mb-3" style="padding-left: 0px;">
                                    <select class="form-select" id="semester_number" name="semester_number" onchange="FunFacultyFeedback(this.value);">
                                        <option value="">--- Select Semester ---</option>
<?php
for ($i = $current_sem_num; $i >= $current_sem_num - 5; $i--) {
    $selected = "";
    if ($semb1 == $i) {
        $selected = "SELECTED";
    }
    ?>
                                            <option value="<?php echo $i; ?>" <?php echo $selected; ?>><?php echo $i; ?></option>
                                        <?php } ?>

                                    </select>

                                </div>

                            </form>

                        </div>

                        <div class="col-lg-2"></div><br/><br/>
                        <!--<A HREF="feedback_gen4_download_summary.php?sno=<?php echo $semb1; ?>"><FONT COLOR="blue"><H4>Download Feedback summary for sem = <?php echo $semb1; ?></H4></FONT></A> -->

                    </div>

                    <div class="table-responsive">
                        <table class="table table-bordered align-middle" id="table_feedback_faculty_id">
                            <thead class="table">
                                <tr>
                                    <th>&nbsp;</th>
                                    <th>Code</th>
                                    <th>Course Title</th>
                                    <th>Reg</th>
                                    <th>Instructor</th>
                                    <th>Res</th>
                                    <th>Abs</th>
                                    <th>Score</th>
                                    <th>Atn(%)</th>
                                    <th>Score</th>

                                </tr>
                            </thead>
                            <tbody>
<?php

$sub_sr = 1;

$query1 = "SELECT concat(sub_code,'*',fac_code) as fc, count(*) as ccc, AVG(score) as avgr FROM isr_answers_gen4_tbl where b1_sem = '$semb1' and abstain = '' and sub_code <> '' group by  fc";
$ObjDB->sproc_name = $query1;
$res_query1 = $ObjDB->SelectQuery();
$num_rows = mysqli_num_rows($res_query1);
$total_rows = $num_rows;
if ($num_rows > 0) {
    while ($row1 = mysqli_fetch_array($res_query1)) {
        $fc = $row1["fc"];
        $ccc = $row1["ccc"];
        $score = round($row1['avgr'], 2);
        $ffcc = explode("*", $fc);
        $sub_code = $ffcc[0];
        $fac_code = $ffcc[1];
        $dis = substr($sub_code, 0, 3);

        $query7 = "SELECT * FROM isr_answers_gen4_tbl where concat(sub_code,'*',fac_code) = '$fc'  and b1_sem = '$semb1' and abstain = 'Y'";
        $ObjDB->sproc_name = $query7;
        $res_query7 = $ObjDB->SelectQuery();
        $abstain = mysqli_num_rows($res_query7);

        /// ---------- Count students Registered  Start ------------
        $sub_regg = 0;

        
        $qbatch = $objadm->MasterActiveBatchFlagSetting();
        while ($rowb = mysqli_fetch_array($qbatch)) {
            if($rowb['flag_active'] == 'Y' || $rowb['flag_active'] == 'Z'){
            $reg_file = 'isr_reg_' . $rowb['flag_user']."_tbl";
            $qreg = "SELECT count(*) as ccc FROM $reg_file where sub_code = '$sub_code' and sub_flag = 'Y' and b1_sem = '$semb1'";
            $ObjDB->sproc_name = $qreg;
            $res_query = $ObjDB->SelectQuery();
            $rowr = mysqli_fetch_array($qreg);
            $sub_regg += $rowr["ccc"];
            }
        }
        if ($sub_regg == 0) {
            $qreg = "SELECT count(*) as ccc FROM ism_gsam_course_student_tbl where course_code = '$sub_code' and b1_sem = '$semb1'";
            $ObjDB->sproc_name = $qreg;
            $res_query = $ObjDB->SelectQuery();
            
            $rowr = mysqli_fetch_array($res_query);
            $sub_regg = $rowr["ccc"];
        }

        /// ---------- Count students Registered  End ------------

        $query3 = "SELECT * FROM isr_curriculum_tbl where sub_code = '$sub_code' and sub_list = '$sub_list'";
        $ObjDB->sproc_name = $query3;
        $res_query3 = $ObjDB->SelectQuery();
        $row3 = mysqli_fetch_array($res_query3);
        $sub_title = $row3["sub_title"];

        if ($sub_title == '') {
            $query33 = "SELECT * FROM isr_gsam_courses_tbl where course_code = '$sub_code' and b1_sem = '$semb1'";
            $ObjDB->sproc_name = $query33;
            $res_query33 = $ObjDB->SelectQuery();
            
            $row33 = mysqli_fetch_array($res_query33);
            $sub_title = $row33["course_name"];
        }
        
        $objadm->fac_roll = $fac_code;
        $query6 = $objadm->GetFacultyList();
        
        $row6 = mysqli_fetch_array($query6);
        $name = $row6["name"];

        echo "<tr>";
        echo "<td >" . $sub_sr ."</td>";
        echo "<td >" . $sub_code ."</td>";
        echo "<td >" . $sub_title ."</td>";
        echo "<td >" . $sub_regg . "</td>";
        echo "<td >" . $name . "</td>";
        echo "<td >" . $ccc . "</td>";
        echo "<td >" . $abstain . "</td>";
        echo "<td >" . $score ."</td>";
//	echo "<td >".$av_atn[$sub_code] ;
        echo "<td >&nbsp;</td>";
        ?>
                                    <td>
                                        <FORM METHOD=POST ACTION="feedback-faculty-details.php">
                                            <INPUT TYPE="hidden" NAME="sno" value="<?php echo $semb1; ?>">
                                            <INPUT TYPE="hidden" NAME="sub_code" value="<?php echo $sub_code; ?>">
                                            <INPUT TYPE="hidden" NAME="sub_title" value="<?php echo $sub_title; ?>">
                                            <INPUT TYPE="hidden" NAME="sub_regg" value="<?php echo $sub_regg; ?>">
                                            <INPUT TYPE="hidden" NAME="fac_code" value="<?php echo $fac_code; ?>">
                                            <INPUT TYPE="hidden" NAME="name" value="<?php echo $name; ?>">
                                            <INPUT TYPE="hidden" NAME="response" value="<?php echo $ccc; ?>">
                                            <INPUT TYPE="hidden" NAME="abstain" value="<?php echo $abstain; ?>">
                                            <INPUT TYPE="submit" name="submit" value="<?php echo $score; ?>">
                                        </FORM>
                                    </td>

        <?php
        echo "<tr>";
        $sub_sr++;
    }
}
?>
                                </tbody>
                        </table>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<?php
$pagemaincontent = ob_get_contents();
ob_clean();
$PAGENAME = 'feedback-faculty';
$HTMLTITLE = '';
$HTMLDESCRIPTION = '';
$HTMLKEYWORDS = '';
include("admin-master.php");
?>

<script type="text/javascript">
    function FunFacultyFeedback(SemNumber) {
        window.location.href="feedback-faculty-semester.php?sno="+SemNumber;
    }
</script>
<script type="text/javascript">
$(document).ready(function() {
    $("#table_feedback_faculty_id").DataTable({
        dom: '<"top"lBf>rt<"bottom"ip><"clear">',
        buttons: [
            {
                extend: 'excelHtml5',
                text: 'Download Excel',
                title: 'Data Export',
                className: 'btn btn-success'
            }
        ]
    });
});
</script>
