Your IP : 216.73.216.91


Current Path : /var/www/html/llcfapp/api/controller/
Upload File :
Current File : /var/www/html/llcfapp/api/controller/Health.php

<?php
class Health extends BaseController implements IValidateParams{
	public function createOne($data){
		$data = $this->unsetArrayKeys($data, ['id', 'gr_num', 'auto_gr_num', 'stu_full_name', 'stu_dob', 'stu_age', 'stu_gender']);
		$data = $this->handleEmptyFields($data);
		$this->response = $this->insert(Config::getTable('health'), $data, false);
		return $this->response;
	}
	public function getOneById($data){
		// $this->response = $this->select(Config::getTable('health'), ["*"], "WHERE id = ?", [$data['id']]);
		$table = Config::getTable('health');		
		$table_stu = Config::getTable('student');		
		$table_fam = Config::getTable('family');		
		$qry = <<<QRY
			SELECT 
				s.id as student_id, s.stu_full_name, s.gr_num, s.auto_gr_num,
				DATE_FORMAT(s.stu_dob, "%Y-%m-%d") as stu_dob, 
				CONCAT(FLOOR((TIMESTAMPDIFF(MONTH, stu_dob, CURDATE()) / 12)), 'Y ', MOD(TIMESTAMPDIFF(MONTH, stu_dob, CURDATE()), 12) , 'M') AS 'stu_age', 
				s.stu_gender,
				f.father_name,
				h.*,
				DATE_FORMAT(h.checkup_date, "%Y-%m-%d") as 'checkup_date'
					FROM {$table} h
				inner join {$table_stu} s
					on s.id = h.student_id
				inner join {$table_fam} f
					on f.id = s.family_id
				where h.id = ?
		QRY;
		$this->response = $this->customSelect($qry, [$data['id']]);
		return $this->response;
	}
	public function updateOne($data){
		$id = $data['id'];
		$data = $this->unsetArrayKeys($data, ['id', 'gr_num', 'auto_gr_num', 'stu_full_name', 'stu_dob', 'stu_age']);
		$this->response = $this->update(Config::getTable('health'), $data, "WHERE id = ?", [$id]);
		return $this->response;
	}
	public function getAll($data){
		$data = $this->unsetArrayKeys($data, ['stu_age']);
		$table = Config::getTable('health');		
		$table_stu = Config::getTable('student');		
		$table_fam = Config::getTable('family');		
		$DTTotalCountQry = <<<QRY
				SELECT 
					count(*) as count
						FROM {$table}
					left join {$table_stu}
						on {$table_stu}.id = {$table}.student_id
					left join {$table_fam}
						on {$table_fam}.id = {$table_stu}.family_id
			QRY;
		$DTTotalCount = $this->customSelect($DTTotalCountQry)['result'][0]->count;
		// $DTTotalCount = $this->customSelect("select count(*) as count from {$table}")['result'][0]->count;
		if(!empty($data['search']['value'])){
			$calCols = [
				"student||stu_age" => "CONCAT(FLOOR((TIMESTAMPDIFF(MONTH, stu_dob, CURDATE()) / 12)), 'Y ', MOD(TIMESTAMPDIFF(MONTH, stu_dob, CURDATE()), 12) , 'M') AS 'student||stu_age',",
				"health||eye_check" => "CONCAT({$table}.eye_left, ' x ', {$table}.eye_right) as '{$table}||eye_check'" 
			];
			$gs = $this->globalSearch($data, $table, $calCols);
			$this->response = $this->customSelect($gs['paging']);
			$this->response = $this->prefixColumnWithTableAndMapReqParamInResponse($data['columns'], $this->response);
			$DTFilteredCount = $this->customSelect($gs['where'])['rowsAffected'];
			$DTResponse = [ 
				'draw' => intval($data['draw']),
				'recordsTotal' => $DTTotalCount ?? 0,
				'recordsFiltered' => $DTFilteredCount ?? 0,
				'data' => $this->response['result'] ?? [],
			];
		}else{
			$DTDataQry = <<<QRY
				SELECT 
					{$table}.id as '{$table}||id', 
					{$table_stu}.gr_num as '{$table_stu}||gr_num', 
					{$table_stu}.auto_gr_num as '{$table_stu}||auto_gr_num', 
					{$table_stu}.stu_full_name as '{$table_stu}||stu_full_name', 
					{$table_fam}.father_name as '{$table_fam}||father_name', 
					DATE_FORMAT({$table_stu}.stu_dob, "%Y-%m-%d") as '{$table_stu}||stu_dob', 
					CONCAT(FLOOR((TIMESTAMPDIFF(MONTH, {$table_stu}.stu_dob, CURDATE()) / 12)), 'Y ', MOD(TIMESTAMPDIFF(MONTH, {$table_stu}.stu_dob, CURDATE()), 12) , 'M') AS '{$table_stu}||stu_age', 
					{$table_stu}.stu_gender as '{$table_stu}||stu_gender', 
					DATE_FORMAT({$table}.checkup_date, "%Y-%m-%d") as '{$table}||checkup_date', 
					{$table}.pulse as '{$table}||pulse', 
					{$table}.body_temp as '{$table}||body_temp', 
					{$table}.respiration as '{$table}||respiration', 
					{$table}.bp as '{$table}||bp', 
					{$table}.height_cm as '{$table}||height_cm', 
					{$table}.weight as '{$table}||weight', 
					{$table}.bmi as '{$table}||bmi', 
					{$table}.bmi_percentile as '{$table}||bmi_percentile', 
					CONCAT({$table}.eye_left, ' x ', {$table}.eye_right) as '{$table}||eye_check', 
					{$table}.pallor as '{$table}||pallor', 
					{$table}.lice as '{$table}||lice', 
					{$table}.consciousness as '{$table}||consciousness', 
					{$table}.diet as '{$table}||diet', 
					{$table}.teeth as '{$table}||teeth', 
					{$table}.history as '{$table}||history', 
					{$table}.diagnosis as '{$table}||diagnosis', 
					{$table}.management as '{$table}||management', 
					{$table}.advice as '{$table}||advice', 
					{$table}.refer as '{$table}||refer', 
					{$table}.followup as '{$table}||followup', 
					{$table}.session as '{$table}||session'
						FROM {$table}
					left join {$table_stu}
						on {$table_stu}.id = {$table}.student_id
					left join {$table_fam}
						on {$table_fam}.id = {$table_stu}.family_id
					{$this->transformDTQuery($data)['paging']}
			QRY;
			// $DTDataQry = "Select *, id as DT_RowId from {$table} {$this->transformDTQuery($data)['paging']}";
			$this->response = $this->customSelect(trim($DTDataQry));

			$DTFilteredCountQry = <<<QRY
				SELECT 
					count(*) as count
						FROM {$table}
					left join {$table_stu}
						on {$table_stu}.id = {$table}.student_id
					{$this->transformDTQuery($data)['where']}
			QRY;
			$DTFilteredCount = $this->customSelect(trim($DTFilteredCountQry))['result'][0]->count;
			// $DTFilteredCount = $this->customSelect("select count(*) as count from {$table} {$this->transformDTQuery($data)['where']}")['result'][0]->count;
			$DTResponse = [ 
				'draw' => intval($data['draw']),
				'recordsTotal' => $DTTotalCount ?? 0,
				'recordsFiltered' => $DTFilteredCount ?? 0,
				'data' => $this->response['result'] ?? [],
			];
		}
		if($this->response['status'] == 'failure'){
			$DTResponse['error'] = $this->response['msg'];
		}
		return $DTResponse;
	}
	public function validateRequestsParams(){
		$this->mendetoryParamsAgainstEachMethod = [
			'createOne' => [
				"student_id", 
				"family_id", 
				"checkup_date",
				// "pulse",
				// "body_temp",
				// "respiration",
				// "bp",
				// "height_cm",
				// "weight",
				// "bmi",
				// "bmi_percentile",
				// "eye_left",
				// "eye_right",
				// "history",
				// "diagnosis",
				// "management",
				// "advice",
				// "refer",
				// "followup",
				// "session",
			],
			'updateOne' => [
				"student_id",
				"family_id", 
				"checkup_date",
				// "pulse",
				// "body_temp",
				// "respiration",
				// "bp",
				// "height_cm",
				// "weight",
				// "bmi",
				// "bmi_percentile",
				// "eye_left",
				// "eye_right",
				// "history",
				// "diagnosis",
				// "management",
				// "advice",
				// "refer",
				// "followup",
				// "session",
			],
			'getOneById' => ['id'],
			'getAll' => [],
		];
		return $this->mendetoryParamsAgainstEachMethod;
	}
}
?>