<?php
function get_teacher($class_id,$section_id){
	include "application/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
	$connected = 0;
				if (!$conn_db) {
					die("Connection failed: " . mysqli_connect_error());
				}


	$teacher_id = 0;


				$sql2 = "SELECT * from `class_teacher` WHERE class_id='$class_id' AND section_id='$section_id' ORDER BY `id` DESC LIMIT 1;";
				$query = mysqli_query($conn_db, $sql2);
				if($query){
					if(mysqli_num_rows($query) == 0){
					//echo "<script>alert(\"Class Teacher not assigned yet! $connected \");</script>";
					return array();
					}
					else {
						$teachers_id_array = array();
						while($row = mysqli_fetch_assoc($query)){
						$teacher_id = $row['staff_id'];
						$teachers_id_array[] = $teacher_id;
						}
						if(!empty($teachers_id_array)){
						//echo "<script>set_teacher($teacher_id);</script>";
						return $teachers_id_array;
						}
						else{
						//echo "<script>null_teacher();</script>";	
						//echo "<script>alert(\"Class Teacher not assigned yet! \");</script>";
						return array();
						}
					}
				}
				else{
				//		echo "<script>alert(\" " . mysqli_connect_error() . " \");</script>";
				}

}	


if(isset($_POST['query_type'])){



//include "application/scripts/connect.php";
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
$connected = 0;
			if (!$conn_db) {
				die("Connection failed: " . mysqli_connect_error());
			}

$class_id = $_POST['class_id'];
$section_id = $_POST['section_id'];
$query_type = $_POST['query_type'];

$teacher_id = 0;

	if($query_type == 'set_client'){

			
			$sql2 = "SELECT * from `class_sections` WHERE class_id='$class_id' AND section_id='$section_id' ORDER BY `id` DESC LIMIT 1;";
			$query = mysqli_query($conn_db, $sql2);
			if($query){
				if(mysqli_num_rows($query) == 0){
					echo "<script>alert(\"Class Teacher not assigned yet! $connected \");</script>";
				}
				else {
					$row = mysqli_fetch_assoc($query);
					$teacher_id = $row['teacher_id'];
					if($teacher_id != ""){
						echo "<script>set_teacher($teacher_id);</script>";
					}
					else{
						echo "<script>null_teacher();</script>";	
					echo "<script>alert(\"Class Teacher not assigned yet! \");</script>";

					}
				}
			}
			else{
					echo "<script>alert(\" " . mysqli_connect_error() . " \");</script>";
			}

	die;
	}
	
	if($query_type == 'update_teacher'){
			
			$class_teacher_id = $_POST['class_teacher_id'];
			
			$sql3 = "UPDATE `class_sections` SET teacher_id='$class_teacher_id' WHERE class_id='$class_id' AND section_id='$section_id';";
			$query2 = mysqli_query($conn_db, $sql3);
			if($query2){
					echo "<script>alert(\" CLASS TEACHER UPDATED! \");</script>";
					if($class_teacher_id != ""){
						echo "<script>set_teacher($class_teacher_id);</script>";
					}
					else{
						echo "<script>null_teacher();</script>";	
					}
			}
			else{
					echo "<script>alert(\" " . mysqli_connect_error() . " \");</script>";
			}

	die;
	}

}
?>


