<?php
//include "application/scripts/connect.php";
if(isset($_POST['active_get_marks'])){
define('BASEPATH', "/smartsms/");
define('ENVIRONMENT', "production");
include "../config/database.php";

$hostname = $db['default']['hostname'];
$username = $db['default']['username'];
$password = $db['default']['password'];
$database = $db['default']['database'];
$dbdriver = $db['default']['dbdriver'];


// Create connection
$conn_db = mysqli_connect($hostname, $username, $password, $database);
// Check connection
if (!$conn_db) {
    die("Connection failed: " . mysqli_connect_error());
}



//echo "<script>alert('Data is Coming!');</script>";
	$ca_array = array("ca1","ca2","ca3","ca4","ca5","ca6","ca7","ca8","ca9","get_marks");
	$asn = 0;
	
	$class_data = $_POST['class_data'];
	$class_data_array = explode('_', $class_data);
	$class_data_class_id = $class_data_array[0];
	$class_data_section_id = $class_data_array[1];
	
	foreach($ca_array as $keyx => $valuex){
	$label_val = "label_" . $valuex;
	$max_val = "max_" . $valuex;
	$description_val = "description_" . $valuex;
	$active_val = "active_" . $valuex;
	
	$$active_val = $_POST[$active_val];
	$$max_val = $_POST[$max_val];
	$$description_val = $_POST[$description_val];
	$$label_val = $_POST[$label_val];
	
	$label = $$label_val;
	$max = $$max_val;
	$description = $$description_val;
	$active = $$active_val;
	
		
		$sql2 = "SELECT * from `exam_ca_label` WHERE class_id='$class_data_class_id' AND section_id='$class_data_section_id'  AND ca_type ='$valuex' ORDER BY `id`;";
	$query2 = mysqli_query($conn_db, $sql2);
		if(mysqli_num_rows($query2) == 0){
		//ca_type, max_mark, label, description, active
		$sql3 = "INSERT INTO `exam_ca_label` (`class_id`, `section_id`, `ca_type`, `max_mark`, `label`, `description`, `active`) 
		VALUES ('$class_data_class_id', '$class_data_section_id', '$valuex', '$max', '$label', '$description', '$active');";
		if($query3 = mysqli_query($conn_db, $sql3)){
			echo "
			<script>
			//alert('Marks Label Inserted $valuex');
			</script>";
			}
			else{
			//	echo "<script>alert('Marks Label failed');</script>";
			}
		}
		else {
	$sql = "UPDATE `exam_ca_label` SET
			`max_mark` = '$max',
			`label` = '$label',
			`description` = '$description',
			`active` = '$active'
			WHERE class_id='$class_data_class_id' AND section_id='$class_data_section_id'  AND ca_type ='$valuex';";
	
		//echo $sql;
		
			if($query = mysqli_query($conn_db, $sql)){	
			echo "
			<script>
		//	alert('Marks Label Updated $valuex');
			</script>";
			}
			else{
		//		echo "<script>alert('Marks Label failed');</script>";
			}
			

		}	
	
	}

}

echo "<script>alert('Settings Updated');</script>";


?>  