load->database(); $this->load->helper('common_helper'); date_default_timezone_set('Asia/Kathmandu'); $this->nepaliDateObject = new NepaliDate(); $this->nepali_current_date = cuurentNepaliDate($this->nepaliDateObject); } public function get_lms_dashboard_records($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE) { if ($limit) $this->db->limit($limit, $offset); if ($where != '') $this->db->where($where); if ($sqlQry != '') $query = $this->db->query($sqlQry); return $query->row_array(); } /**************************************** START of Admin Roles - Nandini ************************************/ public function get_lms_admin_roles($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE) { if ($limit) $this->db->limit($limit, $offset); if ($where != '') $this->db->where($where); $this->db->order_by('lms_admin_roles.id', 'DESC'); $query = $this->db->get('lms_admin_roles'); if ($sqlQry != '') $query = $this->db->query($sqlQry); return $query->result_array(); } public function check_role_exists($id, $rname) { $rId = ($this->input->post('id') !== NULL) ? $this->input->post('id') : $id; $rName = ($this->input->post('role_name') !== NULL) ? $this->input->post('role_name') : $rname; $query = $this->db->get_where('lms_admin_roles', array('role_name' => $rName)); if (!empty($query->row_array())) { if ($rId == 0) return false; //Duplicate role_name else { if ($query->row(0)->id != $rId) return false; //Duplicate role_name } } return true; //No duplicate } function add_edit_admin_role($rId = 0, $data) { if ($rId == 0) { $this->db->insert('lms_admin_roles', $data); if ($this->db->affected_rows() > 0) { $insertid = $this->db->insert_id(); return $insertid; } else { return 0; } } else { $this->db->where('id', $rId); $this->db->update('lms_admin_roles', $data); if ($this->db->affected_rows() > 0) return true; else { if ($this->db->trans_status() === FALSE) return 0; return true; } } } /**************************************** ENDDD of Admin Roles - Nandini ************************************/ /**************************************** START of Admin Menu - Nandini ************************************/ public function get_admin_menu_details($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE) { if ($limit) { $this->db->limit($limit, $offset); } if ($where != '') $this->db->where($where); $this->db->order_by('lms_admin_menu.id', 'DESC'); $query = $this->db->get('lms_admin_menu'); if ($sqlQry != '') $query = $this->db->query($sqlQry); return $query->result_array(); } function recursive_menue($parentId = 0) { $this->db->where('parent_id', $parentId); //$this->db->where('is_active', 'Y'); $this->db->order_by('order_number', 'ASC'); $query = $this->db->get('lms_admin_menu'); $result = $query->result_array(); $adminMenu = array(); if (!empty($result)) { foreach ($result as $res) { array_push($adminMenu, $res); } } return $adminMenu; } function get_admin_menu($parentId = 0) { $zeroMenu = $this->recursive_menue(0); $adminMenu = array(); if (!empty($zeroMenu)) { foreach ($zeroMenu as $zero) { $adminMenu[$zero['id']] = $zero; $oneMenu = $this->recursive_menue($zero['id']); if (!empty($oneMenu)) { foreach ($oneMenu as $one) { $adminMenu[$zero['id']][$zero['id']][$one['id']] = $one; $twoMenu = $this->recursive_menue($one['id']); if (!empty($twoMenu)) { foreach ($twoMenu as $two) { $adminMenu[$zero['id']][$zero['id']][$one['id']][$one['id']][$two['id']] = $two; } } } } } } //echo '
';print_r($adminMenu);
		return $adminMenu;
	}

	public function get_lms_admin_role_permissions($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE)
	{
		if ($limit)
			$this->db->limit($limit, $offset);

		if ($where != '')
			$this->db->where($where);

		$this->db->order_by('lms_admin_role_permissions.id', 'DESC');
		$query = $this->db->get('lms_admin_role_permissions');

		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);

		return $query->result_array();
	}

	function add_edit_admin_role_permissions($rpId = 0, $data)
	{
		if ($rpId == 0) {
			$this->db->insert('lms_admin_role_permissions', $data);
			if ($this->db->affected_rows() > 0) {
				$insertid = $this->db->insert_id();
				return  $insertid;
			} else {
				return 0;
			}
		} else {
			$this->db->where('id', $rpId);
			$this->db->update('lms_admin_role_permissions', $data);
			if ($this->db->affected_rows() > 0)
				return true;
			else {
				if ($this->db->trans_status() === FALSE)
					return 0;

				return true;
			}
		}
	}
	/**************************************** END of Admin Menu - Nandini ************************************/

	public function check_emailid_exists($id, $email)
	{
		$uId = ($this->input->post('id') !== NULL) ? $this->input->post('id') : $id;
		$eId = ($this->input->post('email') !== NULL) ? $this->input->post('email') : $email;

		$query = $this->db->get_where('lms_admin', array('email' => $eId));
		if (!empty($query->row_array())) {
			if ($uId == 0)
				return false;											//Duplicate email id
			else {
				if ($query->row(0)->id != $uId)
					return false;										//Duplicate email id				
			}
		}
		return true;													//No duplicate
	}

	function add_edit_admin_user($uId = 0, $data)
	{
		if ($uId == 0) {
			$this->db->insert('lms_admin', $data);
			if ($this->db->affected_rows() > 0) {
				$insertid = $this->db->insert_id();
				return  $insertid;
			} else {
				return 0;
			}
		} else {
			$this->db->where('id', $uId);
			$this->db->update('lms_admin', $data);
			if ($this->db->affected_rows() > 0)
				return true;
			else {
				if ($this->db->trans_status() === FALSE)
					return 0;

				return true;
			}
		}
	}

	public function get_admin_details($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE)
	{
		if ($limit) {
			$this->db->limit($limit, $offset);
		}
		if ($where != '')
			$this->db->where($where);

		$this->db->order_by('lms_admin.id', 'DESC');
		$query = $this->db->get('lms_admin');

		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);

		return $query->result_array();
	}

	public function check_is_emailid_valid($email)
	{
		$query = $this->db->query("SELECT id FROM lms_admin WHERE email='$email'");
		if ($row = $query->row()) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	/*----------------------------- START of Admin User Login Functionality -----------------------------*/
	public function check_login_credentials($email, $encrypt_password)
	{
		$where = 'email="' . $email . '" AND password="' . $encrypt_password . '"';
		$this->db->where($where);
		$result = $this->db->get('lms_admin');

		if ($result->num_rows() == 1) {
			return true;
		} else {
			return false;
		}
	}
	/*----------------------------- ENDDD of Admin User Login Functionality -----------------------------*/

	/*----------------------------- START of Admin User Password Functionality -----------------------------*/
	public function reset_admin_password($uId, $new_password)
	{
		$data = array(
			'password' => md5($new_password)
		);
		$this->db->where('id', $uId);
		return $this->db->update('lms_admin', $data);
	}
	/*----------------------------- ENDDD of Admin User Password Functionality -----------------------------*/
	/*############################################ ENDDD Of Coding By Nandini ############################################*/

	/*############################################ START Of Coding By Keerthi ############################################*/
	//start book
	function getData($table, $where)
	{
		if ($where != '') {
			$this->db->where($where);
			if (strpos($where, "category_id") !== false) {
				return $this->db->get($table)->result_array();
			} elseif (strpos($where, "returned_on_date") !== false) {
				return $this->db->get($table)->result_array();
			} elseif (strpos($where, "eu_unique_id") !== false) {
				return $this->db->get($table)->result_array();
			} elseif (strpos($where, "is_damaged") !== false) {
				return $this->db->get($table)->result_array();
			} elseif (strpos($where, "book_id") !== false) {
				return $this->db->get($table)->result_array();
			} elseif (strpos($where, "is_active") !== false) {
				return $this->db->get($table)->result_array();
			} else {
				return $this->db->get($table)->row_array();
			}
		} else {
			return $this->db->get($table)->result_array();
		}
	}

	function addEditBook($id, $data, $ttlRemQty = '', $ttlBookQty = '')
	{
		if ($id == 0) {
			$data['remaining_quantity'] = $data['total_quantity'];
			$this->db->insert('lms_books', $data);
			if ($this->db->affected_rows() > 0)
				return  $insert_id = $this->db->insert_id();
			else
				return 0;
		} else {
			$data['remaining_quantity'] = $ttlRemQty;
			$data['total_quantity'] = $ttlBookQty;
			$this->db->where('id', $id);
			$this->db->update('lms_books', $data);
			if ($this->db->trans_status() === FALSE)
				return 0;

			return true;
		}
	}
	//end book

	//start settings
	public function addEditGeneralSettings($id, $data)
	{
		if ($id == 0) {
			$this->db->insert('lms_general_settings', $data);
			if ($this->db->affected_rows() > 0)
				return  $insert_id = $this->db->insert_id();
			else
				return 0;
		} else {
			$this->db->where('id', $id);
			$this->db->update('lms_general_settings', $data);
			if ($this->db->trans_status() === FALSE)
				return 0;

			return true;
		}
	}

	public function getGeneralSettingData($table)
	{
		return $this->db->get($table)->row_array();
	}

	public function addEditCatgeoryData($id, $data)
	{
		if ($id == 0) {
			$this->db->insert('lms_categories', $data);
			if ($this->db->affected_rows() > 0)
				return  $insert_id = $this->db->insert_id();
			else
				return 0;
		} else {
			$this->db->where('id', $id);
			$this->db->update('lms_categories', $data);
			if ($this->db->trans_status() === FALSE)
				return 0;

			return true;
		}
	}

	public function deleteData($tableName, $where)
	{
		$this->db->where($where);
		$this->db->delete($tableName);
		return true;
	}
	//end settings

	//start issued book
	public function get_issed_book_data($table = '', $where = '')
	{
		$this->db->order_by('created_on', 'DESC');

		if (!empty($where)) {
			$this->db->where($where);
		}
		return $this->db->get($table)->result_array();
	}

	public function addEditissuedBookData($idVal, $dataArray)
	{
		if ($idVal == 0) {
			for ($i = 0; $i < count($dataArray['book_id']); $i++) {
				$bid = $dataArray['book_id'][$i];
				$euid = $dataArray['eu_unique_id'][$i];
				$i_date = $dataArray['issued_date'][$i];
				$r_date = $dataArray['return_date'][$i];
				$b_health = $dataArray['book_health'][$i];
				$c_date = $dataArray['created_on'];
				$m_date = $dataArray['modified_on'];

				$data = array(
					'book_id' => $bid,
					'eu_unique_id' => $euid,
					'issued_date' => $i_date,
					'return_date' => $r_date,
					'book_health' => $b_health,
					'created_on' => $c_date,
					'modified_on' => $m_date
				);

				// var_dump($data);
				// exit();

				$insert = $this->db->insert('lms_book_transactions', $data);

				$select = $this->db->get_where('lms_books', ['id' => $bid])->row_array();
				if ($select) {
					$oldremain_qty = $select['remaining_quantity'];
					$remaning_qyt = ($oldremain_qty) - (1);

					$this->db->query("UPDATE lms_books SET remaining_quantity='$remaning_qyt' WHERE id=$bid");
				}
			}
			return true;
		}
	}
	//end issued book

	//start retuned book
	public function get_returned_book_data($table = '', $where = '')
	{


		$this->db->order_by('id', 'DESC');
		$this->db->where($where);
		// echo '
';
		// print_r($this->db->where($where));
		// exit();
		return $this->db->get($table)->result_array();
	}

	public function addCommentData($idVal, $dataArray)
	{
		$this->db->where('id', $idVal);
		$this->db->update('lms_book_transactions', $dataArray);
		return true;
	}

	public function addReturnedBook($returnedId)
	{
		foreach ($returnedId as $ridata) {
			$bdata = $this->db->get_where('lms_book_transactions', ['id' => $ridata])->row_array();
			$bookData = $this->db->get_where('lms_books', ['id' => $bdata['book_id']])->row_array();
			$gsData = $this->LmsAdmin_Model->getGeneralSettingData('lms_general_settings');
			if ($gsData)
				$late_fee = $gsData['due_fee_per_day'];

			$fineperday = 0;
			if ($bookData['fine_per_day'] == 0)
				$fineperday = $late_fee;
			else
				$fineperday = $bookData['fine_per_day'];

			//calculating over due days
			/*$today = date('Y-m-d');
            $return_date = $bdata['return_date'];
            $overdue_diff = strtotime($today)-strtotime($return_date);
            $overdue_days = round($overdue_diff / 86400);
            if($today>$return_date){
                $oddays = $overdue_days;
                $latefee = ($fineperday)*($oddays);
            }elseif($today<$return_date){
                $oddays = 00;
                $latefee = 00.00;
            }*/

			$todayDateObj = new DateTime($this->nepali_current_date);
			$today = $todayDateObj->format('Y-m-d');

			$retDateObj = new DateTime($bdata['return_date']);
			$return_date = $retDateObj->format('Y-m-d');

			$date11 = date_create($return_date);
			$date22 = date_create($today);
			$overdue_days = $date11->diff($date22)->format("%r%a");
			if ($today > $return_date) {
				$oddays = $overdue_days;
				$latefee = ($fineperday) * ($oddays);
			} elseif ($today < $return_date) {
				$oddays = 00;
				$latefee = 00.00;
			}

			//return $oddays.'  ==  '.$latefee;
			$dataArray = array(
				'returned_on_date' => $today,
				'total_overdue_days' => $oddays,
				'late_fee_per_day' => $fineperday,
				'total_late_fee' => $latefee
			);

			$this->db->where('id', $ridata);
			$this->db->update('lms_book_transactions', $dataArray);

			$select = $this->db->get_where('lms_books', ['id' => $bdata['book_id']])->row_array();
			if ($select) {
				$bid = $bdata['book_id'];
				$ttl_quantity = $select['total_quantity'];
				$oldremain_qty = $select['remaining_quantity'];
				$remaning_qyt = ($oldremain_qty) + (1);
				if ($remaning_qyt >= $ttl_quantity) {
					$remQty = $ttl_quantity;
				} else {
					$remQty = $remaning_qyt;
				}

				$this->db->query("UPDATE lms_books SET remaining_quantity='$remQty' WHERE id=$bid");
			}
		}
		return true;
	}
	//end returned book

	//start student
	public function get_user_data($table)
	{
		$CI = &get_instance();
		$ecDB = $CI->load->database('erisnClassroom', TRUE);
		// $query = $ecDB->query("Select s.name,s.email,s.studentId,c.classroom_name as class_name, b.b_name from students s 
		// INNER JOIN student_batch sb on s.id = sb.student_id AND s.batch_id = sb.batch_id
		// INNER JOIN classroom c on c.id = sb.classroom_id
		// INNER JOIN batch b on b.id = s.batch_id
		// ORDER BY sb.id DESC");
		// $query = $ecDB->query("SELECT c.id,c.name, c.studentId,c.mobile,(select classroom_name from classroom a join student_batch b on a.id=b.classroom_id WHERE b.student_id=c.id) as class_name FROM students c ORDER BY c.id");
		$stdSql = $ecDB->query("SELECT s.id,s.studentId,s.name,s.course,s.mobile,s.batch_id,cl.section_id,sb.roll_no,c.course_name,se.section_name 
					FROM students s 
					JOIN student_batch sb ON sb.student_id=s.id AND s.batch_id=sb.batch_id 
					JOIN classroom cl ON cl.id = sb.classroom_id AND s.course=cl.course_id 
					JOIN section se ON cl.section_id=se.id 
					JOIN course c ON c.id=cl.course_id ORDER BY sb.roll_no ASC");
		return $stdSql->result_array();
	}

	public function getStudentInfo($stdId)
	{
		$CI = &get_instance();
		$ecDB = $CI->load->database('erisnClassroom', TRUE);
		$query = $ecDB->query("SELECT c.*,(select classroom_name from classroom a join student_batch b on a.id=b.classroom_id WHERE b.student_id=c.id) as class_name FROM students c WHERE c.id='$stdId'");
		return $query->row_array();
	}

	public function getStudentDataById($stdIs)
	{
		$CI = &get_instance();
		$ecDB = $CI->load->database('erisnClassroom', TRUE);
		$query = $ecDB->query("SELECT c.studentId,c.id,c.mobile,c.name,(select classroom_name from classroom a join student_batch b on a.id=b.classroom_id WHERE b.student_id=c.id) as class_name FROM students c WHERE c.studentId='$stdIs'");
		return $query->row_array();
	}
	//end student
	/*############################################ ENDDD Of Coding By Keerthi ############################################*/
}