Your IP : 216.73.216.91


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

<?php
class Attendance extends BaseController implements IValidateParams{
	public function createOne($data){
		$table = Config::getTable('attendance');
		unset($data['id']);
		$values = '';
		$class = $data['class'];
		$date = $data['date'];
		$data = $this->unsetArrayKeys($data, ['h_date', 'h_class','id', 'class', 'date']);
		foreach($data as $key => $val){
			$values .= "('{$key}','{$class}','{$date}','{$val}'),";
		}
		$values = trim($values, ',');
		$qry = <<<QRY
			INSERT INTO {$table}
				(student_id, class, date, attendance)
			VALUES
				{$values}
				
		QRY;
		$this->response = $this->executeqry($qry);
		return $this->response;
	}

	public function getOneByClassAndDate($data){
		$table = Config::getTable('attendance');
		$tableStu = Config::getTable('student');
		$qry = <<<QRY
			SELECT 
				a.*, a.student_id as id, s.stu_full_name, s.gr_num, s.auto_gr_num
					FROM {$table} a
				INNER JOIN {$tableStu} s
					ON s.id = a.student_id
				WHERE 
					a.class = ? AND a.date = ?
		QRY;
		$this->response = $this->customSelect($qry, [$data['class'], $data['date']]);
		return $this->response;
	}

	public function getAll($data){
		$table = Config::getTable('attendance');		
		$DTTotalCountQry = <<<QRY
				SELECT 
				count(*)
			FROM 
				{$table}
			group by 
				class, `date`;
		QRY;
		$DTTotalCount = json_decode(json_encode($this->customSelect(trim($DTTotalCountQry))), true)['rowsAffected'];
		if(!empty($data['search']['value'])){
			$qry = <<<QRY
					SELECT 
					count(student_id) as student_count, 
					SUM(case when attendance = 'A' then 1 else 0 end) as absent,
					SUM(case when attendance = 'L' then 1 else 0 end) as `leave`,
					SUM(case when attendance = 'P' then 1 else 0 end) as present,
					class, 
					`date` 
				FROM 
					attendance
				group by 
					class, `date`
			QRY;
			$gs = $this->globalSearch($data, $table, [], $qry);
			$this->response = $this->customSelect($gs['paging']);
			$DTFilteredCount = $this->customSelect($gs['where'])['rowsAffected'];
			$DTResponse = [ 
				'draw' => intval($data['draw']),
				'recordsTotal' => $DTTotalCount ?? 0,
				'recordsFiltered' => $DTFilteredCount ?? 0,
				'data' => $this->response['result'] ?? [],
			];
		}else{
			$transform = $this->transformDTQuery($data, true, ['absent', 'present', 'student_count']);
			$DTDataQry = <<<QRY
					SELECT 
					count(student_id) as student_count, 
					SUM(case when attendance = 'A' then 1 else 0 end) as absent,
					SUM(case when attendance = 'L' then 1 else 0 end) as `leave`,
					SUM(case when attendance = 'P' then 1 else 0 end) as present,
					class, 
					`date` 
				FROM 
					{$table}
				{$transform['where']}
				group by 
					class, `date`
				{$transform['having']}
				{$transform['order']}
				{$transform['paging']}
			QRY;
			$this->response = $this->customSelect(trim($DTDataQry));
			$DTFilteredCountQry = <<<QRY
						SELECT 
						count(*) as count
					FROM 
						{$table}
					{$transform['where']}
					group by 
						class, `date`
					{$transform['having']}
					{$transform['order']}
			QRY;
			$DTFilteredCount = json_decode(json_encode($this->customSelect(trim($DTFilteredCountQry))), true)['rowsAffected'] ?? 0;
			$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 updateOne($data){
		$table = Config::getTable('attendance');
		$rowsAffected = 0;
		$count = 0;
		foreach($data as $k => $v){
			if(!in_array($k, ['class', 'date'])){
				$qry = "UPDATE {$table} set attendance = '{$v}' WHERE class = '{$data['class']}' AND date = '{$data['date']}' AND student_id = {$k}";
				$tmp = $this->executeqry($qry);
				if($tmp['status'] == 'success') {
					$rowsAffected++;
				}
				$responses[] = $tmp;
				$count++;
			}
		}
		$this->response['status'] = 'success';
		$this->response['rowsAffected'] = "{$rowsAffected}/{$count}";
		// $this->response['all'] = $responses;
		return $this->response;
	}
	
	public function validateRequestsParams(){
		$this->mendetoryParamsAgainstEachMethod = [
			'createOne' => ['class', 'date'],
			'getOneByClassAndDate' => ['class', 'date'],
			'updateOne' => ['class', 'date'],
			'getAll' => [],
			// 'getOneByGrNumber' => ['gr_num'],
			// 'getAll' => [],
		];
		return $this->mendetoryParamsAgainstEachMethod;
	}
}
?>