<?php
session_start();
if (isset($_SESSION['logged']) && $_SESSION['logged'] == "1" && $_SESSION['role'] == "admin") {

$file = $_FILES['file']['tmp_name'];
require_once('../../assets/constants/config.php');
require_once('../constants/SimpleXLSX.php');

if ($_POST['type'] == "1") {

    $st_rec = 0;
if ( $xlsx = SimpleXLSX::parse($file) ) {
	foreach( $xlsx->rows() as $r ) {

        if ($st_rec == 0) {

        }else{

            $question = ucfirst($r[0]);
            $op1 = ucfirst($r[1]);
            $op2 = ucfirst($r[2]);
            $op3 = ucfirst($r[3]);
            $op4 = ucfirst($r[4]);
            $answer = ''.$r[5].'';
            $type = $_POST['type'];
            $exam = $_POST['examid'];

            try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


            $stmt = $conn->prepare("INSERT INTO tbl_questions (question, type, op1, op2, op3, op4, answer, exam) VALUES (
            :ques, :type, :op1, :op2, :op3, :op4, :answer, :exam)");
            $stmt->bindParam(':ques', $question);
            $stmt->bindParam(':type', $type);
            $stmt->bindParam(':op1', $op1);
            $stmt->bindParam(':op2', $op2);
            $stmt->bindParam(':op3', $op3);
            $stmt->bindParam(':op4', $op4);
            $stmt->bindParam(':answer', $answer);
            $stmt->bindParam(':exam', $exam);
            $stmt->execute();

            $_SESSION['reply'] = "038";
            header("location:../add-questions?node=$exam");

            }catch(PDOException $e)
            {
            echo "Connection failed: " . $e->getMessage();
            }


        }
        $st_rec++;

	}

} else {
	echo SimpleXLSX::parseError();
}

}

if ($_POST['type'] == "2") {

$st_rec = 0;
if ( $xlsx = SimpleXLSX::parse($file) ) {
	foreach( $xlsx->rows() as $r ) {

        if ($st_rec == 0) {

        }else{

        $question = ucfirst($r[0]);
        $answer = $r[1];
        $type = $_POST['type'];
        $exam = $_POST['examid'];

        try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4;collation=utf8_general_ci;prefix=''", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


        $stmt = $conn->prepare("INSERT INTO tbl_questions (question, type, answer, exam) VALUES (
        :ques, :type, :answer, :exam)");
        $stmt->bindParam(':ques', $question);
        $stmt->bindParam(':type', $type);
        $stmt->bindParam(':answer', $answer);
        $stmt->bindParam(':exam', $exam);
        $stmt->execute();

        $_SESSION['reply'] = "038";
        header("location:../add-questions?node=$exam");

        }catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }


        }
        $st_rec++;

	}

} else {
	echo SimpleXLSX::parseError();
}

}





}else{

header("location:../");

}

?>
