Uname:Linux EDL-STRETCH 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64

403WebShell
403Webshell
Server IP : 188.114.97.2  /  Your IP : 104.23.243.200
Web Server : Apache/2.4.59 (Debian)
System : Linux EDL-STRETCH 4.19.0-27-amd64 #1 SMP Debian 4.19.316-1 (2024-06-25) x86_64
User : edlftp ( 1002)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /home/nicolasj/www/sms.formationlangues.be/application/models/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /home/nicolasj/www/sms.formationlangues.be/application/models/Crud_model.php
<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Crud_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('mail_model');
        $this->load->model('customer_model');
    }

    public function clear_cache()
    {
        $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
        $this->output->set_header('Pragma: no-cache');
    }


    /////////Admin///////////
    public function get_admin_list()
    {
        $query = $this->db->get_where('user', array('role_id' => '1'));
        return $query->result_array();
    }
    public function get_name($id)
    {
        $query = $this->db->get_where('user', array('user_id' => $id));
        $query = $query->result_array();
        $name = ucfirst($query[0]['first_name'])." ".ucfirst($query[0]['last_name']);
        return $name;
    }

    ////////PARENT//////////////
    public function get_child_list($id)
    {

        $query = $this->db->get_where('student', array('parent_id' => $id));
        return $query->result_array();
    }
    public function get_child_parent($id)
    {
        $query = $this->db->get_where('student', array('student_id' => $id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['parent_id'];
        }
    }

    public function get_parent_child($id)
    {
        $this->db->order_by('student_id', 'DESC');
        $query = $this->db->get_where('student', array('parent_id' => $id));
        $res = $query->result_array();
        $students = [];
        foreach ($res as $row) {
            $students[] = $row['student_id'];
        }
        return $students;
    }

    public function get_parent_child2($id)
    {
        $this->db->order_by('student_id', 'DESC');
        $query = $this->db->get_where('student', array('parent_id' => $id));
        $res = $query->result_array();
        $students = array();
        foreach ($res as $row):
            array_push($students, array($row['student_id']=>$this->get_student_name($row['student_id'])));
        endforeach;
        return $students;
    }

    public function get_parent_comment($id)
    {
        $query  = $this->db->get_where('parent', array('parent_id'=>$id))->result_array();
        return $query[0]['admin_comment'];
    }

    public function get_student_comment($id)
    {
        $query  = $this->db->get_where('student', array('student_id'=>$id))->result_array();
        return $query[0]['admin_comment'];
    }

    public function check_child_course($studentList, $user)
    {
        if(!empty($studentList)) {
            foreach($studentList as $student):
                $parent = $this->get_child_parent($student['student_id']);
                if($parent == $user) {
                    return true;
                } else {
                    return false;
                }
            endforeach;
        }
        return false;
    }

    public function check_child($lessonList, $user)
    {
        if(!empty($lessonList)) {
            foreach($lessonList as $lesson):
                if($lesson['student_id'] == $user) {
                    return true;
                } else {
                    return false;
                }
            endforeach;
        }
        return false;
    }
    public function is_child($user, $child)
    {
        //$this->db->select('student_id');
        $query = $this->db->get_where('student', array('parent_id' => $user,'student_id' => $child));
        $query = $query->result_array();
        if(!empty($query)) {
            return true;
        } else {
            return false;
        }
    }
    public function get_parent_list()
    {
        $this->db->from('parent');
        $this->db->order_by("parent_id", "desc");
        $query = $this->db->get();

        $query = $query->result_array();
        $result = array();

        foreach ($query as $row) {
            if(!$this->checkArchiveState($row['parent_id'], "user")) {
                array_push($result, $row);
            }
        }

        return $result;
    }
    public function get_parent_info($id)
    {
        $query = $this->db->get_where('parent', array('parent_id' => $id));
        return $query->result_array();
    }
    public function get_parents_children($id)
    {
        $query = $this->db->get_where('student', array('parent_id' => $id));
        return $query->result_array();
    }
    public function get_parent_name($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['first_name']." ".$row['last_name'];
        }
    }
    public function get_parent_lastname($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['last_name'];
        }
    }
    ////////STUDENT/////////////
    public function get_student_list()
    {
        $this->db->from('student');
        $this->db->order_by("student_id", "desc");
        $query = $this->db->get();
        $query = $query->result_array();

        $result = array();
        foreach ($query as $row) {
            if(!$this->checkArchiveState($row['student_id'], "user")) {
                array_push($result, $row);
            }
        }
        return $result;
    }

    public function get_student_list_n()
    {
        $q = "SELECT student.* , user.* FROM `student` JOIN user on student.student_id = user.user_id and (user.Archived = 0 or user.Archived = 2)";
        $query = $this->db->query($q);
        return $query->result_array();

    }
    public function get_student_list_no_course()
    {
        $sql = "SELECT u.user_id,u.email,u.first_name,u.last_name,u.LegitCreationDate,
                    s.parent_id,s.dob,
                    p.first_name as parent_first_name ,p.last_name as parent_last_name
                FROM user u
                JOIN student s ON u.user_id = s.student_id
                LEFT JOIN user p ON s.parent_id = p.user_id
                WHERE (u.Archived = 0 or u.Archived = 2) AND u.role_id = 4";

        $q = $this->db->query($sql)->result_array();
        return $q;
    }
    public function get_student_list_n_bis()
    {
        $sql = "SELECT c.student_id, c.course_id, u.user_id,u.email,
                    u.first_name,u.last_name,u.LegitCreationDate, s.parent_id,s.dob,
                    p.first_name as parent_first_name ,p.last_name as parent_last_name, cs.CourseName
                FROM `course_student_new` as c
                JOIN user as u On c.student_id = u.user_id
                JOIN student as s on c.student_id = s.student_id
                LEFT JOIN user as p On s.parent_id = p.user_id
                JOIN course_new as cs ON c.course_id = cs.ID
                WHERE u.Archived = 0 OR u.Archived = 2
                GROUP BY c.student_id,c.course_id";

        $q = $this->db->query($sql)->result_array();
        return $q;
    }
    public function mix_student_group()
    {
        $query = $this->db->query("SELECT `student_id`,`dob` FROM `student` UNION SELECT `group_id`,`dob` FROM `group` ORDER BY `student_id` DESC");
        $query = $query->result_array();

        $result = array();
        foreach ($query as $row) {
            if(!$this->checkArchiveState($row['student_id'], "user")) {
                array_push($result, $row);
            }
        }
        return $result;
    }


    public function get_students($course_id)
    {
        $query = $this->db->get_where('course_student_new', array('course_id' => $course_id));
        return $query->result_array();
    }
    public function get_students_n($course_id)
    {
        $query = $this->db->get_where('course_student_new', array('course_id' => $course_id));
        $data = $query->result_array();
        $new_array =  array();
        $check = array();
        foreach ($data as $key => $s) {
            if(!in_array($s['student_id'], $check)) {
                $new_array[$key]['id'] = $s['student_id'];
                $new_array[$key]['name'] = $this->get_student_name($s['student_id']);
                array_push($check, $s['student_id']);
            }
        }
        return $new_array;
    }
    public function get_students_course($student_id)
    {
        $query = $this->db->get_where('course_student_new', array('student_id' => $student_id));
        $query = $query->result_array();
        $filtred = array();
        foreach ($query as $value) {
            if(!in_array($value['course_id'], $filtred)) {
                array_push($filtred, $value['course_id']);
            }
        }
        return $filtred;
    }//return array of courseID

    public function get_students_course_recent($student_id)
    {
        $sql ="SELECT * FROM `course_student_new` INNER JOIN course_scheduled_new on course_student_new.lesson_id = course_scheduled_new.ID WHERE LessonDate > '". date("Y-m-d", strtotime("-4 months"))."' AND student_id = ".$student_id.";";
        $query = $this->db->query($sql);
        //echo $this->db->last_query();
        $query = $query->result_array();
        $filtred = array();
        foreach ($query as $value) {
            if(!in_array($value['course_id'], $filtred)) {
                array_push($filtred, $value['course_id']);
            }
        }
        return $filtred;
    }
    public function get_students_count($course_id)
    {
        $query = $this->db->get_where('course_student_new', array('course_id' => $course_id));
        $result = $query->result_array();

        $history = array();
        $count = 0;

        foreach($result as $value) {
            if(!in_array($value['student_id'], $history)) {
                $count ++;
            }
            array_push($history, $value['student_id']);
        }

        if($count != '' or $count != 0) {
            return $count;
        } else {
            return "0";
        }
    }
    public function get_students_count_lesson($lesson_id)
    {
        $query = $this->db->get_where('course_student_new', array('lesson_id' => $lesson_id));
        $result = count($query->result_array());
        if($result != '' or $result != 0) {
            return $result;
        } else {
            return "0";
        }
    }
    public function get_teachers_count_lesson($lesson_id)
    {
        $query = $this->db->get_where('course_teacher', array('lesson_id' => $lesson_id));
        $result = count($query->result_array());
        if($result != '' or $result != 0) {
            return $result;
        } else {
            return "0";
        }
    }
    public function get_student_interval($course_id)
    {
        $query = $this->db->get_where('course_student_new', array('course_id' => $course_id));
    }

    public function get_student_info($student_id)
    {
        $query = $this->db->get_where('student', array('student_id' => $student_id));

        return $query->result_array();
    }
    public function get_group_info($group_id)
    {
        $query = $this->db->get_where('group', array('group_id' => $group_id));

        return $query->result_array();
    }
    public function is_first_session($id)
    {
        $query = $this->db->get_where('user', array('user_id' => $id));
        $data = $query->result_array();
        if($data[0]['first_connection'] == "1") {
            return true;
        } else {
            return false;
        }

    }

    public function get_user_list()
    {
        $this->db->from('user');
        $this->db->order_by("first_name", "asc");
        $query = $this->db->get();
        return $query->result_array();
    }

    public function get_user_lang_iso($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        foreach ($res as $row) {

            $query = $this->db->get_where('mod772_icl_languages', array('id' => $row['lang']));
        }
        $res = $query->result_array();
        foreach ($res as $row) {
            return  strtolower($row['tag']);
        }
    }

    public function get_user_lang($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['lang'];
        }
    }
    public function get_user_info($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));

        return $query->result_array();
    }
    public function get_user_invoice($userId)
    {
        $query = $this->db->get_where('invoices', array('Customer_ID' => $userId));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row;
        }
    }
    public function get_user_age($dob)
    {
        $birthDate = explode("-", $dob);
        $age = (int)date("Y") - (int)$birthDate[0];
        if($age == "49") {
            return "n/a";
        }
        return $age;
    }
    public function get_user_address($adress_id)
    {
        $query = $this->db->get_where('address', array('address_id' => $adress_id));

        return $query->result_array();
    }
    public function guess_user_address($id)
    {
        $role = $this->get_user_info($id)[0]['role_id'];
        //$role = $this->get_user_role($id);
        //return $role;
        switch ($role) {
            case '1':
                return $this->db->get_where('address', array('address_id' => 1))->result_array()[0];
                break;
            case '2':
                return $this->get_user_address($this->get_teacher_info($id)[0]['address_id'])[0];
                break;
            case '3': //parents
                return $this->get_user_address($this->get_parent_info($id)[0]['residential_address_id'])[0];
                break;
            case '4': //student
                return $this->get_user_address($this->get_student_info($id)[0]['residential_address_id'])[0];
                break;
            case '6': //invited

                return $this->get_user_address($this->customer_model->get_transaction()['Address_ID'])[0];
                break;
            default:
                // code...
                break;
        }
    }
    public function get_user_email($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['email'];
        }
    }

    public function check_email($user_id)
    {
        $email = $this->get_user_email($user_id);
        $domain = substr(strrchr($email, "@"), 1);

        if($domain == "ecoledelangues.be" || $domain == "formationlangues.be" || $domain == "formationslangues.be" || $domain == "") {
            return false;
        }
        return true;
    }
    public function get_user_email_notification($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            if($row['email_notification'] == "" || $row['email_notification'] == " " || $row['email_notification'] == null) {
                return $row['email'];
            }
        }
        return $row['email_notification'];
    }

    public function get_email_user($email)
    {
        $query = $this->db->get_where('user', array('email' => $email));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['user_id'];
        }
    }

    public function get_last_user()
    {
        $this->db->order_by('user_id', 'DESC');
        $q = $this->db->get('user')->result_array();
        return $q[0];
    }

    public function get_current_user_name()
    {
        $this->session->userdata('user_id');
    }
    public function get_user_name($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return ucfirst($row['first_name'])." ".ucfirst($row['last_name']);
        }
    }
    public function get_user_creation_timestamp($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        $a = new DateTime(date('Y-m-d', strtotime(substr($res[0]['CreationDate'], 0, 10))));
        $b = new DateTime(date('Y-m-d', strtotime(Date('Y-m-d'))));
        $diff = $a->diff($b);
        $m_d = (($diff->format('%y') * 12) + $diff->format('%m'));
        if($m_d != 0) {
            return $m_d;
        } else {
            $now = time(); // or your date as well
            $date_s = strtotime(substr($res[0]['CreationDate'], 0, 10));
            $datediff = $now - $date_s;

            return round((($datediff / (60 * 60 * 24))/30), 2);
        }

    }

    public function get_student_name($user_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $user_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return ucfirst($row['first_name'])." ".ucfirst($row['last_name']);
        }
    }
    public function get_student_age($id)
    {
        $query = $this->db->get_where('student', array('student_id' => $id));
        $res = $query->result_array();
        return $this->get_user_age($res[0]['dob']);
    }
    public function get_student_register_number($id)
    {
        $query = $this->db->get_where('student', array('student_id' => $id));
        $res = $query->result_array();
        return $res[0]['register_number'];

    }
    public function get_student_birthdate($id)
    {
        $query = $this->db->get_where('student', array('student_id' => $id));
        $res = $query->result_array();
        return ($res[0]['dob']);
    }

    public function get_students_age_interval($courseID)
    {
        return;
        $query = $this->db->get_where('course_student_new', array('course_id' => $courseID));
        $query = $query->result_array();
        $ageList = array();

        foreach ($query as $row) {
            $studentData = $this->get_student_info($row['student_id']);
            array_push($ageList, $studentData[0]['dob']);
            //$test = $studentData[0]['dob'];
        }
        usort($ageList, function ($a, $b) {
            $dateTimestamp1 = strtotime($a);
            $dateTimestamp2 = strtotime($b);

            return $dateTimestamp1 < $dateTimestamp2 ? -1 : 1;
        });
        $ageList = array_filter($ageList);

        if(count($ageList)>1) {
            $younger = $ageList[count($ageList) - 1];
            $younger = date_diff(date_create($younger), date_create('today'))->y;
            $older = $ageList[0];
            $older = date_diff(date_create($older), date_create('today'))->y;

            //$average = round(array_sum($ageList)/count($ageList));
            //$average = date("Y") - $average;
            $average = ($younger + $older) / 2 ;

            $ageInt = $younger." - ". $older ." ($average)";
        } elseif(count($ageList)==1) {
            $ageInt = date_diff(date_create($ageList[0]), date_create('today'))->y;
        } else {
            $ageInt = "--";
        }
        return $ageInt;
    }
    public function get_students_age_interval_lesson($lessonID)
    {
        $query = $this->db->get_where('course_student_new', array('lesson_id' => $lessonID));
        $query = $query->result_array();
        $ageList = array();

        foreach ($query as $row) {
            $studentData = $this->get_student_info($row['student_id']);
            array_push($ageList, $studentData[0]['dob']);
            //$test = $studentData[0]['dob'];
        }
        usort($ageList, function ($a, $b) {
            $dateTimestamp1 = strtotime($a);
            $dateTimestamp2 = strtotime($b);

            return $dateTimestamp1 < $dateTimestamp2 ? -1 : 1;
        });
        $ageList = array_filter($ageList);

        if(count($ageList)>1) {
            $younger = $ageList[count($ageList) - 1];
            $younger = date_diff(date_create($younger), date_create('today'))->y;
            $older = $ageList[0];
            $older = date_diff(date_create($older), date_create('today'))->y;

            //$average = round(array_sum($ageList)/count($ageList));
            //$average = date("Y") - $average;
            $average = ($younger + $older) / 2 ;

            $ageInt = $younger." - ". $older ." ($average)";
        } elseif(count($ageList)==1) {
            $ageInt = date_diff(date_create($ageList[0]), date_create('today'))->y;
        } else {
            $ageInt = "--";
        }
        return $ageInt;
    }
    /////////Language////////////
    public function get_languages()
    {
        $query = $this->db->get('course_language');
        $query = $query->result_array();
        return $query;
    }

    public function get_course_language($course_id)
    {
        $query = $this->db->get_where('course_new', array('ID' => $course_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $this->get_language_name($row['CourseLanguage']);
        }
    }
    public function get_language_name($lan_id)
    {
        $query = $this->db->get_where('course_language', array('course_language_id' => $lan_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['code'];
        }
    }

    /////////TEACHER/////////////
    public function check_teacher($courseId, $teacher)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $courseId,'teacher_id' => $teacher));
        $query = $query->result_array();
        if(!empty($query)) {
            return $query;//true;
        } else {
            return false;//$query;
        }
    }

    public function check_student($courseId, $student)
    {
        $query = $this->db->get_where('course_student_new', array('course_id' => $courseId,'student_id' => $student));
        $query = $query->result_array();
        if(!empty($query)) {
            return $query;//true;
        } else {
            return false;//$query;
        }
    }

    public function check_childs($courseId, $studentArray)
    {
        $authorized = false;
        foreach ($studentArray as $key => $student) {

            if($this->check_student($courseId, $student['student_id']) != false) {
                $authorized = true;
            }
        }
        return $authorized;
    }

    public function check_teacher_students($teacher, $student)
    {
        $course_list = $this->get_students_course($student);
        foreach($course_list as $course):
            if($this->check_teacher($course, $teacher)) {
                return true;
            }
        endforeach;
        return false;
    }
    public function check_teacher_parents($teacher, $parent)
    {
        $childs = $this->get_parent_child($parent);
        foreach ($childs as $child) {
            $course_list = $this->get_students_course($child);
            //return $course_list;
            foreach($course_list as $course):
                if($this->check_teacher($course, $teacher)) {
                    return true;
                }
            endforeach;
        }
        return false;
    }
    public function get_teachers_list($course_id) //duplicate function
    {$query = $this->db->get_where('course_teacher', array('course_id' => $course_id));
        $teachers = $query->result_array();
        $teacherArray = array();

        foreach ($teachers as $teacher) { //filter to prevent duplicate teacher in a course
            $id = $teacher['teacher_id'];
            if(!in_array($id, $teacherArray)) {
                array_push($teacherArray, $id);
            }
        }
        return $teacherArray;
    }

    public function get_teacher_contract($course_id, $teacher_id)
    {
        $query = $this->db->get_where('teacher_contract', array('Course_ID' => $course_id,'Teacher_ID' => $teacher_id));
        $query = $query->result_array();
        foreach ($query as $row) {
            return $row['Price'];
        }
    }
    public function get_teacher_hours($course_id, $teacher_id)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $course_id,'teacher_id' => $teacher_id));
        $lessons = $query->result_array();
        $hours = "";
        foreach ($lessons as $lesson) {
            $duration = $this->get_lesson_duration($lesson['lesson_id']);
            $hours = $duration + $hours;
        }
        return $hours;
    }
    public function get_teacher($course_id)
    {
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get_where('course_teacher', array('course_id' => $course_id));        
        $query = $query->result_array();
        return $query;
    }
    public function get_teacher_plus($course_id)
    {
        $sql = <<<EOD
      SELECT
      u.user_id,
      u.first_name,
      u.last_name,
      t.teacher_id
      FROM course_teacher as t
      JOIN user as u
      ON t.teacher_id = u.user_id
      WHERE t.course_id = {$course_id}
      GROUP BY t.teacher_id
      EOD;

        $teachers = $this->db->query($sql);
        $teachers = $teachers->result_array();


        return $teachers;
        $teachers_data = array();
        /*
        foreach ($teachers as $teacher) {

          $query_t = $this->db->get_where('course_teacher', array('course_id' => $course_id, 'teacher_id' => $teacher));
          $query_t = $query_t->result_array();

          $teacher_data = array();
          $teacher_period = array();

          foreach($query_t as $lesson_t){
           array_push($teacher_period,$this->get_lesson_info_short($lesson_t['lesson_id']));
           //return $this->get_lesson_info_short($lesson_t['lesson_id']);
          }

          //return $teacher_period;
          usort($teacher_period, function($a1, $a2) {
             $v1 = strtotime($a1['date']);
             $v2 = strtotime($a2['date']);
             return $v1 - $v2; // $v2 - $v1 to reverse direction
          });
          //return $teacher_period;

          $period_teacher = "[ ".reset($teacher_period) ." - ". end($teacher_period)." ]";
          //return $period_teacher;
          $teacher_data = array(
            "id" => $teacher,
            "period" => $period_teacher,
            "name" => $this->get_teacher_name($teacher)
          );

          array_push($teachers_data,$teacher_data);
        }
    //  return $teacher_data;
        return $teachers_data;
        */
    }

    public function get_teacher_plus2($course_id)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $course_id));
        $query = $query->result_array();

        $teachers = array();
        $teachers_n = array();
        foreach ($query as $result) {
            if(!in_array($result['teacher_id'], $teachers)) {
                array_push($teachers, $result['teacher_id']);
                array_push($teachers_n, $this->crud_model->get_teacher_name($result['teacher_id']));
            }
        }
        //return $teachers;

        //return $teachers_n;
        $teachers_data = array();
        foreach ($teachers as $teacher) {

            $query_t = $this->db->get_where('course_teacher', array('course_id' => $course_id, 'teacher_id' => $teacher));
            $query_t = $query_t->result_array();

            $teacher_data = array();
            $teacher_period = array();

            foreach($query_t as $lesson_t) {
                array_push($teacher_period, $this->get_lesson_info_short($lesson_t['lesson_id']));
                //return $this->get_lesson_info_short($lesson_t['lesson_id']);
            }

            //return $teacher_period;
            usort($teacher_period, function ($a1, $a2) {
                $v1 = strtotime($a1['date']);
                $v2 = strtotime($a2['date']);
                return $v1 - $v2; // $v2 - $v1 to reverse direction
            });
            //return $teacher_period;

            $period_teacher = "[ ".reset($teacher_period) ." - ". end($teacher_period)." ]";
            //return $period_teacher;
            $teacher_data = array(
              "id" => $teacher,
              "period" => $period_teacher,
              "name" => $this->get_teacher_name($teacher)
            );

            array_push($teachers_data, $teacher_data);
        }
        //  return $teacher_data;
        return $teachers_data;
    }
    public function get_teacher_contract_state_2($teacher_id)
    {
        return $this->get_teacher_contract_state($this->get_teacher_info($teacher_id)[0]['admin_state']);
    }

    public function get_teacher_contract_state($id)
    {
        switch($id) {
            case '0':
                return '#21a9e1';
                break;
            case '1':
                return '#00a651';
                break;
            case '2':
                return 'yellow';
                break;
            case '3':
                return 'red';
                break;
            default:
                // code...
                break;
        }
    }

    public function get_teacher_profile_state($id)
    {
        $query = $this->db->get_where('teacher', array('teacher_id' => $id));
        $query = $query->result_array();
        return $query[0]['admin_state'];
    }

    public function get_teachers($array = null)
    {
        $this->db->from('teacher');
        $this->db->order_by("teacher_id", "desc");
        if($array != null) {
            $query = $this->db->where_in('teacher_id', $array);
        }
        $query = $this->db->get();
        $query = $query->result_array();

        $result = array();
        foreach ($query as $row) {
            $name = $this->get_teacher_name($row['teacher_id']);
            $row['teacher_name']=$name;
            if((!$this->checkArchiveState($row['teacher_id'], "user")) && ($name != "" && $name != " ")) {
                array_push($result, $row);
            }
        }
        return $result;
    }

    /**
     * Return all teachers in database
     *
     * @param array|null $teacher_ids
     * @param boolean $include_archived
     * @return array|null
     */
    public function get_all_teachers(array $teacher_ids = null, bool $include_archived = false) : ?array
    {
        $query = "SELECT * FROM teacher t
                  LEFT JOIN user u ON u.user_id = t.teacher_id
                  WHERE 1=1";
        if(!empty($teacher_ids)) {
            $query = $this->db->where_in('teacher_id', $teacher_ids);
        }
        if($include_archived === false) {
            $query .= ' AND u.Archived != 1';
        }
        return $this->db->query($query)->result_array();
    }

    public function get_teachers_by_lang($target_lang)
    {

        $this->db->from('teacher');
        $this->db->order_by("teacher_id", "desc");
        $query = $this->db->get();
        $query = $query->result_array();


        $result = array();
        foreach ($query as $row) {
            $lang = $this->get_teacher_lang($row['teacher_id']);
            $name = $this->get_teacher_name($row['teacher_id']);
            //return $lang;//['course_language_id'];
            if(($lang[0]['course_language_id'] == $target_lang)) { //&& ($this->checkArchiveState($row['teacher_id'],"user") != true) && ( $name != "" && $name != " ")){
                array_push($result, $row);
            }
        }
        return $result;
    }

    public function get_teacher_name($teacher_id)
    {
        $query = $this->db->get_where('user', array('user_id' => $teacher_id));
        $query = $query->result_array();
        $name = ucfirst($query[0]['first_name'])." ".ucfirst($query[0]['last_name']);
        return $name;
    }

    public function get_teacher_info($teacher_id)
    {
        $query = $this->db->get_where('teacher', array('teacher_id' => $teacher_id));
        return $query->result_array();
    }
    //new functions:
    public function get_teachers_course($teacher_id)
    {
        $query = $this->db->get_where('course_teacher', array('teacher_id' => $teacher_id));
        $query = $query->result_array();
        $filtred = array();
        foreach ($query as $value) {
            if($this->checkArchiveState($value['course_id'], "courses") == false) {
                if(!in_array($value['course_id'], $filtred)) {
                    array_push($filtred, $value['course_id']);
                }
            }
        } // filtred result only courses
        return $filtred;
    }
    public function get_teacher_associate($user_id, $teacher_id)
    {
        $user = $this->get_teachers_course($user_id);
        $teacher = $this->get_teachers_course($teacher_id);
        if(array_intersect($user, $teacher) != null) {
            return true;
        }
        return false;
    }
    public function get_student_associate($user_id, $student_id)
    {
        $user = $this->get_teachers_course($user_id);
        $student = $this->get_students_course($student_id);
        if(array_intersect($user, $student) != null) {
            return true;
        }
        return false;
    }
    public function get_teacher_lang($teacher_id)
    {
        $query = $this->db->get_where('teacher_course_language', array('teacher_id' => $teacher_id));
        return $query->result_array();
    }
    //////////SUBJECT/////////////
    public function get_subjects()
    {
        $query = $this->db->get('subject');
        return $query->result_array();
    }

    public function get_subject_info($subject_id)
    {
        $query = $this->db->get_where('subject', array('subject_id' => $subject_id));
        return $query->result_array();
    }

    public function get_subjects_by_course($course_id)
    {
        $query = $this->db->get_where('subject', array('course_id' => $course_id));
        return $query->result_array();
    }

    public function get_subject_name_by_id($subject_id)
    {
        $query = $this->db->get_where('subject', array('subject_id' => $subject_id))->row();
        return $query->name;
    }

    ////////////COURSE///////////
    //************************//
    //gestion des différences d'heures
    public function differenceInHours($startdate, $enddate)
    {
        $starttimestamp = strtotime($startdate);
        $endtimestamp = strtotime($enddate);
        $difference = abs($endtimestamp - $starttimestamp)/3600;
        return $difference;
    }
    ///////////////////////////:
    public function get_course($course_id)
    {
        $query = $this->db->get_where('course_new', array('ID' => $course_id));
        return $query->result_array();
    }
    public function get_course_price($course_id)
    {
        $query = $this->db->get_where('course_new', array('ID' => $course_id));
        $data = $query->result_array();

        foreach ($data as $row) {
            return $row['CoursePrice'];
        }
    }
    public function course_shop_price($id)
    {
        $query = $this->db->get_where('course_product', array('Course_ID' => $id));
        $data = $query->result_array();
        return $data[0]['Course_Price'];
    }
    public function course_places($id)
    {
        $query = $this->db->get_where('course_product', array('Course_ID' => $id));
        $data = $query->result_array();
        return $data[0]['Course_Availability'];
    }
    public function course_tooltip($id)
    {
        $t = $this->get_course_name($id);
        $t = $t ."[Places: ".$this->get_students_count($id)." / ".$this->course_places($id)."] \n";
        $t = $t . "[Start: ".$this->get_course_start($id)." / End:".$this->get_course_end($id)."] \n";
        $t = $t . "[Price: ".$this->course_shop_price($id)."] \n";
        $t = $t . "[Age range: ".$this->get_students_age_interval($id)."] \n";
        return $t;
    }
    public function get_course_progression($id)
    {
        $query = $this->db->get_where('course_scheduled_new', array('CourseID' => $id));
        $courses = $query->result_array();
        $n = 0;
        $x = 0;

        foreach ($courses as $key => $course) {
            $n++;

            if($course['LessonDate'] > date("Y-m-d")) {

                $x++;
            }
        }
        return round((100-(($x*100)/$n)), 2);
    }
    public function get_course_address($id)
    {
        $query = $this->db->get_where('course_address', array('course_id' => $id));
        $res = $query->result_array();
        return $res;
    }
    public function get_course_name($course_id)
    {
        $query = $this->db->get_where('course_new', array('ID' => $course_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return ucfirst($row['CourseName']);
        }
        //return $course_id;
    }
    public function get_course_teacher($course_id)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $course_id));
        $res = $query->result_array();
        return $res;
        //foreach ($res as $row)
        //return $this->get_teacher_name($row['CourseTeacher']);
    }

    public function get_course_teachers($course_id)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $course_id));
        $res = $query->result_array();
        $teachers_id = array();
        $teachers = array();

        foreach ($res as $row) {
            if(!in_array($row['teacher_id'], $teachers_id)) {

                $teacher = array(
                  $row['teacher_id'],
                  $this->get_teacher_name($row['teacher_id'])
                );

                array_push($teachers_id, $row['teacher_id']);
                array_push($teachers, $teacher);
            }
        }
        return json_encode($teachers);

    }
    public function get_course_teacher_3($course_id)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $course_id));
        $res = $query->result_array();
        $teachers_id = array();
        $teachers = array();

        foreach ($res as $row) {
            if(!in_array($row['teacher_id'], $teachers_id)) {

                $teacher = array(
                  $row['teacher_id'],
                  $this->get_teacher_name($row['teacher_id']),
                  $this->get_user_email($row['teacher_id'])
                );

                array_push($teachers_id, $row['teacher_id'], );
                array_push($teachers, $teacher);
            }
        }
        return ($teachers);

    }
    public function get_course_futur_teacher($c)
    {
        $sql = "SELECT * FROM `course_teacher` as t
                JOIN course_scheduled_new as c on t.lesson_id = c.ID
                WHERE c.LessonDate > '".Date('Y-m-d')."' and t.course_id =" . (int) $c . ";";
        $res = $this->db->query($sql)->result_array();

        $teachers_id = array();
        $teachers = array();

        foreach ($res as $row) {
            if(!in_array($row['teacher_id'], $teachers_id)) {

                $teacher = array(
                  $row['teacher_id'],
                  $this->get_teacher_name($row['teacher_id']),
                  $this->get_user_email($row['teacher_id'])
                );

                array_push($teachers_id, $row['teacher_id']);
                array_push($teachers, $teacher);
            }
        }
        return $teachers;
    }

    public function get_course_students_n($course_id)
    {
        $this->db->select('id, lesson_id, status_id, student_id, course_id');
        $query = $this->db->get_where('course_student_new', array('course_id' => $course_id));
        $res = $query->result_array();

        $students = array();
        $data = array();
        foreach ($res as $row) {
            $id = $row['student_id'];
            if(!in_array($id, $students)) {
                array_push($students, $id);
            }
        }
        return $students;
    }

    public function get_student_courses($student_id)
    {

        $query = $this->db->get_where('course_student_new', array('student_id' => $student_id));
        $res = $query->result_array();
        //return $res;
        $courses = array();
        $data = array();
        foreach ($res as $row) {
            $id = $row['course_id'];
            if(!in_array($id, $courses)) {
                array_push($courses, $id);
            }
        }
        return $courses;
    }

    public function get_student_courses_bis($student_id)
    {
        $sql = <<<EOD
      select c.ID,c.CourseName,c.CoursePrice,c.CourseVat,c.CourseContract,u.first_name,u.last_name,u.user_id
      from course_student_new as s
      join course_new as c
      on s.course_id = c.ID
      join user as u
      on s.student_id = u.user_id
      where s.student_id = '$student_id'
      group by c.ID
      EOD;

        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function get_course_students(int $course_id): array
    {
        $query = $this->db->get_where('course_student_new', array('course_id' => $course_id));
        $res = $query->result_array();

        $students = array();
        $data = array();
        foreach ($res as $row) {
            $id = $row['student_id'];
            $name = $this->get_student_name($id);
            $mail = $this->get_user_email($id);

            $student = [$id, $name, $mail];

            if(!in_array($id, $students)) {
                array_push($data, $student);
            }
            array_push($students, $id);
        }

        return $data;
    }

    public function get_course_teacher_2($course_id)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $course_id));
        $res = $query->result_array();
        $result = array();
        foreach ($res as $row) {
            if(!in_array($row['teacher_id'], $result)) {
                array_push($result, $row['teacher_id']);
            }

        }
        return $result;
        //return $res;

    }
    public function get_course_contract($course_id)
    {
        $query = $this->db->get_where('course_new', array('ID' => $course_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['CourseContract'];
        }
    }

    public function get_course_scheduled($course_id)
    {
        $this->db->order_by('LessonDate', 'ASC');
        $data = $this->db->get_where('course_scheduled_new', array('CourseID' => $course_id))->result_array();
        return $data;
    }

    public function get_student_course_start($student, $course)
    {
        $this->db->order_by('LessonDate', 'ASC');
        $lessons = $this->db->get_where('course_scheduled_new', array('CourseID' => $course))->result_array();
        foreach ($lessons as $key => $l) {
            $s = $this->get_lesson_students($l['ID']);
            if(in_array($student, array_column($s, 'student_id'))) {
                return $l['LessonDate'];
            }
        }
    }

    public function get_student_course_end($student, $course)
    {
        $this->db->order_by('LessonDate', 'DESC');
        $lessons = $this->db->get_where('course_scheduled_new', array('CourseID' => $course))->result_array();
        foreach ($lessons as $key => $l) {
            $s = $this->get_lesson_students($l['ID']);
            if(in_array($student, array_column($s, 'student_id'))) {
                return $l['LessonDate'];
            }
        }
    }

    public function get_course_start($course_id)
    {
        $this->db->order_by('LessonDate', 'ASC');
        $data = $this->db->get_where('course_scheduled_new', array('CourseID' => $course_id), 1)->result_array();
        if($data[0][LessonDate] == "") {
            $data2 = $this->db->get_where('course', array('course_id' => $course_id), 1)->result_array();
            if(substr(date("d-m-y", strtotime($data2[0][start_date])), 0, 10) == "01-01-1970") {
                return "n/a";
            }
            return substr(date("d/m/Y", strtotime($data2[0][start_date])), 0, 10);
        }
        if(substr(date("d-m-y", strtotime($data[0][LessonDate])), 0, 10) == "01-01-1970") {
            return "n/a";
        }
        return substr(date("d/m/Y", strtotime($data[0][LessonDate])), 0, 10);
    }

    public function get_courses_on_period($start, $end) : array
    {
        $this->db->select('CourseID');
        $this->db->from('course_scheduled_new');
        $this->db->where('LessonDate <=', $end);
        $this->db->where('LessonDate >=', $start);
        $lessons = $this->db->get()->result_array();
        $courses_id = [];

        foreach ($lessons as $lesson) {
            if (!empty($lesson['CourseID'])) {
                $courses_id[] = (int) $lesson['CourseID'];
            }
        }
        return array_unique($courses_id, SORT_NUMERIC);
    }

    public function get_course_end($course_id)
    {
        $this->db->order_by('LessonDate', 'DESC');
        $data = $this->db->get_where('course_scheduled_new', array('CourseID' => $course_id), 1)->result_array();
        if($data[0][LessonDate] == "") {
            $data2 = $this->db->get_where('course', array('course_id' => $course_id), 1)->result_array();
            if(substr(date("d-m-Y", strtotime($data2[0][start_date])), 0, 10) == "01-01-1970") {
                return "n/a";
            }
            return substr(date("d/m/Y", strtotime($data2[0][end_date])), 0, 10);
        }
        if(substr(date("d-m-y", strtotime($data[0][LessonDate])), 0, 10) == "01-01-1970") {
            return "n/a";
        }
        return substr(date("d/m/Y", strtotime($data[0][LessonDate])), 0, 10);
    }

    public function get_course_hours_total($course_id)
    {
        $hours = "0";
        $data = $this->db->get_where('course_scheduled_new', array('CourseID' => $course_id))->result_array();
        foreach ($data as $row) {
            $hours = $hours + $this->differenceInHours($row['LessonStart'], $row['LessonEnd']);
        }
        return $hours;//date_format($hours,'H:i');
    }

    public function get_course_hours_remain($course_id)
    {
        $hours = "0";

        $data = $this->db->get_where('course_scheduled_new', array('CourseID' => $course_id,'LessonDate >' => date('Y-m-d')))->result_array();
        //return $this->db->last_query();
        foreach ($data as $row) {

            //if($row['LessonDone'] == null){
            $hours = $hours + $this->differenceInHours($row['LessonStart'], $row['LessonEnd']);
            //}
        }

        return $hours;//date_format($hours,'H:i');

    }
    public function get_course_hours_did($course_id)
    {
        $hours = "0";

        $data = $this->db->get_where('course_scheduled_new', array('CourseID' => $course_id,'LessonDate <' => date('Y-m-d')))->result_array();
        //return $this->db->last_query();
        foreach ($data as $row) {

            //if($row['LessonDone'] == null){
            $hours = $hours + $this->differenceInHours($row['LessonStart'], $row['LessonEnd']);
            //}
        }

        return $hours;//date_format($hours,'H:i');

    }
    public function get_course_name_numeric($course_id)
    {
        $query = $this->db->get_where('course', array('course_id' => $course_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['name_numeric'];
        }
    }
    public function get_courses()
    {

        $this->db->order_by('ID', 'DESC');
        $this->db->where('Archived', '0');
        $this->db->or_where('Archived', '2');
        $query = $this->db->get('course_new');
        $query = $query->result_array();
        return $query;

        $result = array();
        foreach ($query as $row) {
            if(!$this->checkArchiveState($row['ID'], "courses")) {
                array_push($result, $row);
            }
        }
        return $result;
    }
    public function get_courses_plus($s='#', $l="#")
    {
        $param = "";
        if($s!='#') {
            $param ="AND `CourseSociety` = $s ";
        }
        if($l!='#' && $l!='all') {
            $param = $param ."AND `CourseLocal` = $l ";
        } elseif($l=='all') {
            $param = $param ."AND (`CourseLocal` = '1' || `CourseLocal` = '2' || `CourseLocal` = '5') ";
        } else {
        }

        $sql = <<<EOD
        SELECT
        c.`ID`, c.`CourseName`, c.`CourseCertificate`, c.`CourseContract`, c.`CourseType`, c.`CourseLocal`,
        s.student_id,
        u.first_name,u.last_name,u.dob
        FROM `course_new` as c
        LEFT JOIN course_student_new as s
        ON c.ID = s.course_id
        LEFT JOIN user as u
        ON s.student_id = u.user_id
        WHERE c.Archived != 1
        {$param}
        ORDER BY c.ID ASC
        EOD;
        //  GROUP BY s.student_id
        $query = $this->db->query($sql);
        $query = $query->result_array();
        //echo $this->db->last_query();
        $courses = [];

        foreach ($query as $key => $c) {
            if(!array_key_exists($c['ID'], $courses)) {
                $courses[$c['ID']] = $c;
                unset($courses[$c['ID']]['student_id']);
                unset($courses[$c['ID']]['first_name']);
                unset($courses[$c['ID']]['last_name']);
                $courses[$c['ID']]['students'] = array();
            }

            $cur_array = $courses[$c['ID']]['students'];
            //$cur_int = $courses[$c['ID']]['interval'];

            if($cur_array == null) {
                $courses[$c['ID']]['students'] = array();
            }

            //echo $c['student_id'];
            if($c['student_id'] != null && $c['student_id'] != null && !in_array($c['student_id'], array_column($cur_array, 'id'))) {
                array_push($cur_array, array('id'=>$c['student_id'],'name'=>$c['first_name'].' '.$c['last_name'],'dob'=>$c['dob']));
                //  array_push($cur_int,substr($c['dob'],4));
                //var_dump($cur_array);
                $courses[$c['ID']]['students'] = $cur_array;
                //  $courses[$c['ID']]['interval'] = $cur_int;
            }
        }
        //var_dump($courses);
        return $courses;
    }

    public function get_courses_n($s='#')
    {
        $param = "";
        if($s!='#') {
            $param ="AND `CourseSociety` = $s ";
        }
        $sql = <<<EOD
            SELECT
            c.`ID`, c.`CourseName`, c.`CourseCertificate`, c.`CourseContract`, c.`CourseType`, c.`CourseLocal`,
            GROUP_CONCAT(DISTINCT(IFNULL(s.student_id, 'NULL')),',') as students
            FROM `course_new` as c
            LEFT JOIN course_student_new as s
            ON c.ID = s.course_id
            WHERE `Archived` != 1
            {$param}
            GROUP BY c.`ID`
            ORDER BY `c`.`ID` ASC
        EOD;
        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }

    public function get_courses_level()
    {
        $query= $this->db->get('course_level');
        return $query->result_array();
    }
    public function get_courses_type($id = '999')
    {
        $query= $this->db->get('course_type');
        $res = $query->result_array();

        if($id != '999') {
            foreach ($res as $row) {
                if($row['course_type_id']== $id) {
                    return $row['code'];
                }
            }
        } else {
            return $res;//$;query->result_array();
        }
    }
    public function get_bgColor($id)
    {
        //return "1".$id;
        $query = $this->db->get_where('course_local', array('course_local_id' => $id));
        $res = $query->result_array();
        foreach ($res as $row) {
            return $row['color'];
        }
    }

    public function get_courses_local()
    {
        $query= $this->db->get('course_local');
        $res = $query->result_array();
        return $res;
    }
    public function get_course_local($id)
    {
        $query = $this->db->get_where('course_new', array('ID' => $id));
        $res = $query->result_array();

        foreach ($res as $row) {
            return $row['CourseLocal'];
        }
    }

    public function get_local_name($id)
    {
        $query = $this->db->get_where('course_local', array('course_local_id' => $id));
        $res = $query->result_array();
        return $res[0]['code'];
    }



    public function get_courses_society()
    {
        //return "ok";
        $query= $this->db->get('course_society');
        $res = $query->result_array();
        return $res;
    }

    public function debug()
    {
        return "debug";
    }

    public function get_course_society_id($id)
    {

        $query= $this->db->get_where('course_new', array('ID'=>$id));
        $res = $query->result_array();
        return $res[0]['CourseSociety'];
    }

    public function get_society_name($id)
    {
        $query = $this->db->get_where('course_society', array('course_society_id' => $id));
        foreach ($query->result_array() as $row) {
            return $row['code'];
        };
    }

    public function get_course_info($course_id)
    {
        $query = $this->db->get_where('course_new', array('ID' => $course_id));
        return $query->result_array();
    }
    public function get_course_classroom($course_id)
    {
        //$this->db->select('CourseType');
        $query = $this->db->get_where('course_new', array('ID' => $course_id));
        $res = $query->result_array();
        foreach ($res as $row) {
            //return "hell";
            //return $this->get_courses_type($row['CourseType']);
            //return $this->get_course_local($row['CourseLocal'])
        }
    }
    ///////////lesson//////////
    public function get_lessons()
    {
        $this->db->order_by('LessonDate', 'ASC');
        $query = $this->db->get('course_scheduled_new');
        $query = $query->result_array();

        $result = array();

        foreach ($query as $row) {
            if(!$this->checkArchiveState($row['CourseID'], "courses")) {
                array_push($result, $row);
            }
        }
        return $result;
    }
    public function get_lesson($id)
    {
        //$this->db->select('ID');
        $query = $this->db->get_where('course_scheduled_new', array("ID"=>$id));
        $query = $query->result_array();
        return $query[0];
    }
    public function get_lesson_name($id)
    {
        //$this->db->select('ID');
        $query = $this->db->get_where('course_scheduled_new', array("ID"=>$id));
        $query = $query->result_array();
        return $query[0]['LessonName'];
    }
    public function get_course_lessons($id)
    {
        //$this->db->select('ID');
        $query = $this->db->get_where('course_scheduled_new', array("CourseID"=>$id));
        $query = $query->result_array();
        return $query;
    }
    public function get_course_id_from_lesson($lesson_id)
    {
        $query = $this->db->get_where('course_scheduled_new', array("ID"=>$lesson_id));
        $query = $query->result_array();
        return $query[0]['CourseID'];
    }
    public function get_lessons_number($course_id)
    {
        //$this->db->order_by('LessonDate', 'ASC');
        $query = $this->db->get_where('course_scheduled_new', array("CourseID"=>$course_id));
        $query = $query->result_array();

        return count($query);
    }
    public function get_future_lessons($lesson)
    {
        $target = $this->get_lesson($lesson);
        $course = $target['CourseID'];
        $date = $target['LessonDate'];

        $this->db->order_by('LessonDate', 'ASC');
        $this->db->where('LessonDate >=', $date);
        $this->db->where('CourseID', $course);
        $query = $this->db->get('course_scheduled_new');
        $query = $query->result_array();
        return $query;
    }


    public function get_lessons_on_period($start, $end)
    {
        $this->db->select('*');
        $this->db->from('course_scheduled_new');
        $this->db->where('LessonDate <=', $end);
        $this->db->where('LessonDate >=', $start);
        $data = $this->db->get();
        $lessons = $data->result_array();

        return $lessons;
    }
    public function get_lesson_duration($lesson_id)
    {
        $query = $this->db->get_where('course_scheduled_new', array('ID' => $lesson_id));
        $query = $query->result_array();
        foreach ($query as $row) {
            //$ts2 = strtotime($row['date']." ".$row['start']);
            //$ts1 = strtotime($row['date']." ".$row['end']);
            //$diff = abs($ts2 - $ts1) /3600;
            //$diff = (($diff/3600)/60);
            //return var_dump($lesson_id);
            return $this->differenceInHours($row['LessonStart'], $row['LessonEnd']);
            //return $diff;
            //return round($diff,1);
        }
    }
    public function get_lesson_info($lesson_id)
    {
        $query = $this->db->get_where('course_scheduled_new', array('ID' => $lesson_id));
        $query = $query->result_array();
        if($query[0]['LessonName'] != "") {
            $info = "<span style='display:none'>".date("Ymd", strtotime($query[0]['LessonDate']))."</span>".$query[0]['LessonName'].":".date("d/m/Y", strtotime($query[0]['LessonDate']))."[".substr($query[0]['LessonStart'], 0, 5)."-".substr($query[0]['LessonEnd'], 0, 5)."]";// array wtf just get the first ?
        } else {
            $info = "<span style='display:none'>".date("Ymd", strtotime($query[0]['LessonDate']))."</span>".date("d/m/Y", strtotime($query[0]['LessonDate']))."[".substr($query[0]['LessonStart'], 0, 5)."-".substr($query[0]['LessonEnd'], 0, 5)."]";// array wtf just get the first ?

        }
        return $info; //???
    }
    public function get_lesson_info_short($lesson_id)
    {
        $query = $this->db->get_where('course_scheduled_new', array('ID' => $lesson_id));
        $query = $query->result_array();

        $info = date("d/m/Y", strtotime($query[0]['LessonDate']));
        return $info; //???
    }

    public function get_lesson_switch_history($lesson_id = "")
    {
        if($lesson_id != "") {
            $query = $this->db->get_where('course_teacher_history', array('ID' => $lesson_id));
        } else {
            $query = $this->db->get('course_teacher_history');
        }
        return $query->result_array();
    }
    public function get_lesson_course_name($lesson_id = "")
    {
        $query_course = $this->db->get_where('course_scheduled_new', array('ID' => $lesson_id))->result_array();
        return $this->get_course_name($query_course[0]['CourseID']);
    }

    public function get_lesson_info_short_iso($lesson_id)
    {
        $query = $this->db->get_where('course_scheduled_new', array('ID' => $lesson_id));
        $query = $query->result_array();

        $info = date("Y-m-d", strtotime($query[0]['LessonDate']));
        return $info; //???
    }
    public function get_lesson_students($lesson_id)
    {
        $query = $this->db->get_where('course_student_new', array('lesson_id' => $lesson_id));
        $query = $query->result_array();
        return $query;
    }
    public function get_student_lesson($student_id)
    {
        $query = $this->db->get_where('course_student_new', array('student_id' => $student_id));
        $query = $query->result_array();

        return $query;
    }
    public function get_teacher_lesson($teacher_id)
    {
        $query = $this->db->get_where('course_teacher', array('teacher_id' => $teacher_id));
        $query = $query->result_array();
        return $query;
    }
    public function get_teacher_lesson_plus($teacher_id)
    {
        $sql = <<<EOD
      SELECT  cs.*, ct.teacher_id
      FROM `course_teacher` as ct
      JOIN course_scheduled_new as cs
      ON ct.lesson_id = cs.ID
      where `teacher_id` = '$teacher_id' ORDER BY cs.`ID`  DESC
    EOD;
        //return $sql;
        $q = $this->db->query($sql)->result_array();
        return $q;


    }
    public function get_teacher_course_lessons($course_id, $teacher_id)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $course_id,'teacher_id' => $teacher_id));
        $query = $query->result_array();
        //return $this->db->last_query();
        return $query;
    }

    public function get_teacher_courses(int $teacher_id, bool $include_archived = false)
    {
        $sql = "SELECT  c.*, ct.teacher_id
                FROM `course_teacher` as ct
                JOIN course_new as c
                ON ct.course_id = c.ID
                WHERE `teacher_id` = ". $teacher_id;

        if($include_archived === false) {
            $sql .= ' AND c.Archived != 1';
        }
        $sql .= ' GROUP by c.ID';

        $query = $this->db->query($sql);
        $query = $query->result_array();

        return $query;
    }

    public function get_student_course_lessons($course_id, $student_id)
    {
        $query = $this->db->get_where('course_student_new', array('course_id' => $course_id,'student_id' => $student_id));
        $query = $query->result_array();
        //return $this->db->last_query();
        return $query;
    }

    public function get_lesson_teacher($lesson_id)
    {
        //$this->db->select('teacher_id');
        $query = $this->db->get_where('course_teacher', array('lesson_id' => $lesson_id));
        $query = $query->result_array();
        $result = array();
        foreach ($query as $row) {
            array_push($result, $row['teacher_id']);
        }
        return $result;
    }
    public function get_json_lesson_students($lesson_id)
    {
        $jsonStudents ="";
        $studentList = $this->get_lesson_students($lesson_id);
        foreach ($studentList as $key => $row) {
            $id = $row['student_id'];
            $jsonStudents = $jsonStudents ."\"". $key ."\":{\"studentId\":\"".$id."\",\"studentPresence\":\"".$row['status_id']."\",\"studentName\":\"".ucfirst($this->get_student_name($id))."\"},";
        }
        $jsonStudents = "\"students\" : {".rtrim($jsonStudents, ',')."}";
        return $jsonStudents;
    }
    public function get_json_lesson_teacher($lesson_id)
    {
        $jsonTeacher ="";
        $teacherList = $this->get_lesson_teacher($lesson_id);
        foreach ($teacherList as $key => $row) {
            $id = $row;
            $jsonTeacher = $jsonTeacher ."\"". $key ."\":{\"teacherId\":\"".$row."\",\"teacherName\":\"".ucfirst($this->get_teacher_name($row))."\"},";
        }
        $jsonTeacher = "\"teacher\" : {".rtrim($jsonTeacher, ',')."}";
        return $jsonTeacher;
    }


    public function create_log($data)
    {
        $data['timestamp'] = strtotime(date('Y-m-d') . ' ' . date('H:i:s'));
        $data['ip'] = $_SERVER["REMOTE_ADDR"];
        $location = new SimpleXMLElement(file_get_contents('http://freegeoip.net/xml/' . $_SERVER["REMOTE_ADDR"]));
        $data['location'] = $location->City . ' , ' . $location->CountryName;
        $this->db->insert('log', $data);
    }
    public function is_paid($id)
    {
        $query = $this->db->get_where('user', array('user_id'=>$id));
        $array = $query->result_array();
        if($array[0]['is_paid'] == 0) {
            return false;
        } else {
            return true;
        }
    }

    public function get_system_settings()
    {
        $query = $this->db->get('settings');
        return $query->result_array();
    }



    public function curl_request($code = '')
    {
        return true;
        $product_code = $code;

        $personal_token = "FkA9UyDiQT0YiKwYLK3ghyFNRVV9SeUn";
        $url = "https://api.envato.com/v3/market/author/sale?code=".$product_code;
        $curl = curl_init($url);

        //setting the header for the rest of the api
        $bearer   = 'bearer ' . $personal_token;
        $header   = array();
        $header[] = 'Content-length: 0';
        $header[] = 'Content-type: application/json; charset=utf-8';
        $header[] = 'Authorization: ' . $bearer;

        $verify_url = 'https://api.envato.com/v1/market/private/user/verify-purchase:'.$product_code.'.json';
        $ch_verify = curl_init($verify_url . '?code=' . $product_code);

        curl_setopt($ch_verify, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch_verify, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch_verify, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch_verify, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch_verify, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

        $cinit_verify_data = curl_exec($ch_verify);
        curl_close($ch_verify);

        $response = json_decode($cinit_verify_data, true);

        if (count($response['verify-purchase']) > 0) {
            return true;
        } else {
            return false;
        }

    }


    public function delete_student($student_id)
    {
        // deleting data of student from all associated tables
        $tables = array('student', 'attendance', 'book_request', 'enroll', 'invoice', 'mark', 'payment');
        $this->db->delete($tables, array('student_id' => $student_id));
        // deleting data from messages
        $threads = $this->db->get('message_thread')->result_array();
        if (count($threads) > 0) {
            foreach ($threads as $row) {
                $sender = explode('-', $row['sender']);
                $receiver = explode('-', $row['reciever']);
                if (($sender[0] == 'student' && $sender[1] == $student_id) || ($receiver[0] == 'student' && $receiver[1] == $student_id)) {
                    $thread_code = $row['message_thread_code'];
                    $this->db->delete('message', array('message_thread_code' => $thread_code));
                    $this->db->delete('message_thread', array('message_thread_code' => $thread_code));
                }
            }
        }
    }
    ////// TIME / DATE //////
    public function getTimeDiff($dtime, $atime)
    {
        $nextDay = $dtime>$atime ? 1 : 0;
        $dep = explode(':', $dtime);
        $arr = explode(':', $atime);
        $diff = abs(mktime($dep[0], $dep[1], 0, date('n'), date('j'), date('y'))-mktime($arr[0], $arr[1], 0, date('n'), date('j')+$nextDay, date('y')));
        $hours = floor($diff/(60*60));
        $mins = floor(($diff-($hours*60*60))/(60));
        $secs = floor(($diff-(($hours*60*60)+($mins*60))));
        if(strlen($hours)<2) {
            $hours="0".$hours;
        }
        if(strlen($mins)<2) {
            $mins="0".$mins;
        }
        if(strlen($secs)<2) {
            $secs="0".$secs;
        }
        return $hours.':'.$mins;
    }

    public function getLastAssignedS($id)
    {
        $list = $this->get_student_lesson($id);
        if(count($list) != 0) {
            $lastestDate = null;
            foreach ($list as $row) {
                $currDate = $this->date_to_iso($this->get_course_end($row['course_id']));
                if(strtotime($currDate) >= strtotime($lastestDate)) {
                    $lastestDate = $currDate;
                }
            }
            return $lastestDate;
        } else {
            return null;
        }
    }
    public function getLastAssignedT($id)
    {
        $list = $this->get_teacher_lesson($id);
        $debug = array();
        if(count($list) != 0) {
            $lastestDate = null;
            foreach ($list as $row) {
                $currDate = $this->date_to_iso($this->get_course_end($row['course_id']));

                if(strtotime($currDate) >= strtotime($lastestDate)) {
                    $lastestDate = $currDate;
                    array_push($debug, $currDate);

                }
            }
            //return $debug;
            return $lastestDate;
        } else {
            return null;
        }
    }
    public function getCreation($id)
    {
        $query = $this->db->get_where('user', array('user_id' => $id));
        $query = $query->result_array();
        foreach ($query as $row) {
            return $row['CreationDate'];
        }
    }

    public function checkArchiveState($id, $param)
    {
        if($param == "courses") {
            $query = $this->db->get_where('course_new', array('ID' => $id));
            $query = $query->result_array();
            foreach ($query as $row) {
                if($row['Archived'] == 1) {
                    return true;
                } else {
                    return false;
                }
            }
        } elseif($param == "user") {
            $query = $this->db->get_where('user', array('user_id' => $id));
            $query = $query->result_array();
            foreach ($query as $row) {
                if($row['Archived'] == 1) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return false;
        }
    }

    ////// prevent duplication //////
    public function check_assigned_t($cid, $lid, $tid)
    {
        $query = $this->db->get_where('course_teacher', array('course_id' => $cid,'teacher_id' => $tid,'lesson_id' => $lid));
        $query = $query->result_array();
        if(!empty($query)) {
            return true;
        } else {
            return false;
        }
    }
    public function check_assigned_s($cid, $lid, $sid)
    {
        $query = $this->db->get_where('course_student_new', array('course_id' => $cid,'student_id' => $sid,'lesson_id' => $lid));
        $query = $query->result_array();
        if(!empty($query)) {
            return true;
        } else {
            return false;//$query;
        }
    }

    public function is_final($user)
    {
        $this->db->select('is_final');
        $query = $this->db->get_where('user', array('user_id' => $user));
        $query = $query->result_array();
        //return $query;
        //  if(count($query)>0){
        if($query[0]['is_final'] == "0") {
            return false;
        } else {
            return true;
        }
        //  }else{
        //      return false;
        //  }

    }
    ////// notification ////

    public function get_event_name($id)
    {
        $query = $this->db->get_where('notification_event_type', array("ID"=>$id));
        $res = $query->result_array();
        return $res[0]['Type'];
    }
    public function get_notification_rules()
    {
        $query = $this->db->get_where('notification_rules');
        return $query->result_array();
    }
    public function get_notification_events()
    {
        $query = $this->db->get_where('notification_event_rule');
        $res = $query->result_array();
        foreach ($res as $key => $ev) {
            $res[$key]['template'] = $this->mail_model->get_tpl_name($ev['Template_ID']);
            $res[$key]['event'] = $this->get_event_name($ev['Event_ID']);
        }
        return $res;
    }
    public function get_notification_event_types()
    {
        $query = $this->db->get_where('notification_event_type');
        return $query->result_array();
    }
    public function notify_candidate()
    {
        if($this->notify_candidate_s() != "0") {
            return true;
        }
        if($this->notify_pending_s() != "0") {
            return true;
        }
        if($this->notify_candidate_t() != "0") {
            return true;
        }
        if($this->notify_pending_t() != "0") {
            return true;
        }
        return false;
    }

    public function notify_candidate_s()
    {
        $query = $this->db->get_where('candidate_student', array('status' => '0'));
        $query = $query->result_array();
        return count($query);
    }

    public function notify_customer()
    {
        $query = $this->db->query(
            "SELECT COUNT(st.ID) as num
            FROM `shop_transaction` st
            LEFT JOIN user u ON st.User_ID = u.user_id
            WHERE st.Notified = 0 AND u.is_final=0"
        );
        $result = $query->result();
        return (int) $result[0]->num;
    }

    public function notify_pending_s()
    {
        $query = $this->db->get_where('candidate_student', array('status' => '1'));
        $query = $query->result_array();
        return count($query);
    }
    public function notify_candidate_t()
    {
        $query = $this->db->get_where('candidate_teacher', array('status' => '0'));
        $query = $query->result_array();
        return count($query);
    }
    public function notify_pending_t()
    {
        $query = $this->db->get_where('candidate_teacher', array('status' => '1'));
        $query = $query->result_array();
        return count($query);
    }

    //++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++
    //
    //               update
    //
    //++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++
    //++++++++++++++++++++++++++++++++++++++
    public function get_student_hours_total($id)
    {
        $hours = 0;
        $data = $this->db->get_where('course_student_new', array('student_id' => $id))->result_array();
        if(!empty($data)) {
            foreach ($data as $row) {
                $hours = $hours + $this->get_lesson_duration($row['lesson_id']);
            }
        } else {
            $data2 = $this->db->get_where('course_student_new', array('group_id' => $id));
            $data2 = $data2->result_array();
            foreach ($data2 as $row2) {
                $hours = $hours + $this->get_lesson_duration($row2['lesson_id']);
            }
        }
        return $hours;
    }

    public function get_students_course_info($id)
    {
        $courses_id = $this->get_students_course($id);
        $courses = array();
        foreach ($courses_id as $course_id) {
            $data =  $this->get_course_info($course_id);
            $data[0]['Course_Price'] = $this->course_shop_price($course_id);
            $data[0]['Course_Title'] =  $this->get_course_product($course_id)['Course_Title'];
            $data[0]['Course_Sub_Title'] =  $this->get_course_product($course_id)['Course_Sub_Title'];
            $data[0]['Course_Description'] =  $this->get_course_product($course_id)['Course_Description'];
            if($data[0]['Archived']!=1) {
                array_push($courses, $data);
            }

        }
        return $courses;
    }//return array of courseID

    public function get_course_product($id)
    {

        $query= $this->db->get_where('course_product', array('Course_ID'=>$id));
        return $query->result_array()[0];
    }
    
    public function get_bro($id)
    {
        $bro = array();
        $parent_array = $this->get_child_parent($id);
        if($parent_array == 0) {
            return;
        }
        //var_dump($parent_array);
        if(is_array($parent_array)) {
            foreach ($parent_array as $parent) {
                $childs = $this->get_parent_child($parent);

                //echo ">> $childs";
                $bro = array_merge($bro, $childs);
            }
        } else {
            $bro = $this->get_parent_child2($parent_array);
        }
        //
        //$key = array_search($id, $bro[0]);
        foreach ($bro as $key => $b) {
            //var_dump(array_key_first($b));
            if(array_key_first($b) == $id) {
                unset($bro[$key]);
            }
        }
        //var_dump($bro);
        return $bro;
    }

    public function get_role()
    {
        $role = $this->db->get('role')->result_array();
        return $role;
    }

    public function get_roles_ordered()
    {
        $roles = [];
        foreach ($this->db->get('role')->result_array() as $r) {
            $roles[(int) $r['role_id']] = $r;
        }

        return $roles;
    }

    public function get_user_role($id)
    {
        $data = $this->db->get_where('role', array('role_id'=>$id))->result_array();
        return $data[0]['code'];
    }
    public function get_role_of_user($id)
    {
        $data = $this->db->get_where('user', array('user_id'=>$id))->result_array();
        return $data[0]['role_id'];
    }

    public function date_to_iso($date)
    {
        $date = str_replace('/', '-', $date);
        return date('Y-m-d', strtotime($date));
    }
    public function date_to_fr($date)
    {
        return date('d/m/Y', strtotime($date));
    }
    public function date_to_iso_fr($date)
    {
        $date = str_replace('/', '-', $date);

        if(strpos($date, '-')>2) {
            $date = date('d-m-Y', strtotime($date));
        }
        return $date;
    }

    public function clean_json($string)
    {
        $clean_string = strip_tags($string);
        $clean_string = preg_replace('/\s\s+/', ' ', trim($clean_string));
        $clean_string = str_replace("'", "", $clean_string);
        $clean_string = preg_replace('/\R/', '', $clean_string);
        $clean_string = addslashes($clean_string);
        return $clean_string;
    }
    public function cleanArray($array)
    {
        //remove empty fields
        foreach ($array as $key => $value) {

            if($value == "") {
                unset($array[$key]);

            }
        }
        return $array;
    }


    //only for teachers
    public function show_email_parent($id)
    {
        $mail = $this->get_user_email($id);
        if(strpos($mail, 'langue') > -1) {
            $parent = $this->get_child_parent($id);
            $mail_p = $this->get_user_email($parent);
            if($mail_p != "" && $mail_p != null) {
                return $mail_p ." <span class='entypo-info btn btn-info btn-sm' title='parent email'></span>";
            }
        } else {
            return $mail;
        }
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit