<?php
class cms_uploadMgmt
{

	public $upload_id;
	public $college_id;
	public $branch_id;
	public $semester_id;
	public $branchyear_id;
	public $subject_id;
	public $title;
	public $description;
	public $file_name;
	public $assignment_date;
	public $submission_date;
	public $createdon;
	public $active;
	public $login_id;


	function getAllRecords($limit = 0, $start = 0, $condition = "", $order_field = "", $order_type = "")
	{
		$qry = "SELECT up.*,
				(SELECT branch_name 
				FROM cms_branchmaster sec 
				WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id=up.branch_id) AS branch_name,
	  		  	(SELECT year_name 
				FROM cms_yearmaster ym 
				WHERE ym.year_id=up.year_id) AS year_name,
	  		  	(SELECT branchyear_name 
				FROM cms_branchyear bry 
				WHERE bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and bry.branchyear_id=up.branchyear_id) AS branchyear_name,
	  		  	(SELECT semester_name
			   	 FROM cms_semestermaster sem 
			     WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = up.semester_id) AS semester_name,
				 	(SELECT subject_name
			   	 FROM cms_subjectmaster sub 
			     WHERE  sub.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sub.subject_id=up.subject_id) AS subject_name		  	
		  	
		FROM 
				cms_upload up 
		Where
				 up.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;
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;

	}


	function add()
	{
		global $SQLDateFormat;
		$Qry1 = "";
		$Qry1 .= "upload_id   ='" . Escape($this->upload_id) . "',";
		$Qry1 .= "college_id ='" . Escape($this->college_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 .= "title   ='" . Escape($this->title) . "',";
		$Qry1 .= "description   ='" . Escape($this->description) . "',";
		$Qry1 .= "assignment_date = STR_TO_DATE('" . Escape($this->assignment_date) . "','$SQLDateFormat'),";
		$Qry1 .= "submission_date = STR_TO_DATE('" . Escape($this->submission_date) . "','$SQLDateFormat'),";
		$Qry1 .= "active   ='" . Escape($this->active) . "',";
		$Qry1 .= "year_id   ='"  . Escape($this->year_id) .  "',";
		$Qry1 .= "login_id   ='" . Escape($_SESSION["VedantnrUserId"]) . "',";
		$Qry1 .= "createdon   ='" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {

			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "insert into cms_upload set " . $Qry1 . " ";
			//echo $qry;
			dbQuery($qry);
			$this->upload_id = dbInsertedId();

		}

	}

	function edit()
	{
		global $SQLDateFormat;
		$Qry1 = "";
		$Qry1 .= "upload_id   ='" . Escape($this->upload_id) . "',";
		$Qry1 .= "college_id ='" . Escape($this->college_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 .= "title   ='" . Escape($this->title) . "',";
		$Qry1 .= "description   ='" . Escape($this->description) . "',";
		$Qry1 .= "assignment_date = STR_TO_DATE('" . Escape($this->assignment_date) . "','$SQLDateFormat'),";
		$Qry1 .= "submission_date = STR_TO_DATE('" . Escape($this->submission_date) . "','$SQLDateFormat'),";
		$Qry1 .= "active   ='" . Escape($this->active) . "',";
		$Qry1 .= "year_id   ='"  . Escape($this->year_id) .  "',";
		$Qry1 .= "login_id   ='" . Escape($_SESSION["VedantnrUserId"]) . "',";
		$Qry1 .= "createdon   ='" . Escape(date('Y-m-d h:i:s')) . "',";


		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "update cms_upload set " . $Qry1 . " where  upload_id=" . $this->upload_id;
			//echo $qry;
			dbQuery($qry);
		}

	}


	function delete()
	{
		$qry = "delete from cms_upload where upload_id in (" . $this->upload_id . ") ";
		dbQuery($qry);
	}


	function updatefile()
	{
		$qry = "update cms_upload 
				set file_name = '" . $this->file_name . "'
             	where upload_id =   '" . $this->upload_id . "' ";

		dbQuery($qry);
	}


	//get-subject.php
	function getupload($condition)
	{
		$qry =" SELECT up.*,
				(SELECT subject_name 
				FROM cms_subjectmaster sub  
				WHERE sub.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sub.subject_id=up.subject_id ) AS subject_name,
				(SELECT branch_name 
				FROM cms_branchmaster sec 
				WHERE sec.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and sec.branch_id=up.branch_id) AS branch_name,
	  		  	(SELECT year_name 
				FROM cms_yearmaster ym 
				WHERE ym.year_id=up.year_id) AS year_name,
	  		  	(SELECT branchyear_name 
				FROM cms_branchyear bry 
				WHERE bry.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and bry.branchyear_id=up.branchyear_id) AS branchyear_name,
	  		  	(SELECT semester_name
			   	FROM cms_semestermaster sem 
				WHERE  sem.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' AND sem.semester_id = up.semester_id) AS semester_name,		  	
		 		cu.user_name
				FROM 
					cms_usermaster cu,
					cms_upload up	
				WHERE  up.college_id   = '" . $_SESSION["VedantnrCollegeId"] . "' and up.login_id = cu.user_id";
		if ($condition != "") {
			$qry .= $condition;
		}
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

} ?>