<?php
class UserMgmt
{

	public $user_id;
	public $student_id;
	public $user_role;
	public $user_name;
	public $user_password;
	public $college_id;
	public $menu_ids;
	public $module_ids;
	public $page_ids;
	public $createdby;
	public $createdon;




	function UserMgmt()
	{
		$this->bullet = "<font size='2'>&raquo;</font>";
		$this->bullet = htmlentities($this->bullet);
		$this->user_count = 0;
	}

	function getRecordById($uder_id)
	{
		$qry = "select * from cms_usermaster where cms_usermaster.college_id= '" . $_SESSION["VedantnrCollegeId"] . "' ";

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

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$this->user_count = 1;

			$row = dbArray($out);
			extract($row);

			$this->user_id = ($user_id);
			$this->student_id = ($student_id);
			$this->user_role = ($user_role);
			$this->user_name = ($user_name);
			$this->user_password = ($user_password);
			$this->college_id = ($college_id);
			$this->module_ids = ($module_ids);
			$this->menu_ids = ($menu_ids);
			$this->page_ids = ($page_ids);
			$this->createdby = ($createdby);
			$this->createdon = ($createdon);
			$this->user_lastlogin = ($user_lastlogin);
			$this->user_currentlogin = ($user_currentlogin);

			dbResult($out);
		}
	}

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

		$qry = "SELECT * FROM cms_usermaster 
				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->user_count = dbRows($out);
			return $out;
		}
		return $out;
	}

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

		$qry = "SELECT Count(*) AS Count 
				FROM cms_usermaster 
				WHERE 1=1 ";

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


		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$row = dbArray($out);
			extract($row);
			$this->user_count = $Count;
			dbResult($out);
		}
		return $this->user_count;
	}

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

		$qry = "SELECT Count(*) AS Count FROM cms_usermaster where 1=1 ";

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

		$out = dbQuery($qry);

		if (dbRows($out) > 0) {
			$row = dbArray($out);
			extract($row);
			$this->user_count = $Count;
			dbResult($out);
		}
		return $this->user_count;
	}
	function add()
	{

		global $SQLDateFormat;

		$Qry1 = "";
		$Qry1 .= "student_id='" . Escape($this->student_id) . "',";
		$Qry1 .= "user_role='" . Escape($this->user_role) . "',";
		$Qry1 .= "user_name='" . Escape($this->user_name) . "',";
		$Qry1 .= "user_password='" . Escape($this->user_password) . "',";
		$Qry1 .= "college_id='" . Escape($this->college_id) . "',";
		$Qry1 .= "menu_ids='" . Escape($this->menu_ids) . "',";
		$Qry1 .= "module_ids='" . Escape($this->module_ids) . "',";
		$Qry1 .= "page_ids='" . Escape($this->page_ids) . "',";
		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";
		$Qry1 .= "createdby='" . Escape($_SESSION["VedantnrUserId"]) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "insert into cms_usermaster set " . $Qry1 . " ";

			dbQuery($qry);
			$this->user_id = dbInsertedId();
		}
	}

	function editLogin()
	{
		$Qry1 = "";
		$Qry1 .= "user_role='" . Escape($this->user_role) . "',";
		$Qry1 .= "user_name='" . Escape($this->user_name) . "',";
		$Qry1 .= "user_password='" . Escape($this->user_password) . "',";
		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "update cms_usermaster set " . $Qry1 . " where user_id=" . $this->user_id . " And cms_usermaster.college_id= '" . $_SESSION["VedantnrCollegeId"] . "' ";
			dbQuery($qry);
		}
	}
	function editRight()
	{
		$Qry1 = "";

		if ($this->user_role == 'PowerUser') {
			$Qry1 .= "user_role='" . Escape($this->user_role) . "',";
		}
		$Qry1 .= "menu_ids='" . Escape($this->menu_ids) . "',";
		$Qry1 .= "page_ids='" . Escape($this->page_ids) . "',";
		$Qry1 .= "module_ids='" . Escape($this->module_ids) . "',";

		$Qry1 .= "createdon='" . Escape(date('Y-m-d h:i:s')) . "',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "update cms_usermaster set " . $Qry1 . " where user_id=" . $this->user_id . " And cms_usermaster.college_id= '" . $_SESSION["VedantnrCollegeId"] . "' ";

			dbQuery($qry);
		}
	}

	function changePassword()
	{

		$Qry1 = "";
		$Qry1 .= "user_password='" . Escape($this->user_password) . "',";
		//$Qry1 .= "blnBackup = '0',";

		if ($Qry1 != "") {
			$Qry1 = substr($Qry1, 0, strlen($Qry1) - 1);
			$qry = "update cms_usermaster set " . $Qry1 . " where user_id=" . $this->user_id . " And college_id= '" . $_SESSION["VedantnrCollegeId"] . "' ";
			dbQuery($qry);
		}
	}

	function generateUniqueNo($prifix, $bool = false)
	{
		$uniqno = "";
		$uniqno = uniqid($prifix, $bool);
		return $uniqno;
	}

	function generatePIN($uniqno)
	{
		$pin = "";
		$mc = md5($uniqno);
		$pin = substr($mc, 0, 6);
		return $pin;
	}
	function delete()
	{
		$qry = "delete from  cms_usermaster 
             	where user_id in (" . $this->deleteUser_Id . ") 
				and college_id='" . $_SESSION["VedantnrCollegeId"] . "'";
		dbQuery($qry);
	}

	function tep_validate_password($plain, $encrypted)
	{
		if (($plain != "") && ($encrypted != "")) {
			// split apart the hash / salt
			$stack = explode(':', $encrypted);

			if (sizeof($stack) != 2)
				return false;

			if (md5($stack[1] . $plain) == $stack[0]) {
				return true;
			}
		}

		return false;
	}

	////
	// This function makes a new password from a plaintext password. 
	function tep_encrypt_password($plain)
	{
		$password = '';

		for ($i = 0; $i < 10; $i++) {
			$password .= $this->tep_rand();
		}

		$salt = substr(md5($password), 0, 2);

		$password = md5($salt . $plain) . ':' . $salt;

		return $password;
	}

	////
	// Return a random value
	function tep_rand($min = null, $max = null)
	{
		static $seeded;

		if (!$seeded) {
			mt_srand((double) microtime() * 1000000);
			$seeded = true;
		}

		if (isset($min) && isset($max)) {
			if ($min >= $max) {
				return $min;
			} else {
				return mt_rand($min, $max);
			}
		} else {
			return mt_rand();
		}
	}
	function getuser($username, $password)
	{

		$qry = "SELECT * FROM cms_usermaster WHERE user_name = '" . $username . "' AND user_password = '" . $password . "' ";
		$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}



	function getusers($c_login, $c_pass,$module_id)
	{

		$qry = "select 
				u.*,em.employee_active,cc.college_name,cc.college_logo,cc.college_address,cc.college_phone,cc.college_email,cc.college_url
				  from cms_usermaster u left join cms_collegemaster cc on cc.college_id=u.college_id 
				  LEFT JOIN cms_employeemaster as em ON u.user_id = em.user_id
				  where  user_name='$c_login' &&  user_password='$c_pass' 
				  && FIND_IN_SET($module_id, module_ids) AND(em.employee_active='Yes' OR u.user_role='Poweruser' OR u.user_role='Hod'
				  OR u.user_role='Faculty' OR u.user_role='user' 
				  )";
			$out = dbQuery($qry);
		if (dbRows($out) > 0) {
			return $out;
		}
		return $out;
	}

	function getmodule($module_id)
    {

        $qry = "SELECT module_shortname FROM cms_modulemaster WHERE module_id = $module_id";
	
        $out = dbQuery($qry);

        return $out;
    }

	function getemp($condition)
    {

        $qry = "select employee_id,branch_id from cms_employeemaster where user_id ='" . $_SESSION["VedantnrUserId"] . "'";
		if ($condition != "") {
			$qry .= $condition;
		}	
        $out = dbQuery($qry);

        return $out;
    }

	//loginhome.php
	function getuserfees($condition = "")
	{
		$this->user_count = 0;

		$qry = "Select login_id,user_name,sum(case when discount = '0' then receive_amt else total_receive_amt  end) as receive_amt
					from 
					cms_feesreceivemaster fees, 
					cms_usermaster user 
					Where 
					user.user_id=fees.login_id
					And fees.receipt_date='" . date("Y-m-d") . "'
					And fees.college_id='" . $_SESSION["VedantnrCollegeId"] . "'
					group by login_id";

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

		

		$out = dbQuery($qry);

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


}
?>