<?php 
    $course=$yos=$exam=$unit=$classYos=$classSem=$marks="";
    include'include/session.php';
    
    if(isset($_POST['download'])){
    /*    //Function to sanitize values received from the form. Prevents SQL injection
        function clean($str) {
    		$str = @trim($str);
    		if(get_magic_quotes_gpc()) {
    			$str = stripslashes($str);
    		}
    		return mysql_real_escape_string($str);
    	}
    */
        $course = clean($_POST['course']);
        $yos = clean($_POST['yos']);
        $exam = clean($_POST['exam']);
        $unit = clean($_POST['unit']);
        $classYos = clean($_POST['classYoS']);
        $classSem = clean($_POST['classSem']);
        $marks = clean($_POST['marks']);
        
        if(!$course){
            echo'<div class="alert alert-danger 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-danger">Select Course!</span>
                </div>';
        }else
        if(!$yos){
            echo'<div class="alert alert-danger 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-danger">Select Year of Study!</span>
                </div>';
        }else
        if(!$unit){
            echo'<div class="alert alert-danger 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-danger">Select unit!</span>
                </div>';
        }else
        if(!$exam){
            echo'<div class="alert alert-danger 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-danger">Select exam category!</span>
                </div>';
        }else
        if(!$marks){
            echo'<div class="alert alert-danger 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-danger">Enter exam marks!</span>
                </div>';
        }else
        if(!$classYos){
            echo'<div class="alert alert-danger 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-danger">Select current students class Year!</span>
                </div>';
        }else
        if(!$classSem){
            echo'<div class="alert alert-danger 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-danger">Select current students class Block!</span>
                </div>';
        }else
        {
            if(!$select = mysql_query("SELECT f.name AS faculty,c.courseID,c.name AS course,u.unitID,u.name AS unit,u.semisterID,l.staffID,l.staffNo,CONCAT(l.fname,' ',l.lname,' ',l.sname) AS lecturer
                                        FROM staffunit su 
                                        LEFT JOIN staff l
                                        ON su.staffID = l.staffID
                                        LEFT JOIN unit u
                                        ON su.unitID = u.unitID
                                        LEFT JOIN course c
                                        ON u.courseID = c.courseID
                                        LEFT JOIN faculty f
                                        ON c.facultyID = f.facultyID                                       
                                        WHERE su.staffID = '$staffID'
                                        AND su.unitID ='$unit'"))
            {
                echo'<div class="alert alert-danger 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-danger">'.mysql_error().'</span>
                    </div>';
            }else
            if(!$selectx = mysql_query("SELECT studentID,adm FROM student WHERE courseID = '$course' AND  yosID='$classYos' AND semisterID='$classSem'"))
            {
                echo'<div class="alert alert-danger 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-danger">'.mysql_error().'</span>
                    </div>';
            }else    
            {
                $result = mysql_fetch_array($select);
                $faculty = $result['faculty'];
                $courseName = $result['course'];
                $unitName = $result['unit'];
                $sem = $result['semisterID'];
                $lecturer = $result['lecturer'];
                $staffNo = $result['staffNo'];
                
                
                $x = 1;
                $code =substr(str_shuffle(str_repeat("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 9)), 0, 6);
                $output='';
                $output .= '<table class="table" bordered="1" style="width:100%;border:1px solid black;">  
                                    <tr style="text-align:center;font-weight:normal;background-color:#b5e5f9;">  
                                        <th colspan="11"  disabled="disabled">
                                            <h1><b>WAKA CMEC SCHOOL OF '.strtoupper($faculty).'</b></h1>
                                        </th>  
                                    </tr>
                                    <tr style="text-align:center;font-weight:normal;background-color:#b5e5f9;">  
                                        <th colspan="11"  disabled="disabled">
                                            <h2><b>Students Exam Marks Entry Sheet</b></h2>
                                        </th>  
                                    </tr>
                                    <tr style="text-align:center;font-weight:normal;background-color:#b5e5f9;">  
                                        <th colspan="11"  disabled="disabled">
                                            <b>Course:</b> '.$courseName.'
                                        </th>  
                                    </tr>
                                    <tr style="text-align:center;font-weight:normal;background-color:#b5e5f9;">  
                                        <th colspan="11"  disabled="disabled">
                                            <b>Unit:</b> '.$unitName.'. <b>Year of Study:</b> '.$yos.' Yr. Block '.$sem.'
                                        </th>  
                                    </tr>
                                    <tr style="text-align:center;font-weight:normal;background-color:#b5e5f9;">  
                                        <th colspan="11"  disabled="disabled">
                                            <b>Class:</b> '.$classYos.' Yr. Block '.$classSem.'
                                        </th>  
                                    </tr>
                                    <tr style="text-align:center;font-weight:normal;background-color:#b5e5f9;">  
                                        <th colspan="11"  disabled="disabled">
                                            <b>Lecturer:</b> '.$lecturer.' <b>Staff No.</b>'.$staffNo.'
                                        </th>  
                                    </tr>
                                    <tr style="background-color:#e4e3e3;">  
                                        <th  disabled="disabled">#</th>
                                        <th  disabled="disabled">Code</th>
                                        <th  disabled="disabled">L/NO</th>  
                                        <th  disabled="disabled">U/NO</th>  
                                        <th  disabled="disabled">Y/NO</th>
                                        <th  disabled="disabled">B/NO</th>
                                        <th  disabled="disabled">S/NO</th>
                                        <th  disabled="disabled">Exam</th>
                                        <th  disabled="disabled">Marks</th>
                                        <th  disabled="disabled">Student</th>
                                        <th  disabled="disabled">Scored</th>
                                    </tr>';
                                while($rowx = mysql_fetch_array($selectx))
                                {
                                 $output .='<tr style="text-align:center;border:1px solid black;">  
                                                <td style="background-color:#e4e3e3;" disabled="disabled">'.$x++.'</td> 
                                                <td style="background-color:#efefef;" disabled="disabled">'.$code.'</td> 
                                                <td style="background-color:#efefef;" disabled="disabled">'.$staffID.'</td>  
                                                <td style="background-color:#efefef;" disabled="disabled">'.$unit.'</td>  
                                                <td style="background-color:#efefef;" disabled="disabled">'.$yos.'</td>  
                                                <td style="background-color:#efefef;" disabled="disabled">'.$sem.'</td>
                                                <td style="background-color:#efefef;" disabled="disabled">'.$rowx["studentID"].'</td>
                                                <td style="background-color:#efefef;" disabled="disabled">'.$exam.'</td>
                                                <td style="background-color:#efefef;" disabled="disabled">'.$marks.'</td>
                                                <td style="background-color:#efefef;" disabled="disabled">'.$rowx["adm"].'</td>
                                                <td>&nbsp;</td>
                                            </tr>';
                                }
                                $output .= '</table>';
                
                                header('Content-Type: application/xls');
                                header('Content-Disposition: attachment; filename=WAKACMEC-EXAM-SCORE-SHEET_'.$unitName.'.xls');
                                ob_end_clean();
                                echo $output;
                                exit();
            }
        }
    }
?>