<?php
class StudentDetailMgmt
{
	public $studentdetail_id;
	public $student_id;
	public $cur_branchid;
	public $cur_semesterid;
	public $college_id;
	public $current_active;
	public $cur_divison;
	public $year_id;
	public $cur_shift;


	function add()
	{
		$Qry1 = "";
		$Qry1 .= "student_id ='" . Escape($this->student_id) . "',";
		$Qry1 .= "cur_branchid ='" . Escape($this->cur_branchid) . "',";
		$Qry1 .= "cur_semesterid ='" . Escape($this->cur_semesterid) . "',";
		$Qry1 .= "cur_divison ='" . Escape($this->cur_divison) . "',";
		$Qry1 .= "college_id ='" . Escape($_SESSION["VedantnrCollegeId"]) . "',";
		$Qry1 .= "current_active ='" . Escape($this->current_active) . "',";
		$Qry1 .= "year_id ='" . Escape($this->year_id) . "',";
		$Qry1 .= "batch_id ='" . Escape($this->batch_id) . "',";
		$Qry1 .= "cur_shift ='" . Escape($this->cur_shift) . "',";
		$Qry1 .= "student_rollno ='" . Escape($this->student_rollno) . "',";
		$Qry1 .= "msc_group ='" . Escape($this->msc_group) . "',";
		$Qry1 .= "bsc_group ='" . Escape($this->bsc_group) . "',";
		$Qry1 .= "ba_core ='" . Escape($this->ba_core) . "',";
		$Qry1 .= "ba_el1 ='" . Escape($this->ba_el1) . "',";
		$Qry1 .= "ba_el2 ='" . Escape($this->ba_el2) . "',";
		$Qry1 .= "ip_address='" . Escape($_SERVER['REMOTE_ADDR']) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "INSERT INTO cms_studentdetail SET " . $Qry1 . " ";
			dbQuery($qry);
			$this->studentdetail_id = dbInsertedId();
		}
	}

	function AddNewSemester()
	{


		$Qry1 = "";
		$Qry1 .= "student_id ='" . Escape($this->student_id) . "',";
		$Qry1 .= "cur_branchid ='" . Escape($this->cur_branchid) . "',";
		$Qry1 .= "cur_semesterid ='" . Escape($this->cur_semesterid) . "',";
		$Qry1 .= "cur_divison ='" . Escape($this->cur_divison) . "',";
		$Qry1 .= "college_id ='" . Escape($_SESSION["VedantnrCollegeId"]) . "',";
		$Qry1 .= "current_active ='" . Escape($this->current_active) . "',";
		$Qry1 .= "year_id ='" . Escape($_SESSION['aYearId']) . "',";

		$Qry1 .= "cur_shift ='" . Escape($this->cur_shift) . "',";
		$Qry1 .= "ba_core ='" . Escape($this->ba_core) . "',";
		$Qry1 .= "ba_el1 ='" . Escape($this->ba_el1) . "',";
		$Qry1 .= "ba_el2 ='" . Escape($this->ba_el2) . "',";
		$Qry1 .= "bsc_group ='" . Escape($this->bsc_group) . "',";
		$Qry1 .= "msc_group ='" . Escape($this->msc_group) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "INSERT INTO cms_studentdetail SET " . $Qry1 . " ";
			//	echo $qry; exit;
			dbQuery($qry);
			$this->studentdetail_id = dbInsertedId();
		}
	}

	function EditNewSemester()
	{
		$Qry1 = "";
		$Qry1 .= "student_id = '" . Escape($this->student_id) . "',";
		$Qry1 .= "cur_branchid = '" . Escape($this->cur_branchid) . "',";
		$Qry1 .= "cur_semesterid = '" . Escape($this->cur_semesterid) . "',";
		$Qry1 .= "cur_divison = '" . Escape($this->cur_divison) . "',";
		$Qry1 .= "year_id = '" . Escape($_SESSION['aYearId']) . "',";

		$Qry1 .= "cur_shift ='" . Escape($this->cur_shift) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "UPDATE cms_studentdetail SET " . $Qry1 . " 
						WHERE student_id ='" . Escape($this->student_id) . "' AND
						year_id='" . $_SESSION['aYearId'] . "' and
						current_active = 1  ";


			dbQuery($qry);

			$this->studentdetail_id = dbInsertedId();
			//	echo  $qry; exit; 
		}
	}

	function UpdateOldSemester($old_branch_id, $old_semester_id, $new_student_id)
	{
		$qry = "UPDATE 
						cms_studentdetail 
					SET 
						current_active = $this->current_active 
					WHERE 
						cur_branchid = $old_branch_id 
						AND cur_semesterid = $old_semester_id 
						AND student_id in($new_student_id)
						AND year_id = " . $_SESSION['aYearId'];
		dbQuery($qry);
	}


	function UpdateBatch($student_ids, $batch_id)
	{
		$qry = "UPDATE 
						cms_studentdetail 
					SET 
						batch_id = $batch_id
					WHERE 
						 student_id in($student_ids)
						 AND college_id = " . $_SESSION['VedantnrCollegeId'];

		// echo $qry; exit;
		dbQuery($qry);
	}


	function DeleteOldStudent($old_branch_id, $old_semester_id, $new_student_id)
	{
		$qry = "DELETE FROM 
						cms_studentdetail 
					WHERE 
						cur_branchid = $old_branch_id 
						AND cur_semesterid = $old_semester_id 
						AND student_id in($new_student_id)
						AND year_id = " . $_SESSION['aYearId'];
		dbQuery($qry);
	}

	function DeleteStudentDetail()
	{
		$qry = "DELETE FROM 
						cms_studentdetail 
					WHERE 
						student_id in(" . $this->student_id . ")
						AND year_id = " . $_SESSION['aYearId'];
		dbQuery($qry);
	}
	function getAllRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->student_count = 0;

		$qry = "SELECT * FROM cms_studentdetail where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$this->student_count = dbRows($out);
			return $out;
		}
		return;
	}

	//for semester transfer
	function getAllRecords1($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->student_count = 0;

		$qry = "SELECT  * FROM cms_studentdetail where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$this->student_count = dbRows($out);
			return $out;
		}
		return;
	}

	function getAllRecords2($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->student_count = 0;

		$qry = "SELECT  * FROM cms_studentdetail where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}

		//  	echo $qry; exit;
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$this->student_count = dbRows($out);
			return $out;
		}
		return;
	}

	function getnottransfferedids($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->student_count = 0;

		$qry = "SELECT  group_concat(student_id) as student_ids FROM cms_studentdetail where 1=1 ";

		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}
		$qry .= " group by year_id";
		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$row = dbArray($out);
			$student_ids = $row['student_ids'];
			return $student_ids;
		}
		return;
	}

	function divisiontransfer($div, $where = "")
	{
		$qry = "UPDATE  cms_studentdetail SET cur_divison = '" . $div . "' where 1=1 " . $where;
		$out = dbQuery($qry);

		return;

	}

	function getFacilityData($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{

		$qry = " SELECT 
					sm.student_id,
					IF(
						tf.transport_facility = 1,
						'Hostel Facility',
						'Updown Facility') AS Facility,
					IF(
						tf.transport_facility = 1,
						 DATE_FORMAT(tf.hostel_admdate,'%d/%m/%Y') , 
						 DATE_FORMAT(tf.updown_strdate,'%d/%m/%Y') ) AS from_date,
					IF(
						tf.transport_facility = 1, 
						DATE_FORMAT(tf.hostel_candate,'%d/%m/%Y') , 
						DATE_FORMAT(tf.updown_enddate,'%d/%m/%Y')) AS to_date,
					IF(tf.transport_facility = 1,
					(SELECT head_id FROM cms_feeshead WHERE head_name LIKE '%hostel%'),
					(SELECT head_id FROM cms_feeshead WHERE head_name LIKE '%transport%')) AS head_id,
					(SELECT 
						branch_name 
					 FROM 
					 	cms_branchmaster sec 
					 WHERE 
					 	sec.branch_id = sd.cur_branchid) AS branch_name,
	  				(SELECT 
						year_name 
					 FROM 
					 	cms_yearmaster ym  
					 WHERE 
					 	ym.year_id = sd.year_id) AS year_name,
					(SELECT 
						semester_name 
					 FROM 
					 	cms_semestermaster sem  
					 WHERE 
					 	sem.semester_id = sd.cur_semesterid) AS semester_name,
					(SELECT 
						branchyear_name 
					 FROM 
					 	cms_branchyear bry 
			     	 WHERE   
					 	bry.branchyear_id = sd.cur_branchyearid) AS branchyear_name
				 FROM 
				 	cms_transportfacility tf,
					cms_studentdetail sd,
					cms_studentmaster sm
				 WHERE 
				 	sd.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' and 
					sd.student_id=sm.student_id AND 
					sd.student_id=tf.student_id AND 
					sd.current_active =1 and 
					sm.student_active =1 ";
		if ($condition != "") {
			$qry .= $condition;
		}

		if ($order_field != "" && $order_type != "") {
			$qry .= " order by $order_field $order_type";
		}

		if (intval($limit) > 0 && intval($start) >= 0) {
			$qry .= " Limit $start , $limit";
		}
	//echo $qry;exit;
		$out = dbQuery($qry);

		return $out;
	}
}

?>