EVOLUTION-NINJA
Edit File: Attendance_new.php
<?php namespace App\Controllers; class Attendance_new extends BaseController { public function get_batch_attendance() { $id=session()->get('id'); if($id){ $db=\Config\Database::connect(); $builder=$db->table('batches'); // $builder->where('time_table',0); $data=$builder->get()->getResultArray(); if($data){ return $this->response->setJSON(['result'=>1,'data'=>$data]); }else{ return $this->response->setJSON(['result'=>0,'message'=>'No data ']); } }else{ return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } public function get_students_table() { $batch_id=$this->request->getVar('id'); $db=\Config\Database::connect(); $builder=$db->table('student_list'); $builder->select('student_list.*,header.header_name as header,header_value.value_name as value'); $builder->join('header','header.id=student_list.header_name'); $builder->join('header_value','header_value.id=student_list.value_name'); $builder->where('student_list.delete_status','1'); $builder->where('student_list.batch_no',$batch_id); $data=$builder->get()->getResultArray(); // echo 'hello'; // print_r($data);die(); if($data){ return $this->response->setJSON(['result'=>1,'data'=>$data]); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to load data']); } } public function attendance_form() { $batch_id = $this->request->getVar('batch_id'); $date = $this->request->getVar('date'); $students = $this->request->getVar('students'); if ($batch_id && $date && $students && is_array($students)) { $db = \Config\Database::connect(); $builder = $db->table('attendance'); foreach ($students as $student_id => $status) { $data = [ 's_id' => $student_id, 'b_id' => $batch_id, 'date' => $date, 'status' => $status, 'created_at' => date('Y-m-d'), ]; $builder->insert($data); } return $this->response->setJSON(['result' => 1, 'message' => 'Attendance recorded successfully']); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Invalid data provided']); } } public function get_students_table_attendance() { $batch_id = $this->request->getVar('batch_id'); $toDate = date('Y-m-d'); $fromDate = date('Y-m-d', strtotime('-7 days', strtotime($toDate))); $fromdate = $this->request->getPost('fromdate'); $todate = $this->request->getPost('todate'); if (!$fromdate || !$todate) { $fromdate = $fromDate; $todate = $toDate; } $db = \Config\Database::connect(); $builder = $db->table('attendance'); $builder->select('attendance.s_id, attendance.date, attendance.status,student_list.first_name'); $builder->join('student_list', 'student_list.id = attendance.s_id'); $builder->where('attendance.date >=', $fromdate); $builder->where('attendance.date <=', $todate); $builder->where('attendance.b_id', $batch_id); $data = $builder->get()->getResultArray(); $attendanceData = []; foreach ($data as $row) { $attendanceData[$row['first_name']][$row['date']] = $row['status']; } $responseData = []; foreach ($attendanceData as $studentId => $attendance) { $studentData = [ 'student_id' => $studentId, 'attendance' => $attendance ]; array_push($responseData, $studentData); } if($responseData){ return $this->response->setJSON(['result'=>1,'data'=>$responseData]); }else{ return $this->response->setJSON(['result'=>0,'message'=>'No data found']); } } } ?>