EVOLUTION-NINJA
Edit File: Batches.php
<?php namespace App\Controllers; class Batches extends BaseController { public function add_batch_form() { $data=$this->request->getVar(); $db=\Config\Database::connect(); $adno_model = $db->table('batches'); $adno_model->select('batch_no'); $adno_model->like('batch_no', 'GTA'); $adno_data = $adno_model->get()->getResultArray(); $last = end($adno_data); if ($last) { $last_admission_no = $last['batch_no']; $ad1 = intval(substr($last_admission_no, 3)) + 1; $ad_no = 'GTA' . str_pad($ad1, 2, '0', STR_PAD_LEFT); } else { $ad_no = 'GTA01'; } $data['batch_no']=$ad_no; $data['created_at']=date('Y-m-d H:i:s'); $data['created_by']=session()->get('username'); $builder=$db->table('batches'); $insert=$builder->insert($data); if($insert){ return $this->response->setJSON(['result'=>1,'message'=>'Batch Added Successfully']); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed Add Batch']); } } public function batches_list() { $db = \Config\Database::connect(); $builder = $db->table('batches'); $builder->select('batches.*, header.header_name, header_value.value_name'); $builder->join('header','header.id = batches.header'); $builder->join('header_value','header_value.id = batches.value'); $course = $this->request->getGet('course'); if ($course) { $builder->where('batches.header', $course); } $data = $builder->get()->getResultArray(); if ($data) { return $this->response->setJSON($data); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Failed to load data']); } } public function student_list_data_batch() { $id=session()->get('id'); if($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','0'); $data=$builder->get()->getResultArray(); // echo 'hello'; // print_r($data);die(); if($data){ return $this->response->setJSON($data); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to load data']); } }else{ // echo 'hello';die(); return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } public function students_from_batch() { $id=session()->get('id'); if($id){ $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($data); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to load data']); } }else{ // echo 'hello';die(); return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } public function add_student_batch() { $login_id=session()->get('id'); if($login_id){ $db=\Config\Database::connect(); $id=$this->request->getVar('id'); $builder=$db->table('batches'); $builder->where('id',$id); $name=$builder->get()->getRowArray(); $data['batch_no']=$name['batch_no']; $data['id']=$id; return view('batch/add_student_batch' ,$data); }else{ return redirect()->to('/'); } } public function remove_student_batch() { $login_id=session()->get('id'); if($login_id){ $db=\Config\Database::connect(); $id=$this->request->getVar('id'); $builder=$db->table('batches'); $builder->where('id',$id); $name=$builder->get()->getRowArray(); $data['batch_no']=$name['batch_no']; $data['id']=$id; return view('batch/remove_student_batch' ,$data); }else{ return redirect()->to('/'); } } public function students_for_batch() { $students = $this->request->getVar('students[]'); $batch_id = $this->request->getVar('batch_id'); $count=0; foreach ($students as $value) { // echo $value;die(); if($value!==''){ $db=\Config\Database::connect(); $builder=$db->table('student_list'); $builder->where('id',$value); $builder->update( ['batch_no' => $batch_id]); $count++; } } if($count>0){ return $this->response->setJSON(['result' => 1, 'message' => 'Students added to batch successfully']); } else { return $this->response->setJSON(['result' => 0, 'message' => 'No students selected or batch ID missing']); } } public function remove_from_batch() { $id=$this->request->getVar('id'); $db=\Config\Database::connect(); $builder=$db->table('student_list'); $builder->where('id',$id); $update=$builder->update( ['batch_no' =>0]); if($update){ return $this->response->setJSON(['result' => 1, 'message' => 'Student Removed successfully']); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Failed to remove']); } } public function batch_details() { $id=$this->request->getVar('id'); $db=\Config\Database::connect(); $builder=$db->table('student_list'); $builder->select('student_list.admission_no,student_list.first_name,student_list.last_name'); $builder->where('batch_no',$id); $students=$builder->get()->getResultArray(); $builder=$db->table('batches'); $builder->select('batches.*,employee_details.first_name as trainer_name'); $builder->join('employee_details', 'employee_details.id = batches.trainer', 'left'); $builder->where('batches.id',$id); $batch=$builder->get()->getRowArray(); $count=$db->table('student_list')->where('batch_no',$id)->countAllResults(); $batch['count']=$count; if (!empty($batch)) { return $this->response->setJSON(['result' => 1, 'data' => $batch,'table'=>$students]); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Something went wrong']); } } public function edit_batch() { $id=session()->get('id'); if($id){ $bid=$this->request->getVar('id'); // echo $sid;die(); $db=\Config\Database::connect(); $builder=$db->table('batches'); $builder->where('id',$bid); $data=$builder->get()->getRowArray(); // print_r($data);die(); // $data['value']=$data; return view('batch/edit_batch',$data); }else{ return $this->response->setJSON(['result'=>0,'message'=>'please login']); } } public function update_batch_form() { $data=$this->request->getVar(); $id=$data['id']; $db=\Config\Database::connect(); $builder=$db->table('batches'); $builder->where('id',$id); $insert=$builder->update($data); if($insert){ return $this->response->setJSON(['result'=>1,'message'=>'Batch Updated Successfully']); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed Updated Batch']); } } public function trainer_data() { $id=session()->get('id'); if($id){ $db=\Config\Database::connect(); $builder=$db->table('employee_details'); $builder->select('employee_details.id,employee_details.first_name'); $data=$builder->get()->getResultArray(); if($data){ return $this->response->setJSON(['result'=>1,'data'=>$data]); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to load data']); } } } } ?>