<?php 
require 'dao/Global.php';
$success1 = false;
$success2 = false;
$response = 0;
$msg = "";
$err = "";

$a_years = $global->get_a_years();
$semesters = $global->get_semesters();
$tracks = $global->get_tracks();
$globals = $global->get_all_globals();
$current_academic_year = $globals[0]['a_year'];
$current_semester = $globals[0]['sem_name'];


if (isset($_POST['set_global'])) {
	$set_a_year = $_POST['set_a_year'];
	$set_semester = $_POST['set_semester'];
	$set_track = $_POST['set_track'];
	$no_of_days = $_POST['no_of_days'];
	$data = array("semester_id"=>$set_semester,
				  "track_id"=>$set_track,
				  "a_year_id"=>$set_a_year,"no_of_days"=>$no_of_days);
	$global_isset = $global->get_all_globals();
	if ($global_isset) {
		$global_id = $global_isset[0]['global_id'];
	$success2 = $global->update_global($data,$global_id);	
	}else{
    $success1 = $global->set_global($data);
	}

	if ($success1) {
		echo $response = 0;
	}else if($success2){
		echo $response = 2;
	}else{
		echo $response = 3;
	}
}

if (isset($_POST['setVacation'])) {
	$end = $_POST['end'];
	$next = $_POST['next'];
	$data = array('end'=>$end,'next'=>$next);
	$success = $global->setVacation($data,1);
}

if (isset($_POST['setPromotionCondition'])) {
	$promote_all = $_POST['promote_all'];
	$data = array('promote_all'=>$promote_all);
	$success = $global->promoteAll($data,1);
}

if (isset($_POST['update_credentials'])) {
	$password = $_POST['password'];
	$staff_id = $_POST['staff_id'];
	$password = md5($password);
	$data = array("password"=>$password);
	$success  = $global->update_credentials($data,$staff_id);
	if ($success) {
		echo "Ok";
		print_r($data);
	}else{
		print_r($data);
	}
}

$gold = $global->get_all_gold();
$green = $global->get_all_green();
$get_classes_for_promotion = $global->get_classes_for_promotion();
if (isset($_GET['p@'])) {
	foreach ($get_classes_for_promotion as $key) {
	$get_first_letter = substr($key['class_name'], 0,1);
	if ($get_first_letter =='1') {
		$new_name = '2'.substr($key['class_name'], 1);
		$class_id = $key['class_id'];
		$data = array("class_name"=>$new_name);
		$global->promote_students($data,$class_id);
	}
	
	if ($get_first_letter =='2') {
		$new_name = '3'.substr($key['class_name'], 1);
		$class_id = $key['class_id'];
		$data = array("class_name"=>$new_name);
		$global->promote_students($data,$class_id);
	}

	
	if ($get_first_letter =='3') {
		$new_name = 'Alumini';
		$class_id = $key['class_id'];
		$data = array("class_name"=>$new_name);
		$global->promote_students($data,$class_id);
	}
}
}

?>