<?php
	require_once('source/session.php');
	require_once('source/error.php');
	require_once('source/dbconfig.php');
	require_once('source/function.php');
	require_once('source/restrict_admin.php');
	require_once('source/dbconfig2.php');
 
if(isset($_POST['importSubmit'])){

			$teacher_no		= strip_tags($_POST['teacher_no']);
			$s_term		= strip_tags($_POST['term']);
			$s_year		= strip_tags($_POST['year']);
				
			  $rows1 = sql($DBH, "SELECT * FROM tbl_user WHERE hash = ?", 
            	array($teacher_no), "rows");
		foreach($rows1 as $row1)
		{	
			$s_class	 		= $row1['class'];
			$s_subclass	 		= $row1['subclass'];
		}
		
			$rand = (rand());
			$key = $rand;
    //validate whether uploaded file is a csv file
    $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');
    if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$csvMimes)){
        if(is_uploaded_file($_FILES['file']['tmp_name'])){
            
            //open uploaded csv file with read only mode
            $csvFile = fopen($_FILES['file']['tmp_name'], 'r');
            
            //skip first line
            fgetcsv($csvFile);
            
            //parse data from csv file line by line 
            while(($line = fgetcsv($csvFile)) !== FALSE){
                //check whether member already exists in database with same email
                $prevQuery = "SELECT id FROM skills WHERE registration_no = '".$line[1]."' AND csv_no = '".$line[27]."'";
                $prevResult = $db->query($prevQuery);
                if($prevResult->num_rows > 0){
                    //update member data
                    $db->query("UPDATE skills SET name = '".$line[0]."', Creativity = '".$line[2]."', Fluency = '".$line[3]."', Honesty = '".$line[4]."', Leadership = '".$line[5]."' ,
                    Neatness = '".$line[6]."' , Politeness = '".$line[7]."' , Punctuality = '".$line[8]."' , SelfControl = '".$line[9]."' , SenseOfResponsibility = '".$line[10]."' ,
                    Sociability = '".$line[11]."' WHERE registration_no = '".$line[1]."' AND csv_no = '".$line[12]."'");
                }else{
                    //insert member data into database
                    $db->query("INSERT INTO skills (name, registration_no, Creativity, Fluency, Honesty, Leadership, Neatness, Politeness, Punctuality, SelfControl, SenseOfResponsibility, Sociability, class, subclass, subject, year, teacher_no, csv_no, term) 
                    VALUES ('".$line[0]."','".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."','".$line[5]."','".$line[6]."','".$line[7]."',
                    '".$line[8]."','".$line[9]."','".$line[10]."','".$line[11]."','".$s_class."','".$s_subclass."','".$s_subject."','".$s_year."',
                    '".$teacher_no."','".$key."','".$s_term."')");
                }
            }
              
               
            //close opened csv file
            fclose($csvFile);

            $qstring = '?status= suc&val='.$key.'&hsh='.$teacher_no.'&year='.$s_year.'&stclass='.$s_class.'&stterm='.$s_term.'';
        }else{
            $qstring = '?status=err';
        }
    }else{
        $qstring = '?status=invalid_file';
    }
}       
//redirect to the listing page
header("Location: post_skills.php".$qstring);
?>