<?php
session_start();
include '../admin/includes/database.php';
include '../admin/includes/functions.php';
$conn = new Functions();

if(!empty($_POST['new_psychomotor_name']) && !empty($_POST['new_psychomotor_value'])
    && !empty($_POST['student_section'])  && !empty($_POST['student_class']) && !empty($_POST['student_name'])
    && !empty($_POST['exam']) && !empty($_POST['exam_year'])){
    $psychomotor_name = $_POST['new_psychomotor_name'];
    $psychomotor_value = $_POST['new_psychomotor_value'];
    $student_section = $_POST['student_section'];
    $student_class = $_POST['student_class'];
    $student_name = $_POST['student_name'];
    $exam = $_POST['exam'];
    $exam_year = $_POST['exam_year'];

    //check if details already exists

    $sql = "SELECT * FROM students_psychomotor WHERE psychomotor_name=:name AND section =:section AND 
            class =:class AND exam =:exam AND exam_year =:exam_year AND student_name =:student_name";
    $conn->query($sql);
    $conn->bind(':name', $psychomotor_name);
    $conn->bind(':section', $student_section);
    $conn->bind(':class', $student_class);
    $conn->bind(':exam', $exam);
    $conn->bind(':exam_year', $exam_year);
    $conn->bind(':student_name', $student_name);

    $rowCount = $conn->rowCount();

    if($rowCount > 0){

        echo "<script>
            toastr['error']('You have added $psychomotor_name already');
      </script>";
        return false;

    }else{
        //process  inserting  of data into database

        $sql = "INSERT INTO students_psychomotor (psychomotor_name, psychomotor_value, student_name, section, 
              class, exam, exam_year) VALUES (:name, :value, :student_name, :section, :class, :exam, :exam_year)";
        $conn->query($sql);
        $conn->bind(':value', $psychomotor_value);
        $conn->bind(':name', $psychomotor_name);
        $conn->bind(':section', $student_section);
        $conn->bind(':class', $student_class);
        $conn->bind(':exam', $exam);
        $conn->bind(':exam_year', $exam_year);
        $conn->bind(':student_name', $student_name);

        $send = $conn->execute();
        if ($send) {

            echo "<script>
            toastr['success']('Psychomotor Data Inserted Successfully.');
          </script>";

        } else {
            echo "<script>
            toastr['error']('An error occurred while inserting data.');
          </script>";
        }


    }


}else{
    echo "<script>
            toastr['error']('Psychomotor name and value fields are required.');
      </script>";
}



