<?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_and_remarks WHERE registration_no = '".$line[1]."' AND csv_no = '".$line[33]."'";
                $prevResult = $db->query($prevQuery);
                if($prevResult->num_rows > 0){
                    //update member data
                    $db->query("UPDATE skills_and_remarksPriColl SET name = '".$line[0]."', Attendance = '".$line[2]."', Present = '".$line[3]."', Absent = '".$line[4]."', NextTermBegins = '".$line[5]."' , DateOfReport = '".$line[6]."' , ClassTeachersComments = '".$line[7]."' , Honesty = '".$line[8]."' , Cleanliness = '".$line[9]."' , Punctuality = '".$line[10]."' , Carefulness = '".$line[11]."' , Considerate = '".$line[12]."' , Politeness = '".$line[13]."' , Obedience = '".$line[14]."' , PromptToWork = '".$line[15]."' , WorkAnd = '".$line[16]."' , LogicalReas = '".$line[17]."' , EnjoyComPe = '".$line[18]."' , DoesHomework'".$line[19]."' , PartInClub = '".$line[20]."' , PraysOften = '".$line[21]."' , ProgressWithGod = '".$line[22]."' , AttitudeGod = '".$line[23]."' , TrackEvents = '".$line[24]."' , FieldEvents = '".$line[25]."' , FieldEvents = '".$line[26]."' , FieldEvents = '".$line[27]."' , FieldEvents = '".$line[28]."' , FieldEvents = '".$line[29]."' , FieldEvents = '".$line[30]."' , FieldEvents = '".$line[31]."' , FieldEvents = '".$line[32]."' WHERE registration_no = '".$line[1]."' AND csv_no = '".$line[35]."'");
                }else{
                    //insert member data into database
                    $db->query("INSERT INTO skills_and_remarksPriColl (name, registration_no, SchOpen, Absent, DateOfReport, TermBegins, TeachersRemarks, Punctuality, AttClass, Reliability, Neatness, Politeness, Honesty, Initiative, Attentiveness, Perseverance, RelOthers, Obedience, SenseResp, SelfControl, OrgAbility, SpiritCoop, HW, PS, HT, Dr, Painting, Games, Sports, MusicalSk, 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]."','".$line[12]."','".$line[13]."','".$line[14]."','".$line[15]."','".$line[16]."','".$line[17]."','".$line[18]."','".$line[19]."','".$line[20]."','".$line[21]."','".$line[22]."','".$line[23]."','".$line[24]."','".$line[25]."','".$line[26]."','".$line[27]."','".$line[28]."','".$line[29]."','".$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_and_remarksPriColl.php".$qstring);
?>