<?php
class PresentMasterMgmt
{
	public $day_id;
	public $branch_id;
	public $semester_id;
	public $subject_id;
	public $year_id;
	public $batch_id;
	public $lec_id;
	public $day_date;
	public $day_working;
	public $day_present;
	public $day_absent;
	public $day_comment;
	public $login_id;
	public $createdon;
	public $div_id;
	public $lectype;
	public $shift;


	function getAllStudentPresent($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->present_count = 0;
		$qry = "SELECT * FROM cms_presentmaster WHERE college_id = '" . $_SESSION["VedantnrCollegeId"] . "' ";
		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->present_count = dbRows($out);
			return $out;
		}
		return $out;

	}

	function getpresentdetails($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->present_count = 0;
		$qry = "select *,u.user_name ,
		(SELECT branch_name 
				FROM cms_branchmaster sec 
				WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id = pm.branch_id) AS branch_name,
	  		  	(SELECT year_name
			   	 FROM cms_yearmaster ym 
			     WHERE ym.year_id = pm.year_id) AS year_name,
				(SELECT branchyear_name
			   	 FROM 	cms_branchyear bry 
			     WHERE bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND bry.branchyear_id = pm.branchyear_id) AS branchyear_name,
				 (SELECT semester_name
			   	 FROM cms_semestermaster sem 
			     WHERE sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = pm.semester_id) AS semester_name,		   	 
		  	   	 (SELECT subject_name
			   	 FROM cms_subjectmaster sbm 
			     WHERE  pm.subject_id=sbm.subject_id ) AS subject_name,		   	 
		  	   	  (SELECT batch_name
			   	 FROM cms_batchmaster bhm 
			     WHERE pm.batch_id=bhm.batch_id  ) AS batch_name,
				 (SELECT lec_name
			   	 FROM cms_lecmaster lec
			     WHERE pm.lec_id=lec.lec_id ) AS lec_name		   	 
		  	   	 
		from cms_presentmaster pm,
		cms_usermaster u
		where u.user_id=pm.login_id
			and pm.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' ";

		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->present_count = dbRows($out);
			return $out;
		}
		return $out;
	}

	function getAllabsentData($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->present_count = 0;
		$qry = "SELECT pm.*,(SELECT branch_name 
				FROM cms_branchmaster sec 
				WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id = pm.branch_id) AS branch_name,
	  		  	(SELECT year_name
			   	 FROM cms_yearmaster ym 
			     WHERE ym.year_id = pm.year_id) AS year_name,
				(SELECT branchyear_name
			   	 FROM 	cms_branchyear bry 
			     WHERE bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND bry.branchyear_id = pm.branchyear_id) AS branchyear_name,
				 (SELECT semester_name
			   	 FROM cms_semestermaster sem 
			     WHERE sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = pm.semester_id) AS semester_name,		
				  (SELECT lec_name
			   	 FROM cms_lecmaster lec
			     WHERE pm.lec_id=lec.lec_id ) AS lec_name		   	    	 
		  	   	
                               	from cms_presentmaster pm
                                WHERE pm.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' ";

		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->present_count = dbRows($out);
			return $out;
		}
		return $out;
	}

	function getabsentdetails($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$this->present_count = 0;
		$qry = "select pm.lec_id,
			(SELECT year_name
			   	 FROM cms_yearmaster ym 
			     WHERE ym.year_id = pm.year_id) AS year_name,
				(SELECT branchyear_name
			   	 FROM 	cms_branchyear bry 
			     WHERE bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND bry.branchyear_id = pm.branchyear_id) AS branchyear_name,
				 (SELECT semester_name
			   	 FROM cms_semestermaster sem 
			     WHERE sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = pm.semester_id) AS semester_name,		
				  (SELECT lec_name
			   	 FROM cms_lecmaster lec
			     WHERE pm.lec_id=lec.lec_id ) AS lec_name,
				   (SELECT subject_name
			   	 FROM cms_subjectmaster sbm
			     WHERE pm.subject_id=sbm.subject_id ) AS subject_name,
				  (SELECT batch_name
			   	 FROM cms_batchmaster bhm
			     WHERE pm.batch_id=bhm.batch_id  ) AS batch_name,
				 (SELECT lectype_name
			   	 FROM cms_lectype lectp
			     WHERE lectp.lectype_id=pm.lectype ) AS lectype_name
		from cms_presentmaster pm
		where pm.college_id = '" . $_SESSION["VedantnrCollegeId"] . "' ";

		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->present_count = dbRows($out);
			return $out;
		}
		return $out;
	}

	function getabsentdetailscount($branch_id, $semester_id, $year_id, $branchyear_id, $lec_id, $getDate)
	{
		$qry = "SELECT * 
			          FROM cms_presentmaster
			         WHERE college_id	= '" . $_SESSION["VedantnrCollegeId"] . "'
							   AND year_id= '" . $year_id . "'
							   AND branchyear_id= '" . $branchyear_id . "'

							   AND branch_id= '" . $branch_id . "'
							   AND semester_id= '" . $semester_id . "'
							   AND lec_id= '" . $lec_id . "'
							   AND day_date= '" . $getDate . "'";
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return 1;
		} else {
			return 0;
		}
	}


	function add()
	{
		global $SQLDateFormat;
		$lec = explode(",", $this->lec_id);
		for ($i = 0; $i < count($lec); $i++) {
			$Qry1 = "";
			//$Qry1 .= "day_id ='"	    .Escape($this->day_id)."',";		
			$Qry1 .= "branch_id ='" . Escape($this->branch_id) . "',";
			$Qry1 .= "semester_id ='" . Escape($this->semester_id) . "',";
			$Qry1 .= "branchyear_id ='" . Escape($this->branchyear_id) . "',";
			$Qry1 .= "subject_id ='" . Escape($this->subject_id) . "',";
			$Qry1 .= "college_id ='" . Escape($_SESSION['VedantnrCollegeId']) . "',";
			$Qry1 .= "year_id ='" . Escape($this->year_id) . "',";
			$Qry1 .= "batch_id ='" . Escape($this->batch_id) . "',";
			$Qry1 .= "lectype ='" . Escape($this->lectype) . "',";
			$Qry1 .= "div_id ='" . Escape($this->div_id) . "',";
			$Qry1 .= "lec_id ='" . $lec[$i] . "',";
			$Qry1 .= "day_date = 	    STR_TO_DATE('" . Escape($this->day_date) . "','$SQLDateFormat'),";
			$Qry1 .= "day_working ='" . Escape($this->day_working) . "',";
			$Qry1 .= "day_present ='" . Escape($this->day_present) . "',";
			$Qry1 .= "day_absent ='" . Escape($this->day_absent) . "',";
			$Qry1 .= "day_comment ='" . Escape($this->day_comment) . "',";
			$Qry1 .= "login_id='" . Escape($_SESSION['aUserId']) . "',";
			$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";

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

			if ($Qry1 != "") {
				$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
				$qry = "insert into cms_presentmaster set " . $Qry1 . " ";
				dbQuery($qry);
				$this->day_id = dbInsertedId();
			}
		}
	}

	function edit()
	{
		global $SQLDateFormat;
		$Qry1 = "";
		$Qry1 .= "day_id ='" . Escape($this->day_id) . "',";
		$Qry1 .= "branch_id ='" . Escape($this->branch_id) . "',";
		$Qry1 .= "branchyear_id ='" . Escape($this->branchyear_id) . "',";
		$Qry1 .= "semester_id ='" . Escape($this->semester_id) . "',";
		$Qry1 .= "subject_id ='" . Escape($this->subject_id) . "',";
		$Qry1 .= "college_id ='" . Escape($_SESSION['VedantnrCollegeId']) . "',";
		$Qry1 .= "year_id ='" . Escape($this->year_id) . "',";
		$Qry1 .= "batch_id ='" . Escape($this->batch_id) . "',";
		$Qry1 .= "lectype ='" . Escape($this->lectype) . "',";
		$Qry1 .= "div_id ='" . Escape($this->div_id) . "',";
		$Qry1 .= "lec_id ='" . Escape($this->lec_id) . "',";
		$Qry1 .= "day_date = 	    STR_TO_DATE('" . Escape($this->day_date) . "','$SQLDateFormat'),";
		$Qry1 .= "day_working ='" . Escape($this->day_working) . "',";
		$Qry1 .= "day_present ='" . Escape($this->day_present) . "',";
		$Qry1 .= "day_absent ='" . Escape($this->day_absent) . "',";
		$Qry1 .= "day_comment ='" . Escape($this->day_comment) . "',";
		$Qry1 .= "login_id='" . Escape($_SESSION['aUserId']) . "',";
		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";

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

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "update cms_presentmaster set ";
			$qry .= $Qry1 . " where college_id = '" . $_SESSION["VedantnrCollegeId"] . "' AND day_id='" . $this->day_id . "' ";
			dbQuery($qry);
		}
	}
	function delete()
	{
		$qry = "delete from cms_presentmaster where day_id =" . $this->day_id;
		dbQuery($qry);
	}
	function getDistinctNotes($college_id = "")
	{
		$this->student_count = 0;
		$qry = "SELECT DISTINCT day_comment FROM cms_presentmaster WHERE college_id = '" . $_SESSION["VedantnrCollegeId"] . "'  ";
		$qry .= " order by day_comment ASC";
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			$this->student_count = dbRows($out);
			return $out;
		}
		return $out;
	}
}
?>