<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Course_model extends CI_Model 
{

    // Course Category Start
    function get_all_course_category()
    {
        $this->db->select('a.*');
        $this->db->from('course_category a');
        return $this->db->get()->result();
    }

    function get_course_category_by_id($id)
    {
        $this->db->where('category_id', $id);
        $this->db->from('course_category');
        return $this->db->get()->row();
    }

    public function add_new_course_category()
    {
        $data = array(
            'category_name' => $this->input->post('category_name'), 
            'category_slug' => slug($this->input->post('category_name')), 
            'category_icon' => $this->input->post('category_icon'), 
        );

        return $this->db->insert('course_category', $data);
    }

    public function update_course_category($id)
    {
        $data = array(
            'category_name' => $this->input->post('category_name'), 
            'category_slug' => slug($this->input->post('category_name')), 
            'category_icon' => $this->input->post('category_icon'), 
        );

        $this->db->where('category_id', $id);
        return $this->db->update('course_category', $data);
    }

    public function delete_course_category($id)
    {
        $this->db->where('category_id', $id);
        return $this->db->delete('course_category');
    }

    // Course Category End

    // Course Add Start

    public function add_course()
    {
        $outcomes = $this->trim_and_return_json($this->input->post('outcomes'));
        $requirements = $this->trim_and_return_json($this->input->post('requirements'));
        $data['rand_id'] = rand();
        $data['slug'] = slug($this->input->post('title'));
        $data['title'] =$this->input->post('title');
        $data['short_description'] = $this->input->post('short_description');
        $data['description'] = $this->input->post('description');
        $data['outcomes'] = $outcomes;
        $data['language'] = $this->input->post('language_made_in');
        $data['requirements'] = $requirements;
        $data['price'] = $this->input->post('price');
        $data['discount_flag'] = $this->input->post('discount_flag');
        $data['discounted_price'] = $this->input->post('discounted_price');
        $data['level'] = $this->input->post('level');
        $data['is_free_course'] = $this->input->post('is_free_course');
        $data['video_url'] = html_escape($this->input->post('course_overview_url'));
        $data['course_short_name'] = $this->input->post('course_short_name');
        $data['duration'] = $this->input->post('duration');
        $data['couse_time'] = $this->input->post('couse_time');
        $data['time'] = $this->input->post('time');
        $data['flagship_course'] = $this->input->post('flagship_course') == 1 ? 1 : 0;
        if ($this->input->post('course_overview_url') != "") {
            $data['course_overview_provider'] = html_escape($this->input->post('course_overview_provider'));
        } else {
            $data['course_overview_provider'] = "";
        }

        $data['date_added'] = date('Y-m-d');
        $data['section'] = json_encode(array());
        $data['is_top_course'] = $this->input->post('is_top_course');
        $data['user_id'] = $this->session->userdata('user_id');
        $data['meta_description'] = $this->input->post('meta_description');
        $data['meta_keywords'] = $this->input->post('meta_keywords');
        $data['category_id'] = $this->input->post('category_id');
        $data['sub_category_id'] = $this->input->post('sub_category_id');
        $data['online_training_price'] = $this->input->post('online_training_price');
        $data['online_training_has_discount'] = $this->input->post('online_training_has_discount') == 1 ? 1 : 0;
        $data['online_training_discount'] = $this->input->post('online_training_discount') == 1 ? 1 : 0;
        $data['discounted_price'] = $this->input->post('discounted_price');
        $data['online_training_discount'] = $this->input->post('online_training_discount');
        $data['course_adv_payment'] = $this->input->post('course_adv_payment');
        $data['checkout_plans_title']          =  json_encode($this->input->post('checkout_plans_title'));
        $data['checkout_plans_description']    =  json_encode($this->input->post('checkout_plans_description'));
        $data['checkout_heading']              =  $this->input->post('checkout_heading');
        $data['checkout_sub_heading']          =  $this->input->post('checkout_sub_heading');
        $data['checkout_video_or_image']       =  $this->input->post('checkout_video_or_image');
        $data['checkout_iframe']               =  $this->input->post('checkout_iframe');
        $data['checkout_description']          =  $this->input->post('checkout_description');
        $data['checkout_money_sub_heading']    =  $this->input->post('checkout_money_sub_heading');
        $data['checkout_money_heading']        =  $this->input->post('checkout_money_heading');
        $data['checkout_money_description']    =  $this->input->post('checkout_money_description');
        $data['checkout_head_code']            =  $this->input->post('checkout_head_code');
        $data['trainner_profile_heading']            =  $this->input->post('trainner_profile_heading');
        $data['trainner_profile_content']            =  $this->input->post('trainner_profile_content');
        $data['certification']            =  $this->input->post('certification');
        $data['assignments']              =  $this->input->post('assignments');
        $data['project']                  =  $this->input->post('project');
        $data['review_video']             =  json_encode($this->input->post('review_video'));
        $data['checkout_plan_title']      =  $this->input->post('checkout_plan_title');
        $data['meta_title']               =  $this->input->post('meta_title');

        $data['special_fb_group']               =  $this->input->post('special_fb_group');
        $data['special_whatsapp_group']         =  $this->input->post('special_whatsapp_group');
        $data['cook_demo_video']          =  $this->input->post('cook_demo_video');
        $data['schema']                   =  $this->input->post('schema');
        $data['course_subject']             = json_encode($this->input->post('course_subject'));

        if(!empty($_FILES['checkout_image']['name']))
        {
          $_FILES['file']['name'] = $_FILES['checkout_image']['name'];
          $_FILES['file']['type'] = $_FILES['checkout_image']['type'];
          $_FILES['file']['tmp_name'] = $_FILES['checkout_image']['tmp_name'];
          $_FILES['file']['error'] = $_FILES['checkout_image']['error'];
          $_FILES['file']['size'] = $_FILES['checkout_image']['size'];
          $config['upload_path'] = 'uploads/'; 
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
          $config['file_name'] = uniqid().$_FILES['checkout_image']['name'];
          $this->upload->initialize($config);
          $this->load->library('upload',$config); 
          if($this->upload->do_upload('file'))
          {
              $uploadData = $this->upload->data();
              $filename = $uploadData['file_name'];

               if (isset($filename)) 
              {
                  $data['checkout_image'] = 'uploads/' . $filename;

              }
          }
        }
        if(!empty($_FILES['checkout_money_image']['name']))
        {
            $_FILES['file']['name'] = $_FILES['checkout_money_image']['name'];
            $_FILES['file']['type'] = $_FILES['checkout_money_image']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['checkout_money_image']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['checkout_money_image']['error'];
            $_FILES['file']['size'] = $_FILES['checkout_money_image']['size'];
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000';
            $config['file_name'] = uniqid().$_FILES['checkout_money_image']['name'];
            $this->upload->initialize($config);
            $this->load->library('upload',$config); 
            if($this->upload->do_upload('file'))
            {
                $uploadData = $this->upload->data();
                $filename = $uploadData['file_name'];

                 if (isset($filename)) 
                {
                    $data['checkout_money_image'] = 'uploads/' . $filename;

                }
            }
        }
        if(!empty($_FILES['curriculum_pdf']['name']))
        {
            $_FILES['file']['name'] = $_FILES['curriculum_pdf']['name'];
            $_FILES['file']['type'] = $_FILES['curriculum_pdf']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['curriculum_pdf']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['curriculum_pdf']['error'];
            $_FILES['file']['size'] = $_FILES['curriculum_pdf']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'pdf';
            $config['max_size'] = '200000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['curriculum_pdf']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
              if($this->upload->do_upload('file'))
              {
                  // Get data about the file
                  $uploadData = $this->upload->data();
                  $filename = $uploadData['file_name'];

                   if (isset($filename)) 
                  {
                      $data['curriculum_pdf'] = 'uploads/' . $filename;

                  }

              }
        }

        if(!empty($_FILES['syllabus']['name']))
        {
            $_FILES['file']['name'] = $_FILES['syllabus']['name'];
            $_FILES['file']['type'] = $_FILES['syllabus']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['syllabus']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['syllabus']['error'];
            $_FILES['file']['size'] = $_FILES['syllabus']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'pdf';
            $config['max_size'] = '20000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['syllabus']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
              if($this->upload->do_upload('file'))
              {
                  // Get data about the file
                  $uploadData = $this->upload->data();
                  $filename = $uploadData['file_name'];

                   if (isset($filename)) 
                  {
                      $data['syllabus'] = 'uploads/' . $filename;

                  }

              }
        }
        if(!empty($_FILES['online_syllabus']['name']))
        {
            $_FILES['file']['name'] = $_FILES['online_syllabus']['name'];
            $_FILES['file']['type'] = $_FILES['online_syllabus']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['online_syllabus']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['online_syllabus']['error'];
            $_FILES['file']['size'] = $_FILES['online_syllabus']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'pdf';
            $config['max_size'] = '20000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['online_syllabus']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
              if($this->upload->do_upload('file'))
              {
                  // Get data about the file
                  $uploadData = $this->upload->data();
                  $filename = $uploadData['file_name'];

                   if (isset($filename)) 
                  {
                      $data['online_syllabus'] = 'uploads/' . $filename;

                  }

              }
        }
        if(!empty($_FILES['image']['name']))
        {
            $_FILES['file']['name'] = $_FILES['image']['name'];
            $_FILES['file']['type'] = $_FILES['image']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['image']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['image']['error'];
            $_FILES['file']['size'] = $_FILES['image']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/course/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '20000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['image']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
              if($this->upload->do_upload('file'))
              {
                  // Get data about the file
                  $uploadData = $this->upload->data();
                  $filename = $uploadData['file_name'];

                  if (isset($filename)) 
                  {
                      $data['image'] = 'uploads/course/' . $filename;
                      // Create thumbnail
                      $thumbnailPath = 'uploads/course/thumbnails/';
                      $config['image_library'] = 'gd2';
                      $config['source_image'] = $data['image'];
                      $config['new_image'] = $thumbnailPath . $filename;
                      $config['create_thumb'] = TRUE;
                      $config['maintain_ratio'] = TRUE;
                      $config['width'] = 500; // Set the desired thumbnail width
                      $config['height'] = 500; // Set the desired thumbnail height
                      $config['thumb_marker'] = '';

                      $this->load->library('image_lib', $config);
                      $this->image_lib->initialize($config);
                      $this->image_lib->resize();

                      $data['thumbnail'] = $config['new_image'];

                  }

              }
        }
        return $this->db->insert('course', $data);
    }

    public function edit_course($id)
    {
        $outcomes = $this->trim_and_return_json($this->input->post('outcomes'));
        $requirements = $this->trim_and_return_json($this->input->post('requirements'));
        $data['slug'] = slug($this->input->post('title'));
        $data['title'] = html_escape($this->input->post('title'));
        $data['short_description'] = $this->input->post('short_description');
        $data['description'] = $this->input->post('description');
        $data['outcomes'] = $outcomes;
        $data['language'] = $this->input->post('language_made_in');
        $data['requirements'] = $requirements;
        $data['price'] = $this->input->post('price');
        $data['discount_flag'] = $this->input->post('discount_flag');
        $data['discounted_price'] = $this->input->post('discounted_price');
        $data['level'] = $this->input->post('level');
        $data['is_free_course'] = $this->input->post('is_free_course');
        $data['video_url'] = html_escape($this->input->post('course_overview_url'));
        $data['duration'] = $this->input->post('duration');
        $data['couse_time'] = $this->input->post('couse_time');
        $data['time'] = $this->input->post('time');
        $data['course_short_name'] = $this->input->post('course_short_name');
        $data['flagship_course'] = $this->input->post('flagship_course') == 1 ? 1 : 0;
        $data['category_id'] = $this->input->post('category_id');
        if ($this->input->post('course_overview_url') != "") {
            $data['course_overview_provider'] = html_escape($this->input->post('course_overview_provider'));
        } else {
            $data['course_overview_provider'] = "";
        }

        $data['date_added'] = date('Y-m-d');
        $data['section'] = json_encode(array());
        $data['is_top_course'] = $this->input->post('is_top_course');
        $data['user_id'] = $this->session->userdata('user_id');
        $data['meta_description'] = $this->input->post('meta_description');
        $data['meta_keywords'] = $this->input->post('meta_keywords');
        $data['online_training_price'] = $this->input->post('online_training_price');
        $data['online_training_has_discount'] = $this->input->post('online_training_has_discount') == 1 ? 1 : 0;
        $data['online_training_discount'] = $this->input->post('online_training_discount') == 1 ? 1 : 0;
        $data['discounted_price'] = $this->input->post('discounted_price');
        $data['online_training_discount'] = $this->input->post('online_training_discount');
        $data['course_adv_payment'] = $this->input->post('course_adv_payment');
        $data['checkout_plans_title']          =  json_encode($this->input->post('checkout_plans_title'));
        $data['checkout_plans_description']    =  json_encode($this->input->post('checkout_plans_description'));
        $data['checkout_heading']              =  $this->input->post('checkout_heading');
        $data['checkout_sub_heading']          =  $this->input->post('checkout_sub_heading');
        $data['checkout_video_or_image']       =  $this->input->post('checkout_video_or_image');
        $data['checkout_iframe']               =  $this->input->post('checkout_iframe');
        $data['checkout_description']          =  $this->input->post('checkout_description');
        $data['checkout_money_sub_heading']    =  $this->input->post('checkout_money_sub_heading');
        $data['checkout_money_heading']        =  $this->input->post('checkout_money_heading');
        $data['checkout_money_description']    =  $this->input->post('checkout_money_description');
        $data['checkout_head_code']            =  $this->input->post('checkout_head_code');
        $data['trainner_profile_heading']            =  $this->input->post('trainner_profile_heading');
        $data['trainner_profile_content']            =  $this->input->post('trainner_profile_content');
        $data['certification']            =  $this->input->post('certification');
        $data['assignments']              =  $this->input->post('assignments');
        $data['project']                  =  $this->input->post('project');
        $data['review_video']             =  json_encode($this->input->post('review_video'));
        $data['checkout_plan_title']      =  $this->input->post('checkout_plan_title');
        $data['meta_title']               =  $this->input->post('meta_title');
        $data['special_fb_group']               =  $this->input->post('special_fb_group');
        $data['special_whatsapp_group']         =  $this->input->post('special_whatsapp_group');
        $data['cook_demo_video']          =  $this->input->post('cook_demo_video');
        $data['schema']                   =  $this->input->post('schema');
        $data['course_subject']             = json_encode($this->input->post('course_subject'));
        
        if(!empty($_FILES['checkout_image']['name']))
        {
          $_FILES['file']['name'] = $_FILES['checkout_image']['name'];
          $_FILES['file']['type'] = $_FILES['checkout_image']['type'];
          $_FILES['file']['tmp_name'] = $_FILES['checkout_image']['tmp_name'];
          $_FILES['file']['error'] = $_FILES['checkout_image']['error'];
          $_FILES['file']['size'] = $_FILES['checkout_image']['size'];
          $config['upload_path'] = 'uploads/'; 
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
          $config['file_name'] = uniqid().$_FILES['checkout_image']['name'];
          $this->upload->initialize($config);
          $this->load->library('upload',$config); 
          if($this->upload->do_upload('file'))
          {
              $uploadData = $this->upload->data();
              $filename = $uploadData['file_name'];

               if (isset($filename)) 
              {
                  $data['checkout_image'] = 'uploads/' . $filename;

              }
          }
        }
        if(!empty($_FILES['checkout_money_image']['name']))
      {
        $_FILES['file']['name'] = $_FILES['checkout_money_image']['name'];
        $_FILES['file']['type'] = $_FILES['checkout_money_image']['type'];
        $_FILES['file']['tmp_name'] = $_FILES['checkout_money_image']['tmp_name'];
        $_FILES['file']['error'] = $_FILES['checkout_money_image']['error'];
        $_FILES['file']['size'] = $_FILES['checkout_money_image']['size'];
        $config['upload_path'] = 'uploads/'; 
        $config['allowed_types'] = 'jpg|jpeg|png|gif';
        $config['max_size'] = '5000';
        $config['file_name'] = uniqid().$_FILES['checkout_money_image']['name'];
        $this->upload->initialize($config);
        $this->load->library('upload',$config); 
        if($this->upload->do_upload('file'))
        {
            $uploadData = $this->upload->data();
            $filename = $uploadData['file_name'];

             if (isset($filename)) 
            {
                $data['checkout_money_image'] = 'uploads/' . $filename;

            }
        }
      }
        if(!empty($_FILES['curriculum_pdf']['name']))
        {
            $_FILES['file']['name'] = $_FILES['curriculum_pdf']['name'];
            $_FILES['file']['type'] = $_FILES['curriculum_pdf']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['curriculum_pdf']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['curriculum_pdf']['error'];
            $_FILES['file']['size'] = $_FILES['curriculum_pdf']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'pdf';
            $config['max_size'] = '200000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['curriculum_pdf']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
              if($this->upload->do_upload('file'))
              {
                  // Get data about the file
                  $uploadData = $this->upload->data();
                  $filename = $uploadData['file_name'];

                   if (isset($filename)) 
                  {
                      $data['curriculum_pdf'] = 'uploads/' . $filename;

                  }

              }
        }

        if(!empty($_FILES['syllabus']['name']))
         {
            $_FILES['file']['name'] = $_FILES['syllabus']['name'];
            $_FILES['file']['type'] = $_FILES['syllabus']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['syllabus']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['syllabus']['error'];
            $_FILES['file']['size'] = $_FILES['syllabus']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'pdf';
            $config['max_size'] = '20000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['syllabus']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
              if($this->upload->do_upload('file'))
              {
                  // Get data about the file
                  $uploadData = $this->upload->data();
                  $filename = $uploadData['file_name'];

                   if (isset($filename)) 
                  {
                      $data['syllabus'] = 'uploads/' . $filename;

                  }

              }
          }
          if(!empty($_FILES['online_syllabus']['name']))
         {
            $_FILES['file']['name'] = $_FILES['online_syllabus']['name'];
            $_FILES['file']['type'] = $_FILES['online_syllabus']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['online_syllabus']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['online_syllabus']['error'];
            $_FILES['file']['size'] = $_FILES['online_syllabus']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/'; 
            $config['allowed_types'] = 'pdf';
            $config['max_size'] = '20000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['online_syllabus']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
              if($this->upload->do_upload('file'))
              {
                  // Get data about the file
                  $uploadData = $this->upload->data();
                  $filename = $uploadData['file_name'];

                   if (isset($filename)) 
                  {
                      $data['online_syllabus'] = 'uploads/' . $filename;

                  }

              }
          }
         if(!empty($_FILES['image']['name']))
         {
            // $image = $this->get_course_by_org_id($id);
            // if(file_exists($image->image))
            // {
            //     unlink($image->image);
            // }
            $_FILES['file']['name'] = $_FILES['image']['name'];
            $_FILES['file']['type'] = $_FILES['image']['type'];
            $_FILES['file']['tmp_name'] = $_FILES['image']['tmp_name'];
            $_FILES['file']['error'] = $_FILES['image']['error'];
            $_FILES['file']['size'] = $_FILES['image']['size'];

            // Set preference
            $config['upload_path'] = 'uploads/course/'; 
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '20000'; // max_size in kb
            $config['file_name'] = uniqid().$_FILES['image']['name'];
            $this->upload->initialize($config);
            //Load upload library
            $this->load->library('upload',$config); 

            // File upload
              if($this->upload->do_upload('file'))
              {
                  // Get data about the file
                  $uploadData = $this->upload->data();
                  $filename = $uploadData['file_name'];

                  if (isset($filename)) 
                  {
                      $data['image'] = 'uploads/course/' . $filename;

                      // Create thumbnail
                      $thumbnailPath = 'uploads/course/thumbnails/';
                      $config['image_library'] = 'gd2';
                      $config['source_image'] = $data['image'];
                      $config['new_image'] = $thumbnailPath . $filename;
                      $config['create_thumb'] = TRUE;
                      $config['maintain_ratio'] = TRUE;
                      $config['width'] = 500; // Set the desired thumbnail width
                      $config['height'] = 500; // Set the desired thumbnail height
                      $config['thumb_marker'] = '';

                      $this->load->library('image_lib', $config);
                      $this->image_lib->initialize($config);
                      $this->image_lib->resize();

                      $data['thumbnail'] = $config['new_image'];

                  }

              }
          }
        $this->db->where('id', $id);
        return $this->db->update('course', $data);
    }

    function get_all_course()
    {
       return $this->db->get('course')->result();
    }

    function delete_course($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete('course');
    }

    function get_course_by_id($id)
    {
        $this->db->where('id', $id);
        return $this->db->get('course')->row();
    }

    function get_course_name_for_view_all($id)
    {
        $this->db->select('title');
        $this->db->where('id', $id);
        return $this->db->get('course')->row();
    }

    public function get_all_admin_added_course_for_fee_setting($atp_id)
    {

        $this->db->select('id, title');
        // $this->db->where('creator', 1);
        // $this->db->where('course_mode !=', 1);
        return $this->db->get('course')->result();
    }

    public function update_course_price_for_atp()
    {
        $this->db->where('atp_c_p_atp_id', $this->input->post('atp_c_p_atp_id'));
        $this->db->where('atp_c_p_course_id', $this->input->post('atp_c_p_course_id'));
        $price = $this->db->get('atp_course_price')->row();
        if($price)
        {
            $data = array(
                'atp_c_p_course_price' => $this->input->post('atp_c_p_course_price'),
            );
            $this->db->where('atp_c_p_id', $price->atp_c_p_id);
            return $this->db->update('atp_course_price', $data);
        }
        else
        {
            $data = array(
                'atp_c_p_atp_id' => $this->input->post('atp_c_p_atp_id'),
                'atp_c_p_course_id' => $this->input->post('atp_c_p_course_id'),
                'atp_c_p_course_price' => $this->input->post('atp_c_p_course_price'),
            );
            return $this->db->insert('atp_course_price', $data);
        }
    }

    function get_course_by_id_for_enquiry_page($id)
    {
        $this->db->select('title');
        $this->db->where('id', $id);
        return $this->db->get('course')->row();
    }

    // Course Add End

    // Course Lession Add Start

    function get_all_lession()
    {
        $this->db->select('a.*');
        $this->db->from('course_lession a');
        $this->db->order_by('a.order_by', 'ASC');
        return $this->db->get()->result();
    }

    function get_all_lession_by_course($id)
    {
        $this->db->select('a.*');
        $this->db->from('course_lession a');
        $this->db->where('a.course', $id);
        $this->db->order_by('a.order_by', 'ASC');
        return $this->db->get()->result();
    }

    function get_ass_pdf_by_c_id_and_lession_id($c_id, $l_id)
    {
        $this->db->where('clp_l_id', $l_id);
        $this->db->where('clp_c_id', $c_id);
        return $this->db->get('course_lession_assignment_pdf')->result();
    }

    function get_ass_pdf_by_c_id_and_lession_id_for_c_details_page($c_id, $l_id)
    {
        $this->db->where('clp_l_id', $l_id);
        $this->db->where('clp_c_id', $c_id);
        return $this->db->get('course_lession_assignment_pdf')->num_rows();
    }

    function get_mini_pdf_by_c_id_and_lession_id($c_id, $l_id)
    {
        $this->db->where('clpm_l_id', $l_id);
        $this->db->where('clpm_c_id', $c_id);
        return $this->db->get('course_lession_mini_project_pdf')->result();
    }

    function get_mini_pdf_by_c_id_and_lession_id_for_c_details_page($c_id, $l_id)
    {
        $this->db->where('clpm_l_id', $l_id);
        $this->db->where('clpm_c_id', $c_id);
        return $this->db->get('course_lession_mini_project_pdf')->num_rows();
    }

    function add_new_lession()
    {
        $data = array(
            'course' => $this->input->post('course'), 
            'l_title' => $this->input->post('title'), 
            'l_description' => $this->input->post('description'), 
            'order_by' => $this->input->post('order_by'), 
            'section' => json_encode($this->input->post('section')), 
            'content' => json_encode($this->input->post('content')), 
        );
        $this->db->insert('course_lession', $data);
        $insert_id = $this->db->insert_id();

        if(!empty($_FILES['assignments_pdf']['name']))
        {
            $countfiles = count($_FILES['assignments_pdf']['name']);
            for($i=0;$i<$countfiles;$i++)
            {
                if(!empty($_FILES['assignments_pdf']['name'][$i]))
                {
                    $_FILES['file']['name'] = $_FILES['assignments_pdf']['name'][$i];
                    $_FILES['file']['type'] = $_FILES['assignments_pdf']['type'][$i];
                    $_FILES['file']['tmp_name'] = $_FILES['assignments_pdf']['tmp_name'][$i];
                    $_FILES['file']['error'] = $_FILES['assignments_pdf']['error'][$i];
                    $_FILES['file']['size'] = $_FILES['assignments_pdf']['size'][$i];
                    $config['upload_path'] = 'uploads/'; 
                    $config['allowed_types'] = 'pdf';
                    $config['max_size'] = '5000'; // max_size in kb
                    $config['file_name'] = uniqid().$_FILES['assignments_pdf']['name'][$i];
                    $this->load->library('upload',$config); 
                    $this->upload->initialize($config);
                    if($this->upload->do_upload('file'))
                    {
                        $uploadData = $this->upload->data();
                        $filename = $uploadData['file_name'];
                        if (isset($filename)) 
                        {
                            $pdf_a['clp_l_id'] = $insert_id;
                            $pdf_a['clp_c_id'] = $this->input->post('course');
                            $pdf_a['clp_pdf'] = 'uploads/' . $data['filenames'][] = $filename;
                            $this->db->insert('course_lession_assignment_pdf', $pdf_a);
                        }
                    }
                }
            }
        }
        if(!empty($_FILES['project_pdf']['name']))
        {
            $countfiles = count($_FILES['project_pdf']['name']);
            for($i=0;$i<$countfiles;$i++)
            {
                if(!empty($_FILES['project_pdf']['name'][$i]))
                {
                    $_FILES['file']['name'] = $_FILES['project_pdf']['name'][$i];
                    $_FILES['file']['type'] = $_FILES['project_pdf']['type'][$i];
                    $_FILES['file']['tmp_name'] = $_FILES['project_pdf']['tmp_name'][$i];
                    $_FILES['file']['error'] = $_FILES['project_pdf']['error'][$i];
                    $_FILES['file']['size'] = $_FILES['project_pdf']['size'][$i];
                    $config['upload_path'] = 'uploads/'; 
                    $config['allowed_types'] = 'pdf';
                    $config['max_size'] = '5000'; // max_size in kb
                    $config['file_name'] = uniqid().$_FILES['project_pdf']['name'][$i];
                    $this->load->library('upload',$config); 
                    $this->upload->initialize($config);
                    if($this->upload->do_upload('file'))
                    {
                        $uploadData = $this->upload->data();
                        $filename = $uploadData['file_name'];
                        if (isset($filename)) 
                        {
                            $pdf_m['clpm_l_id'] = $insert_id;
                            $pdf_m['clpm_c_id'] = $this->input->post('course');
                            $pdf_m['clpm_pdf'] = 'uploads/' . $data['filenames'][] = $filename;
                            $this->db->insert('course_lession_mini_project_pdf', $pdf_m);
                        }
                    }
                }
            }
        }
        return true;
    }

    function update_lession()
    {
        $data = array(
            'course' => $this->input->post('course'), 
            'l_title' => $this->input->post('title'), 
            'l_description' => $this->input->post('description'), 
            'order_by' => $this->input->post('order_by'), 
            'content' => json_encode($this->input->post('content')),
            'section' => json_encode($this->input->post('section')), 
        );
        $this->db->where('l_id', $this->input->post('id'));
        $this->db->update('course_lession', $data);

        if(!empty($_FILES['assignments_pdf']['name']))
        {
            $countfiles = count($_FILES['assignments_pdf']['name']);
            for($i=0;$i<$countfiles;$i++)
            {
                if(!empty($_FILES['assignments_pdf']['name'][$i]))
                {
                    $_FILES['file']['name'] = $_FILES['assignments_pdf']['name'][$i];
                    $_FILES['file']['type'] = $_FILES['assignments_pdf']['type'][$i];
                    $_FILES['file']['tmp_name'] = $_FILES['assignments_pdf']['tmp_name'][$i];
                    $_FILES['file']['error'] = $_FILES['assignments_pdf']['error'][$i];
                    $_FILES['file']['size'] = $_FILES['assignments_pdf']['size'][$i];
                    $config['upload_path'] = 'uploads/'; 
                    $config['allowed_types'] = 'pdf';
                    $config['max_size'] = '5000'; // max_size in kb
                    $config['file_name'] = uniqid().$_FILES['assignments_pdf']['name'][$i];
                    $this->load->library('upload',$config); 
                    $this->upload->initialize($config);
                    if($this->upload->do_upload('file'))
                    {
                        $uploadData = $this->upload->data();
                        $filename = $uploadData['file_name'];
                        if (isset($filename)) 
                        {
                            $pdf_a['clp_l_id'] = $this->input->post('id');
                            $pdf_a['clp_c_id'] = $this->input->post('course');
                            $pdf_a['clp_pdf'] = 'uploads/' . $data['filenames'][] = $filename;
                            $this->db->insert('course_lession_assignment_pdf', $pdf_a);
                        }
                    }
                } 
            }
        }
        if(!empty($_FILES['project_pdf']['name']))
        {
            $countfiles = count($_FILES['project_pdf']['name']);
            for($i=0;$i<$countfiles;$i++)
            {
                if(!empty($_FILES['project_pdf']['name'][$i]))
                {
                    $_FILES['file']['name'] = $_FILES['project_pdf']['name'][$i];
                    $_FILES['file']['type'] = $_FILES['project_pdf']['type'][$i];
                    $_FILES['file']['tmp_name'] = $_FILES['project_pdf']['tmp_name'][$i];
                    $_FILES['file']['error'] = $_FILES['project_pdf']['error'][$i];
                    $_FILES['file']['size'] = $_FILES['project_pdf']['size'][$i];
                    $config['upload_path'] = 'uploads/'; 
                    $config['allowed_types'] = 'pdf';
                    $config['max_size'] = '5000'; // max_size in kb
                    $config['file_name'] = uniqid().$_FILES['project_pdf']['name'][$i];
                    $this->load->library('upload',$config); 
                    $this->upload->initialize($config);
                    if($this->upload->do_upload('file'))
                    {
                        $uploadData = $this->upload->data();
                        $filename = $uploadData['file_name'];
                        if (isset($filename)) 
                        {
                            $pdf_m['clpm_l_id'] = $this->input->post('id');
                            $pdf_m['clpm_c_id'] = $this->input->post('course');
                            $pdf_m['clpm_pdf'] = 'uploads/' . $data['filenames'][] = $filename;
                            $this->db->insert('course_lession_mini_project_pdf', $pdf_m);
                        }
                    }
                }
            }
        }

        return true;
    }

    function delete_ass_pdf($id)
    {
        $this->db->where('clp_id', $id);
        $data = $this->db->get('course_lession_assignment_pdf')->row();
        if(file_exists($data->clp_pdf))
        {
            unlink($data->clp_pdf);
        }
        $this->db->where('clp_id', $id);
        return $this->db->delete('course_lession_assignment_pdf');
    }

    function delete_mini_pdf($id)
    {
        $this->db->where('clpm_id', $id);
        $data = $this->db->get('course_lession_mini_project_pdf')->row();
        if(file_exists($data->clpm_pdf))
        {
            unlink($data->clpm_pdf);
        }
        $this->db->where('clpm_id', $id);
        return $this->db->delete('course_lession_mini_project_pdf');
    }

    function delete_lession($id)
    {
        $this->db->where('l_id', $id);
        return $this->db->delete('course_lession');
    }

    function count_lession_by_course($id)
    {
        $this->db->where('course', $id);
        return $this->db->get('course_lession')->num_rows();
    }

    public function get_all_home_page_course()
    {
        $this->db->select('slug, thumbnail, duration, level, title, price, short_description, id');
        $this->db->limit(8);
        return $this->db->get('course')->result();
    }

    // Course Lession Add End

    function trim_and_return_json($untrimmed_array)
    {
        $trimmed_array = array();
        if (sizeof($untrimmed_array) > 0) {
            foreach ($untrimmed_array as $row) {
                if ($row != "") {
                    array_push($trimmed_array, $row);
                }
            }
        }
        return json_encode($trimmed_array);
    }

    public function get_all_course_for_admission_by_atp_id($atp_id)
    {
        return $this->db->get('course')->result();
    }

    // Course Lession Start ---------------------------------------

    public function get_all_lession_and_chapter()
    {
        $atp_id = user_id();
        $this->db->select('a.*, b.title');
        $this->db->from('course_lession_and_chapter a');
        $this->db->join('course b', 'b.id = a.ch_course_id');
        $this->db->group_by('a.ch_course_id');
        $this->db->where('a.ch_added_by', $atp_id);
        return $this->db->get()->result();
    }

    public function add_lession_and_chapter()
    {
        $user_id = user_id();
        $data = array(
            'ch_course_id' => $this->input->post('ch_course_id'), 
            'ch_lession_name' => $this->input->post('ch_lession_name'), 
            'ch_chapters' => json_encode($this->input->post('ch_chapters')), 
            'ch_added_by' => $user_id, 
        );

        return $this->db->insert('course_lession_and_chapter', $data);
    }

    public function get_lession_and_chapter_by_id($id)
    {
        $user_id = user_id();
        $this->db->where('ch_course_id', $id);
        $this->db->where('ch_added_by', $user_id);
        return $this->db->get('course_lession_and_chapter')->result();
    }

    public function update_lession_and_chapter()
    {
        foreach ($this->input->post('ch_lession_name') as $key => $ch_lession_name) 
        {
            $data = array(
                'ch_course_id' => $this->input->post('ch_course_id'), 
                'ch_lession_name' => $this->input->post('ch_lession_name')[$key], 
                'ch_chapters' => json_encode($this->input->post('ch_chapters')[$key]), 
            );

            $this->db->where('ch_id', $this->input->post('ch_id')[$key]);
            $this->db->update('course_lession_and_chapter', $data);

        }
        return true;
    }

    public function delete_course_lession($id)
    {
        $this->db->where('ch_id', $id);
        return $this->db->delete('course_lession_and_chapter');
    }

    public function get_all_course_for_adding_lession()
    {
        $this->db->select('id, title');
        return $this->db->get('course')->result();
    }

    // Course Lession End ---------------------------------------

    // Question Start

    public function view_all_questions()
    {

        $this->db->select('a.*, b.subject_name');
        $this->db->from('exam_questions a');
        $this->db->join('course_subject b', 'b.subject_id = a.q_subject');
        $this->db->order_by('a.q_id', 'desc');
        return $this->db->get()->result();
    }

    public function add_question()
    {
        $data = array(
            'q_question'            => $this->input->post('q_question'), 
            'q_ans_a'               => $this->input->post('q_ans_a'), 
            'q_ans_b'               => $this->input->post('q_ans_b'), 
            'q_ans_c'               => $this->input->post('q_ans_c'), 
            'q_ans_d'               => $this->input->post('q_ans_d'), 
            'q_ans_correct'         => $this->input->post('q_ans_correct'), 
            'q_subject_chapter'     => $this->input->post('q_subject_chapter'), 
            // 'q_programm_id' => $this->input->post('q_programm_id'), 
            'q_subject'             => $this->input->post('q_subject'), 
            'q_course_level'        => $this->input->post('q_course_level'), 
            'q_type'                => $this->input->post('q_type'), 
            'q_added_by'            => user_id()
        );

        return $this->db->insert('exam_questions', $data);
    }

    public function get_question_by_id($id)
    {
        $this->db->where('q_id', $id);
        return $this->db->get('exam_questions')->row();
    }

    public function update_question($id)
    {
        $data = array(
            'q_question'            => $this->input->post('q_question'), 
            'q_ans_a'               => $this->input->post('q_ans_a'), 
            'q_ans_b'               => $this->input->post('q_ans_b'), 
            'q_ans_c'               => $this->input->post('q_ans_c'), 
            'q_ans_d'               => $this->input->post('q_ans_d'), 
            'q_ans_correct'         => $this->input->post('q_ans_correct'), 
            'q_subject_chapter'     => $this->input->post('q_subject_chapter'), 
            // 'q_course_id'   => $this->input->post('q_course_id'), 
            // 'q_programm_id' => $this->input->post('q_programm_id'), 
            'q_subject'             => $this->input->post('q_subject'), 
            'q_course_level'        => $this->input->post('q_course_level'), 
            'q_type'                => $this->input->post('q_type'), 
        );
        $this->db->where('q_id', $id);
        return $this->db->update('exam_questions', $data);
    }

    public function delete_question($id)
    {
        $this->db->where('q_id', $id);
        return $this->db->delete('exam_questions');
    }

    function delete_multiple_qns()
    {
        $ids = $this->input->post('ids');  
        $this->db->where_in('q_id', explode(",", $ids));  
        return $this->db->delete('exam_questions');
    }
    // Question End

    public function get_all_lession_by_course_id($id)
    {
        $user_id = user_id();
        $this->db->select('batch_course');
        $this->db->where('batch_id', $id);
        $course_id = $this->db->get('student_batch')->row();

        $this->db->select('a.*, b.title, b.id');
        $this->db->from('course_lession_and_chapter a');
        $this->db->join('course b', 'b.id = a.ch_course_id');
        $this->db->where('a.ch_course_id', $course_id->batch_course);
        // $this->db->where('a.ch_added_by', $user_id);
        return $this->db->get()->result();
    }

    function get_all_doubt_by_batch_id()
    {
        $user_id = user()->user_id;
        $this->db->select('batch_id');
        $this->db->where('instructor_id', $user_id);
        $all_batch = $this->db->get('student_batch')->result();

        $batch_array = [];
        foreach ($all_batch as $batch) 
        {
            $batch_array[] = $batch->batch_id;
        }

        // print_r($batch_array);

        $this->db->select('a.*, b.title, c.*, d.user_name');
        $this->db->from('student_doubt a');
        $this->db->join('course b', 'b.id = a.doubt_course_id');
        $this->db->join('course_lession_and_chapter c', 'c.ch_id = a.doubt_chapter');
        $this->db->join('users d', 'd.user_id = a.doubt_user_id');
        $this->db->where_in('a.doubt_batch', $batch_array);
        $this->db->where('doubt_is_first', 1);
        return $this->db->get()->result();
    }

    function submit_relplay_message_for_doubt()
    {
        $data = [
            'doubt_user_id' => $this->input->post('doubt_user_id'),
            'doubt_chapter' => $this->input->post('doubt_chapter'),
            'doubt_lession' => $this->input->post('doubt_lession'),
            'doubt_course_id' => $this->input->post('doubt_course_id'),
            'doubt_message' => $this->input->post('doubt_message'),
            'doubt_batch' => $this->input->post('doubt_batch'),
            'doubt_replay_id' => $this->input->post('doubt_replay_id'),
            'doubt_is_student' => $this->input->post('doubt_is_student'),
            'doubt_is_first' => $this->input->post('doubt_is_first'),
            'doubt_status' => 0,
        ];

        return $this->db->insert('student_doubt', $data);
    }

    // Subject Section Start

    public function get_all_subject()
    {
        $this->db->select('a.*');
        $this->db->from('course_subject a');
        $this->db->order_by('subject_id', "DESC");
        return $this->db->get()->result();
    }

    function add_new_subject()
    {
        $data['subject_name']                   = $this->input->post('subject_name');
        $data['subject_chapter']                = json_encode($this->input->post('subject_chapter'));
        return $this->db->insert('course_subject', $data);
    }

    function edit_subject($id)
    {
        $data['subject_name']                   = $this->input->post('subject_name');
        $data['subject_chapter']                = json_encode($this->input->post('subject_chapter'));
        $this->db->where('subject_id', $id);
        return $this->db->update('course_subject', $data);
    }

    public function delete_subject($id)
    {
        $this->db->where('subject_id', $id);
        return $this->db->delete('course_subject');
    }

    public function get_subject_by_id($id)
    {
        $this->db->where('subject_id', $id);
        return $this->db->get('course_subject')->row();
    }

    // Subject Section End
}
