<?php 
    include'include/session.php';
    
    $tableName="faculty";
    $tableName1="course";
    $tableName2="course";
    $tableName3="student";
    
    $sql = "SELECT * FROM $tableName";
    $query = mysql_query($sql);
    $rowcount=  mysql_num_rows($query);
    
    if(isset($_GET['id'])){
        $id  = mysql_escape_string($_GET['id']);
        
        $sql1 = "SELECT * FROM $tableName WHERE staffID ='$staffID' AND assignmentID ='$id'";
        $query1 = mysql_query($sql1);
        $rowcount1=  mysql_num_rows($query1);
        
        $sql = "SELECT * FROM unmarkedassignments WHERE assignmentID = '$id' ORDER BY submittedID ASC";
        $query = mysql_query($sql);
        $rowcount=  mysql_num_rows($query);
    }
    
    if(isset($_POST['c_id'])) {
        $sql = mysql_query("select years from `course` where `courseID`=".mysql_real_escape_string($_POST['c_id']));
        $res = mysql_fetch_array($sql);
        $years = $res['years'];
        echo "<option value='' style='cursor:pointer;'>- Select YoS -</option>";
        for($y=1;$y<=$years;$y++){
            echo "<option value='$y' style='cursor:pointer;'>Year $y</option>";
        }
        exit();
    }
    if(isset($_POST['cid'])) {
        $sqlx = mysql_query("select years from `course` where `courseID`=".mysql_real_escape_string($_POST['cid']));
        $resx = mysql_fetch_array($sqlx);
        $yearsx = $resx['years'];
        echo "<option value='' style='cursor:pointer;'>- Class Year -</option>";
        for($z=1;$z<=$yearsx;$z++){
            echo "<option value='$z' style='cursor:pointer;'>Year $z</option>";
        }
        exit();
    }
    if(isset($_POST['yoss'])) {
        $y = mysql_real_escape_string($_POST['yoss']);
        $c = mysql_real_escape_string($_POST['cos']);
        
        
        
        echo "<option value='' style='cursor:pointer;'>- Select Unit -</option>";
        for($p=1;$p<=3;$p++){
            $sqlx = mysql_query("SELECT u.unitID,u.name,u.semisterID
                            FROM staffunit a 
                            LEFT JOIN unit u 
                            ON a.unitID=u.unitID  
                            WHERE a.staffID='$staffID'
                            AND u.courseID='$c' 
                            AND u.yosID='$y'
                            AND semisterID='$p'
                            ORDER BY name ASC");
            $rowsx=  mysql_num_rows($sqlx);
            
            echo "<optgroup label='Block $p'>";
            if($rowsx <= 0){
                echo "<option value='' class='bg-danger text-danger' style='cursor:pointer;'>No Units Found</option>";
            }else
            {
                while($rowx = mysql_fetch_array($sqlx)) {
                    echo "<option value='".$rowx['unitID']."' style='cursor:pointer;'>".$rowx['name']."</option>";
                }            
            }
            echo "</optgroup>";
        }
        exit();
    }
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mark Exams | Lecturer | WAKA CMEC</title>
    <link rel="shortcut icon" href="../../../assets/img/logo.png" />
    <link rel="stylesheet" href="../../../assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400|Roboto:300,400,700">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
    <link rel="stylesheet" href="../../../assets/fonts/font-awesome.min.css">
    <link rel="stylesheet" href="../../../assets/css/ebs-simple-login-form1.css">
    <link rel="stylesheet" href="../../../assets/css/ebs-simple-login-form2.css">
    <link rel="stylesheet" href="../../../assets/css/ebs-simple-login-form.css">
    <link rel="stylesheet" href="../../../assets/css/FPE-Gentella-form-elements.css">
    <link rel="stylesheet" href="../../../assets/css/FPE-Gentella-form-elements1.css">
    <link rel="stylesheet" href="../../../assets/css/MUSA_no-more-tables.css">
    <link rel="stylesheet" href="../../../assets/css/MUSA_no-more-tables1.css">
    <link rel="stylesheet" href="../../../assets/css/MUSA_panel-table.css">
    <link rel="stylesheet" href="../../../assets/css/MUSA_panel-table1.css">
    <link rel="stylesheet" href="../../../assets/css/nav.css">
    <link rel="stylesheet" href="../../../assets/css/Navbar-Fixed-Side1.css">
    <link rel="stylesheet" href="../../../assets/css/Profile-Edit.css">
    <link rel="stylesheet" href="../../../assets/css/Profile-Edit1.css">
    <link rel="stylesheet" href="../../../assets/css/Responsive-Form.css">
    <link rel="stylesheet" href="../../../assets/css/Responsive-Form1.css">
    <link rel="stylesheet" href="../../../assets/css/Sidebar-Menu.css">
    <link rel="stylesheet" href="../../../assets/css/Sidebar-Menu1.css">
    <link rel="stylesheet" href="../../../assets/css/styles.css">
    <link rel="stylesheet" href="../../../assets/css/Data-Table.css">
    <link rel="stylesheet" href="../../../assets/css/Data-Table1.css">
</head>

<body style="background-color:rgb(252,252,252);">
    <div></div>
    <div id="wrapper">
        <div id="sidebar-wrapper" style="background-color:rgb(62,140,228);">
            <ul class="sidebar-nav">
                <li class="sidebar-brand" style="color:rgb(255,255,255);"> 
                    <a href="#" style="font-weight:bold;color:rgb(254,254,254);">Lecturer | WAKA CMEC</a>
                </li>
                <li style="color:rgb(252,253,254);"> 
                    <a href="index.php" style="color:rgb(255,255,255);">Home</a>
                    <a href="course-work.php" style="color:rgb(255,255,255);">Course Work</a>
                    <a href="view-assignments.php" style="color:rgb(255,255,255);">View Assignments</a>
                    <a href="exam.php" style="color:rgb(255,255,255);">View Exam</a>
                    <a href="assigned-units.php" style="color:rgb(255,255,255);">View Units</a>
                </li>
            </ul>
        </div>
        <div class="page-content-wrapper">
            <div class="container-fluid"><a class="btn btn-link" role="button" href="#menu-toggle" id="menu-toggle"><i class="fa fa-bars"></i></a>
                <div class="dropdown" style="width:150px;float:right;margin-top:5px;">
                    <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false" type="button" style="width:150px;background-color:rgb(252,252,252);color:rgb(20,19,19);border:1px solid rgb(252,252,252);text-align:right;"><i class="fa fa-user"></i> &nbsp; <?php echo $lec;?> <span class="caret"></span></button>
                    <ul role="menu" class="dropdown-menu dropdown-menu-right">
                        <li role="presentation"><a href="settings.php"><i class="fa fa-wrench"></i> &nbsp;Settings</a></li>
                        <li role="presentation"><a href="../validate/logout.php"><i class="fa fa-power-off"></i> &nbsp; Logout</a></li>
                    </ul>
                </div>
                <?php
                    if (empty($query)) { // add this check.
                        echo'<div class="alert alert-danger absolue center text-center" role="alert">
                                <span class="text-danger">Server side error occured</span>
                            </div>';
                    }else{
                    
                ?>
                <div class="row">
                    <div class="col-md-12">
                        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel='stylesheet' type='text/css'>

                        <div class="container">
                            <div class="row">

                                <div class="col-md-12 ">

                                    <div class="panel panel-default panel-table">
                                        <div class="panel-heading">
                                            <div class="row">
                                              <div class="col col-xs-12">
                                                <h3 class="panel-title">
                                                    <em class="fa fa-book"></em> 
                                                    <b>Exam Results</b>
                                                 </h3>
                                              </div>
                                            </div>
                                        </div>
                                        
                                    <?php 
                                        if($rowcount<=0){
                                            echo'<div class="alert alert-danger absolue center text-center">
                                                    <strong class="text-danger">No faculty records found</strong>
                                                </div>';
                                        }else{
                                            
                                    ?>
                                        
                                        <div class="row" style="margin:0px;padding:0px;">
                                        <form method="POST" name="results.php" action="results.php"  target="_blank">
                                            
                                            <?php //include'validate/results.php'; ?>
                                            <div class="col col-md-12 bg-info text-info">
                                                <p style="font-weight:bold;padding:5px;">NB:// &nbsp; After downloading Students' Exam Marks Entry excel sheet &AMP; inputing each student's exam scores, 
                                                    ensure to save it as .csv format, for easy upload
                                                </p>
                                            </div>
                                            
                                            <div class="col col-md-12" style="margin:0px;padding:0px;">
                                                <div class="col col-md-5">
                                                    <label>Course: <a class="text-danger">*</a></label>
                                                    <select class="form-control" required id="course" name="course">
                                                        <option value="" style='cursor:pointer;'>- Select Course -</option>
                                                        <?php 
                                                            if(empty($query)){
                                                                echo '<option class="bg-danger text-danger" value="">No faculty found</option>';
                                                            }else
                                                            {
                                                                while($row = mysql_fetch_array($query)) {
                                                        ?>
                                                        <optgroup label="<?php echo $row['name'];?>">
                                                            <?php 
                                                                $sqlx = "select * from `course` where `facultyID`=".mysql_real_escape_string($row['facultyID']);
                                                                $res = mysql_query($sqlx);
                                                                if(empty($res)){
                                                                    echo '<option class="bg-danger text-danger" value="">No course found</option>';
                                                                }else
                                                                if(mysql_num_rows($res) < 1) {
                                                                    echo '<option class="bg-danger text-danger" value="">No course found</option>';
                                                                }else{
                                                                    while($rw = mysql_fetch_array($res)) {
                                                            ?>
                                                            <option value="<?php echo $rw['courseID'];?>" 
                                                                <?php 
                                                                    /*if($course==$rw['courseID']){
                                                                        echo 'selected="selected"';
                                                                    }else*/
                                                                    if(isset($_GET['cid'])){
                                                                        $cid = $_GET['cid'];

                                                                        if($cid==$rw['courseID']){
                                                                            echo "selected=selected";   
                                                                        }
                                                                    }
                                                                ?> 
                                                            style='cursor:pointer;'>
                                                                <?php echo $rw['name'];?>
                                                            </option>
                                                            <?php }} ?>
                                                        </optgroup>
                                                        <?php }} ?>
                                                    </select>
                                                </div>
                                                <div class="col col-md-2">
                                                    <label>Year of Study: <a class="text-danger">*</a></label>
                                                    <select class="form-control" required id="yos" name="yos">
                                                        <option value="" style='cursor:pointer;'>- Select YoS -</option>
                                                    </select>
                                                </div>
                                                <div class="col col-md-5">
                                                    <input type="hidden" id='lec' value="<?php echo $staffID;?>">
                                                    <label>Unit: <a class="text-danger">*</a></label>
                                                    <select class="form-control" required id="unit" name="unit">
                                                        <option value="" style='cursor:pointer;'>- Select Unit -</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="col col-md-12" style="margin:10px 0px 0px 0px;padding:0px;">
                                                <div class="col col-md-3">
                                                    <label>Exam Category: <a class="text-danger">*</a></label>
                                                    <select class="form-control" id="exam" required name="exam">
                                                        <option value="" style='cursor:pointer;'>- Select Exam Category -</option>
                                                        <option value="CAT 1" style='cursor:pointer;'>CAT 1</option>
                                                        <option value="CAT 2" style='cursor:pointer;'>CAT 2</option>
                                                        <option value="CAT 3" style='cursor:pointer;'>CAT 3</option>
                                                        <option value="Main Exam" style='cursor:pointer;'>Main Exam</option>
                                                        <option value="Practicals" style='cursor:pointer;'>Practicals</option>
                                                    </select>
                                                </div>
                                                <div class="col col-md-2">
                                                    <label>Total Marks: <a class="text-danger">*</a></label>
                                                    <input  type="number" value="<?php echo $marks;?>" min="5" name="marks" class="form-control" required>
                                                </div>
                                                <div class="col col-md-2">
                                                    <label>Current Class Year: <a class="text-danger">*</a></label>
                                                    <select class="form-control" required id="classYear" name="classYoS">
                                                        <option value="" style='cursor:pointer;'>- Class Year -</option>
                                                    </select>
                                                </div>
                                                <div class="col col-md-5">
                                                    <label>Current Class Block: <a class="text-danger">*</a></label>
                                                    <select class="form-control" required id="classSem" name="classSem">
                                                        <option value="" style='cursor:pointer;'>- Class Block -</option>
                                                        <option value="1" style='cursor:pointer;'>Block 1</option>
                                                        <option value="2" style='cursor:pointer;'>Block 2</option>
                                                        <option value="3" style='cursor:pointer;'>Block 3</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="col col-xs-12" style="margin:0px;padding:0px;">
                                                <div class="col col-xs-12 text-center">
                                                    <br>
                                                    <button name="download" type="submit" class="btn-success" style="padding:5px 20px;color:#FFFFFF;font-size:14px;">
                                                        <i class="fa fa-download"></i> &nbsp;
                                                        Download Excel
                                                    </button>
                                                    <hr>
                                                </div>    
                                            </div>
                                        </form>
                                    </div>
                                    <?php
                                        }
                                        if(isset($_SESSION['mark'])=="true"){
                                            echo'<div class="alert alert-success absolue center text-center" role="alert">
                                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                                        <span aria-hidden="true">×</span>
                                                    </button>
                                                    <span class="text-success">Assignment Successfully Marked</span>
                                                </div>';
                                            unset($_SESSION['mark']); 
                                        }
                                        if(empty($query)){
                                            echo'<div class="alert alert-danger absolue center text-center">
                                                    <strong class="text-danger">Error occured while trying to fetch student info</strong>
                                                </div>';
                                        }else{
                                    ?>



                                            <div class="container">
                                                <div>
                                                    <form method="POST" enctype="multipart/form-data">
                                                        <div class="form-group" style="margin-top:30px;">
                                                            <div id="formdiv">
                                                                <div class="panel-body">
                                                                    <div class="row" style="margin-right:0px;margin-left:0px;padding-top:10px;">
                                                                        <div class="col-md-12">
                                                                            <?php 
                                                                                include'validate/results.php';
                                                                                if(isset($_SESSION['new'])=="True"){
                                                                                    echo'<div class="alert alert-success absolue center text-center" role="alert">
                                                                                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                                                                                <span aria-hidden="true">×</span>
                                                                                            </button>
                                                                                            <span class="text-success">Results uploaded successfully</span>
                                                                                        </div>';
                                                                                    unset($_SESSION['new']); 
                                                                                }
                                                                            ?>
                                                                        </div>    
                                                                        <div class="col-md-12">
                                                                            <label>Upload Students' Excel Marks</label>
                                                                        </div>
                                                                        <div class="col-md-12">
                                                                            <input type="file" class="form-control" required name="file">
                                                                            <br><br>
                                                                        </div>
                                                                        <div class="col-md-12 text-center">
                                                                            <button name="upload" type="submit" style="padding:5px 20px;color:rgb(1,1,1);">
                                                                                <i class="fa fa-upload"></i> &nbsp;
                                                                                Upload Excel
                                                                            </button>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>   
                                        </div>
                                        <?php }?>
                                    </div>
                                </div>
                            </div>
                        
                    </div>
                </div>
            </div>
            <?php } ?>
        </div>
    </div>
    <script src="../../../assets/js/jquery.min.js"></script>
    <script src="../../../assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="../../../assets/js/ebs-simple-login-form.js"></script>
    <script src="../../../assets/js/Profile-Edit.js"></script>
    <script src="../../../assets/js/Sidebar-Menu.js"></script>
    <script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script>
    <script src="ajax_1.js"></script>
</body>
</html>