Your IP : 216.73.216.91


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

<?php
class Student extends BaseController implements IValidateParams{
	public function createOne($data){
		unset($data['id']);
		$data = $this->unsetArrayKeys($data, ['father_cnic', 'father_name', 'mother_name', 'children_count', 'zakat']);
		$data = $this->handleEmptyDate($data, ['donation_date', 'donation_expiry', 'stu_dob']);
		$this->response = $this->insert(Config::getTable('student'), $data, false);
		return $this->response;
	}
	public function getOneByGrNumber($data){
		$table = Config::getTable('student');
		$tableFam = Config::getTable('family');
		$tableDon = Config::getTable('donor');
		// $this->response = $this->select(Config::getTable('student'), ["*"], "WHERE gr_num = ?", [$data['gr_num']]);
		// DATE_FORMAT(donation_expiry, "%m/%d/%Y") as donation_expiry
		$qry = <<<QRY
			SELECT 
				s.id, s.id as student_id, s.family_id, s.stu_full_name, s.stu_form_b, s.gr_num, s.auto_gr_num, s.class, f.zakat, s.stu_dob, s.stu_gender,
				CONCAT(FLOOR((TIMESTAMPDIFF(MONTH, stu_dob, CURDATE()) / 12)), 'Y ', MOD(TIMESTAMPDIFF(MONTH, stu_dob, CURDATE()), 12) , 'M') AS 'stu_age', 
				DATE_FORMAT(s.donation_expiry, "%Y-%m-%d") as donation_expiry, 
				DATE_FORMAT(s.donation_date, "%Y-%m-%d") as donation_date,
				f.father_cnic, f.father_name, f.mother_name, f.children_count, 
				d.donor_name, s.donor_id 
					FROM {$table} s
				inner join {$tableFam} f
					on f.id = s.family_id
				left join {$tableDon} d
					on d.id = s.donor_id
				where s.gr_num = ?
		QRY;
		$this->response = $this->customSelect($qry, [$data['gr_num']]);
		return $this->response;
	}
	public function getServiceById($data){
		$table = Config::getTable('student');
		$tableStuSer = Config::getTable('student_service');
		$qry = <<<QRY
			SELECT 
				ss.*, 
				s.stu_full_name, 
				s.gr_num, 
				s.auto_gr_num, 
				s.class, 
				s.stu_dob,
				s.stu_gender
			FROM {$tableStuSer} ss
			INNER JOIN {$table} s 
				on s.id = ss.student_id
			WHERE ss.id = ?
		QRY;
		$this->response = $this->customSelect($qry, [$data['service_id']]);
		return $this->response;
	}
	public function getStudentsByClass($data){
		$table = Config::getTable('student');
		$qry = <<<QRY
			SELECT 
				s.id, s.id as student_id, s.stu_full_name, s.gr_num, s.auto_gr_num
			FROM {$table} s
			where s.class = ?
		QRY;
		$this->response = $this->customSelect($qry, [$data['class']]);
		return $this->response;
	}
	public function updateOne($data){
		$id = $data['id'];
		$data = $this->unsetArrayKeys($data, ['father_cnic', 'father_name', 'mother_name', 'children_count', 'zakat']);
		$data = $this->handleEmptyDate($data, ['donation_date', 'donation_expiry', 'stu_dob']);
		$this->response = $this->update(Config::getTable('student'), $data, "WHERE id = ?", [$id]);
		return $this->response;
	}
	public function getAll($data){
		$table = Config::getTable('student');		
		$table_fam = Config::getTable('family');		
		$table_don = Config::getTable('donor');	
		$DTTotalCountQry = <<<QRY
				SELECT 
					count(*) as count
						FROM {$table}
					left join {$table_fam}
						on {$table_fam}.id = {$table}.family_id
					left join {$table_don}
						on {$table_don}.id = {$table}.donor_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'," ,
				"student||stu_days_expired" => "CONCAT(DATEDIFF(donation_expiry, CURDATE()), ' D') AS 'student||stu_days_expired'," ,
			];
			$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_full_name as '{$table}||stu_full_name', 
					{$table}.stu_form_b as '{$table}||stu_form_b', 
					{$table}.stu_dob as '{$table}||stu_dob', 
					CONCAT(FLOOR((TIMESTAMPDIFF(MONTH, stu_dob, CURDATE()) / 12)), 'Y ', MOD(TIMESTAMPDIFF(MONTH, stu_dob, CURDATE()), 12) , 'M') AS '{$table}||stu_age', 
					{$table}.stu_gender as '{$table}||stu_gender', 
					{$table}.gr_num as '{$table}||gr_num', 
					{$table}.auto_gr_num as '{$table}||auto_gr_num', 
					{$table}.class as '{$table}||class', 
					DATE_FORMAT({$table}.donation_expiry, "%Y-%m-%d") as '{$table}||donation_expiry', 
					DATE_FORMAT({$table}.donation_date, "%Y-%m-%d") as '{$table}||donation_date', 
					CONCAT(DATEDIFF(donation_expiry, CURDATE()), ' D') AS '{$table}||stu_days_expired', 
					{$table}.family_id as '{$table}||family_id', 
					{$table_fam}.zakat as '{$table_fam}||zakat', 
					{$table_fam}.father_cnic as '{$table_fam}||father_cnic', 
					{$table_fam}.father_name as '{$table_fam}||father_name', 
					{$table_fam}.father_phone as '{$table_fam}||father_phone', 
					{$table_fam}.mother_name as '{$table_fam}||mother_name', 
					{$table_fam}.emerg_name as '{$table_fam}||emerg_name', 
					{$table_fam}.emerg_relation as '{$table_fam}||emerg_relation', 
					{$table_fam}.emerg_num as '{$table_fam}||emerg_num', 
					{$table_fam}.children_count as '{$table_fam}||children_count', 
					{$table_don}.donor_name as '{$table_don}||donor_name',
					{$table_don}.donor_ref_name as '{$table_don}||donor_ref_name'
						FROM {$table}
					left join {$table_fam}
						on {$table_fam}.id = {$table}.family_id
					left join {$table_don}
						on {$table_don}.id = {$table}.donor_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_fam}
						on {$table_fam}.id = {$table}.family_id
					left join {$table_don}
						on {$table_don}.id = {$table}.donor_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 getAllServices($data){
		$table = Config::getTable('student');		
		$tableStuSer = Config::getTable('student_service');		
		$DTTotalCountQry = <<<QRY
				SELECT 
					count(*) as count
						FROM {$tableStuSer}
					left join {$table}
						on {$table}.id = {$tableStuSer}.student_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'])){
			$gs = $this->globalSearch($data, $tableStuSer);
			$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 
					{$tableStuSer}.id as '{$tableStuSer}||id',
					{$tableStuSer}.id as '{$tableStuSer}||service_id',
					{$tableStuSer}.service as '{$tableStuSer}||service',
					{$tableStuSer}.date as '{$tableStuSer}||date', 
					{$table}.stu_full_name as '{$table}||stu_full_name', 
					{$table}.gr_num as '{$table}||gr_num', 
					{$table}.auto_gr_num as '{$table}||auto_gr_num', 
					{$table}.class as '{$table}||class', 
					{$table}.stu_dob as '{$table}||stu_dob',
					{$table}.stu_gender as '{$table}||stu_gender'
				FROM {$tableStuSer}
				INNER JOIN {$table}
					on {$table}.id = {$tableStuSer}.student_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 {$tableStuSer}
					left join {$table}
						on {$table}.id = {$tableStuSer}.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 createService($data){
		$tableStuSer = Config::getTable('student_service');
		$data = $this->unsetArrayKeys($data, ['service_id', 'auto_gr_num', 'gr_num', 'class', 'stu_full_name', 'stu_dob', 'stu_gender']);
		$this->response = $this->insert($tableStuSer, $data);
		return $this->response;
	}
	public function updateService($data){
		$tableStuSer = Config::getTable('student_service');
		$id = $data['service_id'];
		$data = $this->unsetArrayKeys($data, ['service_id', 'auto_gr_num', 'gr_num', 'class', 'stu_full_name', 'stu_dob', 'stu_gender']);
		$this->response = $this->update($tableStuSer, $data, "WHERE id = ?", [$id]);
		return $this->response;
	}
	public function getAllClasses($data){
		$table = Config::getTable('student');
		$qry = <<<QRY
			SELECT distinct(class) FROM {$table} order by class ASC
		QRY;
		$this->response = $this->customSelect($qry);
		return $this->response;
	}
	public function validateRequestsParams(){
		$this->mendetoryParamsAgainstEachMethod = [
			'createOne' => [
				"stu_full_name",
				"gr_num",
				"auto_gr_num",
				"class",
				"family_id"
			],
			'updateOne' => [
				"stu_full_name",
				"gr_num",
				"auto_gr_num",
				"class",
				"family_id"
			],
			'createService' => [
				'student_id',
				'service',
				'date'
			],
			'updateService' => [
				'student_id',
				'service',
				'date'
			],
			'getOneByGrNumber' => ['gr_num'],
			'getServiceById' => ['service_id'],
			'getStudentsByClass' => ['class'],
			'getAll' => [],
			'getAllServices' => [],
			'getAllClasses' => [],
		];
		return $this->mendetoryParamsAgainstEachMethod;
	}
}
?>