alloted groups are duplicated
I am building this product for practice and I can not fix a problem. I am selecting members from the database and I can not call them uniquely. It is a model for attendance and it does not get unique once I call "allotted groups." I would appreciate it if someone helps me get unique when trying to take the attendance with members in the database.
Code is shown below:
id;
$alloted_groups = user()->alloted_groups;
$alloted_groups = str_replace(",","|",$alloted_groups);
$attendance_cas_group = $this->session->userdata('attendance_cas_group');
if($attendance_cas_group){
$alloted_groups = $attendance_cas_group;
// first check if attendance is taken for the group
$today_attendance = $this->db->where(['attendance_date' => date('Y-m-d'),'teacher_id'=>$teacher_id,'cas_item_id'=>$alloted_groups])->from('attendance')->count_all_results();
if($today_attendance > 0){
$records = [
'data' => [],
'message' => 'You have already taken attendance for this group students'
];
return $records;
}
$query = "SELECT s.*,c.selected_cas_items FROM app_students AS s";
$query .= " LEFT JOIN app_cas_submissions AS c ON s.id = c.student_id";
$query .= " LEFT JOIN app_attendance AS a ON a.student_ids = s.id";
//$query .= " WHERE FIND_IN_SET(" .$alloted_groups. ",selected_cas_items)";
$query .= ' WHERE CONCAT(",", selected_cas_items, ",") REGEXP ",('.$alloted_groups.'),"';
//$query .= ' WHERE c.selected_cas_items LIKE "%'.$alloted_groups.'%"';
if($id){
$query .= " AND a.id =".$id;
}else{
//$query .= " AND a.id IS NULL";
}
$records = $this->db->query($query)->result_array();
if(count($records) == 0){
$records = [
'data' => [],
'message' => 'There is no student in your group'
];
}else{
$records = [
'data' => $records,
'message' => ''
];
}
return $records;
}else{
$records = [
'data' => [],
'message' => 'Please select the CAS group first'
];
}
return $records;
}
public function get($id)
{
$current_date = date('Y-m-d');
$teacher_id = user()->id;
$attendance = $this->db->where(['id'=>$id])->get('attendance')->row();
if($attendance)
{
return $attendance;
}else{
return tableObject("attendance");
}
}
public function save($data)
{
$this->db->insert_batch("attendance",$data);
return $this->db->insert_id();
}
public function update($data)
{
$this->db->update_batch("attendance",$data,'id');
return $this->db->affected_rows();
}
public function getItems()
{
$user_id = user()->id;
$account_type = user()->account_type;
$start = (int)$this->input->post("start");
$length = (int)$this->input->post("length");
$length = ($length)?$length:10;
$columns = $this->input->post("columns");
$order = $this->input->post("order");
$order_by = (isset($columns[$order[0]["column"]]))?$columns[$order[0]["column"]]['data']:"id";
$direction = (isset($order[0]["dir"]))?$order[0]["dir"]:"desc";
$teacher_id = user()->id;
$this->db->select("a.*,IF(teacher_id = ".$user_id.", '1', '0') AS edit_attendance")->from("attendance as a");
if($account_type == 'teacher')
{
$this->db->where("teacher_id",$teacher_id);
}else if($account_type == 'student'){
$this->db->where("FIND_IN_SET(".$user_id.",student_ids)",1);
}
if($this->input->post('cas_items')){
$this->db->where("cas_item_id",(int)$this->input->post('cas_items'));
}
if($this->input->post('attendance_date')){
$this->db->where(["a.attendance_date"=>date('Y-m-d',strtotime($this->input->post('attendance_date')))]);
}
$search = $this->input->post("search");
if(isset($search["value"]) && !empty($search["value"]))
{
$this->db->where(["a.attendance_date"=>date('Y-m-d',strtotime($search["value"]))]);
}
if($account_type == 'teacher'){
$attendance_cas_group = $this->session->userdata('attendance_cas_group');
if($attendance_cas_group){
$this->db->where("cas_item_id",(int)$attendance_cas_group);
}
}
//$this->db->join('cas_submissions as s','s.student_id=a.student_ids');
$this->db->order_by($order_by." ".$direction);
$this->db->limit($length,$start);
$records = $this->db->get()->result_array();
if(count($records) > 0)
{
foreach($records as $key => &$val)
{
$student_ids = explode(",",$val['student_ids']);
$list = [];
foreach($student_ids as $k => $v){
$student = $this->db->where(['id'=>$v])->get('students')->row();
if($student)
{
if($account_type == 'student'){
if($student->id == $user_id){
$list[] = $student->first_name . ' ' . $student->last_name;
}
}else{
$list[] = $student->first_name . ' ' . $student->last_name;
}
}
}
$val['student_ids'] = implode(",",$list);
}
}
return $records;
}
public function get_total_records()
{
$teacher_id = user()->id;
$account_type = user()->account_type;
$this->db->select("COUNT(a.id) as TotalRecord")->from("attendance as a");
if($account_type == 'teacher')
{
$this->db->where("teacher_id",$teacher_id);
}else if($account_type == 'student'){
$this->db->where("FIND_IN_SET(".$teacher_id.",student_ids)",1);
}
if($this->input->post('cas_items')){
$this->db->where("cas_item_id",(int)$this->input->post('cas_items'));
}
if($this->input->post('attendance_date')){
$this->db->where(["a.attendance_date"=>date('Y-m-d',strtotime($this->input->post('attendance_date')))]);
}
$search = $this->input->post("search");
if(isset($search["value"]) && !empty($search["value"]))
{
$this->db->where(["a.attendance_date"=>date('Y-m-d',strtotime($search["value"]))]);
}
if($account_type == 'teacher'){
$attendance_cas_group = $this->session->userdata('attendance_cas_group');
if($attendance_cas_group){
$this->db->where("cas_item_id",(int)$attendance_cas_group);
}
}
$total_record = $this->db->get()->row();
if($total_record)
{
return $total_record->TotalRecord;
}else{
return 0;
}
}
public function getCategoryDropDown($id)
{
$categories = $this->db->where("id !=",$id)->get("departments")->result();
$catArray = ["0"=>"-- Select Category --"];
if(count($categories) > 0){
foreach($categories as $key=>$val){
$catArray[$val->id] = $val->title;
}
$categories = $catArray;
}
return $categories;
}
public function get_cas_items()
{
$categories = $this->db->where("parent_id !=",0)->get("cas")->result();
$catArray = ["0"=>"-- Select Item --"];
if(count($categories) > 0){
foreach($categories as $key=>$val){
$catArray[$val->id] = $val->title;
}
$categories = $catArray;
}
return $categories;
}
public function get_cas_groups()
{
$user = user();
if($user->account_type == 'teacher'){
$alloted_groups = $user->alloted_groups;
$alloted_groups = explode(",",$alloted_groups);
}else{
$alloted_groups = [];
}
foreach($alloted_groups as $key => $val)
{
$this->db->or_where('id',$val);
}
$categories = $this->db->get("cas")->result();
$catArray = ["0"=>"-- Select CAS group --"];
if(count($categories) > 0){
foreach($categories as $key=>$val){
$catArray[$val->id] = $val->title;
}
$categories = $catArray;
}
return $categories;
}
public function delete($id)
{
$this->db->where("id",$id)->delete("cas");
return $this->db->affected_rows();
}
public function checkSlug($where)
{
return $this->db->where($where)->count_all_results("cas");
}
}