<?php                                                                        
    $course=$yos=$sem=$period="";
    
    //select course, year of study, semester & period, for time table
    if(isset($_POST['activate'])){
        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']);
        $sem=clean($_POST['sem']);
        $period=clean($_POST['period']);
        
        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(!$sem){
            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 Semester!</span>
            </div>';
        }else{
            //header("Location:time-table.php");
            echo '<script type="text/javascript"> document.location = "add-time-table.php?step=1&cid='.$course.'&yos='.$yos.'&sem='.$sem.'&period='.$period.'"; </script>';
            $course=$yos=$sem=$period="";
            exit();
        }
            
    }else
    if(isset($_POST['submit'])){
        //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($_GET['cid']);
        $yos = clean($_GET['yos']);
        $sem = clean($_GET['sem']);
        $period = clean($_GET['period']);
        $currentDate = date("Y-m-d");
        
        
        if(!$staffID){
            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">Failed to identify authorising officer!</span>
                </div>';
        }else
        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">Failed to identify 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">Failed to identify Year of Study!</span>
            </div>';
        }else
        if(!$sem){
            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">Failed to identify Semester!</span>
            </div>';
        }else
        if(!$period){
            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">Failed to identify Semester Period!</span>
            </div>';
        }else
        if(!$_FILES['file']['tmp_name']){
            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 document to upload!</span>
                </div>';
        }else{    
        {
            $allowed = array("pdf");

            $file_location = $_FILES['file']['tmp_name'];//get file location
            $info = pathinfo($_FILES['file']['name']);//get file info 
            $ext = $info['extension']; // get the extension of the file
            $file_name = "WakaTimeTable".date("Ymd").date("His").".".$ext;//rename file
            //$size = filesize($file_name);//size of file
            $folder="../../uploads/time_table/lecture/";

            $target_file=$folder . basename($file_name);
            //check if uploaded file extension is in allowed list
            if(!in_array($ext, $allowed)){
                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">Invalid file format. Only files of extension: <b>.pdf</b> are allowed</span>
                    </div>';
            }else
            if(file_exists($target_file)) {
                echo'<div class="alert alert-warning 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-warning">Document exists. Try renaming your document.</span>
                    </div>';
            }else
            /*if($size > 5000000){
                echo'<div class="alert alert-warning 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-warning">Size of file is too big. Max allowed: 4.5MB!</span>
                    </div>';
            }else*/
            if(!move_uploaded_file($file_location,$folder.$file_name)){
                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">Unable to upload file<br> check file name or contact system administrator!></span>
                    </div>';
            }else{
                $dir=$file_name;
            
                if(!$insert = mysql_query("INSERT INTO lecture_time_table (document,courseID,yos,semister,periodID,added_by) VALUES('$dir','$course','$yos','$sem','$period','$stafNo')")){
                        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 
                {
                        $course=$yos=$sem=$period="";
                        $_SESSION['success']='true';
                        header('Location:add-time-table.php');
                        echo "<script type='text/javascript'> document.location = 'add-time-table.php'; </script>";
                        exit();
                }
            }
        }
        } 
    }else
        
    if(isset($_POST['exit'])){
        $course=$yos=$sem=$period="";
        header('Location:add-time-table.php');
        echo "<script type='text/javascript'> document.location = 'add-time-table.php'; </script>";
        exit();  
    }        
?>                                                             