MMCT TEAM
Server IP : 192.185.129.71  /  Your IP : 18.189.189.4
Web Server : Apache
System : Linux bh-ht-3.webhostbox.net 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64
User : svymadmin ( 4072)
PHP Version : 7.4.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home4/svymadmin/public_html/vivekaexcel.edu.in/bkp/school/application/controllers/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home4/svymadmin/public_html/vivekaexcel.edu.in/bkp/school/application/controllers/Admin.php
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Admin extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->database();
        $this->load->library('session');
        $this->load->model('Barcode_model');
        $this->load->model('Laboratory_model');
        $this->load->model('Library_model');
        $this->load->model('SchoolStockRegister_model');
        $this->load->model(array('Ajaxdataload_model' => 'ajaxload'));

       /*cache control*/
		$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');
    }

    /***default functin, redirects to login page if no admin logged in yet***/
    public function index()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($this->session->userdata('admin_login') == 1)
            redirect(site_url('admin/dashboard'), 'refresh');
    }

    /***ADMIN DASHBOARD***/
    function dashboard()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['page_name']  = 'dashboard';
        $page_data['page_title'] = get_phrase('admin_dashboard');
        $this->load->view('backend/index', $page_data);
    }

    /****MANAGE STUDENTS CLASSWISE*****/
	function student_add()
	{
		if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

		$page_data['page_name']  = 'student_add';
		$page_data['page_title'] = get_phrase('add_student');
		$this->load->view('backend/index', $page_data);
	}

	function student_bulk_add()
	{
		if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
		$page_data['page_name']  = 'student_bulk_add';
		$page_data['page_title'] = get_phrase('add_bulk_student');
		$this->load->view('backend/index', $page_data);
	}
    function parent_bulk_add()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['page_name']  = 'parent_bulk_add';
        $page_data['page_title'] = get_phrase('add_bulk_parent');
        $this->load->view('backend/index', $page_data);
    }

  function student_profile($student_id)
  {
    if ($this->session->userdata('admin_login') != 1) {
      redirect(site_url('login'), 'refresh');
    }
    $page_data['page_name']  = 'student_profile';
	$page_data['page_title'] = get_phrase('student_profile');
    $page_data['student_id']  = $student_id;
		$this->load->view('backend/index', $page_data);
  }

    function get_sections($class_id)
    {
        $page_data['class_id'] = $class_id;
        $this->load->view('backend/admin/student_bulk_add_sections' , $page_data);
    }

	function student_information($class_id = '')
	{
		if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

		$page_data['page_name']  	= 'student_information';
		$page_data['page_title'] 	= get_phrase('student_information'). " - ".get_phrase('class')." : ".
											$this->crud_model->get_class_name($class_id);
		$page_data['class_id'] 	= $class_id;
		$this->load->view('backend/index', $page_data);
	}
    
    function student_documents($param1="",$stdid=""){
        if($this->session->userdata('admin_login') != 1)
         redirect(site_url('login'),'refresh');
         if($param1=="upload"){
             $student=$this->db->where("student_id",$stdid)->get("student")->result_array();
             if($student!=NULL){
                $path="uploads/student_documents/".$student[0]['name']."_".$student[0]['student_id'];
               if(! is_dir($path)){
                    mkdir($path,0777);
                }
                if($_FILES['adharCard']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["adharCard"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$student[0]['name']."_AdharCard.".$FileType; 
                move_uploaded_file($_FILES['adharCard']['tmp_name'], $path."/".$file_name);
                $data["adharCard"] = $file_name;
                }
                if($_FILES['passBook']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["passBook"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$student[0]['name']."_PassBook.".$FileType; 
                move_uploaded_file($_FILES['passBook']['tmp_name'], $path."/".$file_name);
                $data["passBook"] = $file_name;
                }
                if($_FILES['incomeCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["incomeCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$student[0]['name']."_IncomeCertificate.".$FileType; 
                move_uploaded_file($_FILES['incomeCertificate']['tmp_name'], $path."/".$file_name);
                $data["incomeCertificate"] = $file_name;
                }
                if($_FILES['casteCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["casteCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$student[0]['name']."_CasteCertificate.".$FileType; 
                move_uploaded_file($_FILES['casteCertificate']['tmp_name'], $path."/".$file_name);
                $data["casteCertificate"] = $file_name;
                }
                if($_FILES['phCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["phCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$student[0]['name']."_PHCertificate.".$FileType; 
                move_uploaded_file($_FILES['phCertificate']['tmp_name'], $path."/".$file_name);
                $data["phCertificate"] = $file_name;
                }
                if($_FILES['birthCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["birthCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$student[0]['name']."_birthCertificate.".$FileType; 
                move_uploaded_file($_FILES['birthCertificate']['tmp_name'], $path."/".$file_name);
                $data["birthCertificate"] = $file_name;
                $data["bc_verified"]=$this->input->post("verified");
                }
                if($_FILES['transferCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["transferCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$student[0]['name']."_TransferCertificate.".$FileType; 
                move_uploaded_file($_FILES['transferCertificate']['tmp_name'], $path."/".$file_name);
                $data["transferCertificate"] = $file_name;
                }
                if($_FILES['doc1_file']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["doc1_file"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 
                $doc_name=$this->input->post("doc1_name"); 
                $file_name=$student[0]['name']."_".$doc_name.".".$FileType; 
                move_uploaded_file($_FILES['doc1_file']['tmp_name'], $path."/".$file_name);
                $data["document1"] = $file_name;
                $data["doc1_name"] = $doc_name;
                }
                if($_FILES['doc2_file']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["doc2_file"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 
                $doc_name=$this->input->post("doc2_name"); 
                $file_name=$student[0]['name']."_".$doc_name.".".$FileType; 
                move_uploaded_file($_FILES['doc2_file']['tmp_name'], $path."/".$file_name);
                $data["document2"] = $file_name;
                $data["doc2_name"] = $doc_name;
                }
                if($_FILES['doc3_file']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["doc3_file"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 
                $doc_name=$this->input->post("doc3_name"); 
                $file_name=$student[0]['name']."_".$doc_name.".".$FileType; 
                move_uploaded_file($_FILES['doc3_file']['tmp_name'], $path."/".$file_name);
                $data["document3"] = $file_name;
                $data["doc3_name"] = $doc_name;
                }
                $temp=$this->db->get_where("studentdocument",array('stdId' => $stdid ))->result_array();
                if($temp==NULL){
                  $data['stdId'] = $stdid;
                  $this->db->insert("studentdocument",$data);
                  $this->session->set_flashdata('flash_message' , get_phrase('Documents_Uploaded_successfully'));
                }else{
                  $this->db->where("stdId",$stdid)->update("studentdocument",$data);  
                  $this->session->set_flashdata('flash_message' , get_phrase('Documents_Uploaded_successfully'));
                }
            } 
             redirect(site_url('admin/student_documents/view/'.$stdid), 'refresh'); 
         }
         if($param1=="download"){   
             $this->load->library('zip');

             $doc=$this->db->get_where("studentdocument",array('stdId' => $stdid ))->result_array();
             if($temp==NULL){ 
              $student=$this->db->where("student_id",$stdid)->get("student")->result_array();

             if($student!=NULL){
              $path="uploads/student_documents/".$student[0]['name']."_".$student[0]['student_id']."/";        
              $this->zip->read_dir($path);
              // Download the file to your desktop. Name it "my_backup.zip"
               $this->zip->download($student[0]['name']."_".$student[0]['student_id'].".zip");
              }else{
                redirect(site_url('admin/student_documents/view/'.$stdid), 'refresh'); 
              }
         } }
         if($param1=="view"){
         $page_data['page_name']  = "student_documents";
         $page_data['page_title']  = get_phrase("student_documents");
         $page_data['student_id']  = $stdid;
    
         $this->load->view('backend/index', $page_data); 
         }
    }

    function get_students($class_id, $running_year) {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $columns = array(
            0 => 'id',
            1 => 'photo',
            2 => 'name',
            3 => 'address',
            4 => 'email',
            5 => 'options',
            6 => 'id'
        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir   = $this->input->post('order')[0]['dir'];

        $totalData = $this->ajaxload->all_students_count();
        $totalFiltered = $totalData;

        if(empty($this->input->post('search')['value'])) {
            $students = $this->ajaxload->all_students($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value'];
            $students =  $this->ajaxload->student_search($limit,$start,$search,$order,$dir);
            $totalFiltered = $this->ajaxload->student_search_count($search);
        }

        $data = array();
        if(!empty($students)) {
            foreach ($students as $row) {
                $nestedData['id'] = $row->enroll_code;
                $nestedData['photo'] = '1';
                $nestedData['name'] = '2';
                $nestedData['address'] = '3';
                $nestedData['email'] = '4';
                $nestedData['options'] = '5';

                $data[] = $nestedData;
            }
        }

        $json_data = array(
            "draw"            => intval($this->input->post('draw')),
            "recordsTotal"    => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data"            => $data
        );

        echo json_encode($json_data);
    }

    function student_marksheet($student_id = '') {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $class_id     = $this->db->get_where('enroll' , array(
            'student_id' => $student_id , 'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
        ))->row()->class_id;
        $student_name = $this->db->get_where('student' , array('student_id' => $student_id))->row()->name;
        $class_name   = $this->db->get_where('class' , array('class_id' => $class_id))->row()->name;
        $page_data['page_name']  =   'student_marksheet';
        $page_data['page_title'] =   get_phrase('marksheet_for') . ' ' . $student_name . ' (' . get_phrase('class') . ' ' . $class_name . ')';
        $page_data['student_id'] =   $student_id;
        $page_data['class_id']   =   $class_id;
        $this->load->view('backend/index', $page_data);
    }

    function student_marksheet_print_view($student_id , $exam_id) {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $class_id     = $this->db->get_where('enroll' , array(
            'student_id' => $student_id , 'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
        ))->row()->class_id;
        $class_name   = $this->db->get_where('class' , array('class_id' => $class_id))->row()->name;

        $page_data['student_id'] =   $student_id;
        $page_data['class_id']   =   $class_id;
        $page_data['exam_id']    =   $exam_id;
        $this->load->view('backend/admin/student_marksheet_print_view', $page_data);
    }

    function student($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $running_year = $this->db->get_where('settings' , array(
            'type' => 'running_year'
        ))->row()->description;

        if ($param1 == 'create') {
            $temp = $this->db->where('student_code', $this->input->post('student_code'))->get('student')->result_array();
            if($temp == NULL){
            $data['name']         = $this->input->post('name');
            if($this->input->post('birthday') != null){
              $data['birthday']     = $this->input->post('birthday');
            }
            if($this->input->post('sex') != null){
              $data['sex']          = $this->input->post('sex');
            }
            if($this->input->post('address') != null){
              $data['address'] = $this->input->post('address');
            }
            if($this->input->post('phone') != null){
              $data['phone']        = $this->input->post('phone');
            }
            if($this->input->post('student_code') != null){
                $data['student_code'] = $this->input->post('student_code');
                $code_validation = code_validation_insert($data['student_code']);
                if(!$code_validation) {
                    $this->session->set_flashdata('error_message' , get_phrase('same_addmission_numbers_exists'));
                    redirect(site_url('admin/student_add'), 'refresh');
                }
            }

            $data['email']        = $this->input->post('email');
            $data['password']     = sha1($this->input->post('password'));


            if($this->input->post('parent_id') != null){
                $data['parent_id']    = $this->input->post('parent_id');
            }
            if($this->input->post('nationality') != null){
                $data['nationality']    = $this->input->post('nationality');
            }
            if($this->input->post('caste') != null){
                $data['caste']    = $this->input->post('caste');
            }
            if($this->input->post('category') != null){
                $data['category']    = $this->input->post('category');
            }
            if($this->input->post('annualIncome') != null){
                $data['annualIncome']    = $this->input->post('annualIncome');
            }
            if($this->input->post('physicallyChallenged') != null){
                $data['physicallyChallenged']    = $this->input->post('physicallyChallenged');
            }
            if($this->input->post('phtype') != null){
                $ph=$this->input->post('phtype');
                $temp=NULL;$c=0;
                foreach ($ph as $row) { if($c==0){$temp.=$row;}else{$temp.=",".$row;} $c++;}
                $data['TOD']    = $temp;
            }
            if($this->input->post('adharNo') != null){
                $data['adharNo']    = $this->input->post('adharNo');
            }
            if($this->input->post('sts') != null){
                $data['sts']    = $this->input->post('sts');
            }
            if($this->input->post('rationNo') != null){
                $data['rationNo']    = $this->input->post('rationNo');
            }
            if($this->input->post('siblings') != null){
                $data['siblings']    = $this->input->post('siblings');
            }
            if($this->input->post('dormitory_id') != null){
                $data['dormitory_id'] = $this->input->post('dormitory_id');
            }
            if($this->input->post('transport_id') != null){
                $data['transport_id'] = $this->input->post('transport_id');
            }
            if($this->input->post('religion') != null){
                $data['religion'] = $this->input->post('religion');
            }
            if($this->input->post('elective') != null){
                $data['elective'] = $this->input->post('elective');
            }
            $validation = email_validation($data['email']);
            if($validation == 1) {
                $this->db->insert('student', $data);
                $student_id = $this->db->insert_id();

                $data2['student_id']     = $student_id;
                $data2['enroll_code']    = substr(md5(rand(0, 1000000)), 0, 7);
                if($this->input->post('class_id') != null){
                  $data2['class_id']       = $this->input->post('class_id');
                }
                if ($this->input->post('section_id') != '') {
                    $data2['section_id'] = $this->input->post('section_id');
                }
                if ($this->input->post('roll') != '') {
                    $data2['roll']           = $this->input->post('roll');
                }
                $data2['date_added']     = strtotime(date("Y-m-d H:i:s"));
                $data2['year']           = $running_year;
                $this->db->insert('enroll', $data2);
                move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/student_image/' . $student_id . '.jpg');

                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
                $this->email_model->account_opening_email('student', $data['email']); //SEND EMAIL ACCOUNT OPENING EMAIL
            }
            else {
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }
            }else{
               $this->session->set_flashdata('error_message',get_phrase('admission_number_exists')) ;
            }
            redirect(site_url('admin/student_add'), 'refresh');
        }
        if ($param1 == 'do_update') {
            $data['name']           = $this->input->post('name');
            $data['email']          = $this->input->post('email');
            $data['parent_id']      = $this->input->post('parent_id');
            if ($this->input->post('birthday') != null) {
                $data['birthday']   = $this->input->post('birthday');
            }
            if ($this->input->post('sex') != null) {
                $data['sex']            = $this->input->post('sex');
            }
            if ($this->input->post('address') != null) {
               $data['address']        = $this->input->post('address');
            }
            if ($this->input->post('nationality') != null) {
               $data['nationality']        = $this->input->post('nationality');
            }
            if ($this->input->post('religion') != null) {
               $data['religion']        = $this->input->post('religion');
            }
            if ($this->input->post('caste') != null) {
               $data['caste']        = $this->input->post('caste');
            }
            if ($this->input->post('adharNo') != null) {
               $data['adharNo']        = $this->input->post('adharNo');
            }
            if ($this->input->post('sts') != null) {
               $data['sts']        = $this->input->post('sts');
            }
            if ($this->input->post('rationNo') != null) {
               $data['rationNo']        = $this->input->post('rationNo');
            }             
            if ($this->input->post('annualIncome') != null) {
               $data['annualIncome']        = $this->input->post('annualIncome');
            }
            if ($this->input->post('phone') != null) {
                $data['phone']          = $this->input->post('phone');
            }
            if ($this->input->post('dormitory_id') != null) {
               $data['dormitory_id']   = $this->input->post('dormitory_id');
            }
            if ($this->input->post('transport_id') != null) {
                $data['transport_id']   = $this->input->post('transport_id');
            }
            if($this->input->post('elective') != null){
                $data['elective'] = $this->input->post('elective');
            }

            //student id
            if($this->input->post('student_code') != null){
                $data['student_code'] = $this->input->post('student_code');
                $code_validation = code_validation_update($data['student_code'],$param2);
                if(!$code_validation){
                    $this->session->set_flashdata('error_message' , get_phrase('this_id_no_is_not_available'));
                    redirect(site_url('admin/student_information/' . $param3), 'refresh');
                }
            }

            $validation = email_validation_for_edit($data['email'], $param2, 'student');
            if($validation == 1){
                $this->db->where('student_id', $param2);
                $this->db->update('student', $data);

                $data2['section_id'] = $this->input->post('section_id');
                if ($this->input->post('roll') != null) {
                  $data2['roll'] = $this->input->post('roll');
                }
                else{
                  $data2['roll'] = null;
                }
                $running_year = $this->db->get_where('settings' , array('type'=>'running_year'))->row()->description;
                $this->db->where('student_id' , $param2);
                $this->db->where('year' , $running_year);
                $this->db->update('enroll' , array(
                    'section_id' => $data2['section_id'] , 'roll' => $data2['roll']
                ));

                move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/student_image/' . $param2 . '.jpg');
                $this->crud_model->clear_cache();
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
           }
           else{
             $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
           }
            redirect(site_url('admin/student_information/' . $param3), 'refresh');
        }
    }

    function delete_student($student_id = '', $class_id = '') {
      $this->crud_model->delete_student($student_id);
      $this->session->set_flashdata('flash_message' , get_phrase('student_deleted'));
      redirect(site_url('admin/student_information/' . $class_id), 'refresh');
    }

    // STUDENT PROMOTION
    function student_promotion($param1 = '' , $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if($param1 == 'promote') {
            $running_year  =   $this->input->post('running_year');
            $from_class_id =   $this->input->post('promotion_from_class_id');
            $students_of_promotion_class =   $this->db->get_where('enroll' , array(
                'class_id' => $from_class_id , 'year' => $running_year
            ))->result_array();
            foreach($students_of_promotion_class as $row) {
                $sections = $this->db->get_where('section', array('class_id' => $this->input->post('promotion_status_'.$row['student_id'])))->row_array();
                $enroll_data['enroll_code']     =   substr(md5(rand(0, 1000000)), 0, 7);
                $enroll_data['student_id']      =   $row['student_id'];
                $enroll_data['class_id']        =   $this->input->post('promotion_status_'.$row['student_id']);
                $enroll_data['section_id']      =   $sections['section_id'];
                $enroll_data['year']            =   $this->input->post('promotion_year');
                $enroll_data['date_added']      =   strtotime(date("Y-m-d H:i:s"));
                $this->db->insert('enroll' , $enroll_data);
            }
            $this->session->set_flashdata('flash_message' , get_phrase('new_enrollment_successfull'));
            redirect(site_url('admin/student_promotion'), 'refresh');
        }

        $page_data['page_title']    = get_phrase('student_promotion');
        $page_data['page_name']  = 'student_promotion';
        $this->load->view('backend/index', $page_data);
    }

    function get_students_to_promote($class_id_from , $class_id_to , $running_year , $promotion_year)
    {
        $page_data['class_id_from']     =   $class_id_from;
        $page_data['class_id_to']       =   $class_id_to;
        $page_data['running_year']      =   $running_year;
        $page_data['promotion_year']    =   $promotion_year;
        $this->load->view('backend/admin/student_promotion_selector' , $page_data);
    }

     /****MANAGE PARENTS CLASSWISE*****/
    function parent($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']        			= $this->input->post('name');
            $data['email']       			= $this->input->post('email');
            $data['password']    			= sha1($this->input->post('password'));
            if ($this->input->post('phone') != null) {
               $data['phone'] = $this->input->post('phone');
            }
            if ($this->input->post('address') != null) {
               $data['address'] = $this->input->post('address');
            }
            if ($this->input->post('profession') != null) {
               $data['profession'] = $this->input->post('profession');
            }
            if ($this->input->post('phone2') != null) {
               $data['phone2'] = $this->input->post('phone2');
            }
            $data['motherName']   = $this->input->post('motherName');
            $data['motherOccupation']   = $this->input->post('motherOccupation');
            $data['fatherEducation']   = $this->input->post('fatherEducation');
            $data['motherEducation']   = $this->input->post('motherEducation');
            $data['currentAddress']   = $this->input->post('currentAddress');
            $data['empSvym']   = $this->input->post('empSvym');
            $data['father_aadhar']   = $this->input->post('father_aadhar');
            $data['mother_aadhar']   = $this->input->post('mother_aadhar');
            $validation = email_validation($data['email']);
            if($validation == 1){
                $this->db->insert('parent', $data);
                $id=$this->db->insert_id();

                if($this->input->post("empSvym")==1){
                    $ename = $this->input->post('ename');
                    $department = $this->input->post('department');
                    $experience = $this->input->post('experience');
                    $designation = $this->input->post('designation');
                    $empId = $this->input->post('empId');
                    for($i=0;$i<sizeof($ename);$i++){ 
                    if($empId[$i]!=NULL && $ename[$i] !=NULL && $department[$i]!=NULL && $designation[$i]!=NULL && $experience[$i] !=NULL ){
                    $emp['empId']=$empId[$i];    
                    $emp['ename'] =  $ename[$i];
                    $emp['department'] = $department[$i];
                    $emp['designation'] = $designation[$i];
                    $emp['experience'] = $experience[$i]; 
                    $emp['pid'] = $id;             
                    $this->db->insert("parentAtSvym",$emp);
                      } 
                    } 
                }
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
                $this->email_model->account_opening_email('parent', $data['email']); //SEND EMAIL ACCOUNT OPENING EMAIL
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }

            redirect(site_url('admin/parent'), 'refresh');
        }
        if ($param1 == 'edit') {
            $data['name']                   = $this->input->post('name');
            $data['email']                  = $this->input->post('email');
            if ($this->input->post('phone') != null) {
               $data['phone'] = $this->input->post('phone');
            }
            else{
              $data['phone'] = null;
            }
            if ($this->input->post('address') != null) {
                $data['address'] = $this->input->post('address');
            }
            else{
               $data['address'] = null;
            }
            if ($this->input->post('profession') != null) {
                $data['profession'] = $this->input->post('profession');
            }
            else{
                $data['profession'] = null;
            }
            $data['motherName']   = $this->input->post('motherName');
            $data['motherOccupation']   = $this->input->post('motherOccupation');
            $data['fatherEducation']   = $this->input->post('fatherEducation');
            $data['motherEducation']   = $this->input->post('motherEducation');
            $data['currentAddress']   = $this->input->post('currentAddress');
            $data['phone2']   = $this->input->post('phone2');
            $data['father_aadhar']   = $this->input->post('father_aadhar');
            $data['mother_aadhar']   = $this->input->post('mother_aadhar');

            $validation = email_validation_for_edit($data['email'], $param2, 'parent');
            if ($validation == 1) {
                $this->db->where('parent_id' , $param2);
                $this->db->update('parent' , $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }

            redirect(site_url('admin/parent'), 'refresh');
        }
        if ($param1 == 'delete') {
            $this->db->where('parent_id' , $param2);
            $this->db->delete('parent');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/parent'), 'refresh');
        }
        $page_data['page_title'] 	= get_phrase('all_parents');
        $page_data['page_name']  = 'parent';
        $this->load->view('backend/index', $page_data);
    }
    
    // bulk student_add using CSV
    function generate_bulk_parent_csv()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $file   = fopen("uploads/bulk_parent.csv", "w");
        $line   = array('Father name','Mother Name','email','Password','Phone','Phone(2)','Address','Current Address','Profession','Mother Occupation','Father Education','Mother Education','Father Aadhar Number','Mother Aadhar Number');
        fputcsv($file, $line, ',');
       echo $file_path = base_url() . 'uploads/bulk_parent.csv';
    }
    // CSV IMPORT
    function bulk_parent_add_using_csv($param1 = '') {

        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

       if ($param1 == 'import') {
              move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/bulk_parent.csv');
              $csv = array_map('str_getcsv', file('uploads/bulk_parent.csv'));
              $count = 1;
              $array_size = sizeof($csv);
             
             foreach ($csv as $row) {
                  if ($count == 1) {
                      $count++;
                      continue;
                  }
                  $password = $row[3];

                  $data['name']      = $row[0];
                  $data['motherName']  = $row[1]; 
                  $data['email']  = $row[2];
                  $data['phone']  = $row[4];
                  $data['phone2']  = $row[5];
                  $data['address']  = $row[6]; 
                  $data['currentAddress']  = $row[7];
                  $data['profession']  = $row[8];
                  $data['motherOccupation']  = $row[9];
                  $data['fatherEducation']  = $row[10];
                  $data['motherEducation']  = $row[11];
                  $data['father_aadhar']  = $row[12];
                  $data['mother_aadhar']  = $row[13];
                  $data['password']  = sha1($row[3]);
                  $data['empSvym'] =0;
                 
                  $validation = phone_validation($data['phone']);
                  if ($validation == 1) {
                    $this->db->insert('parent', $data);
                    $student_id = $this->db->insert_id();
                  }
                  else{
                    if ($array_size == 2) {
                      $this->session->set_flashdata('error_message', get_phrase('this_email_id_"').$data['email'].get_phrase('"_is_not_available'));
                      redirect(site_url('admin/parent_bulk_add'), 'refresh');
                    }
                    elseif($array_size > 2){
                      $this->session->set_flashdata('error_message', get_phrase('some_phone_number_are_not_available'));
                    }
                  }

              }
              $this->session->set_flashdata('flash_message', get_phrase('parents_imported'));
              redirect(site_url('admin/parent_bulk_add'), 'refresh');
        }

        $page_data['page_name']  = 'parent_bulk_add';
        $page_data['page_title'] = get_phrase('add_bulk_parent');
        $this->load->view('backend/index', $page_data);
    }
    function get_parents() {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $columns = array(
            0 => 'parent_id',
            1 => 'name',
            2 => 'email',
            3 => 'phone',
            4 => 'profession',
            5 => 'options',
            6 => 'parent_id'
        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir   = $this->input->post('order')[0]['dir'];

        $totalData = $this->ajaxload->all_parents_count();
        $totalFiltered = $totalData;

        if(empty($this->input->post('search')['value'])) {
            $parents = $this->ajaxload->all_parents($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value'];
            $parents =  $this->ajaxload->parent_search($limit,$start,$search,$order,$dir);
            $totalFiltered = $this->ajaxload->parent_search_count($search);
        }

        $data = array();
        if(!empty($parents)) {
            foreach ($parents as $row) {

                $options = '<div class="btn-group"><button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
                                    Action <span class="caret"></span></button><ul class="dropdown-menu dropdown-default pull-right" role="menu"><li><a href="#" onclick="parent_edit_modal('.$row->parent_id.')"><i class="entypo-pencil"></i>&nbsp;'.get_phrase('edit').'</a></li><li class="divider"></li><li><a href="#" onclick="parent_delete_confirm('.$row->parent_id.')"><i class="entypo-trash"></i>&nbsp;'.get_phrase('delete').'</a></li></ul></div>';

                $nestedData['parent_id'] = $row->parent_id;
                $nestedData['name'] = $row->name;
                $nestedData['email'] = $row->email;
                $nestedData['phone'] = $row->phone;
                $nestedData['profession'] = $row->profession;
                $nestedData['options'] = $options;

                $data[] = $nestedData;
            }
        }

        $json_data = array(
            "draw"            => intval($this->input->post('draw')),
            "recordsTotal"    => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data"            => $data
        );

        echo json_encode($json_data);
    }


    /****MANAGE TEACHERS*****/
    function teacher($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']     = $this->input->post('name');
            $data['email']    = $this->input->post('email');
            $data['password'] = sha1($this->input->post('password'));
            $data['teacher_code']     = $this->input->post('teacher_code');
            $data['shortcut_name']     = $this->input->post('shortcut_name');
            if ($this->input->post('birthday') != null) {
                $data['birthday'] = $this->input->post('birthday');
            }
            if ($this->input->post('sex') != null) {
               $data['sex'] = $this->input->post('sex');
            }
            if ($this->input->post('address') != null) {
                $data['address'] = $this->input->post('address');
            }
            if ($this->input->post('phone') != null) {
                $data['phone'] = $this->input->post('phone');
            }
            if ($this->input->post('designation') != null) {
                $data['designation'] = $this->input->post('designation');
            }
            if ($this->input->post('show_on_website') != null) {
                $data['show_on_website'] = $this->input->post('show_on_website');
            }
            $links = array();
            $social['facebook'] = $this->input->post('facebook');
            $social['twitter'] = $this->input->post('twitter');
            $social['linkedin'] = $this->input->post('linkedin');
            array_push($links, $social);
            $data['social_links'] = json_encode($links);

            $validation = email_validation($data['email']);
            if($validation == 1){
                $this->db->insert('teacher', $data);
                $teacher_id = $this->db->insert_id();
                move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/teacher_image/' . $teacher_id . '.jpg');
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
                $this->email_model->account_opening_email('teacher', $data['email']); //SEND EMAIL ACCOUNT OPENING EMAIL
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }

            redirect(site_url('admin/teacher'), 'refresh');
        }
        if ($param1 == 'do_update') {
            $data['name']        = $this->input->post('name');
            $data['email']       = $this->input->post('email');
            if ($this->input->post('shortcut_name') != null) {
               $data['shortcut_name']       = $this->input->post('shortcut_name');
            }
            else{
              $data['shortcut_name'] = null;
            }
            if($this->input->post("teacher_code") != NULL){
                $data['teacher_code']     = $this->input->post('teacher_code');
            }
            if ($this->input->post('birthday') != null) {
                $data['birthday'] = $this->input->post('birthday');
            }
            else{
              $data['birthday'] = null;
            }
            if ($this->input->post('sex') != null) {
                $data['sex']         = $this->input->post('sex');
            }
            if ($this->input->post('address') != null) {
                $data['address']     = $this->input->post('address');
            }
            else{
              $data['address'] = null;
            }
            if ($this->input->post('phone') != null) {
               $data['phone']       = $this->input->post('phone');
            }
            else{
              $data['phone'] = null;
            }
            if ($this->input->post('designation') != null) {
               $data['designation']       = $this->input->post('designation');
            }
            else{
              $data['designation'] = null;
            }
            if ($this->input->post('show_on_website') != null) {
               $data['show_on_website']       = $this->input->post('show_on_website');
            }
            else{
              $data['show_on_website'] = null;
            }
            $links = array();
            $social['facebook'] = $this->input->post('facebook');
            $social['twitter'] = $this->input->post('twitter');
            $social['linkedin'] = $this->input->post('linkedin');
            array_push($links, $social);
            $data['social_links'] = json_encode($links);

            $validation = email_validation_for_edit($data['email'], $param2, 'teacher');
            if($validation == 1){
                $this->db->where('teacher_id', $param2);
                $this->db->update('teacher', $data);
                move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/teacher_image/' . $param2 . '.jpg');
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }

            redirect(site_url('admin/teacher'), 'refresh');
        }
        else if ($param1 == 'personal_profile') {
            $page_data['personal_profile']   = true;
            $page_data['current_teacher_id'] = $param2;
        }
        else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('teacher', array(
                'teacher_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('teacher_id', $param2);
            $this->db->delete('teacher');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/teacher'), 'refresh');
        }
        $page_data['teachers']   = $this->db->get('teacher')->result_array();
        $page_data['page_name']  = 'teacher';
        $page_data['page_title'] = get_phrase('manage_teacher');
        $this->load->view('backend/index', $page_data);
    }


    function teacher_profile($param1="",$teacher_id=""){
        if ($this->session->userdata('admin_login') != 1) {
            redirect(site_url('login'), 'refresh');}
        if($param1=="upload"){
             $teacher=$this->db->where("teacher_id",$teacher_id)->get("teacher")->result_array();
             if($teacher!=NULL){
                $path="uploads/teacher_documents/".$teacher[0]['name']."_".$teacher[0]['teacher_id'];
               if(! is_dir($path)){
                    mkdir($path,0777);
                }
                if($_FILES['adharCard']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["adharCard"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_AdharCard.".$FileType; 
                move_uploaded_file($_FILES['adharCard']['tmp_name'], $path."/".$file_name);
                $data["adharCard"] = $file_name;
                }
                if($_FILES['passBook']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["passBook"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_PassBook.".$FileType; 
                move_uploaded_file($_FILES['passBook']['tmp_name'], $path."/".$file_name);
                $data["passBook"] = $file_name;
                }
                if($_FILES['sslc']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["sslc"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_SSLC.".$FileType; 
                move_uploaded_file($_FILES['sslc']['tmp_name'], $path."/".$file_name);
                $data["sslc"] = $file_name;
                }
                if($_FILES['puc']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["puc"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_PUC.".$FileType; 
                move_uploaded_file($_FILES['puc']['tmp_name'], $path."/".$file_name);
                $data["puc"] = $file_name;
                }
                if($_FILES['degreeCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["degreeCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_DegreeCertificate.".$FileType; 
                move_uploaded_file($_FILES['degreeCertificate']['tmp_name'], $path."/".$file_name);
                $data["degreeCertificate"] = $file_name;
                }
                if($_FILES['BEdCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["BEdCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_BEdCertificate.".$FileType; 
                move_uploaded_file($_FILES['BEdCertificate']['tmp_name'], $path."/".$file_name);
                $data["BEdCertificate"] = $file_name;
                }
                if($_FILES['DL']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["DL"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_Drivinglicence.".$FileType; 
                move_uploaded_file($_FILES['DL']['tmp_name'], $path."/".$file_name);
                $data["DL"] = $file_name;
                }
                if($_FILES['experienceCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["experienceCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_ExperienceCertificate.".$FileType; 
                move_uploaded_file($_FILES['experienceCertificate']['tmp_name'], $path."/".$file_name);
                $data["experienceCertificate"] = $file_name;
                }
                if($_FILES['medicalCertificate']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["medicalCertificate"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$teacher[0]['name']."_MedicalCertificate.".$FileType; 
                move_uploaded_file($_FILES['medicalCertificate']['tmp_name'], $path."/".$file_name);
                $data["medicalCertificate"] = $file_name;
                }
                if($_FILES['doc1_file']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["doc1_file"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 
                $doc_name=$this->input->post("doc1_name"); 
                $file_name=$teacher[0]['name']."_".$doc_name.".".$FileType; 
                move_uploaded_file($_FILES['doc1_file']['tmp_name'], $path."/".$file_name);
                $data["doc1_file"] = $file_name;
                $data["doc1_name"] = $doc_name;
                }
                if($_FILES['doc2_file']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["doc2_file"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 
                $doc_name=$this->input->post("doc2_name"); 
                $file_name=$teacher[0]['name']."_".$doc_name.".".$FileType; 
                move_uploaded_file($_FILES['doc2_file']['tmp_name'], $path."/".$file_name);
                $data["doc2_file"] = $file_name;
                $data["doc2_name"] = $doc_name;
                }
                if($_FILES['doc3_file']['tmp_name'] !=NULL){
                $target_file = $path . basename($_FILES["doc3_file"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 
                $doc_name=$this->input->post("doc3_name"); 
                $file_name=$teacher[0]['name']."_".$doc_name.".".$FileType; 
                move_uploaded_file($_FILES['doc3_file']['tmp_name'], $path."/".$file_name);
                $data["doc3_file"] = $file_name;
                $data["doc3_name"] = $doc_name;
                }
                $temp=$this->db->get_where("teacherdocument",array('teacherId' => $teacher_id ))->result_array();
                if($temp==NULL){
                  $data['teacherId'] = $teacher_id;
                  $this->db->insert("teacherdocument",$data);
                  $this->session->set_flashdata('flash_message' , get_phrase('Documents_Uploaded_successfully'));
                }else{
                  $this->db->where("teacherId",$teacher_id)->update("teacherdocument",$data);  
                  $this->session->set_flashdata('flash_message' , get_phrase('Documents_Uploaded_successfully'));
                }
            } 
             redirect(site_url('admin/teacher_profile/view/'.$teacher_id), 'refresh'); 
         }
         if($param1=="download"){   
             $this->load->library('zip');
             $doc=$this->db->get_where("teacherdocument",array('teacherId' => $teacher_id ))->result_array();
             if($doc!=NULL){ 
              $teacher=$this->db->where("teacher_id",$teacher_id)->get("teacher")->result_array();
             if($teacher!=NULL){
              $path="uploads/teacher_documents/".$teacher[0]['name']."_".$teacher[0]['teacher_id']."/";        
              $this->zip->read_dir($path);
              // Download the file to your desktop. Name it "my_backup.zip"
               $this->zip->download($teacher[0]['name']."_".$teacher[0]['teacher_id'].".zip");
              }else{
                redirect(site_url('admin/teacher_profile/view/'.$teacher_id), 'refresh'); 
              }
            } 
          } 
        if($param1=="view"){            
        $page_data['page_name']  = 'teacher_profile';
        $page_data['page_title'] = get_phrase('teacher_profile');
        $page_data['teacher_id']  = $teacher_id;
        $this->load->view('backend/index', $page_data);
        }            
    }

    function get_teachers() {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $columns = array(
            0 => 'teacher_id',
            1 => 'photo',
            2 => 'name',
            3 => 'email',
            4 => 'phone',
            5 => 'options',
            6 => 'teacher_id'
        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir   = $this->input->post('order')[0]['dir'];

        $totalData = $this->ajaxload->all_teachers_count();
        $totalFiltered = $totalData;

        if(empty($this->input->post('search')['value'])) {
            $teachers = $this->ajaxload->all_teachers($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value'];
            $teachers =  $this->ajaxload->teacher_search($limit,$start,$search,$order,$dir);
            $totalFiltered = $this->ajaxload->teacher_search_count($search);
        }

        $data = array();
        if(!empty($teachers)) {
            foreach ($teachers as $row) {

                $photo = '<img src="'.$this->crud_model->get_image_url('teacher', $row->teacher_id).'" class="img-circle" width="30" />';

                $options = '<div class="btn-group"><button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
                                    Action <span class="caret"></span></button><ul class="dropdown-menu dropdown-default pull-right" role="menu">
                                    <li><a href="'.site_url('admin/teacher_profile/view/'.$row->teacher_id).'"><i class="entypo-user"></i>&nbsp;'.get_phrase('proflie').'</a></li><li class="divider"><li><a href="#" onclick="teacher_edit_modal('.$row->teacher_id.')"><i class="entypo-pencil"></i>&nbsp;'.get_phrase('edit').'</a></li><li class="divider"></li><li><a href="#" onclick="teacher_delete_confirm('.$row->teacher_id.')"><i class="entypo-trash"></i>&nbsp;'.get_phrase('delete').'</a></li></ul></div>';

                $nestedData['teacher_id'] = $row->teacher_id;
                $nestedData['photo'] = $photo;
                $nestedData['name'] = $row->name;
                $nestedData['email'] = $row->email;
                $nestedData['phone'] = $row->phone;
                $nestedData['options'] = $options;

                $data[] = $nestedData;
            }
        }

        $json_data = array(
            "draw"            => intval($this->input->post('draw')),
            "recordsTotal"    => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data"            => $data
        );

        echo json_encode($json_data);
    }

    /****MANAGE SUBJECTS*****/
    function subject($param1 = '', $param2 = '' , $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']       = $this->input->post('name');
            $data['class_id']   = $this->input->post('class_id');
            $data['year']       = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            if ($this->input->post('teacher_id') != null) {
                $data['teacher_id'] = $this->input->post('teacher_id');
            }

            $this->db->insert('subject', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/subject/' . $data['class_id']), 'refresh');
        }
        if ($param1 == 'do_update') {
            $data['name']       = $this->input->post('name');
            $data['class_id']   = $this->input->post('class_id');
            $data['teacher_id'] = $this->input->post('teacher_id');
            $data['year']       = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;

            $this->db->where('subject_id', $param2);
            $this->db->update('subject', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/subject/' . $data['class_id']), 'refresh');
        } else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('subject', array(
                'subject_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('subject_id', $param2);
            $this->db->delete('subject');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/subject/' . $param3), 'refresh');
        }
        $running_year = $this->db->get_where('settings', array('type' => 'running_year'))->row()->description;
		    $page_data['class_id']   = $param1;
        $page_data['subjects']   = $this->db->get_where('subject' , array('class_id' => $param1, 'year' => $running_year))->result_array();
        $page_data['page_name']  = 'subject';
        $page_data['page_title'] = get_phrase('manage_subject');
        $this->load->view('backend/index', $page_data);
    }

    /****MANAGE CLASSES*****/
    function classes($param1 = '', $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']         = $this->input->post('name');
            $data['teacher_id']   = $this->input->post('teacher_id');
            if ($this->input->post('name_numeric') != null) {
                $data['name_numeric'] = $this->input->post('name_numeric');
            }

            $this->db->insert('class', $data);
            $class_id = $this->db->insert_id();
            //create a section by default
            $data2['class_id']  =   $class_id;
            $data2['name']      =   'A';
            $data2['teacher_id']=$data['teacher_id'];
            $this->db->insert('section' , $data2);

            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/classes'), 'refresh');
        }
        if ($param1 == 'do_update') {
            $data['name']         = $this->input->post('name');
            $data['teacher_id']   = $this->input->post('teacher_id');
            if ($this->input->post('name_numeric') != null) {
                $data['name_numeric'] = $this->input->post('name_numeric');
            }
            else{
               $data['name_numeric'] = null;
            }
            $this->db->where('class_id', $param2);
            $this->db->update('class', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/classes'), 'refresh');
        } else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('class', array(
                'class_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('class_id', $param2);
            $this->db->delete('class');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/classes'), 'refresh');
        }
        $page_data['classes']    = $this->db->get('class')->result_array();
        $page_data['page_name']  = 'class';
        $page_data['page_title'] = get_phrase('manage_class');
        $this->load->view('backend/index', $page_data);
    }
     function get_subject($class_id)
    {
        $subject = $this->db->get_where('subject' , array(
            'class_id' => $class_id
        ))->result_array();
        foreach ($subject as $row) {
            echo '<option value="' . $row['subject_id'] . '">' . $row['name'] . '</option>';
        }
    }
    // ACADEMIC SYLLABUS
    function academic_syllabus($class_id = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        // detect the first class
        if ($class_id == '')
            $class_id           =   $this->db->get('class')->first_row()->class_id;

        $page_data['page_name']  = 'academic_syllabus';
        $page_data['page_title'] = get_phrase('academic_syllabus');
        $page_data['class_id']   = $class_id;
        $this->load->view('backend/index', $page_data);
    }

    function upload_academic_syllabus()
    {
        $data['academic_syllabus_code'] =   substr(md5(rand(0, 1000000)), 0, 7);
        if ($this->input->post('description') != null) {
           $data['description'] = $this->input->post('description');
        }
        $data['title']                  =   $this->input->post('title');
        $data['class_id']               =   $this->input->post('class_id');
        $data['subject_id']             =   $this->input->post('subject_id');
        $data['uploader_type']          =   $this->session->userdata('login_type');
        $data['uploader_id']            =   $this->session->userdata('login_user_id');
        $data['year']                   =   $this->db->get_where('settings',array('type'=>'running_year'))->row()->description;
        $data['timestamp']              =   strtotime(date("Y-m-d H:i:s"));
        //uploading file using codeigniter upload library
        $files = $_FILES['file_name'];
        $this->load->library('upload');
        $config['upload_path']   =  'uploads/syllabus/';
        $config['allowed_types'] =  '*';
        $_FILES['file_name']['name']     = $files['name'];
        $_FILES['file_name']['type']     = $files['type'];
        $_FILES['file_name']['tmp_name'] = $files['tmp_name'];
        $_FILES['file_name']['size']     = $files['size'];
        $this->upload->initialize($config);
        $this->upload->do_upload('file_name');

        $data['file_name'] = $_FILES['file_name']['name'];

        $this->db->insert('academic_syllabus', $data);
        $this->session->set_flashdata('flash_message' , get_phrase('syllabus_uploaded'));
        redirect(site_url('admin/academic_syllabus/' . $data['class_id']), 'refresh');

    }

    function download_academic_syllabus($academic_syllabus_code)
    {
        $file_name = $this->db->get_where('academic_syllabus', array(
            'academic_syllabus_code' => $academic_syllabus_code
        ))->row()->file_name;
        $this->load->helper('download');
        $data = file_get_contents("uploads/syllabus/" . $file_name);
        $name = $file_name;

        force_download($name, $data);
    }

    function delete_academic_syllabus($academic_syllabus_code) {
      $file_name = $this->db->get_where('academic_syllabus', array(
          'academic_syllabus_code' => $academic_syllabus_code
      ))->row()->file_name;
      if (file_exists('uploads/syllabus/'.$file_name)) {
        // unlink('uploads/syllabus/'.$file_name);
      }
      $this->db->where('academic_syllabus_code', $academic_syllabus_code);
      $this->db->delete('academic_syllabus');

      $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
      redirect(site_url('admin/academic_syllabus'), 'refresh');

    }

    /****MANAGE SECTIONS*****/
    function section($class_id = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        // detect the first class
        if ($class_id == '')
            $class_id           =   $this->db->get('class')->first_row()->class_id;

        $page_data['page_name']  = 'section';
        $page_data['page_title'] = get_phrase('manage_sections');
        $page_data['class_id']   = $class_id;
        $this->load->view('backend/index', $page_data);
    }

    function sections($param1 = '' , $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']       =   $this->input->post('name');
            $data['class_id']   =   $this->input->post('class_id');
            $data['teacher_id'] =   $this->input->post('teacher_id');
            if ($this->input->post('nick_name') != null) {
               $data['nick_name'] = $this->input->post('nick_name');
            }
            $validation = duplication_of_section_on_create($data['class_id'], $data['name']);
            if($validation == 1){
                $this->db->insert('section' , $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('duplicate_name_of_section_is_not_allowed'));
            }

            redirect(site_url('admin/section/' . $data['class_id']), 'refresh');
        }

        if ($param1 == 'edit') {
            $data['name']       =   $this->input->post('name');
            $data['class_id']   =   $this->input->post('class_id');
            $data['teacher_id'] =   $this->input->post('teacher_id');
            if ($this->input->post('nick_name') != null) {
                $data['nick_name'] = $this->input->post('nick_name');
            }
            else{
                $data['nick_name'] = null;
            }
            $validation = duplication_of_section_on_edit($param2, $data['class_id'], $data['name']);
            if ($validation == 1) {
               $this->db->where('section_id' , $param2);
               $this->db->update('section' , $data);
               $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }
            else{
               $this->session->set_flashdata('error_message' , get_phrase('duplicate_name_of_section_is_not_allowed'));
            }

            redirect(site_url('admin/section/' . $data['class_id']), 'refresh');
        }

        if ($param1 == 'delete') {
            $this->db->where('section_id' , $param2);
            $this->db->delete('section');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/section'), 'refresh');
        }
    }

    function get_class_section($class_id)
    {
        $sections = $this->db->get_where('section' , array(
            'class_id' => $class_id
        ))->result_array();
        foreach ($sections as $row) {
            echo '<option value="' . $row['section_id'] . '">' . $row['name'] . '</option>';
        }
    }

    function get_class_section_selector($class_id){
        $page_data['class_id'] = $class_id;
        $this->load->view('backend/admin/get_class_section_selector', $page_data);
    }

    function get_class_subject_selector($class_id){
        $page_data['class_id'] = $class_id;
        $this->load->view('backend/admin/get_class_subject_selector', $page_data);
    }

    function get_class_subject($class_id)
    {
        $subjects = $this->db->get_where('subject' , array(
            'class_id' => $class_id
        ))->result_array();
        foreach ($subjects as $row) {
            echo '<option value="' . $row['subject_id'] . '">' . $row['name'] . '</option>';
        }
    }

    function get_class_students($class_id)
    {
        $students = $this->db->get_where('enroll' , array(
            'class_id' => $class_id , 'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
        ))->result_array();
        foreach ($students as $row) {
            $name = $this->db->get_where('student' , array('student_id' => $row['student_id']))->row()->name;
            echo '<option value="' . $row['student_id'] . '">' . $name . '</option>';
        }
    }

    function get_class_fees($param1="",$class_id)
    {   
        if($param1 == 'get_amount') {
           $fees=$this->db->query("SELECT sum(amount) as totalamount FROM `fee_particulars` WHERE class_id='".$class_id."' or class_id='all'")->result_array();

        foreach ($fees as $row) { echo  $row['totalamount'];} 

        } 
        if($param1 == 'get_fee_particulars') {
           $fees=$this->db->query("SELECT * FROM `fee_particulars` WHERE class_id='".$class_id."' or class_id='all'")->result_array();
           $totalamount=$this->db->query("SELECT sum(amount) as totalamount FROM `fee_particulars` WHERE class_id='".$class_id."' or class_id='all'")->row()->totalamount;
           if($fees){
           $count=0;  
           foreach ($fees as $row) {
                $count++; 
                echo "<tr><td>$count</td><td>$row[particulars]</td><td>$row[amount]</td><tr>";
               } 
                echo "<tr><td></td><td>Total</td><td>$totalamount</td><tr>";
            }
        }
    }

    function get_class_students_mass($class_id)
    {
        $students = $this->db->get_where('enroll' , array(
            'class_id' => $class_id , 'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
        ))->result_array();
        echo '<div class="form-group">
                <label class="col-sm-3 control-label">' . get_phrase('students') . '</label>
                <div class="col-sm-9">';
        foreach ($students as $row) {
             $name = $this->db->get_where('student' , array('student_id' => $row['student_id']))->row()->name;
            echo '<div class="checkbox">
                    <label><input type="checkbox" class="check" name="student_id[]" value="' . $row['student_id'] . '">' . $name .'</label>
                </div>';
        }
        echo '<br><button type="button" class="btn btn-default" onClick="select()">'.get_phrase('select_all').'</button>';
        echo '<button style="margin-left: 5px;" type="button" class="btn btn-default" onClick="unselect()"> '.get_phrase('select_none').' </button>';
        echo '</div></div>';
    }



    /****MANAGE EXAMS*****/
    function exam($param1 = '', $param2 = '' , $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']    = $this->input->post('name');
            $data['exam_type']    = $this->input->post('exam_type');
            $data['date']    = $this->input->post('date');
            $data['year']    = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            if ($this->input->post('comment') != null) {
                $data['comment'] = $this->input->post('comment');
            }
            $this->db->insert('exam', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/exam'), 'refresh');
        }
        if ($param1 == 'edit' && $param2 == 'do_update') {
            $data['name']    = $this->input->post('name');
            $data['date']    = $this->input->post('date');
            if ($this->input->post('comment') != null) {
                $data['comment'] = $this->input->post('comment');
            }
            else{
              $data['comment'] = null;
            }
            $data['year']    = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;

            $this->db->where('exam_id', $param3);
            $this->db->update('exam', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/exam'), 'refresh');
        }
        else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('exam', array(
                'exam_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('exam_id', $param2);
            $this->db->delete('exam');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/exam'), 'refresh');
        }
        $running_year = $this->db->get_where('settings', array('type' => 'running_year'))->row()->description;
        $page_data['exams']      = $this->db->get_where('exam', array('year' => $running_year))->result_array();
        $page_data['page_name']  = 'exam';
        $page_data['page_title'] = get_phrase('manage_exam');
        $this->load->view('backend/index', $page_data);
    }

    /****** SEND EXAM MARKS VIA SMS ********/
    function exam_marks_sms($param1 = '' , $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($param1 == 'send_sms') {

            $exam_id    =   $this->input->post('exam_id');
            $class_id   =   $this->input->post('class_id');
            $receiver   =   $this->input->post('receiver');
            if ($exam_id != '' && $class_id != '' && $receiver != '') {
            // get all the students of the selected class
            $students = $this->db->get_where('enroll' , array(
                'class_id' => $class_id,
                    'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
            ))->result_array();
            // get the marks of the student for selected exam
            foreach ($students as $row) {
                if ($receiver == 'student')
                    $receiver_phone = $this->db->get_where('student' , array('student_id' => $row['student_id']))->row()->phone;
                if ($receiver == 'parent') {
                    $parent_id =  $this->db->get_where('student' , array('student_id' => $row['student_id']))->row()->parent_id;
                    if($parent_id != '' || $parent_id != null) {
                        $receiver_phone = $this->db->get_where('parent' , array('parent_id' => $row['parent_id']))->row()->phone;
                        if($receiver_phone == null){
                          $this->session->set_flashdata('error_message' , get_phrase('parent_phone_number_is_not_found'));
                        }
                    }
                }
                $running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
                $this->db->where('exam_id' , $exam_id);
                $this->db->where('student_id' , $row['student_id']);
                $this->db->where('year', $running_year);
                $marks = $this->db->get('mark')->result_array();

                $message = '';
                foreach ($marks as $row2) {
                    $subject       = $this->db->get_where('subject' , array('subject_id' => $row2['subject_id']))->row()->name;
                    $mark_obtained = $row2['mark_obtained'];
                    $message      .= $row2['student_id'] . $subject . ' : ' . $mark_obtained . ' , ';

                }
                // send sms
                $this->sms_model->send_sms( $message , $receiver_phone );
            }
            $this->session->set_flashdata('flash_message' , get_phrase('message_sent'));
          }
          else{
            $this->session->set_flashdata('error_message' , get_phrase('select_all_the_fields'));
          }
            redirect(site_url('admin/exam_marks_sms'), 'refresh');
        }

        $page_data['page_name']  = 'exam_marks_sms';
        $page_data['page_title'] = get_phrase('send_marks_by_sms');
        $this->load->view('backend/index', $page_data);
    }

    function marks_manage()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['page_name']  =   'marks_manage';
        $page_data['page_title'] = get_phrase('manage_exam_marks');
        $this->load->view('backend/index', $page_data);
    }

    function marks_manage_view($exam_id = '' , $class_id = '' , $section_id = '' , $subject_id = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['exam_id']    =   $exam_id;
        $page_data['class_id']   =   $class_id;
        $page_data['subject_id'] =   $subject_id;
        $page_data['section_id'] =   $section_id;
        $page_data['page_name']  =   'marks_manage_view';
        $page_data['page_title'] = get_phrase('manage_exam_marks');
        $this->load->view('backend/index', $page_data);
    }

    function marks_selector()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $data['exam_id']    = $this->input->post('exam_id');
        $data['class_id']   = $this->input->post('class_id');
        $data['section_id'] = $this->input->post('section_id');
        $data['subject_id'] = $this->input->post('subject_id');
        $data['year']       = $this->db->get_where('settings' , array('type'=>'running_year'))->row()->description;
        if($data['class_id'] != '' && $data['exam_id'] != ''){
        $query = $this->db->get_where('mark' , array(
                    'exam_id' => $data['exam_id'],
                        'class_id' => $data['class_id'],
                            'section_id' => $data['section_id'],
                                'subject_id' => $data['subject_id'],
                                    'year' => $data['year']
                ));
        if($query->num_rows() < 1) {
            $students = $this->db->get_where('enroll' , array(
                'class_id' => $data['class_id'] , 'section_id' => $data['section_id'] , 'year' => $data['year']
            ))->result_array();
            foreach($students as $row) {
                $data['student_id'] = $row['student_id'];
                $this->db->insert('mark' , $data);
            }
        }
        redirect(site_url('admin/marks_manage_view/' . $data['exam_id'] . '/' . $data['class_id'] . '/' . $data['section_id'] . '/' . $data['subject_id']), 'refresh');
    }
    else{
        $this->session->set_flashdata('error_message' , get_phrase('select_all_the_fields'));
        $page_data['page_name']  =   'marks_manage';
        $page_data['page_title'] = get_phrase('manage_exam_marks');
        $this->load->view('backend/index', $page_data);
    }
}

    function marks_update($exam_id = '' , $class_id = '' , $section_id = '' , $subject_id = '')
    {
        if ($class_id != '' && $exam_id != '') {
        $running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
        $marks_of_students = $this->db->get_where('mark' , array(
            'exam_id' => $exam_id,
                'class_id' => $class_id,
                    'section_id' => $section_id,
                        'year' => $running_year,
                            'subject_id' => $subject_id
        ))->result_array();
        foreach($marks_of_students as $row) {
            $obtained_marks = $this->input->post('marks_obtained_'.$row['mark_id']);
            $comment = $this->input->post('comment_'.$row['mark_id']);
            $max_marks= $this->input->post('max_marks_'.$row['mark_id']);
            $this->db->where('mark_id' , $row['mark_id']);
            $this->db->update('mark' , array('mark_obtained' => $obtained_marks , 'max_marks' =>  $max_marks ,'comment' => $comment));
        }
        $this->session->set_flashdata('flash_message' , get_phrase('marks_updated'));
        redirect(site_url('admin/marks_manage_view/' . $exam_id . '/' . $class_id . '/' . $section_id . '/' . $subject_id), 'refresh');
    }
    else{
        $this->session->set_flashdata('error_message' , get_phrase('select_all_the_fields'));
        $page_data['page_name']  =   'marks_manage';
        $page_data['page_title'] = get_phrase('manage_exam_marks');
        $this->load->view('backend/index', $page_data);
    }
}
    function marks_get_subject($class_id)
    {
        $page_data['class_id'] = $class_id;
        $this->load->view('backend/admin/marks_get_subject' , $page_data);
    }

    // TABULATION SHEET
    function tabulation_sheet($class_id = '' , $exam_id = '') {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($this->input->post('operation') == 'selection') {
            $page_data['exam_id']    = $this->input->post('exam_id');
            $page_data['class_id']   = $this->input->post('class_id');

            if ($page_data['exam_id'] > 0 && $page_data['class_id'] > 0) {
                redirect(site_url('admin/tabulation_sheet/' . $page_data['class_id'] . '/' . $page_data['exam_id']), 'refresh');
            } else {
                $this->session->set_flashdata('mark_message', 'Choose class and exam');
                redirect(site_url('admin/tabulation_sheet'), 'refresh');
            }
        }
        $page_data['exam_id']    = $exam_id;
        $page_data['class_id']   = $class_id;

        $page_data['page_info'] = 'Exam marks';

        $page_data['page_name']  = 'tabulation_sheet';
        $page_data['page_title'] = get_phrase('tabulation_sheet');
        $this->load->view('backend/index', $page_data);

    }

    function tabulation_sheet_print_view($class_id , $exam_id) {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['class_id'] = $class_id;
        $page_data['exam_id']  = $exam_id;
        $this->load->view('backend/admin/tabulation_sheet_print_view' , $page_data);
    }


    /****MANAGE GRADES*****/
    function grade($param1 = '', $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']        = $this->input->post('name');
            $data['exam_id']     = $this->input->post("exam_id");
            $data['grade_point'] = $this->input->post('grade_point');
            $data['mark_from']   = $this->input->post('mark_from');
            $data['mark_upto']   = $this->input->post('mark_upto');
            if ($this->input->post('comment') != null) {
                $data['comment'] = $this->input->post('comment');
            }

            $this->db->insert('grade', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/grade'), 'refresh');
        }
        if ($param1 == 'do_update') {
            $data['name']        = $this->input->post('name');
            $data['grade_point'] = $this->input->post('grade_point');
            $data['mark_from']   = $this->input->post('mark_from');
            $data['mark_upto']   = $this->input->post('mark_upto');
            if ($this->input->post('comment') != null) {
                $data['comment'] = $this->input->post('comment');
            }
            else{
              $data['comment'] = null;
            }

            $this->db->where('grade_id', $param2);
            $this->db->update('grade', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/grade'), 'refresh');
        } else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('grade', array(
                'grade_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('grade_id', $param2);
            $this->db->delete('grade');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/grade'), 'refresh');
        }
        $page_data['grades']     = $this->db->get('grade')->result_array();
        $page_data['page_name']  = 'grade';
        $page_data['page_title'] = get_phrase('manage_grade');
        $this->load->view('backend/index', $page_data);
    }

    /**********MANAGING CLASS ROUTINE******************/
   function class_routine($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {

            if($this->input->post('class_id') != null){
               $data['class_id']       = $this->input->post('class_id');
            }

            $data['section_id']     = $this->input->post('section_id');
            $data['subject_id']     = $this->input->post('subject_id');
            $data['time_start']     = $this->input->post('time_start');
            $data['time_end']   = $this->input->post('time_end');
            // // 12 AM for starting time
            // if ($this->input->post('time_start') == 12 && $this->input->post('starting_ampm') == 1) {
            //     $data['time_start'] = 24;
            // }
            // // 12 PM for starting time
            // else if ($this->input->post('time_start') == 12 && $this->input->post('starting_ampm') == 2) {
            //     $data['time_start'] = 12;
            // }
            // // otherwise for starting time
            // else{
            //     $data['time_start']     = $this->input->post('time_start') + (12 * ($this->input->post('starting_ampm') - 1));
            // }
            // // 12 AM for ending time
            // if ($this->input->post('time_end') == 12 && $this->input->post('ending_ampm') == 1) {
            //     $data['time_end'] = 24;
            // }
            // // 12 PM for ending time
            // else if ($this->input->post('time_end') == 12 && $this->input->post('ending_ampm') == 2) {
            //     $data['time_end'] = 12;
            // }
            // // otherwise for ending time
            // else{
            //     $data['time_end']       = $this->input->post('time_end') + (12 * ($this->input->post('ending_ampm') - 1));
            // }

            // $data['time_start_min'] = $this->input->post('time_start_min');
            // $data['time_end_min']   = $this->input->post('time_end_min');
            $data['day']            = $this->input->post('day');
            $data['year']           = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            // checking duplication
            $array = array(
               'section_id'    => $data['section_id'],
               'class_id'      => $data['class_id'],
               'time_start'    => $data['time_start'],
               'time_end'      => $data['time_end'],
               // 'time_start_min'=> $data['time_start_min'],
               // 'time_end_min'  => $data['time_end_min'],
               'day'           => $data['day']
               // 'year'          => $data['year']
            );
            $validation = duplication_of_class_routine_on_create($array);
            if ($validation == 1) {
                $this->db->insert('class_routine', $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('time_conflicts'));
            }

            redirect(site_url('admin/class_routine_add'), 'refresh');
        }
        if ($param1 == 'do_update') {
            $data['class_id']       = $this->input->post('class_id');
            if($this->input->post('section_id') != '') {
                $data['section_id'] = $this->input->post('section_id');
            }
            $data['subject_id']     = $this->input->post('subject_id');

            // // 12 AM for starting time
            // if ($this->input->post('time_start') == 12 && $this->input->post('starting_ampm') == 1) {
            //     $data['time_start'] = 24;
            // }
            // // 12 PM for starting time
            // else if ($this->input->post('time_start') == 12 && $this->input->post('starting_ampm') == 2) {
            //     $data['time_start'] = 12;
            // }
            // // otherwise for starting time
            // else{
            //     $data['time_start']     = $this->input->post('time_start') + (12 * ($this->input->post('starting_ampm') - 1));
            // }
            // // 12 AM for ending time
            // if ($this->input->post('time_end') == 12 && $this->input->post('ending_ampm') == 1) {
            //     $data['time_end'] = 24;
            // }
            // // 12 PM for ending time
            // else if ($this->input->post('time_end') == 12 && $this->input->post('ending_ampm') == 2) {
            //     $data['time_end'] = 12;
            // }
            // // otherwise for ending time
            // else{
            //     $data['time_end']       = $this->input->post('time_end') + (12 * ($this->input->post('ending_ampm') - 1));
            // }

            $data['time_start'] = $this->input->post('time_start');
            $data['time_end']   = $this->input->post('time_end');
            $data['day']            = $this->input->post('day');
            // $data['year']           = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            if ($data['subject_id'] != '') {
            // checking duplication
            $array = array(
               'section_id'    => $data['section_id'],
               'class_id'      => $data['class_id'],
               'time_start'    => $data['time_start'],
               'time_end'      => $data['time_end'],
               // 'time_start_min'=> $data['time_start_min'],
               // 'time_end_min'  => $data['time_end_min'],
               'day'           => $data['day']
               // 'year'          => $data['year']
            );
            $validation = duplication_of_class_routine_on_edit($array, $param2);

            if ($validation == 1) {
                $this->db->where('class_routine_id', $param2);
                $this->db->update('class_routine', $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('time_conflicts'));
            }
          }
          else{
            $this->session->set_flashdata('error_message' , get_phrase('subject_is_not_found'));
          }

            redirect(site_url('admin/class_routine_view/' . $data['class_id']), 'refresh');
        }
        else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('class_routine', array(
                'class_routine_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $class_id = $this->db->get_where('class_routine' , array('class_routine_id' => $param2))->row()->class_id;
            $this->db->where('class_routine_id', $param2);
            $this->db->delete('class_routine');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/class_routine_view/' . $class_id), 'refresh');
        }

    }
     function class_routine_hour($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($param1 == 'create') 
        {
            $year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            $temp = $this->db->query("SELECT * FROM `class_routine_hour` WHERE day= '".$this->input->post(day)."' AND `time_from` = '".$this->input->post(time_start)."' AND `time_to` = '".$this->input->post(time_end)."' AND `time_from_min` = '".$this->input->post(time_start_min)."' AND `time_to_min` = '".$this->input->post(time_end_min)."'")->result_array();
            if($temp == NULL){
                $time['time_from_min'] = $this->input->post('time_start_min');
                $time['time_to_min']   = $this->input->post('time_end_min');
                $time['year']           = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
                $time['day'] = $this->input->post('day');  
                $time['time_from'] = $this->input->post('time_start');
                $time['time_to'] = $this->input->post('time_end');
                $this->db->insert('class_routine_hour',$time);
                $this->session->set_flashdata('flash_message',get_phrase('data_insert'));
            }else{
               $this->session->set_flashdata('error_message',get_phrase('same_hour_period')); 
            }
            redirect(site_url('admin/class_routine_hour/view'),'refresh');
        }
        if($param1 == 'do_update')
        {
            $temp = $this->db->query("SELECT * FROM `class_routine_hour` WHERE day= '".$this->input->post(day)."' AND `time_from` = '".$this->input->post(time_from)."' AND `time_to` = '".$this->input->post(time_to)."' AND `time_from_min` = '".$this->input->post(time_start_min)."' AND `time_to_min` = '".$this->input->post(time_end_min)."'")->result_array();
            if($temp == NULL){
                $time['time_from_min'] = $this->input->post('time_from_min');
                $time['time_to_min']   = $this->input->post('time_to_min');
                $time['year']           = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
                $time['day'] = $this->input->post('day');  
                $time['time_from'] = $this->input->post('time_from');
                $time['time_to'] = $this->input->post('time_to');
                $this->db->where('class_hour_id', $param2);
                $this->db->update('class_routine_hour', $time);
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }else{
               $this->session->set_flashdata('error_message',get_phrase('same_hour_period')); 
            }
                redirect(site_url('admin/class_routine_hour/view'),'refresh');
        }
        
        if($param1 == 'delete')
        {
            $this->db->where('class_hour_id', $param2);
            $this->db->delete('class_routine_hour');
            $this->db->where('day',$param2);
            $this->db->delete('class_routine');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/class_routine_hour/view'), 'refresh');
        }
        if($param1 == 'view'){    
        $page_data['class_hour'] = $this->db->get('class_routine_hour')->result_Array();
        $page_data['page_name']  = 'class_routine_hour';
        $page_data['page_title'] = get_phrase('add_class_hour');
        $this->load->view('backend/index', $page_data);
        }
    }
    
    function class_routine_add()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['page_name']  = 'class_routine_add';
        $page_data['page_title'] = get_phrase('add_class_routine');
        $this->load->view('backend/index', $page_data);
    }

    function class_routine_view($class_id)
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['page_name']  = 'class_routine_view';
        $page_data['class_id']  =   $class_id;
        $page_data['page_title'] = get_phrase('class_routine');
        $this->load->view('backend/index', $page_data);
    }

    function class_routine_print_view($class_id , $section_id)
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['class_id']   =   $class_id;
        $page_data['section_id'] =   $section_id;
        $this->load->view('backend/admin/class_routine_print_view' , $page_data);
    }

    function get_class_section_subject($class_id)
    {
        $page_data['class_id'] = $class_id;
        $this->load->view('backend/admin/class_routine_section_subject_selector' , $page_data);
    }

    function section_subject_edit($class_id , $class_routine_id)
    {
        $page_data['class_id']          =   $class_id;
        $page_data['class_routine_id']  =   $class_routine_id;
        $this->load->view('backend/admin/class_routine_section_subject_edit' , $page_data);
    }

    function manage_attendance()
    {
        if($this->session->userdata('admin_login')!=1)
            redirect(site_url('login'), 'refresh');

        $page_data['page_name']  =  'manage_attendance';
        $page_data['page_title'] =  get_phrase('manage_attendance_of_class');
        $this->load->view('backend/index', $page_data);
    }

    function manage_attendance_view($class_id = '' , $section_id = '' , $timestamp = '',$session='')
    {
        if($this->session->userdata('admin_login')!=1)
            redirect(site_url('login'), 'refresh');

        $class_name = $this->db->get_where('class' , array(
            'class_id' => $class_id
        ))->row()->name;
        $page_data['class_id'] = $class_id;
        $page_data['timestamp'] = $timestamp;
        $page_data['session'] =  $session;
        $page_data['page_name'] = 'manage_attendance_view';
        $section_name = $this->db->get_where('section' , array(
            'section_id' => $section_id
        ))->row()->name;
        $page_data['section_id'] = $section_id;
        $page_data['page_title'] = get_phrase('manage_attendance_of_class') . ' ' . $class_name . ' : ' . get_phrase('section') . ' ' . $section_name;
        $this->load->view('backend/index', $page_data);
    }
    function get_section($class_id) {
          $page_data['class_id'] = $class_id;
          $this->load->view('backend/admin/manage_attendance_section_holder' , $page_data);
    }
    function attendance_selector()
    {
        $data['class_id']   = $this->input->post('class_id');
        $data['year']       = $this->input->post('year');
        $data['timestamp']  = date("Y-m-d",strtotime($this->input->post('timestamp')));
        $data['section_id'] = $this->input->post('section_id');
        $data['session']    = $this->input->post('session'); 
        $query = $this->db->get_where('attendance' ,array(
            'class_id'=>$data['class_id'],
                'section_id'=>$data['section_id'],
                    'year'=>$data['year'],
                        'timestamp'=>$data['timestamp']
        ));
        if($query->num_rows() < 1) {
            $students = $this->db->get_where('enroll' , array(
                'class_id' => $data['class_id'] , 'section_id' => $data['section_id'] , 'year' => $data['year']
            ))->result_array();

            foreach($students as $row) {
                $attn_data['class_id']   = $data['class_id'];
                $attn_data['year']       = $data['year'];
                $attn_data['timestamp']  = $data['timestamp'];
                $attn_data['section_id'] = $data['section_id'];
                $attn_data['student_id'] = $row['student_id'];
                $this->db->insert('attendance' , $attn_data);
            }

        }
        redirect(site_url('admin/manage_attendance_view/' . $data['class_id'] . '/' . $data['section_id'] . '/' . $data['timestamp'].'/'.$data['session']),'refresh');
    }

    function attendance_update($class_id = '' , $section_id = '' , $timestamp = '',$session)
    {
        $dt=date("Y-m-d",$timestamp);
        $running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
        $active_sms_service = $this->db->get_where('settings' , array('type' => 'active_sms_service'))->row()->description;
        $attendance_of_students = $this->db->get_where('attendance' , array(
            'class_id'=>$class_id,'section_id'=>$section_id,'year'=>$running_year,'timestamp'=>$dt
        ))->result_array();
        foreach($attendance_of_students as $row) {
            $attendance_status = $this->input->post('status_'.$row['attendance_id']);
            $this->db->where('attendance_id' , $row['attendance_id']);
            if($session==1){
            $this->db->update('attendance' , array('status' => $attendance_status));
            }else{
            $this->db->update('attendance' , array('status1' => $attendance_status));      
            }

            if ($attendance_status == 2) {

                if ($active_sms_service != '' || $active_sms_service != 'disabled') {
                    $student_name   = $this->db->get_where('student' , array('student_id' => $row['student_id']))->row()->name;
                    $parent_id      = $this->db->get_where('student' , array('student_id' => $row['student_id']))->row()->parent_id;
                    $message        = 'Your child' . ' ' . $student_name . 'is absent today.';
                    if($parent_id != null && $parent_id != 0){
                        $receiver_phone = $this->db->get_where('parent' , array('parent_id' => $parent_id))->row()->phone;
                        if($receiver_phone != '' || $receiver_phone != null){
                            $this->sms_model->send_sms($message,$receiver_phone);
                        }
                        else{
                            $this->session->set_flashdata('error_message' , get_phrase('parent_phone_number_is_not_found'));
                        }
                    }
                    else{
                        $this->session->set_flashdata('error_message' , get_phrase('parent_phone_number_is_not_found'));
                    }
                }
            }
        }
        $this->session->set_flashdata('flash_message' , get_phrase('attendance_updated'));
        redirect(site_url('admin/manage_attendance_view/'.$class_id.'/'.$section_id.'/'.$timestamp) , 'refresh');
    }

	/****** DAILY ATTENDANCE *****************/
	function manage_attendance2($date='',$month='',$year='',$class_id='' , $section_id = '' , $session = '')
	{
		if($this->session->userdata('admin_login')!=1)
            redirect(site_url('login') , 'refresh');

        $active_sms_service = $this->db->get_where('settings' , array('type' => 'active_sms_service'))->row()->description;
        $running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;


		if($_POST)
		{
			// Loop all the students of $class_id
            $this->db->where('class_id' , $class_id);
            if($section_id != '') {
                $this->db->where('section_id' , $section_id);
            }
            //$session = base64_decode( urldecode( $session ) );
            $this->db->where('year' , $session);
            $students = $this->db->get('enroll')->result_array();
            foreach ($students as $row)
            {
                $attendance_status  =   $this->input->post('status_' . $row['student_id']);

                $this->db->where('student_id' , $row['student_id']);
                $this->db->where('date' , $date);
                $this->db->where('year' , $year);
                $this->db->where('class_id' , $row['class_id']);
                if($row['section_id'] != '' && $row['section_id'] != 0) {
                    $this->db->where('section_id' , $row['section_id']);
                }
                $this->db->where('session' , $session);

                $this->db->update('attendance' , array('status' => $attendance_status));

                if ($attendance_status == 2) {

                    if ($active_sms_service != '' || $active_sms_service != 'disabled') {
                        $student_name   = $this->db->get_where('student' , array('student_id' => $row['student_id']))->row()->name;
                        $parent_id      = $this->db->get_where('student' , array('student_id' => $row['student_id']))->row()->parent_id;
                        $receiver_phone = $this->db->get_where('parent' , array('parent_id' => $parent_id))->row()->phone;
                        $message        = 'Your child' . ' ' . $student_name . 'is absent today.';
                        $this->sms_model->send_sms($message,$receiver_phone);
                    }
                }

            }

			$this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
			redirect(site_url('admin/manage_attendance/'.$date.'/'.$month.'/'.$year.'/'.$class_id.'/'.$section_id.'/'.$session) , 'refresh');
		}
        $page_data['date']       =	$date;
        $page_data['month']      =	$month;
        $page_data['year']       =	$year;
        $page_data['class_id']   =  $class_id;
        $page_data['section_id'] =  $section_id;
        $page_data['session']    =  $session;

        $page_data['page_name']  =	'manage_attendance';
        $page_data['page_title'] =	get_phrase('manage_daily_attendance');
		$this->load->view('backend/index', $page_data);
	}
	function attendance_selector2()
	{
        //$session = $this->input->post('session');
        //$encoded_session = urlencode( base64_encode( $session ) );
		redirect(site_url('admin/manage_attendance/'.$this->input->post('date').'/'.
					$this->input->post('month').'/'.
						$this->input->post('year').'/'.
							$this->input->post('class_id').'/'.
                                $this->input->post('section_id').'/'.
                                    $this->input->post('session')) , 'refresh');
	}
        ///////ATTENDANCE REPORT /////
     function attendance_report() {
         $page_data['month']        = date('m');
         $page_data['page_name']    = 'attendance_report';
         $page_data['page_title']   = get_phrase('attendance_report');
         $this->load->view('backend/index',$page_data);
     }
     function attendance_report_view($class_id = '', $section_id = '', $month = '', $sessional_year = '')
     {
         if($this->session->userdata('admin_login')!=1)
            redirect(base_url() , 'refresh');

        $class_name                     = $this->db->get_where('class', array('class_id' => $class_id))->row()->name;
        $section_name                   = $this->db->get_where('section', array('section_id' => $section_id))->row()->name;
        $page_data['class_id']          = $class_id;
        $page_data['section_id']        = $section_id;
        $page_data['month']             = $month;
        $page_data['sessional_year']    = $sessional_year;
        $page_data['page_name']         = 'attendance_report_view';
        $page_data['page_title']        = get_phrase('attendance_report_of_class') . ' ' . $class_name . ' : ' . get_phrase('section') . ' ' . $section_name;
        $this->load->view('backend/index', $page_data);
     }
     function attendance_report_print_view($class_id ='' , $section_id = '' , $month = '', $sessional_year = '') {
          if ($this->session->userdata('admin_login') != 1)
            redirect(base_url(), 'refresh');

        $page_data['class_id']          = $class_id;
        $page_data['section_id']        = $section_id;
        $page_data['month']             = $month;
        $page_data['sessional_year']    = $sessional_year;
        $this->load->view('backend/admin/attendance_report_print_view' , $page_data);
    }

    function attendance_report_selector()
    {   if($this->input->post('class_id') == '' || $this->input->post('sessional_year') == '') {
            $this->session->set_flashdata('error_message' , get_phrase('please_make_sure_class_and_sessional_year_are_selected'));
            redirect(site_url('admin/attendance_report'), 'refresh');
        }
        $data['class_id']       = $this->input->post('class_id');
        $data['section_id']     = $this->input->post('section_id');
        $data['month']          = $this->input->post('month');
        $data['sessional_year'] = $this->input->post('sessional_year');
        redirect(site_url('admin/attendance_report_view/' . $data['class_id'] . '/' . $data['section_id'] . '/' . $data['month'] . '/' . $data['sessional_year']), 'refresh');
    }

    /******MANAGE BILLING / INVOICES WITH STATUS*****/
    function invoice($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($param1 == 'create') {
            $data['student_id']         = $this->input->post('student_id');
            $data['title']              = $this->input->post('title');
            $data['amount']             = $this->input->post('amount');
            $data['amount_paid']        = $this->input->post('amount_paid');
            $data['due']                = $data['amount'] - $data['amount_paid'];
            $data['status']             = $this->input->post('status');
            $data['creation_timestamp'] = strtotime($this->input->post('date'));
            $data['year']               = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            if ($this->input->post('description') != null) {
                $data['description']    = $this->input->post('description');
            }

            $this->db->insert('invoice', $data);
            $invoice_id = $this->db->insert_id();

            $data2['invoice_id']        =   $invoice_id;
            $data2['student_id']        =   $this->input->post('student_id');
            $data2['title']             =   $this->input->post('title');
            $data2['payment_type']      =  'income';
            $data2['method']            =   $this->input->post('method');
            $data2['amount']            =   $this->input->post('amount_paid');
            $data2['timestamp']         =   strtotime($this->input->post('date'));
            $data2['year']              =  $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            if ($this->input->post('description') != null) {
                $data2['description']    = $this->input->post('description');
            }
            $this->db->insert('payment' , $data2);

            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/student_payment'), 'refresh');
        }

        if ($param1 == 'create_mass_invoice') {
            foreach ($this->input->post('student_id') as $id) {

                $data['student_id']         = $id;
                $data['title']              = $this->input->post('title');
                $data['description']        = $this->input->post('description');
                $data['amount']             = $this->input->post('amount');
                $data['amount_paid']        = $this->input->post('amount_paid');
                $data['due']                = $data['amount'] - $data['amount_paid'];
                $data['status']             = $this->input->post('status');
                $data['creation_timestamp'] = strtotime($this->input->post('date'));
                $data['year']               = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;

                $this->db->insert('invoice', $data);
                $invoice_id = $this->db->insert_id();

                $data2['invoice_id']        =   $invoice_id;
                $data2['student_id']        =   $id;
                $data2['title']             =   $this->input->post('title');
                $data2['description']       =   $this->input->post('description');
                $data2['payment_type']      =  'income';
                $data2['method']            =   $this->input->post('method');
                $data2['amount']            =   $this->input->post('amount_paid');
                $data2['timestamp']         =   strtotime($this->input->post('date'));
                $data2['year']               =   $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;

                $this->db->insert('payment' , $data2);
            }

            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/student_payment'), 'refresh');
        }

        if ($param1 == 'do_update') {
            $data['student_id']         = $this->input->post('student_id');
            $data['title']              = $this->input->post('title');
            $data['description']        = $this->input->post('description');
            $data['amount']             = $this->input->post('amount');
            $data['status']             = $this->input->post('status');
            $data['creation_timestamp'] = strtotime($this->input->post('date'));

            $this->db->where('invoice_id', $param2);
            $this->db->update('invoice', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/income'), 'refresh');
        } else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('invoice', array(
                'invoice_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'take_payment') {
            $data['invoice_id']   =   $this->input->post('invoice_id');
            $data['student_id']   =   $this->input->post('student_id');
            $data['title']        =   $this->input->post('title');
            $data['description']  =   $this->input->post('description');
            $data['payment_type'] =   'income';
            $data['method']       =   $this->input->post('method');
            $data['amount']       =   $this->input->post('amount');
            $data['timestamp']    =   strtotime($this->input->post('timestamp'));
            $data['year']         =   $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            $this->db->insert('payment' , $data);

            $status['status']   =   $this->input->post('status');
            $this->db->where('invoice_id' , $param2);
            $this->db->update('invoice' , array('status' => $status['status']));

            $data2['amount_paid']   =   $this->input->post('amount');
            $data2['status']        =   $this->input->post('status');
            $this->db->where('invoice_id' , $param2);
            $this->db->set('amount_paid', 'amount_paid + ' . $data2['amount_paid'], FALSE);
            $this->db->set('due', 'due - ' . $data2['amount_paid'], FALSE);
            $this->db->update('invoice');

            $this->session->set_flashdata('flash_message' , get_phrase('payment_successfull'));
            redirect(site_url('admin/income'), 'refresh');
        }

        if ($param1 == 'delete') {
            $this->db->where('invoice_id', $param2);
            $this->db->delete('invoice');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/income'), 'refresh');
        }
        
        $page_data['page_name']  = 'invoice';
        $page_data['page_title'] = get_phrase('manage_invoice/payment');
        $this->db->order_by('creation_timestamp', 'desc');
        $page_data['invoices'] = $this->db->get('invoice')->result_array();
        $this->load->view('backend/index', $page_data);
    }

    function income($param1 = 'invoices', $param2 = '', $param3 = '') {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $page_data['page_name'] = 'income';
        $page_data['inner'] = $param1;
        $page_data['page_title'] = get_phrase('student_payments');
        $this->load->view('backend/index', $page_data);
    }

    function get_invoices() {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $columns = array(
            0 => 'invoice_id',
            1 => 'student',
            2 => 'title',
            3 => 'total',
            4 => 'paid',
            5 => 'status',
            6 => 'date',
            7 => 'options',
            8 => 'invoice_id'
        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir   = $this->input->post('order')[0]['dir'];

        $totalData = $this->ajaxload->all_invoices_count();
        $totalFiltered = $totalData;

        if(empty($this->input->post('search')['value'])) {
            $invoices = $this->ajaxload->all_invoices($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value'];
            $invoices =  $this->ajaxload->invoice_search($limit,$start,$search,$order,$dir);
            $totalFiltered = $this->ajaxload->invoice_search_count($search);
        }

        $data = array();
        if(!empty($invoices)) {
            foreach ($invoices as $row) {

                if ($row->due == 0) {
                    $status = '<button class="btn btn-success btn-xs">'.get_phrase('paid').'</button>';
                    $payment_option = '';
                } else {
                    $status = '<button class="btn btn-danger btn-xs">'.get_phrase('unpaid').'</button>';
                    $payment_option = '<li><a href="#" onclick="invoice_pay_modal('.$row->invoice_id.')"><i class="entypo-bookmarks"></i>&nbsp;'.get_phrase('take_payment').'</a></li><li class="divider"></li>';
                }


                $options = '<div class="btn-group"><button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
                                    Action <span class="caret"></span></button><ul class="dropdown-menu dropdown-default pull-right" role="menu">'.$payment_option.'<li><a href="#" onclick="invoice_view_modal('.$row->invoice_id.')"><i class="entypo-credit-card"></i>&nbsp;'.get_phrase('view_invoice').'</a></li><li class="divider"></li><li><a href="#" onclick="invoice_edit_modal('.$row->invoice_id.')"><i class="entypo-pencil"></i>&nbsp;'.get_phrase('edit').'</a></li><li class="divider"></li><li><a href="#" onclick="invoice_delete_confirm('.$row->invoice_id.')"><i class="entypo-trash"></i>&nbsp;'.get_phrase('delete').'</a></li></ul></div>';

                $nestedData['invoice_id'] = $row->invoice_id;
                $nestedData['student'] = $this->crud_model->get_type_name_by_id('student',$row->student_id);
                $nestedData['title'] = $row->title;
                $nestedData['total'] = $row->amount;
                $nestedData['paid'] = $row->amount_paid;
                $nestedData['status'] = $status;
                $nestedData['date'] = date('d M,Y', $row->creation_timestamp);
                $nestedData['options'] = $options;

                $data[] = $nestedData;

            }
        }

        $json_data = array(
            "draw"            => intval($this->input->post('draw')),
            "recordsTotal"    => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data"            => $data
        );

        echo json_encode($json_data);
    }

    function get_payments() {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $columns = array(
            0 => 'payment_id',
            1 => 'title',
            2 => 'description',
            3 => 'method',
            4 => 'amount',
            5 => 'date',
            6 => 'options',
            7 => 'payment_id'
        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir   = $this->input->post('order')[0]['dir'];

        $totalData = $this->ajaxload->all_payments_count();
        $totalFiltered = $totalData;

        if(empty($this->input->post('search')['value'])) {
            $payments = $this->ajaxload->all_payments($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value'];
            $payments =  $this->ajaxload->payment_search($limit,$start,$search,$order,$dir);
            $totalFiltered = $this->ajaxload->payment_search_count($search);
        }

        $data = array();
        if(!empty($payments)) {
            foreach ($payments as $row) {

                if ($row->method == 1)
                    $method = get_phrase('cash');
                else if ($row->method == 2)
                    $method = get_phrase('cheque');
                else if ($row->method == 3)
                    $method = get_phrase('card');
                else if ($row->method == 'Paypal')
                    $method = 'Paypal';
                else
                    $method = 'Stripe';


                $options = '<a href="#" onclick="invoice_view_modal('.$row->invoice_id.')"><i class="entypo-credit-card"></i>&nbsp;'.get_phrase('view_invoice').'</a>';

                $nestedData['payment_id'] = $row->payment_id;
                $nestedData['title'] = $row->title;
                $nestedData['description'] = $row->description;
                $nestedData['method'] = $method;
                $nestedData['amount'] = $row->amount;
                $nestedData['date'] = date('d M,Y', $row->timestamp);
                $nestedData['options'] = $options;

                $data[] = $nestedData;

            }
        }

        $json_data = array(
            "draw"            => intval($this->input->post('draw')),
            "recordsTotal"    => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data"            => $data
        );

        echo json_encode($json_data);
    }

    function student_payment($param1 = '' , $param2 = '' , $param3 = '') {

        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $page_data['page_name']  = 'student_payment';
        $page_data['page_title'] = get_phrase('create_student_payment');
        $this->load->view('backend/index', $page_data);
    }

    function expense($param1 = '' , $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['title']               =   $this->input->post('title');
            $data['expense_category_id'] =   $this->input->post('expense_category_id');
            $data['payment_type']        =   'expense';
            $data['method']              =   $this->input->post('method');
            $data['amount']              =   $this->input->post('amount');
            $data['timestamp']           =   strtotime($this->input->post('timestamp'));
            $data['year']                =   $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            if ($this->input->post('description') != null) {
                $data['description']     =   $this->input->post('description');
            }
            $this->db->insert('payment' , $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/expense'), 'refresh');
        }

        if ($param1 == 'edit') {
            $data['title']               =   $this->input->post('title');
            $data['expense_category_id'] =   $this->input->post('expense_category_id');
            $data['payment_type']        =   'expense';
            $data['method']              =   $this->input->post('method');
            $data['amount']              =   $this->input->post('amount');
            $data['timestamp']           =   strtotime($this->input->post('timestamp'));
            $data['year']                =   $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            if ($this->input->post('description') != null) {
                $data['description']     =   $this->input->post('description');
            }
            else{
                $data['description']     =   null;
            }
            $this->db->where('payment_id' , $param2);
            $this->db->update('payment' , $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/expense'), 'refresh');
        }

        if ($param1 == 'delete') {
            $this->db->where('payment_id' , $param2);
            $this->db->delete('payment');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/expense'), 'refresh');
        }

        $page_data['page_name']  = 'expense';
        $page_data['page_title'] = get_phrase('expenses');
        $this->load->view('backend/index', $page_data);
    }

    function get_expenses() {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $columns = array(
            0 => 'payment_id',
            1 => 'title',
            2 => 'category',
            3 => 'method',
            4 => 'amount',
            5 => 'date',
            6 => 'options',
            7 => 'payment_id'
        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir   = $this->input->post('order')[0]['dir'];

        $totalData = $this->ajaxload->all_expenses_count();
        $totalFiltered = $totalData;

        if(empty($this->input->post('search')['value'])) {
            $expenses = $this->ajaxload->all_expenses($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value'];
            $expenses =  $this->ajaxload->expense_search($limit,$start,$search,$order,$dir);
            $totalFiltered = $this->ajaxload->expense_search_count($search);
        }

        $data = array();
        if(!empty($expenses)) {
            foreach ($expenses as $row) {
                $category = $this->db->get_where('expense_category', array('expense_category_id' => $row->expense_category_id))->row()->name;
                if ($row->method == 1)
                    $method = get_phrase('cash');
                else if ($row->method == 2)
                    $method = get_phrase('cheque');
                else
                    $method = get_phrase('card');
                $options = '<div class="btn-group"><button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
                                    Action <span class="caret"></span></button><ul class="dropdown-menu dropdown-default pull-right" role="menu"><li><a href="#" onclick="expense_edit_modal('.$row->payment_id.')"><i class="entypo-pencil"></i>&nbsp;'.get_phrase('edit').'</a></li><li class="divider"></li><li><a href="#" onclick="expense_delete_confirm('.$row->payment_id.')"><i class="entypo-trash"></i>&nbsp;'.get_phrase('delete').'</a></li></ul></div>';

                $nestedData['payment_id'] = $row->payment_id;
                $nestedData['title'] = $row->title;
                $nestedData['category'] = $category;
                $nestedData['method'] = $method;
                $nestedData['amount'] = $row->amount;
                $nestedData['date'] = date('d M,Y', $row->timestamp);
                $nestedData['options'] = $options;

                $data[] = $nestedData;
            }
        }

        $json_data = array(
            "draw"            => intval($this->input->post('draw')),
            "recordsTotal"    => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data"            => $data
        );

        echo json_encode($json_data);
    }

    function expense_category($param1 = '' , $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']   =   $this->input->post('name');
            $this->db->insert('expense_category' , $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/expense_category'), 'refresh');
        }
        if ($param1 == 'edit') {
            $data['name']   =   $this->input->post('name');
            $this->db->where('expense_category_id' , $param2);
            $this->db->update('expense_category' , $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/expense_category'), 'refresh');
        }
        if ($param1 == 'delete') {
            $this->db->where('expense_category_id' , $param2);
            $this->db->delete('expense_category');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/expense_category'), 'refresh');
        }

        $page_data['page_name']  = 'expense_category';
        $page_data['page_title'] = get_phrase('expense_category');
        $this->load->view('backend/index', $page_data);
    }

    //added 
    
    function fee_particulars($param1 = '', $param2 = '', $param3 = ''){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if($param1 == 'create') {
            $data['class_id']   = $this->input->post('class_id');
            $data['particulars'] = $this->input->post('particulars');
            $data['amount']      = $this->input->post('amount');

            $this->db->insert("fee_particulars",$data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/fee_particulars'), 'refresh');
        }
        if($param1 == 'do_update') {
            $data['particulars'] = $this->input->post('particulars');
            $data['amount']      = $this->input->post('amount');

            $this->db->where('id', $param2);
            $this->db->update('fee_particulars', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/fee_particulars'), 'refresh');
        }
        if($param1 == 'delete') {
            $this->db->where('id', $param2);
            $this->db->delete('fee_particulars');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/fee_particulars'), 'refresh');      
        }

        $page_data['page_name']  = 'fee_particulars';
        $page_data['page_title'] = get_phrase('fee_particulars');
        $this->load->view('backend/index', $page_data);
    }
    
    // MANAGE TRANSPORT
    function transference($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($param1 == 'create') {
            $data['name']       = $this->input->post('name');
            $data['email']      = $this->input->post('email');
            $data['password']   = sha1($this->input->post('password'));
            $validation = email_validation($data['email']);
            if ($validation == 1) {
                $this->db->insert('transference', $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
                $this->email_model->account_opening_email('transference', $data['email'], $this->input->post('password')); //SEND EMAIL ACCOUNT OPENING EMAIL
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }
            redirect(site_url('admin/transference'), 'refresh');
        }

        if ($param1 == 'edit') {
            $data['name']   = $this->input->post('name');
            $data['email']  = $this->input->post('email');
            $validation = email_validation_for_edit($data['email'], $param2, 'transference');
            if ($validation == 1) {
                $this->db->where('transference_id' , $param2);
                $this->db->update('transference' , $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }

            redirect(site_url('admin/transference'), 'refresh');
        }

        if ($param1 == 'delete') {
            $this->db->where('transference_id' , $param2);
            $this->db->delete('transference');

            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/transference'), 'refresh');
        }

        $page_data['page_title']    = get_phrase('all_transport');
        $page_data['page_name']     = 'transference';
        $this->load->view('backend/index', $page_data);
    }
    
    /**********MANAGE TRANSPORT / VEHICLES / ROUTES********************/
    function transport($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect('login', 'refresh');
        if ($param1 == 'create') {
            $data['route_name']        = $this->input->post('route_name');
            $data['number_of_vehicle'] = $this->input->post('number_of_vehicle');
            if ($this->input->post('description') != null) {
               $data['description']    = $this->input->post('description');
            }
            if ($this->input->post('route_fare') != null) {
               $data['route_fare']     = $this->input->post('route_fare');
            }
            $this->db->insert('transport', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/transport'), 'refresh');
        }
        if($param1 == "vehicle_create") {
            $data['v_no'] = $this->input->post('v_no');
            $data['v_type'] = $this->input->post('v_type');
            if ($this->input->post('v_incharge') != null) {
            $data['v_incharge'] = $this->input->post('v_incharge');
            }
            if ($this->input->post('v_insurance') != null) {
            $data['v_insurance'] = $this->input->post('v_insurance');
            }
            if ($this->input->post('v_servicedue') != null) {
            $data['v_servicedue'] = $this->input->post('v_servicedue');
            }
            if ($this->input->post('phone') != null) {
            $data['phone'] = $this->input->post('phone');
            }
            if($_FILES['v_rc']['tmp_name'] !=NULL){
                $path="uploads/transport";
                $target_file = $path . basename($_FILES["v_rc"]["name"]);  
                $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));  
                $file_name=$this->input->post('v_no').".".$FileType; 
                move_uploaded_file($_FILES['v_rc']['tmp_name'], $path."/".$file_name);
                $data["v_rc"] = $file_name;
            }
            $this->db->insert('vehicles', $data);
            $vehicle_id=$this->db->insert_id();
             if($vehicle_id){
             $transport=$this->input->post("transport_id");   
             foreach ($transport as $row){
                $route_vehicle= array('transport_id' => $row,'vehicle_id' =>$vehicle_id );
                $this->db->insert('routes_vehicles', $route_vehicle); 
             }               
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));    
            }
            redirect(site_url('admin/transport'), 'refresh');
        }
        if($param1 == "vehicle_edit") {
            $data['v_no'] = $this->input->post('v_no');
            $data['v_type'] = $this->input->post('v_type');
            if ($this->input->post('v_incharge') != null) {
            $data['v_incharge'] = $this->input->post('v_incharge');
            }
            if ($this->input->post('v_insurance') != null) {
            $data['v_insurance'] = $this->input->post('v_insurance');
            }
            if ($this->input->post('v_servicedue') != null) {
            $data['v_servicedue'] = $this->input->post('v_servicedue');
            }
            if ($this->input->post('phone') != null) {
            $data['phone'] = $this->input->post('phone');
            }
            $this->db->where("id",$param2);
            $this->db->update('vehicles', $data);              
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated')); 
        }
        if($param1 =='delete_vehicle') {
           $vehicle=$this->db->get_where("vehicles", array('id' => $param2 ))->result_array();  
           if($vehicle){
             if(file_exists("uploads/transport/".$vehicle[0]['v_rc'])){
                unlink("uploads/transport/".$vehicle[0]['v_rc']); 
             }
            $this->db->where('vehicle_id', $param2);
            $this->db->delete('routes_vehicles');

            $this->db->where('id', $param2);
            $this->db->delete('vehicles');

            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/transport'), 'refresh');
           }       
        }  
        if ($param1 == 'do_update') {
            $data['route_name']        = $this->input->post('route_name');
            $data['number_of_vehicle'] = $this->input->post('number_of_vehicle');
            if ($this->input->post('description') != null) {
               $data['description']    = $this->input->post('description');
            }
            else{
                $data['description'] = null;
            }
            if ($this->input->post('route_fare') != null) {
               $data['route_fare']   = $this->input->post('route_fare');
            }
            else{
                $data['route_fare']  = null;
            }

            $this->db->where('transport_id', $param2);
            $this->db->update('transport', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/transport'), 'refresh');
        } else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('transport', array(
                'transport_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('transport_id', $param2);
            $this->db->delete('transport');

            $this->db->where('transport_id', $param2);
            $this->db->delete('routes_vehicles');

            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/transport'), 'refresh');
        }
        $page_data['transports'] = $this->db->get('transport')->result_array();
        $page_data['vehicles'] = $this->db->get('vehicles')->result_array();
        $page_data['page_name']  = 'transport';
        $page_data['page_title'] = get_phrase('manage_transport');
        $this->load->view('backend/index', $page_data);

    }
//=====================================ROUTE POINT===========================================//
    function route_point($param1="",$param2="")
    {
        if($this->session->userdata('admin_login') != 1)
            redirect('login', 'refresh');
        if($param1== "add_route_point")
        {
            for($i=0;$i<count($this->input->post('from_point_name'));$i++):
                $data['transport_id']        = $this->input->post('transport_id');
                $data['from_point_name'] = $this->input->post('from_point_name')[$i];
                $data['route_point_fare']        = $this->input->post('route_point_fare')[$i];
                $data['timing'] = $this->input->post('timing')[$i];
                $this->db->insert('route_point', $data);
            endfor;
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully1'));
            redirect(site_url('admin/route_point'), 'refresh');
        }
        if($param1=="do_update")
        {
            if($this->input->post('from_point_name') != NULL && $this->input->post('route_point_fare') != NULL && $this->input->post('timing') != NULL){
            $data['from_point_name'] = $this->input->post('from_point_name');
            $data['route_point_fare']        = $this->input->post('route_point_fare');
            $data['timing'] = $this->input->post('timing');
            $this->db->update('route_point', $data,array("route_point_id"=>$param2));
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully2'));
            redirect(site_url('admin/route_point'), 'refresh');
            }
        }
        if($param1=="delete")
        {
            $this->db->where('route_point_id', $param2);
            $this->db->delete('route_point');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/route_point'), 'refresh');
        }
        $page_data["route_point"]   = $this->db->get_where("route_point")->result_array();
        $page_data['transport']    = $this->db->get('transport')->result_array();
        $page_data['page_name']  = 'route_point';
        $page_data['page_title'] = get_phrase('manage_route_point');
        $this->load->view('backend/index', $page_data);    
    }
    
    //=====================================ROUTE POINT===========================================//
    function transport_route_student($param1="",$param2="")
    {
        if($this->session->userdata('admin_login') != 1)
            redirect('login', 'refresh');
            
        $page_data['page_name']  = 'transport_route_student';
        $page_data['page_title'] = get_phrase('manage_transport_route_student');
        $this->load->view('backend/index', $page_data);    
    }
    /**********MANAGE DORMITORY / HOSTELS / ROOMS ********************/
    function dormitory($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['name']           = $this->input->post('name');
            $data['number_of_room'] = $this->input->post('number_of_room');
            if ($this->input->post('description') != null) {
                $data['description']    = $this->input->post('description');
            }

            $this->db->insert('dormitory', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/dormitory'), 'refresh');
        }
        if ($param1 == 'do_update') {
            $data['name']           = $this->input->post('name');
            $data['number_of_room'] = $this->input->post('number_of_room');
            if ($this->input->post('description') != null) {
                $data['description']    = $this->input->post('description');
            }
            else{
                $data['description'] = null;
            }
            $this->db->where('dormitory_id', $param2);
            $this->db->update('dormitory', $data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/dormitory'), 'refresh');
        } else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('dormitory', array(
                'dormitory_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('dormitory_id', $param2);
            $this->db->delete('dormitory');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/dormitory'), 'refresh');
        }
        $page_data['dormitories'] = $this->db->get('dormitory')->result_array();
        $page_data['page_name']   = 'dormitory';
        $page_data['page_title']  = get_phrase('manage_dormitory');
        $this->load->view('backend/index', $page_data);

    }

    /***MANAGE EVENT / NOTICEBOARD, WILL BE SEEN BY ALL ACCOUNTS DASHBOARD**/
    function noticeboard($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($param1 == 'create') {
            $data['notice_title']     = $this->input->post('notice_title');
            $data['notice']           = $this->input->post('notice');
            $data['show_on_website']  = $this->input->post('show_on_website');
            $data['create_timestamp'] = strtotime($this->input->post('create_timestamp'));
            if ($_FILES['image']['name'] != '') {
              $data['image']  = $_FILES['image']['name'];
              move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/frontend/noticeboard/'. $_FILES['image']['name']);
            }
            $this->db->insert('noticeboard', $data);

            $check_sms_send = $this->input->post('check_sms');

            if ($check_sms_send == 1) {
                // sms sending configurations

                $parents  = $this->db->get('parent')->result_array();
                $students = $this->db->get('student')->result_array();
                $teachers = $this->db->get('teacher')->result_array();
                $date     = $this->input->post('create_timestamp');
                $message  = $data['notice_title'] . ' ';
                $message .= get_phrase('on') . ' ' . $date;
                foreach($parents as $row) {
                    $reciever_phone = $row['phone'];
                    $this->sms_model->send_sms($message , $reciever_phone);
                }
                foreach($students as $row) {
                    $reciever_phone = $row['phone'];
                    $this->sms_model->send_sms($message , $reciever_phone);
                }
                foreach($teachers as $row) {
                    $reciever_phone = $row['phone'];
                    $this->sms_model->send_sms($message , $reciever_phone);
                }
            }

            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/noticeboard'), 'refresh');
        }
        if ($param1 == 'do_update') {
            $image = $this->db->get_where('noticeboard', array('notice_id' => $param2))->row()->image;
            $data['notice_title']     = $this->input->post('notice_title');
            $data['notice']           = $this->input->post('notice');
            $data['show_on_website']  = $this->input->post('show_on_website');
            $data['create_timestamp'] = strtotime($this->input->post('create_timestamp'));
            if ($_FILES['image']['name'] != '') {
              $data['image']  = $_FILES['image']['name'];
              move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/frontend/noticeboard/'. $_FILES['image']['name']);
            } else {
              $data['image']  = $image;
            }

            $this->db->where('notice_id', $param2);
            $this->db->update('noticeboard', $data);

            $check_sms_send = $this->input->post('check_sms');

            if ($check_sms_send == 1) {
                // sms sending configurations

                $parents  = $this->db->get('parent')->result_array();
                $students = $this->db->get('student')->result_array();
                $teachers = $this->db->get('teacher')->result_array();
                $date     = $this->input->post('create_timestamp');
                $message  = $data['notice_title'] . ' ';
                $message .= get_phrase('on') . ' ' . $date;
                foreach($parents as $row) {
                    $reciever_phone = $row['phone'];
                    $this->sms_model->send_sms($message , $reciever_phone);
                }
                foreach($students as $row) {
                    $reciever_phone = $row['phone'];
                    $this->sms_model->send_sms($message , $reciever_phone);
                }
                foreach($teachers as $row) {
                    $reciever_phone = $row['phone'];
                    $this->sms_model->send_sms($message , $reciever_phone);
                }
            }

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/noticeboard'), 'refresh');
        } else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('noticeboard', array(
                'notice_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $this->db->where('notice_id', $param2);
            $this->db->delete('noticeboard');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/noticeboard'), 'refresh');
        }
        if ($param1 == 'mark_as_archive') {
            $this->db->where('notice_id' , $param2);
            $this->db->update('noticeboard' , array('status' => 0));
            redirect(site_url('admin/noticeboard'), 'refresh');
        }

        if ($param1 == 'remove_from_archived') {
            $this->db->where('notice_id' , $param2);
            $this->db->update('noticeboard' , array('status' => 1));
            redirect(site_url('admin/noticeboard'), 'refresh');
        }
        $page_data['page_name']  = 'noticeboard';
        $page_data['page_title'] = get_phrase('manage_noticeboard');
        $this->load->view('backend/index', $page_data);
    }

    function noticeboard_edit($notice_id) {
      if ($this->session->userdata('admin_login') != 1)
          redirect(site_url('login'), 'refresh');

      $page_data['page_name']  = 'noticeboard_edit';
      $page_data['notice_id'] = $notice_id;
      $page_data['page_title'] = get_phrase('edit_notice');
      $this->load->view('backend/index', $page_data);
    }

    function reload_noticeboard() {
        $this->load->view('backend/admin/noticeboard');
    }
    /* private messaging */

    function message($param1 = 'message_home', $param2 = '', $param3 = '') {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $max_size = 2097152;
        if ($param1 == 'send_new') {
            if (!file_exists('uploads/private_messaging_attached_file/')) {
              $oldmask = umask(0);  // helpful when used in linux server
              mkdir ('uploads/private_messaging_attached_file/', 0777);
            }
            if ($_FILES['attached_file_on_messaging']['name'] != "") {
              if($_FILES['attached_file_on_messaging']['size'] > $max_size){
                $this->session->set_flashdata('error_message' , get_phrase('file_size_can_not_be_larger_that_2_Megabyte'));
                redirect(site_url('admin/message/message_new'), 'refresh');
              }
              else{
                $file_path = 'uploads/private_messaging_attached_file/'.$_FILES['attached_file_on_messaging']['name'];
                move_uploaded_file($_FILES['attached_file_on_messaging']['tmp_name'], $file_path);
              }
            }

            $message_thread_code = $this->crud_model->send_new_private_message();
            $this->session->set_flashdata('flash_message', get_phrase('message_sent!'));
            redirect(site_url('admin/message/message_read/' . $message_thread_code), 'refresh');
        }

        if ($param1 == 'send_reply') {

            if (!file_exists('uploads/private_messaging_attached_file/')) {
              $oldmask = umask(0);  // helpful when used in linux server
              mkdir ('uploads/private_messaging_attached_file/', 0777);
            }
            if ($_FILES['attached_file_on_messaging']['name'] != "") {
              if($_FILES['attached_file_on_messaging']['size'] > $max_size){
                $this->session->set_flashdata('error_message' , get_phrase('file_size_can_not_be_larger_that_2_Megabyte'));
                redirect(site_url('admin/message/message_read/' . $param2), 'refresh');
              }
              else{
                $file_path = 'uploads/private_messaging_attached_file/'.$_FILES['attached_file_on_messaging']['name'];
                move_uploaded_file($_FILES['attached_file_on_messaging']['tmp_name'], $file_path);
              }
            }

            $this->crud_model->send_reply_message($param2);  //$param2 = message_thread_code
            $this->session->set_flashdata('flash_message', get_phrase('message_sent!'));
            redirect(site_url('admin/message/message_read/' . $param2), 'refresh');
        }

        if ($param1 == 'message_read') {
            $page_data['current_message_thread_code'] = $param2;  // $param2 = message_thread_code
            $this->crud_model->mark_thread_messages_read($param2);
        }

        $page_data['message_inner_page_name']   = $param1;
        $page_data['page_name']                 = 'message';
        $page_data['page_title']                = get_phrase('private_messaging');
        $this->load->view('backend/index', $page_data);
    }

    function group_message($param1 = "group_message_home", $param2 = ""){
      if ($this->session->userdata('admin_login') != 1)
          redirect(site_url('login'), 'refresh');
      $max_size = 2097152;
      if ($param1 == "create_group") {
        $this->crud_model->create_group();
      }
      elseif ($param1 == "edit_group") {
        $this->crud_model->update_group($param2);
      }
      elseif ($param1 == 'group_message_read') {
        $page_data['current_message_thread_code'] = $param2;
      }
      else if($param1 == 'send_reply'){
        if (!file_exists('uploads/group_messaging_attached_file/')) {
          $oldmask = umask(0);  // helpful when used in linux server
          mkdir ('uploads/group_messaging_attached_file/', 0777);
        }
        if ($_FILES['attached_file_on_messaging']['name'] != "") {
          if($_FILES['attached_file_on_messaging']['size'] > $max_size){
            $this->session->set_flashdata('error_message' , get_phrase('file_size_can_not_be_larger_that_2_Megabyte'));
            redirect(site_url('admin/group_message/group_message_read/' . $param2), 'refresh');
          }
          else{
            $file_path = 'uploads/group_messaging_attached_file/'.$_FILES['attached_file_on_messaging']['name'];
            move_uploaded_file($_FILES['attached_file_on_messaging']['tmp_name'], $file_path);
          }
        }

        $this->crud_model->send_reply_group_message($param2);  //$param2 = message_thread_code
        $this->session->set_flashdata('flash_message', get_phrase('message_sent!'));
        redirect(site_url('admin/group_message/group_message_read/' . $param2), 'refresh');
      }
      $page_data['message_inner_page_name']   = $param1;
      $page_data['page_name']                 = 'group_message';
      $page_data['page_title']                = get_phrase('group_messaging');
      $this->load->view('backend/index', $page_data);
    }
    /********************** group sms message***********************/
        function sms_message($param1 = 'sms_message_home', $param2 = '', $param3 = '') {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $max_size = 2097152;
        if ($param1 == 'send_new') {
            if (!file_exists('uploads/private_messaging_attached_file/')) {
              $oldmask = umask(0);  // helpful when used in linux server
              mkdir ('uploads/private_messaging_attached_file/', 0777);
            }
            if ($_FILES['attached_file_on_messaging']['name'] != "") {
              if($_FILES['attached_file_on_messaging']['size'] > $max_size){
                $this->session->set_flashdata('error_message' , get_phrase('file_size_can_not_be_larger_that_2_Megabyte'));
                redirect(site_url('admin/sms_message/sms_message_new'), 'refresh');
              }
              else{
                $file_path = 'uploads/private_messaging_attached_file/'.$_FILES['attached_file_on_messaging']['name'];
                move_uploaded_file($_FILES['attached_file_on_messaging']['tmp_name'], $file_path);
              }
            }

            $message_thread_code = $this->crud_model->send_new_private_sms_message();
            $this->session->set_flashdata('flash_message', get_phrase('message_sent!'));
            redirect(site_url('admin/sms_message/'), 'refresh');
        }

        if ($param1 == 'send_reply') {
            if (!file_exists('uploads/private_messaging_attached_file/')) {
              $oldmask = umask(0);  // helpful when used in linux server
              mkdir ('uploads/private_messaging_attached_file/', 0777);
            }
            if ($_FILES['attached_file_on_messaging']['name'] != "") {
              if($_FILES['attached_file_on_messaging']['size'] > $max_size){
                $this->session->set_flashdata('error_message' , get_phrase('file_size_can_not_be_larger_that_2_Megabyte'));
                redirect(site_url('admin/sms_message/sms_message_read/' . $param2), 'refresh');
              }
              else{
                $file_path = 'uploads/private_messaging_attached_file/'.$_FILES['attached_file_on_messaging']['name'];
                move_uploaded_file($_FILES['attached_file_on_messaging']['tmp_name'], $file_path);
              }
            }
            
            $this->crud_model->send_reply_sms_message($param2);  //$param2 = message_thread_code
            $this->session->set_flashdata('flash_message', get_phrase('message_sent!'));
            redirect(site_url('admin/sms_message/sms_message_read/' . $param2), 'refresh');
        }

        if ($param1 == 'message_read') {
            $page_data['current_message_thread_code'] = $param2;  // $param2 = message_thread_code
            $this->crud_model->mark_thread_messages_read($param2);
        }

        $page_data['message_inner_page_name']   = $param1;
        $page_data['page_name']                 = 'sms_message';
        $page_data['page_title']                = get_phrase('private_messaging');
        $this->load->view('backend/index', $page_data);
    }
    
    function group_sms_message($param1 = "group_sms_message_home", $param2 = ""){
      if ($this->session->userdata('admin_login') != 1)
          redirect(site_url('login'), 'refresh');
      $max_size = 2097152;
      if ($param1 == "create_group") {
        $this->crud_model->create_group_sms();
      }
      elseif ($param1 == "edit_group") {
        $this->crud_model->update_group_sms($param2);
      }
      elseif ($param1 == 'group_sms_message_read') {
        $page_data['current_message_thread_code'] = $param2;
      }
      else if($param1 == 'send_reply'){
        if (!file_exists('uploads/group_messaging_attached_file/')) {
          $oldmask = umask(0);  // helpful when used in linux server
          mkdir ('uploads/group_messaging_attached_file/', 0777);
        }
        if ($_FILES['attached_file_on_messaging']['name'] != "") {
          if($_FILES['attached_file_on_messaging']['size'] > $max_size){
            $this->session->set_flashdata('error_message' , get_phrase('file_size_can_not_be_larger_that_2_Megabyte'));
            redirect(site_url('admin/group_sms_message/group_sms_message_read/' . $param2), 'refresh');
          }
          else{
            $file_path = 'uploads/group_messaging_attached_file/'.$_FILES['attached_file_on_messaging']['name'];
            move_uploaded_file($_FILES['attached_file_on_messaging']['tmp_name'], $file_path);
          }
        }

        $this->crud_model->send_sms_reply_group_message($param2);  //$param2 = message_thread_code
        $this->session->set_flashdata('flash_message', get_phrase('message_sent!'));
        redirect(site_url('admin/group_sms_message/group_sms_message_read/' . $param2), 'refresh');
      }
      $page_data['message_inner_page_name']   = $param1;
      $page_data['page_name']                 = 'group_sms_message';
      $page_data['page_title']                = get_phrase('group_messaging');
      $this->load->view('backend/index', $page_data);
    }


    /*****SITE/SYSTEM SETTINGS*********/
    function system_settings($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($param1 == 'do_update') {

            if (isset($_POST['disable_frontend'])) {
                $data['description'] = 1;
                $this->db->where('type', 'disable_frontend');
                $this->db->update('settings' , $data);
            }
            else{
              $data['description'] = 0;
              $this->db->where('type', 'disable_frontend');
              $this->db->update('settings' , $data);
            }

            $this->update_default_controller();

            $data['description'] = $this->input->post('system_name');
            $this->db->where('type' , 'system_name');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('system_title');
            $this->db->where('type' , 'system_title');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('address');
            $this->db->where('type' , 'address');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('phone');
            $this->db->where('type' , 'phone');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('paypal_email');
            $this->db->where('type' , 'paypal_email');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('currency');
            $this->db->where('type' , 'currency');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('system_email');
            $this->db->where('type' , 'system_email');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('system_name');
            $this->db->where('type' , 'system_name');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('language');
            $this->db->where('type' , 'language');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('text_align');
            $this->db->where('type' , 'text_align');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('running_year');
            $this->db->where('type' , 'running_year');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('purchase_code');
            $this->db->where('type' , 'purchase_code');
            $this->db->update('settings' , $data);

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/system_settings'), 'refresh');
        }
        if ($param1 == 'upload_logo') {
            move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/logo.png');
            $this->session->set_flashdata('flash_message', get_phrase('settings_updated'));
            redirect(site_url('admin/system_settings'), 'refresh');
        }
        if ($param1 == 'change_skin') {
            $data['description'] = $param2;
            $this->db->where('type' , 'skin_colour');
            $this->db->update('settings' , $data);
            $this->session->set_flashdata('flash_message' , get_phrase('theme_selected'));
            redirect(site_url('admin/system_settings'), 'refresh');
        }
        $page_data['page_name']  = 'system_settings';
        $page_data['page_title'] = get_phrase('system_settings');
        $page_data['settings']   = $this->db->get('settings')->result_array();
        $this->load->view('backend/index', $page_data);
    }

    // update default controller
    function update_default_controller() {
      $status = $this->db->get_where('settings' , array('type' =>'disable_frontend'))->row()->description;
      if ($status == 1) {
        $default_controller          = 'login';
        $previous_default_controller = 'home';
      }else{
        $default_controller          = 'home';
        $previous_default_controller = 'login';
      }
      // write routes.php
      $data = file_get_contents('./application/config/routes.php');
      $data = str_replace($previous_default_controller,	$default_controller,	$data);
      file_put_contents('./application/config/routes.php', $data);
    }

    //Payment settings
    function payment_settings($param1 = ""){
      if ($this->session->userdata('admin_login') != 1)
          redirect(site_url('login'), 'refresh');

      if ($param1 == 'update_stripe_keys') {
            $this->crud_model->update_stripe_keys();
            $this->session->set_flashdata('flash_message', get_phrase('payment_settings_updated'));
            redirect(site_url('admin/payment_settings'), 'refresh');
      }

      if ($param1 == 'update_paypal_keys') {
          $this->crud_model->update_paypal_keys();
          $this->session->set_flashdata('flash_message', get_phrase('payment_settings_updated'));
          redirect(site_url('admin/payment_settings'), 'refresh');
      }
      if ($param1 == 'update_payumoney_keys') {
        $this->crud_model->update_payumoney_keys();
        $this->session->set_flashdata('flash_message', get_phrase('payment_settings_updated'));
        redirect(site_url('admin/payment_settings'), 'refresh');
      }
      $page_data['page_name']  = 'payment_settings';
      $page_data['page_title'] = get_phrase('payment_settings');
      $page_data['settings']   = $this->db->get('settings')->result_array();
      $this->load->view('backend/index', $page_data);
    }
    // FRONTEND

    function frontend_pages($param1 = '', $param2 = '', $param3 = '') {
      if ($this->session->userdata('admin_login') != 1) {
        redirect(site_url('login'), 'refresh');
      }
      if ($param1 == 'events') {
        $page_data['page_content']  = 'frontend_events';
      }
      if ($param1 == 'gallery') {
        $page_data['page_content']  = 'frontend_gallery';
      }
      if ($param1 == 'privacy_policy') {
        $page_data['page_content']  = 'frontend_privacy_policy';
      }
      if ($param1 == 'about_us') {
        $page_data['page_content']  = 'frontend_about_us';
      }
      if ($param1 == 'terms_conditions') {
        $page_data['page_content']  = 'frontend_terms_conditions';
      }
      if ($param1 == 'homepage_slider') {
        $page_data['page_content']  = 'frontend_slider';
      }
      if ($param1 == '' || $param1 == 'general') {
        $page_data['page_content']  = 'frontend_general_settings';
      }
      if ($param1 == 'gallery_image') {
        $page_data['page_content']  = 'frontend_gallery_image';
        $page_data['gallery_id']  = $param2;
      }
      $page_data['page_name'] = 'frontend_pages';
      $page_data['page_title']  = get_phrase('pages');
      $this->load->view('backend/index', $page_data);
    }

    function frontend_events($param1 = '', $param2 = '') {
      if ($param1 == 'add_event') {
        $this->frontend_model->add_event();
        $this->session->set_flashdata('flash_message' , get_phrase('event_added_successfully'));
        redirect(site_url('admin/frontend_pages/events'), 'refresh');
      }
      if ($param1 == 'edit_event') {
        $this->frontend_model->edit_event($param2);
        $this->session->set_flashdata('flash_message' , get_phrase('event_updated_successfully'));
        redirect(site_url('admin/frontend_pages/events'), 'refresh');
      }
      if ($param1 == 'delete') {
        $this->frontend_model->delete_event($param2);
        $this->session->set_flashdata('flash_message' , get_phrase('event_deleted'));
        redirect(site_url('admin/frontend_pages/events'), 'refresh');
      }
    }

    function frontend_gallery($param1 = '', $param2 = '', $param3 = '') {
      if ($param1 == 'add_gallery') {
        $this->frontend_model->add_gallery();
        $this->session->set_flashdata('flash_message' , get_phrase('gallery_added_successfully'));
        redirect(site_url('admin/frontend_pages/gallery'), 'refresh');
      }
      if ($param1 == 'edit_gallery') {
        $this->frontend_model->edit_gallery($param2);
        $this->session->set_flashdata('flash_message' , get_phrase('gallery_updated_successfully'));
        redirect(site_url('admin/frontend_pages/gallery'), 'refresh');
      }
      if ($param1 == 'upload_images') {
        $this->frontend_model->add_gallery_images($param2);
        $this->session->set_flashdata('flash_message' , get_phrase('images_uploaded'));
        redirect(site_url('admin/frontend_pages/gallery_image/'.$param2), 'refresh');
      }
      if ($param1 == 'delete_image') {
        $this->frontend_model->delete_gallery_image($param2);
        $this->session->set_flashdata('flash_message' , get_phrase('images_deleted'));
        redirect(site_url('admin/frontend_pages/gallery_image/'.$param3), 'refresh');
      }

    }

    function frontend_news($param1 = '', $param2 = '') {
      if ($param1 == 'add_news') {
        $this->frontend_model->add_news();
        $this->session->set_flashdata('flash_message' , get_phrase('news_added_successfully'));
        redirect(site_url('admin/frontend_pages/news'), 'refresh');
      }
      if ($param1 == 'edit_news') {

      }
      if ($param1 == 'delete') {
        $this->frontend_model->delete_news($param2);
        $this->session->set_flashdata('flash_message' , get_phrase('news_was_deleted'));
        redirect(site_url('admin/frontend_pages/news'), 'refresh');
      }
    }

    function frontend_settings($task) {
      if ($task == 'update_terms_conditions') {
        $this->frontend_model->update_terms_conditions();
        $this->session->set_flashdata('flash_message' , get_phrase('terms_updated'));
        redirect(site_url('admin/frontend_pages/terms_conditions'), 'refresh');
      }
      if ($task == 'update_about_us') {
        $this->frontend_model->update_about_us();
        $this->session->set_flashdata('flash_message' , get_phrase('about_us_updated'));
        redirect(site_url('admin/frontend_pages/about_us'), 'refresh');
      }
      if ($task == 'update_privacy_policy') {
        $this->frontend_model->update_privacy_policy();
        $this->session->set_flashdata('flash_message' , get_phrase('privacy_policy_updated'));
        redirect(site_url('admin/frontend_pages/privacy_policy'), 'refresh');
      }
      if ($task == 'update_general_settings') {
        $this->frontend_model->update_frontend_general_settings();
        $this->session->set_flashdata('flash_message' , get_phrase('general_settings_updated'));
        redirect(site_url('admin/frontend_pages/general'), 'refresh');
      }
      if ($task == 'update_slider_images') {
        $this->frontend_model->update_slider_images();
        $this->session->set_flashdata('flash_message' , get_phrase('slider_images_updated'));
        redirect(site_url('admin/frontend_pages/homepage_slider'), 'refresh');
      }
    }

    function frontend_themes() {
      if ($this->session->userdata('admin_login') != 1) {
        redirect(site_url('login'), 'refresh');
      }
      $page_data['page_name'] = 'frontend_themes';
      $page_data['page_title']  = get_phrase('themes');
      $this->load->view('backend/index', $page_data);
    }

    // FRONTEND


    function get_session_changer()
    {
        $this->load->view('backend/admin/change_session');
    }

    function change_session()
    {
        $data['description'] = $this->input->post('running_year');
        $this->db->where('type' , 'running_year');
        $this->db->update('settings' , $data);
        $this->session->set_flashdata('flash_message' , get_phrase('session_changed'));
        redirect(site_url('admin/dashboard'), 'refresh');
    }

	/***** UPDATE PRODUCT *****/

	function update( $task = '', $purchase_code = '' ) {

        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        // Create update directory.
        $dir    = 'update';
        if ( !is_dir($dir) )
            mkdir($dir, 0777, true);

        $zipped_file_name   = $_FILES["file_name"]["name"];
        $path               = 'update/' . $zipped_file_name;

        move_uploaded_file($_FILES["file_name"]["tmp_name"], $path);

        // Unzip uploaded update file and remove zip file.
        $zip = new ZipArchive;
        $res = $zip->open($path);
        if ($res === TRUE) {
            $zip->extractTo('update');
            $zip->close();
            unlink($path);
        }

        $unzipped_file_name = substr($zipped_file_name, 0, -4);
        $str                = file_get_contents('./update/' . $unzipped_file_name . '/update_config.json');
        $json               = json_decode($str, true);

		// Run php modifications
		require './update/' . $unzipped_file_name . '/update_script.php';

        // Create new directories.
        if(!empty($json['directory'])) {
            foreach($json['directory'] as $directory) {
                if ( !is_dir( $directory['name']) )
                    mkdir( $directory['name'], 0777, true );
            }
        }

        // Create/Replace new files.
        if(!empty($json['files'])) {
            foreach($json['files'] as $file)
                copy($file['root_directory'], $file['update_directory']);
        }

        $this->session->set_flashdata('flash_message' , get_phrase('product_updated_successfully'));
        redirect(site_url('admin/system_settings'), 'refresh');
    }

    /*****SMS SETTINGS*********/
    function sms_settings($param1 = '' , $param2 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'clickatell') {

            $data['description'] = $this->input->post('clickatell_user');
            $this->db->where('type' , 'clickatell_user');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('clickatell_password');
            $this->db->where('type' , 'clickatell_password');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('clickatell_api_id');
            $this->db->where('type' , 'clickatell_api_id');
            $this->db->update('settings' , $data);

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/sms_settings'), 'refresh');
        }

        if ($param1 == 'twilio') {

            $data['description'] = $this->input->post('twilio_account_sid');
            $this->db->where('type' , 'twilio_account_sid');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('twilio_auth_token');
            $this->db->where('type' , 'twilio_auth_token');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('twilio_sender_phone_number');
            $this->db->where('type' , 'twilio_sender_phone_number');
            $this->db->update('settings' , $data);

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/sms_settings'), 'refresh');
        }
        if ($param1 == 'msg91') {

            $data['description'] = $this->input->post('authentication_key');
            $this->db->where('type' , 'msg91_authentication_key');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('sender_ID');
            $this->db->where('type' , 'msg91_sender_ID');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('msg91_route');
            $this->db->where('type' , 'msg91_route');
            $this->db->update('settings' , $data);

            $data['description'] = $this->input->post('msg91_country_code');
            $this->db->where('type' , 'msg91_country_code');
            $this->db->update('settings' , $data);

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/sms_settings'), 'refresh');
        }

        if ($param1 == 'active_service') {

            $data['description'] = $this->input->post('active_sms_service');
            $this->db->where('type' , 'active_sms_service');
            $this->db->update('settings' , $data);

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            redirect(site_url('admin/sms_settings'), 'refresh');
        }

        $page_data['page_name']  = 'sms_settings';
        $page_data['page_title'] = get_phrase('sms_settings');
        $page_data['settings']   = $this->db->get('settings')->result_array();
        $this->load->view('backend/index', $page_data);
    }

    /*****LANGUAGE SETTINGS*********/
    function manage_language($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
			redirect(site_url('login'), 'refresh');

		if ($param1 == 'edit_phrase') {
			$page_data['edit_profile'] 	= $param2;
		}
		if ($param1 == 'update_phrase') {
			$language	=	$param2;
			$total_phrase	=	$this->input->post('total_phrase');
			for($i = 1 ; $i < $total_phrase ; $i++)
			{
				//$data[$language]	=	$this->input->post('phrase').$i;
				$this->db->where('phrase_id' , $i);
				$this->db->update('language' , array($language => $this->input->post('phrase'.$i)));
			}
			redirect(site_url('admin/manage_language/edit_phrase/'.$language), 'refresh');
		}
		if ($param1 == 'do_update') {
			$language        = $this->input->post('language');
			$data[$language] = $this->input->post('phrase');
			$this->db->where('phrase_id', $param2);
			$this->db->update('language', $data);
			$this->session->set_flashdata('flash_message', get_phrase('settings_updated'));
			redirect(site_url('admin/manage_language'), 'refresh');
		}
		if ($param1 == 'add_phrase') {
			$data['phrase'] = $this->input->post('phrase');
			$this->db->insert('language', $data);
			$this->session->set_flashdata('flash_message', get_phrase('settings_updated'));
			redirect(site_url('admin/manage_language'), 'refresh');
		}
		if ($param1 == 'add_language') {
			$language = $this->input->post('language');
			$this->load->dbforge();
			$fields = array(
				$language => array(
					'type' => 'LONGTEXT'
				)
			);
			$this->dbforge->add_column('language', $fields);

			$this->session->set_flashdata('flash_message', get_phrase('settings_updated'));
			redirect(site_url('admin/manage_language'), 'refresh');
		}
		if ($param1 == 'delete_language') {
			$language = $param2;
			$this->load->dbforge();
			$this->dbforge->drop_column('language', $language);
			$this->session->set_flashdata('flash_message', get_phrase('settings_updated'));

			redirect(site_url('admin/manage_language'), 'refresh');
		}
		$page_data['page_name']        = 'manage_language';
		$page_data['page_title']       = get_phrase('manage_language');
		//$page_data['language_phrases'] = $this->db->get('language')->result_array();
		$this->load->view('backend/index', $page_data);
    }

    /******MANAGE OWN PROFILE AND CHANGE PASSWORD***/
    function manage_profile($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'update_profile_info') {
            $data['name']  = $this->input->post('name');
            $data['email'] = $this->input->post('email');

            $admin_id = $param2;

            $validation = email_validation_for_edit($data['email'], $admin_id, 'admin');
            if($validation == 1){
                $this->db->where('admin_id', $this->session->userdata('admin_id'));
                $this->db->update('admin', $data);
                move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/admin_image/' . $this->session->userdata('admin_id') . '.jpg');
                $this->session->set_flashdata('flash_message', get_phrase('account_updated'));
            }
            else{
                $this->session->set_flashdata('error_message', get_phrase('this_email_id_is_not_available'));
            }
            redirect(site_url('admin/manage_profile'), 'refresh');
        }
        if ($param1 == 'change_password') {
            $data['password']             = sha1($this->input->post('password'));
            $data['new_password']         = sha1($this->input->post('new_password'));
            $data['confirm_new_password'] = sha1($this->input->post('confirm_new_password'));

            $current_password = $this->db->get_where('admin', array(
                'admin_id' => $this->session->userdata('admin_id')
            ))->row()->password;
            if ($current_password == $data['password'] && $data['new_password'] == $data['confirm_new_password']) {
                $this->db->where('admin_id', $this->session->userdata('admin_id'));
                $this->db->update('admin', array(
                    'password' => $data['new_password']
                ));
                $this->session->set_flashdata('flash_message', get_phrase('password_updated'));
            } else {
                $this->session->set_flashdata('error_message', get_phrase('password_mismatch'));
            }
            redirect(site_url('admin/manage_profile'), 'refresh');
        }
        $page_data['page_name']  = 'manage_profile';
        $page_data['page_title'] = get_phrase('manage_profile');
        $page_data['edit_data']  = $this->db->get_where('admin', array(
            'admin_id' => $this->session->userdata('admin_id')
        ))->result_array();
        $this->load->view('backend/index', $page_data);
    }

    // VIEW QUESTION PAPERS
    function question_paper($param1 = "", $param2 = "")
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $data['page_name']  = 'question_paper';
        $data['page_title'] = get_phrase('question_paper');
        $this->load->view('backend/index', $data);
    }

    // MANAGE LIBRARIANS
    function librarian($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($param1 == 'create') {
            $data['name']       = $this->input->post('name');
            $data['email']      = $this->input->post('email');
            $data['password']   = sha1($this->input->post('password'));
            $validation = email_validation($data['email']);
            if ($validation == 1) {
                $this->db->insert('librarian', $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
                $this->email_model->account_opening_email('librarian', $data['email'], $this->input->post('password')); //SEND EMAIL ACCOUNT OPENING EMAIL
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }
            redirect(site_url('admin/librarian'), 'refresh');
        }

        if ($param1 == 'edit') {
            $data['name']   = $this->input->post('name');
            $data['email']  = $this->input->post('email');
            $validation = email_validation_for_edit($data['email'], $param2, 'librarian');
            if ($validation == 1) {
                $this->db->where('librarian_id' , $param2);
                $this->db->update('librarian' , $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }

            redirect(site_url('admin/librarian'), 'refresh');
        }

        if ($param1 == 'delete') {
            $this->db->where('librarian_id' , $param2);
            $this->db->delete('librarian');

            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/librarian'), 'refresh');
        }

        $page_data['page_title']    = get_phrase('all_librarians');
        $page_data['page_name']     = 'librarian';
        $this->load->view('backend/index', $page_data);
    }

    // MANAGE ACCOUNTANTS
    function accountant($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($param1 == 'create') {
            $data['name']       = $this->input->post('name');
            $data['email']      = $this->input->post('email');
            $data['password']   = sha1($this->input->post('password'));

            $validation = email_validation($data['email']);
            if ($validation == 1) {
                $this->db->insert('accountant', $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
                $this->email_model->account_opening_email('accountant', $data['email'], $this->input->post('password')); //SEND EMAIL ACCOUNT OPENING EMAIL
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }

            redirect(site_url('admin/accountant'), 'refresh');
        }

        if ($param1 == 'edit') {
            $data['name']   = $this->input->post('name');
            $data['email']  = $this->input->post('email');

            $validation = email_validation_for_edit($data['email'], $param2, 'accountant');
            if($validation == 1){
                $this->db->where('accountant_id' , $param2);
                $this->db->update('accountant' , $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));
            }

            redirect(site_url('admin/accountant'), 'refresh');
        }

        if ($param1 == 'delete') {
            $this->db->where('accountant_id' , $param2);
            $this->db->delete('accountant');

            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/accountant'), 'refresh');
        }

        $page_data['page_title']    = get_phrase('all_accountants');
        $page_data['page_name']     = 'accountant';
        $this->load->view('backend/index', $page_data);
    }


    // bulk student_add using CSV
    function generate_bulk_student_csv($class_id = '', $section_id = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $data['class_id']   = $class_id;
        $data['section_id'] = $section_id;
        $data['year']       = $this->db->get_where('settings', array('type'=>'running_year'))->row()->description;

        $file   = fopen("uploads/bulk_student.csv", "w");
        $line   = array('StudentName', 'Admission Number','birthday','religion','caste','category','nationality','annualIncome','physicallyChallenged','adharNo','sts','rationNo','siblings','Email', 'Password', 'Phone', 'Address', 'ParentID', 'Gender');
        fputcsv($file, $line, ',');
       echo $file_path = base_url() . 'uploads/bulk_student.csv';
    }
    // CSV IMPORT
    function bulk_student_add_using_csv($param1 = '') {

        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

       if ($param1 == 'import') {
          if ($this->input->post('class_id') != '' && $this->input->post('section_id') != '') {

              move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/bulk_student.csv');
              $csv = array_map('str_getcsv', file('uploads/bulk_student.csv'));
              $count = 1;
              $array_size = sizeof($csv);

             foreach ($csv as $row) {
                  if ($count == 1) {
                      $count++;
                      continue;
                  }
                  $password = $row[14];

                  $data['name']      = $row[0];

                  $data['student_code']  = $row[1]; 
                  $data['birthday']  = $row[2];
                  $data['religion']  = $row[3];
                  $data['caste']  = $row[4];
                  $data['category']  = $row[5]; 
                  $data['nationality']  = $row[6];
                  $data['annualIncome']  = $row[7];
                  $data['physicallyChallenged']  = $row[8];
                  $data['adharNo']  = $row[9];
                  $data['sts']  = $row[10];
                  $data['rationNo']  = $row[11];
                  $data['siblings']  = $row[12];
                  $data['email']     = $row[13];
                  $data['password']  = sha1($row[14]);
                  $data['phone']     = $row[15];
                  $data['address']   = $row[16];
                  $data['parent_id'] = $row[17];
                  $data['sex']       = strtolower($row[18]);
                 //student id (code) validation
                 $code_validation = code_validation_insert($data['student_code']);
                 if(!$code_validation){
                     $this->session->set_flashdata('error_message' , get_phrase('this_id_no_is_not_available'));
                     redirect(site_url('admin/student_add'), 'refresh');
                 }
                 //student id validation ends

                  $validation = email_validation($data['email']);
                  if ($validation == 1) {
                    $this->db->insert('student', $data);
                    $student_id = $this->db->insert_id();

                    $data2['student_id']  = $student_id;
                    $data2['class_id']    = $this->input->post('class_id');
                    $data2['section_id']  = $this->input->post('section_id');
//                    $data2['roll']        = $row[1];
                    $data2['enroll_code'] =   substr(md5(rand(0, 1000000)), 0, 7);
                    $data2['date_added']  =   strtotime(date("Y-m-d H:i:s"));
                    $data2['year']        =   $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
                    $this->db->insert('enroll' , $data2);
                  }
                  else{
                    if ($array_size == 2) {
                      $this->session->set_flashdata('error_message', get_phrase('this_email_id_"').$data['email'].get_phrase('"_is_not_available'));
                      redirect(site_url('admin/student_bulk_add'), 'refresh');
                    }
                    elseif($array_size > 2){
                      $this->session->set_flashdata('error_message', get_phrase('some_email_IDs_are_not_available'));
                    }
                  }

              }


              $this->session->set_flashdata('flash_message', get_phrase('student_imported'));
              redirect(site_url('admin/student_bulk_add'), 'refresh');
           }
           else{
             $this->session->set_flashdata('error_message', get_phrase('please_make_sure_class_and_section_is_selected'));
             redirect(site_url('admin/student_bulk_add'), 'refresh');
           }
        }
        $page_data['page_name']  = 'student_bulk_add';
        $page_data['page_title'] = get_phrase('add_bulk_student');
        $this->load->view('backend/index', $page_data);
    }
    function study_material($task = "", $document_id = "")
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($task == "create")
        {
            $this->crud_model->save_study_material_info();
            $this->session->set_flashdata('flash_message' , get_phrase('study_material_info_saved_successfuly'));
            redirect(site_url('admin/study_material'), 'refresh');
        }

        if ($task == "update")
        {
            $this->crud_model->update_study_material_info($document_id);
            $this->session->set_flashdata('flash_message' , get_phrase('study_material_info_updated_successfuly'));
            redirect(site_url('admin/study_material'), 'refresh');
        }

        if ($task == "delete")
        {
            $this->crud_model->delete_study_material_info($document_id);
            redirect(site_url('admin/study_material'), 'refresh');
        }

        $data['study_material_info']    = $this->crud_model->select_study_material_info();
        $data['page_name']              = 'study_material';
        $data['page_title']             = get_phrase('study_material');
        $this->load->view('backend/index', $data);
    }

    //new code
    function print_id($id){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $data['id'] = $id;
        $this->load->view('backend/admin/print_id', $data);
    }

    function create_barcode()
    {
        //$student_id
        return $this->Barcode_model->create_barcode($_GET);

    }

    // Details of searched student
    function student_details($param1 = ""){
      if ($this->session->userdata('admin_login') != 1)
          redirect(site_url('login'), 'refresh');

      $student_identifier = $this->input->post('student_identifier');
      $query_by_code = $this->db->get_where('student', array('student_code' => $student_identifier));

      if ($query_by_code->num_rows() == 0) {
        $this->db->like('name', $student_identifier);
        $query_by_name = $this->db->get('student');
        if ($query_by_name->num_rows() == 0) {
          $this->session->set_flashdata('error_message' , get_phrase('no_student_found'));
          redirect(site_url('admin/dashboard'), 'refresh');
        }
        else{
          $page_data['student_information'] = $query_by_name->result_array();
        }
      }
      else{
        $page_data['student_information'] = $query_by_code->result_array();
      }
      $page_data['page_name']  	= 'search_result';
  		$page_data['page_title'] 	= get_phrase('search_result');
  		$this->load->view('backend/index', $page_data);
    }


    // online exam
    function manage_online_exam($param1 = "", $param2 = ""){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $running_year = get_settings('running_year');

        if ($param1 == '') {
            $match = array('status !=' => 'expired', 'running_year' => $running_year);
            $page_data['status'] = 'active';
            $this->db->order_by("exam_date", "dsc");
            $page_data['online_exams'] = $this->db->where($match)->get('online_exam')->result_array();
        }

        if ($param1 == 'expired') {
            $match = array('status' => 'expired', 'running_year' => $running_year);
            $page_data['status'] = 'expired';
            $this->db->order_by("exam_date", "dsc");
            $page_data['online_exams'] = $this->db->where($match)->get('online_exam')->result_array();
        }

        if ($param1 == 'create') {
            if ($this->input->post('class_id') > 0 && $this->input->post('section_id') > 0 && $this->input->post('subject_id') > 0) {
                $this->crud_model->create_online_exam();
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
                redirect(site_url('admin/manage_online_exam'), 'refresh');
            }
            else {
                $this->session->set_flashdata('error_message' , get_phrase('make_sure_to_select_valid_class_').','.get_phrase('_section_and_subject'));
                redirect(site_url('admin/manage_online_exam'), 'refresh');
            }
        }
        if ($param1 == 'edit') {
            if ($this->input->post('class_id') > 0 && $this->input->post('section_id') > 0 && $this->input->post('subject_id') > 0) {
                $this->crud_model->update_online_exam();
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
                redirect(site_url('admin/manage_online_exam'), 'refresh');
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('make_sure_to_select_valid_class_').','.get_phrase('_section_and_subject'));
                redirect(site_url('admin/manage_online_exam'), 'refresh');
            }
        }
        if ($param1 == 'delete') {
            $this->db->where('online_exam_id', $param2);
            $this->db->delete('online_exam');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/manage_online_exam'), 'refresh');
        }
        $page_data['page_name'] = 'manage_online_exam';
        $page_data['page_title'] = get_phrase('manage_online_exam');
        $this->load->view('backend/index', $page_data);
    }

    function online_exam_questions_print_view($online_exam_id, $answers) {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $page_data['online_exam_id'] = $online_exam_id;
        $page_data['answers'] = $answers;
        $page_data['page_title'] = get_phrase('questions_print');
        $this->load->view('backend/admin/online_exam_questions_print_view', $page_data);
    }

    function create_online_exam(){
        $page_data['page_name'] = 'add_online_exam';
        $page_data['page_title'] = get_phrase('add_an_online_exam');
        $this->load->view('backend/index', $page_data);
    }

    function update_online_exam($param1 = ""){
        $page_data['online_exam_id'] = $param1;
        $page_data['page_name'] = 'edit_online_exam';
        $page_data['page_title'] = get_phrase('update_online_exam');
        $this->load->view('backend/index', $page_data);
    }

    function manage_online_exam_status($online_exam_id = "", $status = ""){
        $this->crud_model->manage_online_exam_status($online_exam_id, $status);
        redirect(site_url('admin/manage_online_exam'), 'refresh');
    }

    function load_question_type($type, $online_exam_id) {
        $page_data['question_type'] = $type;
        $page_data['online_exam_id'] = $online_exam_id;
        $this->load->view('backend/admin/online_exam_add_'.$type, $page_data);
    }

    function manage_online_exam_question($online_exam_id = "", $task = "", $type = ""){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        if ($task == 'add') {
            if ($type == 'multiple_choice') {
                $this->crud_model->add_multiple_choice_question_to_online_exam($online_exam_id);
            }
            elseif ($type == 'true_false') {
                $this->crud_model->add_true_false_question_to_online_exam($online_exam_id);
            }
            elseif ($type == 'fill_in_the_blanks') {
                $this->crud_model->add_fill_in_the_blanks_question_to_online_exam($online_exam_id);
            }
            redirect(site_url('admin/manage_online_exam_question/'.$online_exam_id), 'refresh');
        }

        $page_data['online_exam_id'] = $online_exam_id;
        $page_data['page_name'] = 'manage_online_exam_question';
        $page_data['page_title'] = $this->db->get_where('online_exam', array('online_exam_id'=>$online_exam_id))->row()->title;
        $this->load->view('backend/index', $page_data);
    }

    function update_online_exam_question($question_id = "", $task = "", $online_exam_id = "") {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $online_exam_id = $this->db->get_where('question_bank', array('question_bank_id' => $question_id))->row()->online_exam_id;
        $type = $this->db->get_where('question_bank', array('question_bank_id' => $question_id))->row()->type;
        if ($task == "update") {
            if ($type == 'multiple_choice') {
                $this->crud_model->update_multiple_choice_question($question_id);
            }
            elseif($type == 'true_false'){
                $this->crud_model->update_true_false_question($question_id);
            }
            elseif($type == 'fill_in_the_blanks'){
                $this->crud_model->update_fill_in_the_blanks_question($question_id);
            }
            redirect(site_url('admin/manage_online_exam_question/'.$online_exam_id), 'refresh');
        }
        $page_data['question_id'] = $question_id;
        $page_data['page_name'] = 'update_online_exam_question';
        $page_data['page_title'] = get_phrase('update_question');
        $this->load->view('backend/index', $page_data);
    }

    function delete_question_from_online_exam($question_id){
        $online_exam_id = $this->db->get_where('question_bank', array('question_bank_id' => $question_id))->row()->online_exam_id;
        $this->crud_model->delete_question_from_online_exam($question_id);
        $this->session->set_flashdata('flash_message' , get_phrase('question_deleted'));
        redirect(site_url('admin/manage_online_exam_question/'.$online_exam_id), 'refresh');
    }

    function manage_multiple_choices_options() {
        $page_data['number_of_options'] = $this->input->post('number_of_options');
        $this->load->view('backend/admin/manage_multiple_choices_options', $page_data);
    }

    function get_sections_for_ssph($class_id) {
        $sections = $this->db->get_where('section', array('class_id' => $class_id))->result_array();
        $options = '';
        foreach ($sections as $row) {
            $options .= '<option value="'.$row['section_id'].'">'.$row['name'].'</option>';
        }
        echo '<select class="" name="section_id" id="section_id">'.$options.'</select>';
    }

    function get_students_for_ssph($class_id, $section_id) {
        $enrolls = $this->db->get_where('enroll', array('class_id' => $class_id, 'section_id' => $section_id))->result_array();
        $options = '';
        foreach ($enrolls as $row) {
            $name = $this->db->get_where('student', array('student_id' => $row['student_id']))->row()->name;
            $options .= '<option value="'.$row['student_id'].'">'.$name.'</option>';
        }
        echo '<select class="" name="student_id" id="student_id">'.$options.'</select>';
    }

    function get_payment_history_for_ssph($student_id) {
        $page_data['student_id'] = $student_id;
        $this->load->view('backend/admin/student_specific_payment_history_table', $page_data);
    }

    function view_online_exam_result($online_exam_id){
        $page_data['page_name'] = 'view_online_exam_results';
        $page_data['page_title'] = get_phrase('result');
        $page_data['online_exam_id'] = $online_exam_id;
        $this->load->view('backend/index',$page_data);
    }
 
 //======================================================Certificates ======================================================
    function certificate($param1="",$param2=""){
        if ($this->session->userdata('admin_login') != 1)
          redirect(site_url('login'), 'refresh');
        
        if($param1=="create"){
           $data= array(
               'name' =>$this->input->post("name"),
               'top_title' =>$this->input->post("top_title"),
               'main_text' =>$this->input->post("main_text"),
               'footer_left' =>$this->input->post("footer_left"),
               'footer_middle' =>$this->input->post("footer_middle"),
               'footer_right' =>$this->input->post("footer_right")
             ); 
           $this->db->insert("certificates",$data);
           $id=$this->db->insert_id();

            if($_FILES['background']['tmp_name'] !=NULL){
                move_uploaded_file($_FILES['background']['tmp_name'], 'uploads/certificate/certificate_' .$id . '.jpg');
                $this->db->set("background","certificate_" .$id . ".jpg")->where("id",$id)->update("certificates");
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            }
            redirect(site_url('admin/certificate'), 'refresh');
        }
        if($param1 =="view"){
           $page_data['certificates']=$this->db->get("certificates")->result_array(); 
           $page_data['certificate']=$this->db->where("id",$param2)->get("certificates")->result_array();
           $page_data['page_name']  = 'certificate';
           $page_data['page_title'] = get_phrase('manage_certificates');
            $this->load->view('backend/index', $page_data);
        } 
        if($param1=="delete"){
            $data=$this->db->where("id",$param2)->get("certificates")->result_array();
            if($data!=NULL){
            if(file_exists("uploads/certificate/".$data[0]['background'])){
                unlink("uploads/certificate/".$data[0]['background']);
            }
             $this->db->where("id",$param2)->delete("certificates");
             $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
             redirect(site_url('admin/certificate'), 'refresh');
            }
        } 
        if($param1 ==""){
        $page_data['certificates']=$this->db->get("certificates")->result_array();
        $page_data['page_name']  = 'certificate';
        $page_data['page_title'] = get_phrase('manage_certificates');
        $this->load->view('backend/index', $page_data);
       }
    }
    
    function certificate_generate($param1="")
       {
        if ($this->session->userdata('admin_login') != 1)
          redirect(site_url('login'), 'refresh'); 
        if($param1=="view")
        {    
        $page_data['certificate']=$this->db->where("id",1)->get("certificates")->result_array();
        $page_data['page_name']="certificate";
        $page_data["page_title"]=get_phrase('result');
        $this->load->view('backend/admin/generate',$page_data); 
        }
        if($param2==""){
        $certificate_id=$this->input->post("certificate_id");
        $class_id=$this->input->post("");   
        $page_data['page_name']  =  'manageCertificates';
        $page_data['page_title'] =  get_phrase('manage_certificates');
        $this->load->view('backend/index', $page_data);  
        }
      
     }
     
      //******************************************** Laboratory ************************************************************//
     
      function laboratory_items($param1="",$id=""){
        if ($this->session->userdata('admin_login') != 1)
         redirect(site_url('login'), 'refresh');
        
        if($param1=="create"){
            
           $data= array(
               'item_name' =>$this->input->post("item_name"),
               'price' =>$this->input->post("price"),
               'service_date' =>$this->input->post("service_date"),
               'limits' =>$this->input->post("limits"),
               'units' =>$this->input->post("units"),
               'review_status' =>$this->input->post("review_status"),
               'item_type' =>$this->input->post("item_type"),
               //'subject_id' =>$this->input->post("subject_id")
             ); 
            $id=$this->Laboratory_model->insert("stock_item",$data);
        
            if($_FILES['image']['tmp_name'] !=NULL){
                move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/laboratory/item_' .$id .'.jpg');
                $this->Laboratory_model->update("stock_item",array("image" => "item_" .$id . ".jpg"),array("item_id" => $id ));
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            }
            redirect(site_url('admin/laboratory_items'), 'refresh');
        }
        if($param1=="edit"){
           
           $data= array(
               'item_name' =>$this->input->post("item_name"),
               'price' =>$this->input->post("price"),
               'service_date' => date("m/d/Y",strtotime($this->input->post("service_date"))),
               'limits' =>$this->input->post("limits"),
               'units' =>$this->input->post("units"),
               'review_status' =>$this->input->post("review_status"),
               'item_type' =>$this->input->post("item_type"),
               //'subject_id' =>$this->input->post("subject_id")
             ); 
          $this->Laboratory_model->update("stock_item",$data,array("item_id" => $id ));

            if($_FILES['image']['tmp_name'] !=NULL){
                if(file_exists('uploads/laboratory/item_' .$id .'.jpg')){
                    unlink('uploads/laboratory/item_' .$id .'.jpg');
                }
                move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/laboratory/item_' .$id .'.jpg');
                $this->Laboratory_model->update("stock_item",array("image" => "item_" .$id . ".jpg"),array("item_id" => $id ));
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            }
            redirect(site_url('admin/laboratory_items/'), 'refresh');
        }
        if($param1=="delete"){
            $data=$this->Laboratory_model->get_where("stock_item",array("item_id"=>$id));
            if($data!=NULL){
                if(file_exists('uploads/laboratory/'.$data[0]["image"])){ unlink('uploads/laboratory/'.$data[0]["image"]); }
            }
            $this->Laboratory_model->delete("stock_item","item_id",$id);
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/laboratory_items/'), 'refresh');
        }  
        if($param1 ==""){
        $page_data['stock_item']=$this->Laboratory_model->get("stock_item");
        $page_data['page_name']  = 'laboratory_items';
        $page_data['page_title'] = get_phrase('manage_laboratory_items');
        $this->load->view('backend/index', $page_data);
       }
          
      }
      
      function laboratoryStockRegister($param1="",$id=""){
        
        if ($this->session->userdata('admin_login') != 1)
         redirect(site_url('login'), 'refresh');
        
        if($param1=="create"){
           $data= array(
               'item_id' =>$this->input->post("item_id"),
               'supplier_details' =>$this->input->post("supplier_details"),
               'total_price' =>$this->input->post("total_price"),
               'quantity' =>$this->input->post("quantity"),
               'purchase_date' =>$this->input->post("purchase_date"),
               'invoice' => $this->input->post("invoice"),
               'units' => $this->input->post("units")
             ); 
           $id= $this->Laboratory_model->insert("stock_register",$data);
           
           $this->db->query("UPDATE `stock_item` SET `quantity`= (quantity + ".$this->input->post('quantity').") ,`remaining`=(quantity-issue) WHERE item_id=".$this->input->post("item_id"));
           $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
          redirect(site_url('admin/laboratoryStockRegister'), 'refresh');
        }
        if($param1=="edit"){
            $data=$this->Laboratory_model->get_where("stock_register",array("id" => $id));
            if($data != NULL){
                $temp_quantity=$data[0]['quantity'] - $this->input->post("quantity");  
                $this->db->query("UPDATE `stock_item` SET `quantity`= quantity - (".$temp_quantity.") , `remaining`=(quantity-issue) WHERE item_id=".$this->input->post("item_id"));
               $temp= array(
               'supplier_details' =>$this->input->post("supplier_details"),
               'total_price' =>$this->input->post("total_price"),
               'quantity' =>$this->input->post("quantity"),
               'purchase_date' =>date("m/d/Y",strtotime($this->input->post("purchase_date"))),
               'invoice' => $this->input->post("invoice"),
               'units' => $this->input->post("units")
                ); 
              $this->Laboratory_model->update("stock_register",$temp,array("id" => $id)); 
            }
            $this->session->set_flashdata('flash_message' , get_phrase('data_update'));
            redirect(site_url('admin/laboratoryStockRegister'), 'refresh');
        }
        if($param1 ==""){
        $page_data['StockRegister']=$this->Laboratory_model->get("stock_register");
        $page_data['page_name']  = 'laboratoryStockRegister';
        $page_data['page_title'] = get_phrase('manage_Stock_Register');
        $this->load->view('backend/index', $page_data);
         }
         
      }
      
   //******************************************** Experiment ************************************************************//
     
      function experiment_name($param1="",$id=""){
        if ($this->session->userdata('admin_login') != 1)
         redirect(site_url('login'), 'refresh');
        
        if($param1=="create"){
           $data= array(
               'class_id' =>$this->input->post("class_id"),
               'subject_id' =>$this->input->post("subject_id"),
               'name' =>$this->input->post("name"),
               'description' =>$this->input->post("description")
             ); 
           $id=$this->Laboratory_model->insert("experiments",$data);
          
           $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
           redirect(site_url('admin/experiment_name'), 'refresh');
        }
        if($param1=="edit"){
           $data= array(
               'name' =>$this->input->post("name"),
               'description' =>$this->input->post("description")
             ); 
          $this->Laboratory_model->update("experiments",$data,array("exp_id" => $id));
          
           $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
           redirect(site_url('admin/experiment_name'), 'refresh');
        }
        if($param1=="delete"){
            $this->Laboratory_model->delete("experiment_item","exp_id",$id);
            $this->Laboratory_model->delete("experiments","exp_id",$id);
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/experiment_name/'), 'refresh');
        }   
        if($param1 ==""){
        $page_data['experiments']=$this->Laboratory_model->get_experiments();
        $page_data['page_name']  = 'experiment_name';
        $page_data['page_title'] = get_phrase('manage_experiments');
        $this->load->view('backend/index', $page_data);
       }
          
      }
      
    function experiment_item($param1="",$id){
        if ($this->session->userdata('admin_login') != 1)
         redirect(site_url('login'), 'refresh');
        
        if($param1=="create"){
           $data= array(
               'exp_id' =>$this->input->post("exp_id"),
               'item_id' =>$this->input->post("item_id"),
               'quantity' =>$this->input->post("quantity"),
               'units' => $this->input->post("units")
             ); 
          $id=$this->Laboratory_model->insert("experiment_item",$data);
         
           $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
           redirect(site_url('admin/experiment_item/view/'.$this->input->post("exp_id")), 'refresh');
        }
        if($param1=="edit"){
           $data= array(
               'quantity' =>$this->input->post("quantity"),
               'units' => $this->input->post("units")
             ); 
           $this->Laboratory_model->update("experiment_item",$data,array("id" => $id));
     
           $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfuly'));
           redirect(site_url('admin/experiment_item/view/'.$this->input->post("exp_id")), 'refresh');
        }
        
        if($param1=="delete"){
            $this->Laboratory_model->delete("experiment_item","id",$id);
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/experiment_item/view/'.$id), 'refresh');
        }
        if($param1=="view"){
        $page_data['exp_id']=$id;  
        $page_data['experiment_item']=$this->Laboratory_model->get_experiment_item($id);
        $page_data['page_name']  = 'experiment_item';
        $page_data['page_title'] = get_phrase('manage_experiment_items');
        $this->load->view('backend/index', $page_data);
       }
          
    }
    
    function laboratory_manage($param1="",$id=""){
        if($this->session->userdata('admin_login')!=1)
            redirect(site_url('login'), 'refresh');
        
       if($param1=="issue"){
          $temp =  $this->Laboratory_model->get_issue($this->input->post('exp_id'));
            foreach($temp as $row)  {    
          if($row['remain'] > $row['quan']){
             $temp_remaining=$row['remain'] - $row['quan'];
             $temp_issue = $row['iss'] + $row['quan'];
             $this->Laboratory_model->update("stock_item",array("remaining" => $temp_remaining, "issue" =>  $temp_issue),array("item_id" => $row['id']));
          }else{
               $this->session->set_flashdata('error_message' , get_phrase('item_quantity_is_less_to_issue'));
              redirect(site_url('admin/laboratory_manage/view/'), 'refresh');
          }
                
            }$data=array(
                 "exp_id" => $this->input->post('exp_id'),
                 "student_id" => $this->input->post('student_id'),
                 "status" => "issued",
                 "issue_date" => $this->input->post("issue_date"),
                 "remark"=>$this->input->post("remark")
                 );  
          $id=$this->Laboratory_model->insert("item_issue",$data);  
          $this->session->set_flashdata('flash_message' , get_phrase('item_issued'));
          redirect(site_url('admin/laboratory_manage/view/'), 'refresh');
        }
        if($param1=="damage"){
           $item_id=$this->input->POST("item_id");
           $student_id=$this->input->post("student_id");
           $exp_id=$this->input->post("exp_id");
           $price= $this->input->post('price');
           $quantity = $this->input->post('quantity'); 
           $issued_id= $this->input->post('id');
           $issued_quantity = $this->input->post('issuedquantity');
          
            for($i = 0;$i < count($item_id);$i++){
                //$remain_qut=$issued_quantity[$i] - $quantity[$i];
                $this->Laboratory_model->update_stock_ondamage($quantity[$i],$issued_quantity[$i],$item_id[$i]);
                if($quantity[$i] > 0){
                    $fine=$quantity[$i] * $price[$i];
                    $this->Laboratory_model->insert("damage_item",array("item_id"=>$item_id[$i],"quantity" => $quantity[$i],'student_id' => $student_id,'fine' => $fine ,"fine_date" => $this->input->post('return_date') ));
                 }
                } 
            $this->Laboratory_model->update("item_issue",array("status" => 'return', "return_date" =>  $this->input->post('return_date')),array("id" => $issued_id));  
            
            $this->session->set_flashdata('flash_message' , get_phrase('experiment_items_return_successfully'));
            redirect(site_url('admin/laboratory_manage/view/'), 'refresh');
        }
        
        if($param1=="view"){
        //$page_data['exp_id']=$id;  
        $page_data['item_issue']=$this->Laboratory_model->get_laboratory_issue();
        $page_data['damage_item']=$this->Laboratory_model->get_laboratory_damage();
        $page_data['page_name']  =  'laboratory_manage';
        $page_data['page_title'] =  get_phrase('laboratory_manage');
        $this->load->view('backend/index', $page_data);
        }  
      }
      
    function get_exp_std()
    {
        if($this->session->userdata('admin_login')!=1)
            redirect(site_url('login'), 'refresh');
            
        $page_data['class_id'] = $this->input->post("class_id");
        $page_data['section_id'] = $this->input->post("section_id");
        $page_data['subject_id'] = $this->input->post("subject_id");
        
        $this->load->view('backend/admin/experiment_student_selector' , $page_data);
    }
    function get_experiment_items($exp_id){
        if($this->session->userdata('admin_login')!=1)
            redirect(site_url('login'), 'refresh');
        $exp_items= $this->Laboratory_model->get_experiment_item($exp_id);
         echo " <table class='table table-bordered'>
                  <thead>
                     <th>Sl no.</th>
                     <th>Item Name</th>
                     <th>quantity</th>
                  <thead>
                  <tbody> ";
                  $count=1;
                  foreach($exp_items as $row){
                      echo "
                        <tr>
                         <td>".$count."</td>
                         <td>".$row['item_name']."</td>
                         <td>".$row['quantity']." ". $row['units']."</td>
                       </tr>
                      ";
                      $count++;
                   }
                  
            echo "</tbody> 
            </table> ";
    }
    
     /**********MANAGE LIBRARY / BOOKS********************/
     
      function book($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            for($i=0;$i<$this->input->post('number_of_copies');$i++){
            //$data['title']        = implode(' ', str_word_count($this->input->post('title'),1));
            $data['title']        = $this->input->post('title');
            // $data['published_by']    = $this->input->post('published_by');
            // $data['description'] = $this->input->post('description');
            $data['per_unit_price'] =   $this->input->post('per_unit_price');
            $data['author'] =      $this->input->post('author');
            $data['category_name'] =      $this->input->post('category_name');
            $data['material_name'] =      $this->input->post('material_name');
            $data['isbn'] =      $this->input->post('isbn');
            $data['number_of_page'] =   $this->input->post('number_of_page');
            $accession_number = $this->input->post('accession_number')[$i];
            $data['accession_number'] =   $accession_number;
            $data['call_number'] =   $this->input->post('call_number');
            $data['language'] =   $this->input->post('language');
            $data['subject'] =   $this->input->post('subject');
            $data['location'] =   $this->input->post('location');
            $data['rack_number'] =   $this->input->post('rack_number');
            $data['shelves_number'] =   $this->input->post('shelves_number');
            $data['publisher_name'] = $this->input->post("publisher_name");
            $data['publication_place'] = $this->input->post("publication_place");
            $data['publication_year'] = $this->input->post("publication_year");
            $data['total_copies'] = '1'; 
            $data['remaining_book'] = '1';

            $id=$this->Library_model->insert("book",$data); 

             if(!empty($_FILES["file_name"]["name"])) {
              $target_file =  "uploads/books/" . basename($_FILES["file_name"]["name"]);     
              $FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); 
              $file_name=$id.".".$FileType;
              move_uploaded_file($_FILES["file_name"]["tmp_name"], "uploads/books/" . $file_name );
              $this->Library_model->update("book",array("file_name" => $file_name),array("book_id" => $id ));      
             }
            } 
            // $this->Library_model->update_book_quantity('1',$id);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/book/view'), 'refresh');
        }

        if ($param1 == 'edit') {
            $data['title']        = implode(' ', str_word_count($this->input->post('title'),1));
            $data['published_by']    = $this->input->post('published_by');
            $data['description'] = $this->input->post('description');
            $data['per_unit_price'] =   $this->input->post('per_unit_price');
            $data['author'] =      $this->input->post('author');
            $data['category_name'] =      $this->input->post('category_name');
            $data['material_name'] =      $this->input->post('material_name');
            $data['isbn'] =      $this->input->post('isbn');
            $data['number_of_page'] =   $this->input->post('number_of_page');
            $data['accession_number'] =   $this->input->post('accession_number');
            $data['call_number'] =   $this->input->post('call_number');
            $data['language'] =   $this->input->post('language');
            $data['subject'] =   $this->input->post('subject');
            $data['location'] =   $this->input->post('location');
            $data['rack_number'] =   $this->input->post('rack_number');
            $data['shelves_number'] =   $this->input->post('shelves_number');
            $data['publisher_name'] = $this->input->post("publisher_name");
            $data['publication_place'] = $this->input->post("publication_place");
            $data['publication_year'] = $this->input->post("publication_year"); 
            $id=$this->Library_model->update("book",$data,array("book_id" => $param2 ));  
            // if(!empty($_FILES["file_name"]["name"])) {
            //     move_uploaded_file($_FILES["file_name"]["tmp_name"], "uploads/document/" . $_FILES["file_name"]["name"]);
            // }

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
            redirect(site_url('admin/book/view'), 'refresh');
        }
        if ($param1 == 'delete') {
            $this->db->where('book_id', $param2);
            $this->db->delete('book');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/book/view'), 'refresh');
        }
        if($param1 == 'view'){
        $page_data["books"] = $this->Library_model->get_book();    
        $page_data['page_name']  = 'book';
        $page_data['page_title'] = get_phrase('manage_library_books');
        $this->load->view('backend/index', $page_data);
        }
    }
    
    function category($param1="",$param2="")
    {
        if($param1=="create"){
            $data= array(
              "category_name" => $this->input->post("category_name")
            );
            $id=$this->Library_model->insert("category",$data);         
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/category/view'), 'refresh');
        }
        if($param1=="edit"){
            $data= array(
              "category_name" => $this->input->post("category_name")
            );
            
            $id=$this->Library_model->update("category",$data,array("category_id" => $param2));         

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
            redirect(site_url('admin/category/view'), 'refresh');
        } 
        if($param1=="delete"){
            
            $id=$this->Library_model->delete("category","category_id",$param2);   
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted_successfully'));
            redirect(site_url('admin/category/view'), 'refresh');
        }  
            $page_data["category"] =$this->Library_model->get("category");
            $page_data['page_name']  = 'category';
            $page_data['page_title'] =get_phrase('manage_category');
            $this->load->view('backend/index', $page_data);        
    }
    function source($param1="",$param2="")
    {
        if($param1=="create"){
            $data= array(
              "source_name" => $this->input->post("source_name")
            );
            $id=$this->Library_model->insert("source",$data);         
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/source/view'), 'refresh');
        }
        if($param1=="edit"){
            $data= array(
              "source_name" => $this->input->post("source_name")
            );
            
            $id=$this->Library_model->update("source",$data,array("source_id" => $param2));         

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
            redirect(site_url('admin/source/view'), 'refresh');
        } 
        if($param1=="delete"){
            
            $id=$this->Library_model->delete("source","source_id",$param2);   
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted_successfully'));
            redirect(site_url('admin/source/view'), 'refresh');
        }  
            $page_data["source"] =$this->Library_model->get("source");
            $page_data['page_name']  = 'source';
            $page_data['page_title'] =get_phrase('manage_source');
            $this->load->view('backend/index', $page_data);        
    }
    function material($param1="",$param2="")
    {
        if($param1=="create"){
            $data= array(
              "material_name" => $this->input->post("material_name")
            );
            $id=$this->Library_model->insert("material",$data);         
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/material/view'), 'refresh');
        }
        if($param1=="edit"){
            $data= array(
              "material_name" => $this->input->post("material_name")
            );
            
            $id=$this->Library_model->update("material",$data,array("material_id" => $param2));         

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
            redirect(site_url('admin/material/view'), 'refresh');
        } 
        if($param1=="delete"){
            
            $id=$this->Library_model->delete("material","material_id",$param2);   
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted_successfully'));
            redirect(site_url('admin/material/view'), 'refresh');
        }  
            $page_data["material"] =$this->Library_model->get("material");
            $page_data['page_name']  = 'material';
            $page_data['page_title'] =get_phrase('manage_material');
            $this->load->view('backend/index', $page_data);        
    }

    
    function publisher($param1 = '',$id="") {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
            
        if($param1=="create"){
            $data= array(
              "publisher_name" => $this->input->post("publisher_name"),
              "publisher_details" => $this->input->post("publisher_details")
            );
            $id=$this->Library_model->insert("publisher",$data);         
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/publisher/view'), 'refresh');
        }
        if($param1=="edit"){
            $data= array(
              "publisher_name" => $this->input->post("publisher_name"),
              "publisher_details" => $this->input->post("publisher_details")
            );
            
            $id=$this->Library_model->update("publisher",$data,array("publisher_id" => $id));         

            $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
            redirect(site_url('admin/publisher/view'), 'refresh');
        } 
        if($param1=="delete"){
            
            $id=$this->Library_model->delete("publisher","publisher_id",$id);         
            
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted_successfully'));
            redirect(site_url('admin/publisher/view'), 'refresh');
        }  
        if($param1=="view"){
            $page_data["publisher"] =$this->Library_model->get("publisher");
            $page_data['page_name']  = 'publisher';
            $page_data['page_title'] =get_phrase('manage_publisher');
            $this->load->view('backend/index', $page_data);    
        }     
    }
    
    function library_stock_register($param1="",$id){
         if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
         
         if($param1=="create"){
            //   $this->Library_model->update_book_quantity($this->input->post("total_quantity"),$this->input->post("book_id"));
             $data= array(
              "supplier_name" => $this->input->post("supplier_name"),
              "supplier_details" => $this->input->post("supplier_details"),
              "total_quantity" => $this->input->post("total_quantity"),
              "total_price" => $this->input->post("total_price"),
              "invoice_details" => $this->input->post("invoice_details"),
              "receive_date" => $this->input->post("receive_date"),
              "book_id" => $this->input->post("book_id")
            );
            $id=$this->Library_model->insert("library_stock_register",$data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/library_stock_register/view'), 'refresh');
         }
         if($param1=="edit"){
              $data=$this->Library_model->get_where("library_stock_register",array("id" => $id));
            if($data != NULL){
                // $temp_quantity=$data[0]['total_quantity'] - $this->input->post("quantity");  
                // $this->Library_model->update_book_register($temp_quantity,$this->input->post("book_id"));
                $data= array(
                  "supplier_name" => $this->input->post("supplier_name"),
                  "supplier_details" => $this->input->post("supplier_details"),
                  "total_quantity" => $this->input->post("quantity"),
                  "total_price" => $this->input->post("total_price"),
                  "invoice_details" => $this->input->post("invoice_details"),
                  "receive_date" => $this->input->post("receive_date"),
                );
              $this->Library_model->update("library_stock_register",$data,array("id" => $id)); 
              $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
              redirect(site_url('admin/library_stock_register/view'), 'refresh');
            }
         }
         if($param1=="view"){
            $page_data["library_stock_register"] = $this->Library_model->get_libraryStockRegister();
            $page_data['page_name']  = 'library_stock_register';
            $page_data['page_title'] =get_phrase('manage_library_stock_register');
            $this->load->view('backend/index', $page_data);    
        }    
    }
    
    function library_settings($param1=""){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
          if($param1=="edit"){
             $data= array(
              "number_of_book" => $this->input->post("number_of_book"),
              "per_day_cost" => $this->input->post("per_day_cost"),
              "maximum_date" => $this->input->post("maximum_date")
            );
            $this->Library_model->update("library_settings",$data,array("user_type" => $this->input->post("user_type")));
            $this->session->set_flashdata('flash_message' , get_phrase('setting_updated_successfully'));
            redirect(site_url('admin/library_settings/view'), 'refresh');
         }  
        if($param1="view"){
            $page_data["library_settings"] = $this->Library_model->get("library_settings");
            $page_data['page_name']  = 'library_settings';
            $page_data['page_title'] =get_phrase('library_settings');
            $this->load->view('backend/index', $page_data); 
        }
    }

    function get_books() {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $columns = array(
            0 => 'book_id',
            1 => 'name',
            2 => 'author',
            3 => 'description',
            4 => 'price',
            5 => 'class',
            6 => 'download',
            7 => 'options',
            8 => 'book_id'
        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir   = $this->input->post('order')[0]['dir'];

        $totalData = $this->ajaxload->all_books_count();
        $totalFiltered = $totalData;

        if(empty($this->input->post('search')['value'])) {
            $books = $this->ajaxload->all_books($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value'];
            $books =  $this->ajaxload->book_search($limit,$start,$search,$order,$dir);
            $totalFiltered = $this->ajaxload->book_search_count($search);
        }

        $data = array();
        if(!empty($books)) {
            foreach ($books as $row) {
                if ($row->file_name == null)
                    $download = '';
                else
                    $download = '<a href="'.site_url("uploads/document/$row->file_name").'" class="btn btn-blue btn-icon icon-left"><i class="entypo-download"></i>'.get_phrase('download').'</a>';

                $options = '<div class="btn-group"><button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown">
                                    Action <span class="caret"></span></button><ul class="dropdown-menu dropdown-default pull-right" role="menu"><li><a href="#" onclick="book_edit_modal('.$row->book_id.')"><i class="entypo-pencil"></i>&nbsp;'.get_phrase('edit').'</a></li><li class="divider"></li><li><a href="#" onclick="book_delete_confirm('.$row->book_id.')"><i class="entypo-trash"></i>&nbsp;'.get_phrase('delete').'</a></li></ul></div>';

                $nestedData['book_id'] = $row->book_id;
                $nestedData['name'] = $row->name;
                $nestedData['author'] = $row->author;
                $nestedData['description'] = $row->description;
                $nestedData['price'] = $row->price;
                $nestedData['class'] = $this->db->get_where('class', array('class_id' => $row->class_id))->row()->name;
                $nestedData['download'] = $download;
                $nestedData['options'] = $options;

                $data[] = $nestedData;
            }
        }

        $json_data = array(
            "draw"            => intval($this->input->post('draw')),
            "recordsTotal"    => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data"            => $data
        );

        echo json_encode($json_data);
    }
     function library_register($param1="",$param2="",$param3=" ",$book_id=""){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
            
        if($param1=="user"){
            if($param2==""){
                $user_type=$this->input->post("user_type");
                $user_id=$this->input->post("user_id");
                $student_id =$this->input->post("student_id");
            }else{
                $user_type=$param2;
                $user_id=$param3;
                $student_id =$param3;
            }
            if($user_type == 'student'){
              $user_id=$student_id;
              //$page_data["issued_books"]=$this->Library_model->get_issued_book($user_type,$student_id);
              $page_data["issued_books"]=$this->Library_model->issued_book_get($user_type,$user_id);
            }else{
            //$page_data["issued_books"]=$this->Library_model->get_issued_book($user_type,$user_id); 
            $page_data["issued_books"]=$this->Library_model->issued_book_get($user_type,$user_id);
            } 
            // $page_data['user_type']=$user_type;
            // $page_data['user_id']=$user_id;
            // $page_data["books"]=$this->Library_model->get_book_for_issue($user_type,$user_id);  
            // $page_data["fine_table"]= $this->Library_model->get_fine_details($user_type,$user_id); 
            // $page_data["book_requests"]= $this->Library_model->get_requested_book($user_type,$user_id);
            
            // $page_data["library_settings"]=$this->Library_model->get_where("library_settings",array("user_type" => $user_type ));
            
            $page_data['user_type']=$user_type;
            $page_data['user_id']=$user_id;
            $page_data["books"]=$this->Library_model->book_for_issue_get($user_type,$user_id); 
           
            $page_data["fine_table"]= $this->Library_model->fine_details_get($user_type,$user_id);
            $page_data["book_requests"]= $this->Library_model->requested_book_get($user_type,$user_id);
            $page_data["library_settings"]=$this->Library_model->get_where("library_settings",array("user_type" => $user_type ));
            
            $name=$this->db->get_where($user_type,array($user_type."_id" => $user_id))->row()->name;
            $page_data['page_name']  = 'library_user_page';
            $page_data['page_title'] =get_phrase('library_register-'.$name);
            $this->load->view('backend/index', $page_data); 
        }
        if($param1=="barcode"){
            if($param2==""){
                $user_type=$this->input->post("user_type");
                $user_details= $this->db->get_where($user_type,array($user_type.'_code'=> $this->input->post("user_code")))->result_array();
                
             
                if($user_details!=NULL){
                    $user_id=$user_details[0][$user_type.'_id'];
                }
           
            }else{
                $user_type=$param2;
                $user_id=$param3;
                $student_id =$param3;
            }
            if($user_type == 'student'){
            //$user_id=$student_id;
              //$page_data["issued_books"]=$this->Library_model->get_issued_book($user_type,$user_id);    
              $page_data["issued_books"]=$this->Library_model->issued_book_get($user_type,$user_id);
            }else{
            //$page_data["issued_books"]=$this->Library_model->get_issued_book($user_type,$user_id); 
            $page_data["issued_books"]=$this->Library_model->issued_book_get($user_type,$user_id);
            } 
            
            $page_data['user_type']=$user_type;
            $page_data['user_id']=$user_id;
            $page_data["books"]=$this->Library_model->book_for_issue_get($user_type,$user_id); 
           
            $page_data["fine_table"]= $this->Library_model->fine_details_get($user_type,$user_id);
            $page_data["book_requests"]= $this->Library_model->requested_book_get($user_type,$user_id);
            $page_data["library_settings"]=$this->Library_model->get_where("library_settings",array("user_type" => $user_type ));
            // if($user_type == 'student'){
            //  $user_id=$student_id;
            //   $page_data["issued_books"]=$this->Library_model->get_issued_book($user_type,$user_id);    
            // }else{
            // $page_data["issued_books"]=$this->Library_model->get_issued_book($user_type,$user_id);  
            // } 
            // $page_data['user_type']=$user_type;
            // $page_data['user_id']=$user_id;
            // $page_data["books"]=$this->Library_model->get_book_for_issue($user_type,$user_id);  
            // $page_data["fine_table"]= $this->Library_model->get_fine_details($user_type,$user_id); 
            // $page_data["book_requests"]= $this->Library_model->get_requested_book($user_type,$user_id);
            
            // $page_data["library_settings"]=$this->Library_model->get_where("library_settings",array("user_type" => $user_type ));
            $name=$this->db->get_where($user_type,array($user_type."_id" => $user_id))->row()->name;
            $page_data['page_name']  = 'library_user_page';
            $page_data['page_title'] =get_phrase('library_register-'.$name);
            $this->load->view('backend/index', $page_data); 
        }
        if($param1=="issue_book"){
            $data =array(
                   'user_id' => $param3,
                   'user_type' =>  $param2,
                   'book_id' =>$book_id,
                   'status' => "issued",
                   'register_date' => date("Y-m-d")
                   );
            $this->Library_model->insert("library_register",$data);
            $this->Library_model->book_update($book_id);
            $this->session->set_flashdata('flash_message' , get_phrase('book_has_been_issued_successfully'));
            redirect(site_url('admin/library_register/user/'.$param2.'/'.$param3), 'refresh');
            
        } 
        if($param1=="return_book"){
            $this->Library_model->return_book($param2,$param3,$book_id);
            $this->session->set_flashdata('flash_message' , get_phrase('book_has_been_returned_successfully'));
            redirect(site_url('admin/library_register/user/'.$param2.'/'.$param3), 'refresh');
        }
        if($param1=="view"){
            $page_data['page_name']  = 'library_register';
            $page_data['page_title'] =get_phrase('library_register');
            $this->load->view('backend/index', $page_data); 
        }
    }
    function get_other_lib_user($type){
        $data=$this->Library_model->get($type);
        echo  "<option value=''>select user</option>";
        foreach($data as $row){
            $temp=$type.'_id';
         echo  "<option value='".$row[$temp]."'>".$row['name']."</option>";
        }
    }
    function get_lib_section($class_id){
        $sections =$this->Library_model->get_where('section' , array('class_id' => $class_id)); 
        foreach($sections as $row){
         echo  "<option value='".$row['section_id']."'>".$row['name']."</option>";
        }
    }
    function get_lib_student($class_id,$section_id){
        $students = $this->Library_model->get_where('enroll' , array( 'class_id' => $class_id ,'section_id' => $section_id, 'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description ));
        foreach ($students as $row) {
            $student = $this->Library_model->get_where('student' , array('student_id' => $row['student_id']));
            foreach ($student as $row1) {
            echo '<option value="' . $row1['student_id'] . '">' .$row1['name'].'['. $row1['student_code'] . '] </option>';
            }
        }
    }
    function other_staff($param1 = '',$id=""){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
       if($param1=="create"){
           $data= array(
               'name' =>$this->input->post("staff_name"),
               'phonenumber' =>$this->input->post("phonenumber"),
               'address' =>$this->input->post("address"),
               'designation' =>$this->input->post("designation")
             ); 
           $id=$this->Library_model->insert("other_staff",$data);
          
           $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
           redirect(site_url('admin/other_staff/view'), 'refresh');
        }
        if($param1=="edit"){
           $data= array(
                'staff_name' =>$this->input->post("staff_name"),
               'phonenumber' =>$this->input->post("phonenumber"),
               'address' =>$this->input->post("address"),
               'designation' =>$this->input->post("designation")
             ); 
             $this->Library_model->update("other_staff",$data,array("other_staff_id" => $id)); 
              $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
              redirect(site_url('admin/other_staff/view'), 'refresh');
        }
        if($param1=="delete"){
            $this->Library_model->delete("other_staff","other_staff_id",$id);
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/other_staff/view'), 'refresh');
        }   
        
        if($param1=="view"){
            $page_data['other_staff'] = $this->Library_model->get("other_staff");
            $page_data['page_name']  = 'other_staff';
            $page_data['page_title'] =get_phrase('visitor');
            $this->load->view('backend/index', $page_data); 
        }
        
    }
    
       function generate_bulk_book_csv()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $file   = fopen("uploads/bulk_book.csv", "w");
        $line   = array('book Title','Author name','category_name','material_name','book price','ISBN','number_of_page','accession_number','call_number','location','rack_number','shelves_number','publisher_name','publication_place','publication_year');
        fputcsv($file, $line, ',');
        echo $file_path = base_url() .'uploads/bulk_book.csv';
    }

    function bulk_book_add_using_csv($param1 = '') {

        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

       if ($param1 == 'import') {
              move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/bulk_book.csv');
              $csv = array_map('str_getcsv', file('uploads/bulk_book.csv'));
              $count = 1;
              $array_size = sizeof($csv);
             
             foreach ($csv as $row) {
                  if ($count == 1) {
                      $count++;
                      continue;
                  }
                  $data['title']      = $row[0];
                  $data['author']  = $row[1]; 
                  $data['category_name']  = $row[2];
                  $data['material_name']  = $row[3];
                  $data['per_unit_price']  = $row[4];
                  $data['isbn']  = $row[5]; 
                  $data['number_of_page']  = $row[6];
                  $data['accession_number'] = $row[7];
                  $data['call_number'] = $row[8];
                  $data['location'] = $row[9];
                  $data['rack_number'] = $row[10];
                  $data['shelves_number'] = $row[11];
                  $data['publisher_name'] = $row[12];
                  $data['publication_place'] = $row[13];
                  $data['publication_year'] = $row[14];
                  $data['total_copies'] = '1';
                  $data['remaining_book'] = '1';
                 $this->Library_model->insert('book', $data);
              }
              $this->session->set_flashdata('flash_message', get_phrase('book_imported'));
              redirect(site_url('admin/book/view'), 'refresh');
        }

    }
     function generate_bulk_publisher_csv()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

        $file   = fopen("uploads/bulk_publisher.csv", "w");
        $line   = array('publisher_name','publisher_details');
        fputcsv($file, $line, ',');
       echo $file_path = base_url() . 'uploads/bulk_publisher.csv';
    }

    function bulk_publisher_add_using_csv($param1 = '') {

        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

       if ($param1 == 'import') {
              move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/bulk_publisher.csv');
              $csv = array_map('str_getcsv', file('uploads/bulk_publisher.csv'));
              $count = 1;
              $array_size = sizeof($csv);
             
             foreach ($csv as $row) {
                  if ($count == 1) {
                      $count++;
                      continue;
                  }
                  $data['publisher_name']      = $row[0];
                  $data['publisher_details']  = $row[1]; 
                 
                 $this->Library_model->insert('publisher', $data);
              }
              $this->session->set_flashdata('flash_message', get_phrase('publishers_imported'));
              redirect(site_url('admin/publisher/view'), 'refresh');
        }

    }
      /**********MANAGE STOCK QUANTITY********************/
     
    function school_stock_items($param1 = '', $param2 = '', $param3 = '')
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        if ($param1 == 'create') {
            $data['particular_name'] = $this->input->post('particular_name');
            $data['particular_type']    = $this->input->post('particular_type');
            $data['description'] = $this->input->post('description');
            $data['per_unit_price'] =   $this->input->post('per_unit_price');
            $data['expiry_date'] =      date('Y-m-d',strtotime($this->input->post('expiry_date')));
            $data['warranty'] =      $this->input->post('warranty');
            $data['remarks'] =      $this->input->post('remarks');
            $id=$this->SchoolStockRegister_model->insert("school_stock_items",$data);         
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/school_stock_items/view'), 'refresh');
        }

        if ($param1 == 'edit') {
            $data['particular_name']        = $this->input->post('particular_name');
            $data['particular_type']    = $this->input->post('particular_type');
            $data['description'] = $this->input->post('description');
            $data['per_unit_price'] =   $this->input->post('per_unit_price');
            $data['expiry_date'] =      date('Y-m-d',strtotime($this->input->post('expiry_date')));
            $data['warranty'] =      $this->input->post('warranty');
            $data['remarks'] =      $this->input->post('remarks');
            $id=$this->SchoolStockRegister_model->update("school_stock_items",$data,array("stockitem_id" => $param2 ));         
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
            redirect(site_url('admin/school_stock_items/view'), 'refresh');
        }
        if ($param1 == 'delete') {
            $this->db->where('stockitem_id', $param2);
            $this->db->delete('school_stock_items');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/school_stock_items/view'), 'refresh');
        }
        if($param1 == 'view'){
        $page_data["school_stock_item"]=$this->SchoolStockRegister_model->get("school_stock_items");       
        $page_data['page_name']  = 'school_stock_items';
        $page_data['page_title'] = get_phrase('school_stock_items');
        $this->load->view('backend/index', $page_data);
        }
    }
    
    function generate_bulk_school_stock_csv()
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
        $file   = fopen("uploads/bulk_school_stock.csv", "w");
        $line   = array('particular_name','particular_type','description','per_unit_price','expiry_date','warranty','remarks');
        fputcsv($file, $line, ',');
        echo $file_path = base_url() .'uploads/bulk_school_stock.csv';
    }

    function bulk_school_stock_add_using_csv($param1 = '') {

        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');

       if ($param1 == 'import') {
              move_uploaded_file($_FILES['userfile']['tmp_name'], 'uploads/bulk_school_stock.csv');
              $csv = array_map('str_getcsv', file('uploads/bulk_school_stock.csv'));
              $count = 1;
              $array_size = sizeof($csv);
             
             foreach ($csv as $row) {
                  if ($count == 1) {
                      $count++;
                      continue;
                  }
                  $data['particular_name']      = $row[0];
                  $data['particular_type']  = $row[1]; 
                  $data['description']  = $row[2];
                  $data['per_unit_price']  = $row[3];
                  $data['expiry_date']  = date('Y-m-d',strtotime($row[4]));
                  $data['warranty']  = $row[5]; 
                  $data['remarks']  = $row[6];
                 $this->Library_model->insert('school_stock_items', $data);
              }
              $this->session->set_flashdata('flash_message', get_phrase('book_imported'));
              redirect(site_url('admin/school_stock_items/view'), 'refresh');
        }

    }
    
    function school_stock_register($param1="",$id){
         if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
         
         if($param1=="create"){
              $this->SchoolStockRegister_model->update_schoolstock_quantity($this->input->post("total_quantity"),$this->input->post("stockitem_id"));
             $data= array(
              "supplier_name" => $this->input->post("supplier_name"),
              "supplier_details" => $this->input->post("supplier_details"),
              "total_quantity" => $this->input->post("total_quantity"),
              "total_price" => $this->input->post("total_price"),
              "invoice_details" => $this->input->post("invoice_details"),
              "supplier_date" => $this->input->post("supplier_date"),
              "stockitem_id" => $this->input->post("stockitem_id")
            );
            $id=$this->SchoolStockRegister_model->insert("school_stock_register",$data);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            redirect(site_url('admin/school_stock_register/view'), 'refresh');
         }
         if($param1=="edit"){
              $data=$this->SchoolStockRegister_model->get_where("school_stock_register",array("id" => $id));
            if($data != NULL){
                $temp_quantity=$data[0]['total_quantity'] - $this->input->post("total_quantity");  
                $this->SchoolStockRegister_model->update_schoolstock_register($temp_quantity,$this->input->post("stockitem_id"));
                $data= array(
                  "supplier_name" => $this->input->post("supplier_name"),
                  "supplier_details" => $this->input->post("supplier_details"),
                  "total_quantity" => $this->input->post("total_quantity"),
                  "total_price" => $this->input->post("total_price"),
                  "invoice_details" => $this->input->post("invoice_details"),
                  "supplier_date" => $this->input->post("supplier_date"),
                );
              $this->SchoolStockRegister_model->update("school_stock_register",$data,array("id" => $id)); 
              $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
              redirect(site_url('admin/school_stock_register/view'), 'refresh');
            }
         }
         if($param1=="view"){
            $page_data["school_stock_register"] = $this->SchoolStockRegister_model->get_schoolStockRegister();
            $page_data['page_name']  = 'school_stock_register';
            $page_data['page_title'] =get_phrase('school_stock_register');
            $this->load->view('backend/index', $page_data);    
        }    
    }

    function school_stock_manage($param1="",$param2="",$param3=" ",$stockitem_id=""){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
            
        if($param1=="create"){
            $param2 = $this->input->post("user_type");
            $param3 = $this->input->post("user_id");
             $data= array(
              "user_type" => $param2,
              "user_id" => $param3,
              "stockitem_id" => $this->input->post("stockitem_id"),
              "quantity" => $this->input->post("quantity"),
              "issued_date" => $this->input->post("issued_date"),
              "status" => "issued"
            );
            $id=$this->SchoolStockRegister_model->insert("school_stock_issued",$data);
            $this->SchoolStockRegister_model->stock_update($this->input->post("stockitem_id"),$this->input->post("quantity"));
            $this->session->set_flashdata('flash_message' , get_phrase('stock_added_successfully')); 
            $page_data['school_stock_issued'] = $this->SchoolStockRegister_model->stock_issued($param2,$param3);
            redirect(site_url('admin/school_stock_manage/view'), 'refresh');
         }
        if($param1=="issue_book"){
            $data =array(
                   'user_id' => $param3,
                   'user_type' =>  $param2,
                   'stockitem_id' =>$stockitem_id,
                   'status' => "issued",
                   'register_date' => date("Y-m-d")
                   );
                   print_r($data);exit;
            $this->SchoolStockRegister_model->insert("school_stock_issued",$data);
            $this->SchoolStockRegister_model->stock_update($stockitem_id);
            $this->session->set_flashdata('flash_message' , get_phrase('stock_has_been_issued_successfully'));
            redirect(site_url('admin/school_stock_manage/user/'.$param2.'/'.$param3), 'refresh');
            
        } 
        if($param1 == "damage" ){
            
           $issued_id=$this->input->POST("issued_id");
           $stockitem_id=$this->input->post("stockitem_id");
           $user_id=$this->input->post("user_id");
           $user_type= $this->input->post('user_type');
           $quantity = $this->input->post('quantity'); 
           $return_date= $this->input->post('return_date');
            
            $this->SchoolStockRegister_model->insert("damge_stock",array("stockitem_id" => $stockitem_id,"user_id" => $user_id,"user_type" => $user_type,"quantity" => $quantity,"damage_date" => $return_date));
            $this->SchoolStockRegister_model->damage_update_item($stockitem_id,$quantity);
            $this->SchoolStockRegister_model->damage_updateissued_item($issued_id,$return_date);
            
            $this->session->set_flashdata('flash_message' , get_phrase('damage_items_return_successfully'));
            redirect(site_url('admin/school_stock_manage/view/'), 'refresh');
        }
        if($param1=="view"){
            $page_data["schoolstock_issue"] = $this->SchoolStockRegister_model->issued_stock_details();
            $page_data['page_name']  = 'school_stock_manage';
            $page_data['page_title'] =get_phrase('school_stock_manage');
            $this->load->view('backend/index', $page_data); 
        }
    }
    
    function get_type($user_type){
        
        if($user_type=="teacher"){
          $data = $this->db->query("SELECT teacher.teacher_id ,teacher.`name`,teacher.`birthday`, teacher.`sex`, teacher.`religion`, teacher.`blood_group`, teacher.`address`,teacher.`phone`,teacher.`email`,teacher.`password`,  class.name as cname,class.class_id FROM `teacher` JOIN class ON teacher.teacher_id=class.teacher_id")->result_array(); 
          foreach($data as $row){
                echo '<option value="'.$row['teacher_id'].'">'.$row['name'].' ['.$row['cname'].'] </option>';
            }
        }else{
           $data = $this->db->get($user_type)->result_array();
            foreach($data as $row){
                echo '<option value="'.$row[$user_type.'_id'].'">'.$row['name'].'</option>';
            }
        }
    }
    
    function get_stockitem($stock_id){
        $data = $this->db->where('stockitem_id',$stock_id)->get('school_stock_items')->result_array();
        foreach($data as $row){
            echo '<option value="'.$row[$stock_id].'">'.$row['total_quantity'].'</option>';
        }
    }
    
  //################################################# daily_attendance_report ##########################################################//
    function daily_attendance_report(){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh'); 
           
            $page_data['page_name']  = 'daily_attendance_report';
            $page_data['page_title'] =get_phrase('daily_attendance_report');
            $this->load->view('backend/index', $page_data);         
    }    
    function daily_attendance_report_print(){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh'); 
           
            $this->load->view('backend/admin/daily_attendance_report_print');         
    }    
     //################################################# monthly_attendance_report ##########################################################//
    function attendance_monthly_report(){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh'); 
           
            $page_data['page_name']  = 'attendance_monthly_report';
            $page_data['page_title'] =get_phrase('attendance_monthly_report');
            $this->load->view('backend/index', $page_data);         
    }    
     
     function attendance_monthly_report_view()
     {
         if($this->session->userdata('admin_login')!=1)
            redirect(base_url() , 'refresh');

        $class_name                     = $this->db->get_where('class', array('class_id' => $this->input->post('class_id')))->row()->name;
        $section_name                   = $this->db->get_where('section', array('section_id' =>$this->input->post('section_id') ))->row()->name;
        $page_data['class_id']          = $this->input->post('class_id');
        $page_data['section_id']        = $this->input->post('section_id');
        $page_data['start_date']        = date("Y-m-d",strtotime($this->input->post('start_date')));
        $page_data['end_date']          = date("Y-m-d",strtotime($this->input->post('end_date')));
        $page_data['sessional_year']    = $this->input->post('sessional_year');
        $page_data['page_name']         = 'attendance_month_report';
        $page_data['page_title']        = get_phrase('attendance_report_of_class') . ' ' . $class_name . ' : ' . get_phrase('section') . ' ' . $section_name;
        $this->load->view('backend/index', $page_data);
     }
      
     //################################################# Xlsx reports ##########################################################//
    
    function daily_attendance_excel_report()
    { 
         if ($this->session->userdata('admin_login') != 1)
            redirect(base_url(), 'refresh');
        $this->load->library('excel');
       
        $html = $this->load->view('backend/admin/daily_attendance_excel_report', 1, true);
      
        // Put the html into a temporary file
        $tmpfile = time().'.html';
        file_put_contents($tmpfile, $html);

        // Read the contents of the file into PHPExcel Reader class
        $reader = new PHPExcel_Reader_HTML; 
        $content = $reader->load($tmpfile); 
        
        $filename= "daily_attendance_report_".date('d-m-Y').".xlsx";
        
        header('Content-Type: application/vnd.ms-excel'); 
 
        header('Content-Disposition: attachment;filename="'.$filename.'"'); 
 
        header('Cache-Control: max-age=0');
        
        // Pass to writer and output as needed
        $objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
        
        $objWriter->save('php://output');
        
        // Delete temporary file
        unlink($tmpfile);
    }
    
    function attendance_excel_report_view($class_id ='' , $section_id = '' , $month = '', $sessional_year = '') {
          if ($this->session->userdata('admin_login') != 1)
            redirect(base_url(), 'refresh');
         
        $this->load->library('excel');        

        $page_data['class_id']          = $class_id;
        $page_data['section_id']        = $section_id;
        $page_data['month']             = $month;
        $page_data['sessional_year']    = $sessional_year;
        $html = $this->load->view('backend/admin/attendance_excel_report_view' , $page_data, true);
        if($month == 1) $m = 'January';
        else if($month == 2) $m='February';
        else if($month == 3) $m='March';
        else if($month == 4) $m='April';
        else if($month == 5) $m='May';
        else if($month == 6) $m='June';
        else if($month == 7) $m='July';
        else if($month == 8) $m='August';
        else if($month == 9) $m='Sepetember';
        else if($month == 10) $m='October';
        else if($month == 11) $m='November';
        else if($month == 12) $m='December';
        
         // Put the html into a temporary file
        $tmpfile = time().'.html';
        file_put_contents($tmpfile, $html);

        // Read the contents of the file into PHPExcel Reader class
        $reader = new PHPExcel_Reader_HTML; 
        $content = $reader->load($tmpfile); 
        
        $filename= "attendance_report_of_".$m."_".$sessional_year.".xlsx";
        
        header('Content-Type: application/vnd.ms-excel'); 
 
        header('Content-Disposition: attachment;filename="'.$filename.'"'); 
 
        header('Cache-Control: max-age=0');
        
        // Pass to writer and output as needed
        $objWriter = PHPExcel_IOFactory::createWriter($content, 'Excel2007');
        
        $objWriter->save('php://output');
        
        // Delete temporary file
        unlink($tmpfile);
         
    }
    
    function getclasswisebarcode($class_id)
    {
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
            
		$page_data['class_id'] 	= $class_id;
		$this->load->view('backend/admin/classwisebarcode', $page_data);
    }
    
    public function library_report($param1 = '', $param2 = '', $param3 = ''){
        if($param1=="view"){
            $page_data['page_name']  = 'library_report';
            $page_data['page_title'] =get_phrase('school_stock_register');
            $this->load->view('backend/index', $page_data);    
        }    
    }
    
    
    //==========================perodic_test========================//
    function perodic_test($param1="",$param2=""){
       if($param1=="create"){
           $ptest=$this->db->get_where("periodicTest",array("class_id" =>  $this->input->post("class_id"),'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description))->result_array();
           if($ptest == NULL){    
            $data=array(
                 "name" =>  $this->input->post("name"),
                 "class_id" =>  $this->input->post("class_id"),
                 'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
                );
            $this->db->insert("periodicTest",$data);
            $id=$this->db->insert_id();
            $data["periodicTest_id"]=$id;
            $data['page_name']  = 'manage_periodic_test';
            $data['page_title'] =get_phrase('perodic_test');
            $this->load->view('backend/index', $data);  
           }else{
              $this->session->set_flashdata('flash_error' , get_phrase('test_already_created'));
              redirect(site_url('admin/perodic_test/view'), 'refresh');
           }
        }
         if ($param1 == 'edit') {
            $data['name']= $this->input->post("name");
            $id=$this->db->update("periodicTest",$data,array("periodicTest_id" => $param2 ));         
            $this->session->set_flashdata('flash_message' , get_phrase('data_updated_successfully'));
            redirect(site_url('admin/perodic_test/view'), 'refresh');
        }
        if($param1=="view")
        { 
            $page_data['perodic_test']=$this->db->get_where("periodicTest",array('year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description))->result_array();
            $page_data['page_name']  = 'perodic_test';
            $page_data['page_title'] =get_phrase('perodic_test');
            $this->load->view('backend/index', $page_data);  
        }
    }
    function manage_periodic_test($param1="",$param2=""){
        if($param1=="view")
        { 
            $page_data['page_name']  = 'manage_periodic_test';
            $page_data['page_title'] =get_phrase('manage_periodic_test');
            $this->load->view('backend/index', $page_data);  
        }
        if($param1 =="create"){
            
           $pptest["max_marks"]=$this->input->post("ppmaxmarks");
           $pptest["reduce_marks"]=$this->input->post("reduce_marks");
           
           $ne["neName1"]=$this->input->post("neName1");
           $ne["neName2"]=$this->input->post("neName2");
           $ne["neName3"]=$this->input->post("neName3");
           $ne["neName4"]=$this->input->post("neName4");
           $ne["neMaxmarks1"]=$this->input->post("neMaxmarks1");
           $ne["neMaxmarks2"]=$this->input->post("neMaxmarks2");
           $ne["neMaxmarks3"]=$this->input->post("neMaxmarks3");
           $ne["neMaxmarks4"]=$this->input->post("neMaxmarks4");    
           
           $sea["seaName1"]=$this->input->post("seaName1");
           $sea["seaName2"]=$this->input->post("seaName2");
           $sea["seaName3"]=$this->input->post("seaName3");
           $sea["seaName4"]=$this->input->post("seaName4");
           $sea["seaMaxmarks1"]=$this->input->post("seaMaxmarks1");
           $sea["seaMaxmarks2"]=$this->input->post("seaMaxmarks2");
           $sea["seaMaxmarks3"]=$this->input->post("seaMaxmarks3");
           $sea["seaMaxmarks4"]=$this->input->post("seaMaxmarks4");    
           
           $data=array(
               "PP_TEST" => json_encode($pptest),
               "NE" =>  json_encode($ne),
               "SEA" =>  json_encode($sea)
               );
           $this->db->update('periodicTest',$data,array("periodicTest_id" => $this->input->post("periodicTest_id")));
           $this->session->set_flashdata('flash_message' , get_phrase('test_created_successfully'));
           redirect(site_url('admin/perodic_test/view'), 'refresh');
        }
        if($param1=="edit"){
            $page_data['periodic_test']=$this->db->get_where("periodicTest",array('periodicTest_id' => $param2))->result_array();
            $page_data['page_name']  = 'modal_edit_manage_periodic_test';
            $page_data['page_title'] =get_phrase('upadate_perodic_test');
            $this->load->view('backend/index', $page_data);  
        }
        if($param1=="update"){
            
           $pptest["max_marks"]=$this->input->post("ppmaxmarks");
           $pptest["reduce_marks"]=$this->input->post("reduce_marks");
           
           $ne["neName1"]=$this->input->post("neName1");
           $ne["neName2"]=$this->input->post("neName2");
           $ne["neName3"]=$this->input->post("neName3");
           $ne["neName4"]=$this->input->post("neName4");
           $ne["neMaxmarks1"]=$this->input->post("neMaxmarks1");
           $ne["neMaxmarks2"]=$this->input->post("neMaxmarks2");
           $ne["neMaxmarks3"]=$this->input->post("neMaxmarks3");
           $ne["neMaxmarks4"]=$this->input->post("neMaxmarks4");    
           
           $sea["seaName1"]=$this->input->post("seaName1");
           $sea["seaName2"]=$this->input->post("seaName2");
           $sea["seaName3"]=$this->input->post("seaName3");
           $sea["seaName4"]=$this->input->post("seaName4");
           $sea["seaMaxmarks1"]=$this->input->post("seaMaxmarks1");
           $sea["seaMaxmarks2"]=$this->input->post("seaMaxmarks2");
           $sea["seaMaxmarks3"]=$this->input->post("seaMaxmarks3");
           $sea["seaMaxmarks4"]=$this->input->post("seaMaxmarks4");    
           
           $data=array(
               "PP_TEST" => json_encode($pptest),
               "NE" =>  json_encode($ne),
               "SEA" =>  json_encode($sea)
               );
           $this->db->update('periodicTest',$data,array("periodicTest_id" => $this->input->post("periodicTest_id")));
           $this->session->set_flashdata('flash_message' , get_phrase('test_created_successfully'));
           redirect(site_url('admin/perodic_test/view'), 'refresh');
        }    
    }
    //==================================Alumini Student==================================================//
    function alumini_student($param1="",$param2=""){
        if($param1 == "create"){
            if($this->input->post('name') != null){
                $data['name']    = $this->input->post('name');
            }
            if($this->input->post('parents_name') != null){
                $data['parents_name']    = $this->input->post('parents_name');
            }
            if($this->input->post('student_code') != null){
                $data['student_code'] = $this->input->post('student_code');
                $code_validation = code_validation_insert($data['student_code']);
                if(!$code_validation) {
                    $this->session->set_flashdata('error_message' , get_phrase('this_id_no_is_not_available'));
                    redirect(site_url('admin/alumini_student/view'), 'refresh');
                }
            }
            if($this->input->post('phone') != null){
                $data['phone']    = $this->input->post('phone');
            }
            if($this->input->post('email') != null){
                $data['email']    = $this->input->post('email');
            }
            if($this->input->post('education') != null){
                $data['education']    = $this->input->post('education');
            }
            if($this->input->post('education_details') != null){
                $data['education_details']    = $this->input->post('education_details');
            }
            if($this->input->post('job_studying') != null){
                $data['job_studying']    = $this->input->post('job_studying');
            }
            if($this->input->post('job_studying') == "job"){
                if($this->input->post('job_location') != null){
                    $data['job_location']    = $this->input->post('job_location');
                }
                if($this->input->post('job_type') != null){
                    $data['job_type']    = $this->input->post('job_type');
                }
            }else{
                if($this->input->post('studying_location') != null){
                $data['studying_location']    = $this->input->post('studying_location');
                }
                if($this->input->post('studying_class') != null){
                    $data['studying_class']    = $this->input->post('studying_class');
                }  
            }
            $validation = email_validation($data['email']);
            if($validation == 1) {
            
            $this->db->insert('student', $data);
            $student_id = $this->db->insert_id();
            
            $data2['student_id']     = $student_id;
            $data2['enroll_code']    = substr(md5(rand(0, 1000000)), 0, 7);
            
            if($this->input->post("class_id") != null){
                $data2['class_id']    = $this->input->post('class_id');
            }
            if($this->input->post('section_id') != null){
                $data2['section_id']    = $this->input->post('section_id');
            }
            if ($this->input->post('roll') != '') {
                    $data2['roll']           = $this->input->post('roll');
            }
                $data2['date_added']     = strtotime(date("Y-m-d H:i:s"));
            if($this->input->post("running_year")!= null)
            {
                $data2['year']  = $this->input->post("running_year");
            }
            $this->db->insert('enroll', $data2);
            $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            }else{
                $this->session->set_flashdata('error_message' , get_phrase('this_email_id_is_not_available'));     
            }
            redirect(site_url('admin/alumini_student/view'), 'refresh');
        }
        if($param1=="view"){
            
            $page_data['alumini_student'] = $this->db->get_where("enroll",array("class_id"=>"13"))->result_array();
            $page_data['page_name']  = 'alumini_student';
            $page_data['page_title'] =get_phrase('alumini_student');
            $this->load->view('backend/index', $page_data); 
        }
    }
    
     /**********MANAGE CLASS TIMING********************/
     
     function add_class_timing($param1 = '', $param2 = '' ,$param3 = '')
     {
         if($this->session->userdata('admin_login') != 1)
         redirect(site_url('login'),'refresh');
         if ($param1 == 'create') {

            if($this->input->post('class_id') != null){
               $data['class_id']       = $this->input->post('class_id');
            }

            $data['section_id']     = $this->input->post('section_id');
            //$data['subject_id']     = $this->input->post('subject_id');

            // 12 AM for starting time
            if ($this->input->post('time_start') == 12 && $this->input->post('starting_ampm') == 1) {
                $data['time_start'] = 24;
            }
            // 12 PM for starting time
            else if ($this->input->post('time_start') == 12 && $this->input->post('starting_ampm') == 2) {
                $data['time_start'] = 12;
            }
            // otherwise for starting time
            else{
                $data['time_start']     = $this->input->post('time_start') + (12 * ($this->input->post('starting_ampm') - 1));
            }
            // 12 AM for ending time
            if ($this->input->post('time_end') == 12 && $this->input->post('ending_ampm') == 1) {
                $data['time_end'] = 24;
            }
            // 12 PM for ending time
            else if ($this->input->post('time_end') == 12 && $this->input->post('ending_ampm') == 2) {
                $data['time_end'] = 12;
            }
            // otherwise for ending time
            else{
                $data['time_end']       = $this->input->post('time_end') + (12 * ($this->input->post('ending_ampm') - 1));
            }

            $data['time_start_min'] = $this->input->post('time_start_min');
            $data['time_end_min']   = $this->input->post('time_end_min');
            $data['day']            = $this->input->post('day');
            $data['year']           = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            // checking duplication
            $array = array(
               'section_id'    => $data['section_id'],
               'class_id'      => $data['class_id'],
               'time_start'    => $data['time_start'],
               'time_end'      => $data['time_end'],
               'time_start_min'=> $data['time_start_min'],
               'time_end_min'  => $data['time_end_min'],
               'day'           => $data['day'],
               'year'          => $data['year']
            );
            $validation = duplication_of_class_routine_on_create($array);
            if ($validation == 1) {
                $this->db->insert('class_routine', $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_added_successfully'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('time_conflicts'));
            }

            redirect(site_url('admin/add_class_timing'), 'refresh');
        }
        if ($param1 == 'do_update') {
            $data['class_id']       = $this->input->post('class_id');
            if($this->input->post('section_id') != '') {
                $data['section_id'] = $this->input->post('section_id');
            }
            //$data['subject_id']     = $this->input->post('subject_id');

            // 12 AM for starting time
            if ($this->input->post('time_start') == 12 && $this->input->post('starting_ampm') == 1) {
                $data['time_start'] = 24;
            }
            // 12 PM for starting time
            else if ($this->input->post('time_start') == 12 && $this->input->post('starting_ampm') == 2) {
                $data['time_start'] = 12;
            }
            // otherwise for starting time
            else{
                $data['time_start']     = $this->input->post('time_start') + (12 * ($this->input->post('starting_ampm') - 1));
            }
            // 12 AM for ending time
            if ($this->input->post('time_end') == 12 && $this->input->post('ending_ampm') == 1) {
                $data['time_end'] = 24;
            }
            // 12 PM for ending time
            else if ($this->input->post('time_end') == 12 && $this->input->post('ending_ampm') == 2) {
                $data['time_end'] = 12;
            }
            // otherwise for ending time
            else{
                $data['time_end']       = $this->input->post('time_end') + (12 * ($this->input->post('ending_ampm') - 1));
            }

            $data['time_start_min'] = $this->input->post('time_start_min');
            $data['time_end_min']   = $this->input->post('time_end_min');
            $data['day']            = $this->input->post('day');
            $data['year']           = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
            if ($data['subject_id'] != '') {
            // checking duplication
            $array = array(
               'section_id'    => $data['section_id'],
               'class_id'      => $data['class_id'],
               'time_start'    => $data['time_start'],
               'time_end'      => $data['time_end'],
               'time_start_min'=> $data['time_start_min'],
               'time_end_min'  => $data['time_end_min'],
               'day'           => $data['day'],
               'year'          => $data['year']
            );
            $validation = duplication_of_class_routine_on_edit($array, $param2);

            if ($validation == 1) {
                $this->db->where('class_routine_id', $param2);
                $this->db->update('class_routine', $data);
                $this->session->set_flashdata('flash_message' , get_phrase('data_updated'));
            }
            else{
                $this->session->set_flashdata('error_message' , get_phrase('time_conflicts'));
            }
          }
          else{
            $this->session->set_flashdata('error_message' , get_phrase('subject_is_not_found'));
          }

            redirect(site_url('admin/add_class_timing/' . $data['class_id']), 'refresh');
        }
        else if ($param1 == 'edit') {
            $page_data['edit_data'] = $this->db->get_where('class_routine', array(
                'class_routine_id' => $param2
            ))->result_array();
        }
        if ($param1 == 'delete') {
            $class_id = $this->db->get_where('class_routine' , array('class_routine_id' => $param2))->row()->class_id;
            $this->db->where('class_routine_id', $param2);
            $this->db->delete('class_routine');
            $this->session->set_flashdata('flash_message' , get_phrase('data_deleted'));
            redirect(site_url('admin/add_class_timing/' . $class_id), 'refresh');
        }
         $page_data['page_name'] = 'add_class_timing';
         $page_data['page_title'] = get_phrase('manage_class_time');
         $this->load->view('backend/index',$page_data);
     }
     
    function get_class_section_selection($class_id)
    {
        $page_data['class_id'] = $class_id;
        $this->load->view('backend/admin/class_routine_section_selector' , $page_data);
    }  
    
        
    //===========================================Report===========================================//
    /*  Fine Amount of Student*/
    
    function fine_amount($param1 = "",$param2 = ""){
        if ($this->session->userdata('admin_login') != 1)
            redirect(site_url('login'), 'refresh');
            
        if($param1 == "monthly_report")
        {
            $month = $this->input->post('month');
            $year = $this->input->post('year');
            $pagedata['monthly_report'] = $this->Library_model->monthly_report($year,$month);
            echo $this->load->view('backend/librarian/monthly_report_fine_amount_page',$pagedata,true);
        }
        if($param1 == "view"){
            $page_data['total_fine_amount'] = $this->Library_model->total_fine_amount();
            $page_data['page_name']  = 'fine_amount';
            $page_data['page_title'] =get_phrase('fine_amount_report');
            $this->load->view('backend/index',$page_data);
        }
    }
    
    /* Monthly Book Issued Details */
    
    function monthly_book_issued($param1 = "",$param2 = "")
    {
        if($param1 == "view"){
            $page_data['total_book_issued'] = $this->Library_model->total_book_issued();
            $page_data['page_name']  = 'monthly_book_issued';
            $page_data['page_title'] =get_phrase('monthly_issued_book_report');
            $this->load->view('backend/index',$page_data);
        }
    }
    
    function attendance_report_details($param1= "",$param2=""){
        if($param1 == "view"){
            $page_data['page_name']  = 'attendance_report_details';
            $page_data['page_title'] =get_phrase('attendance_report_details');
            $this->load->view('backend/index',$page_data);
        }
    }
    
    function attendance_report_value()
    {   if($this->input->post('class_id') == '' || $this->input->post('sessional_year') == '') {
            $this->session->set_flashdata('error_message' , get_phrase('please_make_sure_class_and_sessional_year_are_selected'));
            redirect(site_url('admin/attendance_report'), 'refresh');
        }
        $data['class_id']       = $this->input->post('class_id');
        $data['section_id']     = $this->input->post('section_id');
        $data['student_id']      = $this->input->post('student_id');
        $data['sessional_year'] = $this->input->post('sessional_year');
        
        redirect(site_url('admin/attendance_value_report/' . $data['class_id'] . '/' . $data['section_id'] . '/' . $data['student_id'] . '/' . $data['sessional_year']), 'refresh');
    }

    function attendance_value_report($class_id = '', $section_id = '', $student_id = '', $sessional_year = '')
     {
         if($this->session->userdata('admin_login')!=1)
            redirect(base_url() , 'refresh');

        $class_name                     = $this->db->get_where('class', array('class_id' => $class_id))->row()->name;
        $section_name                   = $this->db->get_where('section', array('section_id' => $section_id))->row()->name;
        $page_data['class_id']          = $class_id;
        $page_data['section_id']        = $section_id;
        $page_data['student_id']     = $student_id;
        $page_data['sessional_year']    = $sessional_year;
        $page_data['sort_attendance']          =   $this->db->query("SELECT SUM(`status`) as persent, MONTHNAME(`timestamp`) as month FROM `attendance` WHERE `class_id` = '".$class_id."' AND `section_id` = '".$section_id."' AND `student_id` = '".$student_id."' AND `year` = '".$sessional_year."' AND `status` = '1' GROUP BY MONTH(`timestamp`), YEAR(`timestamp`)")->result_array();
        $page_data['page_name']         = 'attendance_value_report';
        $page_data['page_title']        = get_phrase('attendance_value_report') . ' ' . $class_name . ' : ' . get_phrase('section') . ' ' . $section_name;
        $this->load->view('backend/index', $page_data);
     }
     
    /*  Class wise monthly attendance report*/
    
    function class_attendance_report($param1="",$param2="")
    {
        if($param1 == "view"){
            $page_data['page_name']  = 'class_attendance_report';
            $page_data['page_title'] =get_phrase('class_attendance_report');
            $this->load->view('backend/index',$page_data);
        }
    }
    
    function class_attendance_value()
    {
           if($this->input->post('month') == '' || $this->input->post('sessional_year') == '') {
            $this->session->set_flashdata('error_message' , get_phrase('please_make_sure_class_and_sessional_year_are_selected'));
            redirect(site_url('admin/attendance_report'), 'refresh');
        }
        $data['month']       = $this->input->post('month');
        $data['sessional_year'] = $this->input->post('sessional_year');
        redirect(site_url('admin/class_attendance_value_report/' . $data['month']  . '/' . $data['sessional_year']), 'refresh');
    
    }
    
    function class_attendance_value_report($month = '', $sessional_year = '')
     {
         if($this->session->userdata('admin_login')!=1)
            redirect(base_url() , 'refresh');
            $running_year = $this->db->get_where('settings' , array(
            'type' => 'running_year'
        ))->row()->description;
            
        $page_data['month']     = $month;
        $page_data['sessional_year']    = $sessional_year;
        $page_data['sort_attendance']          =   $this->db->query("SELECT SUM(attend.`status`) as persent, class.name FROM `attendance` as attend JOIN class ON class.class_id = attend.class_id WHERE attend.`year` = '".$running_year."' AND attend.`status` = '1' AND attend.`timestamp` LIKE '".$sessional_year."-".$month."%' GROUP BY attend.class_id")->result_array();
        $page_data['page_name']         = 'class_attendance_value_report';
        $page_data['page_title']        = get_phrase('class_attendance_report');
        $this->load->view('backend/index', $page_data);
     }
}

MMCT - 2023