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

class Promotion extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->helper('text');
        $this->load->helper(array('form','url'));
        date_default_timezone_set('Asia/Kolkata');
        $this->load->model('SchoolModel');
        $this->load->model('WelcomeModel');
        $this->load->model('NewFeeModel');
        $this->load->model('DeveloperModel');
        $this->load->helper('date');
        $this->load->database();    
        $this->load->library('form_validation');
        $this->load->helper(array('form', 'url'));
        $this->load->library('session');
        $this->load->library('Pdf');
    }
    
    public function index(){
        
    }
    
    public function course_students(){
        $data['success']=''; $data['error']='';
        if(isset($_POST['promote_students'])){
            $userid=$this->session->userdata('tid');
            $new_course_id=$_POST['new_course'];
            $sid_list=$_POST['sid_list'];
            $courses_table='courses';
            $promotion_count=0;
            $duplicate_count=0;
            $course='';
            $new_ac_year=new_academic_year;
            $subject_stream='';
            $section=$_POST['section'];
            $student_course_table='student_courses_data';
            $get_course_info=$this->WelcomeModel->get_item_data($new_course_id,$courses_table);
            if($get_course_info){
                foreach($get_course_info as $course_info){
                    $course=$course_info['course'];
                    $subject_stream=$course_info['subject_stream'];
                }
            }
            $cat=date('Y-m-d H:i:s');
            $date_of_promotion=date('Y-m-d');
            $new_acedemic_session=new_session;
            foreach($sid_list as $list){
                
                $sid= $list;
                $check_student_in_courses_data=$this->WelcomeModel->get_student_in_acedemic_session($sid,$new_acedemic_session);
                if($check_student_in_courses_data){
                    $duplicate_count=$duplicate_count + 1;
                }else{
                    $data_array=array("sid"=>$sid,"acedemic_session"=>$new_acedemic_session,"class"=>$course,"section"=>$section,"subject_stream"=>$subject_stream,"course_id"=>$new_course_id,"ac_year"=>$new_ac_year,"cat"=>$cat,"date_of_promotion"=>$date_of_promotion,"cat_by"=>$userid);
                    $add_student_in_course_data=$this->WelcomeModel->insert_data($data_array,$student_course_table);
                    if($add_student_in_course_data){
                        $promotion_count=$promotion_count + 1;
                    }
                }
            }
        $data['error']=$duplicate_count.' Students Already Exist';
        $data['success']=$promotion_count.' Students Promoted Successfully !';    
        }
        
        $this->load->view('erp/header');
        $this->load->view('erp/sidebar');
        $this->load->view('erp/promotion/course-students',$data);
        $this->load->view('erp/footer');
    }
}