<?php

require_once './connection.php';

if (!isset($_POST['ss_stud_name']) == "" && !isset($_POST['select_cluster']) == "") {

    $stud_name = $_POST['ss_stud_name'];
    $cluster_name = $_POST['select_cluster'];
    $school_name = $_POST['school_name'];
    $select_district = $_POST['select_district'];
    $select_taluka = $_POST['select_taluka'];

//    echo 'stud_name ' . $stud_name ;

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    $_SESSION['stud_name'] = $stud_name;
    $_SESSION['cluster_name'] = $cluster_name;
    $_SESSION['school_name'] = $school_name;
    $_SESSION['select_taluka'] = $select_taluka;
    $_SESSION['select_district'] = $select_district;
//    
    echo 'stud_name ' . $_SESSION['stud_name'];
} else {
    echo "else ";
}


if (!isset($_POST['go_to_index']) == "") {
    session_start();
    session_destroy();
    unset($_SESSION);
} else {
    echo "else ";
}

if (!isset($_POST['que1']) == "" && !isset($_POST['que2']) == "") {

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    $stud_name = $_SESSION['stud_name'];
    $cluster_name = $_SESSION['cluster_name'];
    $school_name = $_SESSION['school_name'];
    $district = $_SESSION['select_district'];
    $taluka = $_SESSION['select_taluka'];

//    
    echo 'stud_name ' . $_SESSION['stud_name'];
    $que1 = $_POST['que1'];
    $que2 = $_POST['que2'];
    $que3 = $_POST['que3'];
    $que4 = $_POST['que4'];
    $que5 = $_POST['que5'];
    $que6 = $_POST['que6'];
    $que7 = $_POST['que7'];
    $que8 = $_POST['que8'];
    $que9 = $_POST['que9'];
    $que10 = $_POST['que10'];
    $que11 = $_POST['que11'];
    $que12 = $_POST['que12'];
    $que13 = $_POST['que13'];
    $que14 = $_POST['que14'];
    $que15 = $_POST['que15'];
    $que16 = $_POST['que16'];
    $que17 = $_POST['que17'];
    $que18 = $_POST['que18'];
    $que19 = $_POST['que19'];
    $que20 = $_POST['que20'];
    
    $ans_1 = "B";
    $ans_2 = "A";
    $ans_3 = "D";
    $ans_4 = "D";
    $ans_5 = "A";
    $ans_6 = "A";
    $ans_7 = "D";
    $ans_8 = "A";
    $ans_9 = "D";
    $ans_10 = "C";
    $ans_11 = "C";
    $ans_12 = "C";
    $ans_13 = "C";
    $ans_14 = "B";
    $ans_15 = "D";
    $ans_16 = "B";
    $ans_17 = "A";
    $ans_18 = "B";
    $ans_19 = "A";
    $ans_20 = "B";
    

    $_SESSION['que1'] = $que1;
    $_SESSION['que2'] = $que2;
    $_SESSION['que3'] = $que3;
    $_SESSION['que4'] = $que4;
    $_SESSION['que5'] = $que5;
    $_SESSION['que6'] = $que6;
    $_SESSION['que7'] = $que7;
    $_SESSION['que8'] = $que8;
    $_SESSION['que9'] = $que9;
    $_SESSION['que10'] = $que10;
    $_SESSION['que11'] = $que11;
    $_SESSION['que12'] = $que12;
    $_SESSION['que13'] = $que13;
    $_SESSION['que14'] = $que14;
    $_SESSION['que15'] = $que15;
    $_SESSION['que16'] = $que16;
    $_SESSION['que17'] = $que17;
    $_SESSION['que18'] = $que18;
    $_SESSION['que19'] = $que19;
    $_SESSION['que20'] = $que20;

    $_SESSION['ans_1'] = $ans_1;
    $_SESSION['ans_2'] = $ans_2;
    $_SESSION['ans_3'] = $ans_3;
    $_SESSION['ans_4'] = $ans_4;
    $_SESSION['ans_5'] = $ans_5;
    $_SESSION['ans_6'] = $ans_6;
    $_SESSION['ans_7'] = $ans_7;
    $_SESSION['ans_8'] = $ans_8;
    $_SESSION['ans_9'] = $ans_9;
    $_SESSION['ans_10'] = $ans_10;
    $_SESSION['ans_11'] = $ans_11;
    $_SESSION['ans_12'] = $ans_12;
    $_SESSION['ans_13'] = $ans_13;
    $_SESSION['ans_14'] = $ans_14;
    $_SESSION['ans_15'] = $ans_15;
    $_SESSION['ans_16'] = $ans_16;
    $_SESSION['ans_17'] = $ans_17;
    $_SESSION['ans_18'] = $ans_18;
    $_SESSION['ans_19'] = $ans_19;
    $_SESSION['ans_20'] = $ans_20;

    $count_ans = 0;
    if ($que1 == $ans_1) {
        $user_que1_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que1_ans = 0;
    }

    if ($que2 == $ans_2) {
        $user_que2_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que2_ans = 0;
    }

    if ($que3 == $ans_3) {
        $user_que3_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que3_ans = 0;
    }

    if ($que4 == $ans_4) {
        $user_que4_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que4_ans = 0;
    }

    if ($que5 == $ans_5) {
        $user_que5_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que5_ans = 0;
    }

    if ($que6 == $ans_6) {
        $user_que6_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que6_ans = 0;
    }
    if ($que7 == $ans_7) {
        $user_que7_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que7_ans = 0;
    }

    if ($que8 == $ans_8) {
        $user_que8_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que8_ans = 0;
    }
    if ($que9 == $ans_9) {
        $user_que9_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que9_ans = 0;
    }

    if ($que10 == $ans_10) {
        $user_que10_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que10_ans = 0;
    }
    
    if ($que11 == $ans_11) {
        $user_que11_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que11_ans = 0;
    }
    
    if ($que12 == $ans_12) {
        $user_que12_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que12_ans = 0;
    }
    
    if ($que13 == $ans_13) {
        $user_que13_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que13_ans = 0;
    }
    
    if ($que14 == $ans_14) {
        $user_que14_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que14_ans = 0;
    }
    
    if ($que15 == $ans_15) {
        $user_que15_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que15_ans = 0;
    }
    
    if ($que16 == $ans_16) {
        $user_que16_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que16_ans = 0;
    }
    if ($que17 == $ans_17) {
        $user_que17_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que17_ans = 0;
    }

    if ($que18 == $ans_18) {
        $user_que18_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que18_ans = 0;
    }
    if ($que19 == $ans_19) {
        $user_que19_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que19_ans = 0;
    }

    if ($que20 == $ans_20) {
        $user_que20_ans = 1;
        $count_ans = $count_ans + 1;
    } else {
        $user_que20_ans = 0;
    }
    
    

    echo '$count_ans ' . $count_ans;
    $percentage = $count_ans / 20 * 100;
    echo '$percentage ' . $percentage;
    $grade='';
    if($count_ans==20){
        $grade='A+';
    }else if($count_ans>=18 && $count_ans<=19){
        $grade='A';
    }else if($count_ans>=16 && $count_ans<=17){
        $grade='B';
    }else{
        $grade='';
    }

    echo 'que1 ' . $que1;

    date_default_timezone_set('Asia/Kolkata');
    echo 'date' . date('d-m-Y h:i:s');
    $dt = date('Y-m-d h:i:s');
    
//    $total_get_Mark=$count_ans+0;

//    $_SESSION['percentage'] = $percentage;
    $_SESSION['quiz_date'] = date('d-m-Y');
//
    $query1 = " INSERT INTO `tbl_student`(`name`, `cluster_name`, `school_name`, `taluko`, `district`, `que1_ans`, `que2_ans`, `que3_ans`, `que4_ans`, `que5_ans`, `que6_ans`, `que7_ans`, `que8_ans`, `que9_ans`, `que10_ans`,`que11_ans`,`que12_ans`,`que13_ans`,`que14_ans`,`que15_ans`,`que16_ans`, `que17_ans`, `que18_ans`, `que19_ans`, `que20_ans`, `total_get_mark`,`grade`, `quiz_date`) VALUES ('$stud_name','$cluster_name','$school_name','$taluka','$district','$user_que1_ans','$user_que2_ans','$user_que3_ans','$user_que4_ans','$user_que5_ans','$user_que6_ans','$user_que7_ans','$user_que8_ans','$user_que9_ans','$user_que10_ans','$user_que11_ans','$user_que12_ans','$user_que13_ans','$user_que14_ans','$user_que15_ans','$user_que16_ans','$user_que17_ans','$user_que18_ans','$user_que19_ans','$user_que20_ans','$count_ans','$grade','$dt')";
//            . " values ('$customer_id','$quantity','$price','$total_amt','$paid','$dt') ";
    echo 'query1 ' . $query1;
    try {
        if ($con) {
            mysqli_set_charset($con,'utf8');
            if (mysqli_query($con, $query1)) {
                $last_id = mysqli_insert_id($con);
                
//                $last_id = 1;
                $cnt = strlen($last_id);
                if ($cnt == 1) {
                    $_SESSION['certificate_id'] = 'WMYODI-CE00000' . $last_id;
                } elseif ($cnt == 2) {
                    $_SESSION['certificate_id'] = 'WMYODI-CE0000' . $last_id;
                } elseif ($cnt == 3) {
                    $_SESSION['certificate_id'] = 'WMYODI-CE000' . $last_id;
                } elseif ($cnt == 4) {
                    $_SESSION['certificate_id'] = 'WMYODI-CE00' . $last_id;
                } elseif ($cnt == 5) {
                    $_SESSION['certificate_id'] = 'WMYODI-CE0' . $last_id;
                } elseif ($cnt == 6) {
                    $_SESSION['certificate_id'] = 'WMYODI-CE' . $last_id;
                }

//                $_SESSION['percentage'] = $percentage;
            } else {
                echo "Error:01";
            }
        } else {
            echo "Error:02";
        }
    } catch (Exception $ex) {
        echo "Error:03" . $ex;
    }
} else {
    echo "else ";
}
?>