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

class Password extends CI_Controller
{
    function __construct() {

        parent::__construct();

        $this->load->database();
		$this->load->model('usermodel','',TRUE);
		$this->load->model("usermodel", "login",TRUE);
		$this->load->model("Studentprofilemodel", "login",TRUE);

   if(($this->session->userdata('logged_in')=="")) {
            $this->session->set_flashdata('flash_data', 'You don\'t have access!');
            redirect('login');
        }

    }
 	
		


		public function updatepassword()
{
   	$oldpassword = $this->input->post("oldpassword");
	 $newpassword = $this->input->post("newpassword");
     $cnewpassword = $this->input->post("cnewpassword");
 		$id = $this->input->post("id");

 	$this->form_validation->set_rules("oldpassword", "oldpassword", "trim|required");
          	$this->form_validation->set_rules("newpassword", "newpassword ", "trim|required");
          	$this->form_validation->set_rules("cnewpassword", "Confirm Password ", "trim|required");
	$data['errors'] = validation_errors();
          if ($this->form_validation->run() == FALSE)
        {
		$this->load->view("template/headerpage");
		

		$this->load->view('userdata/resetpassword');
		$this->load->view("template/footerpage");

		}else
		{
		
	 		if ($this->input->post('btn_register') == "changepassword")
			{
				
				


   			 if($oldpassword==$newpassword){
       		 //return "false";


            $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong>New  Password cant be the same Old Password</div>');
            redirect('user/passwordform');
	   		 }else
	   			 {
					 
					 if($newpassword!=$cnewpassword){ 
				$this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong>New  Password and confirm password do not match</div>');
            redirect('user/passwordform');	 
					 
					 return false;
					 }
					 		$confirmpassword = $this->usermodel->populateall('user','password',$newpassword);

 if(is_array($confirmpassword)) {
   $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> Your new  Password is rejected. Try a different and strong password</div>');
            redirect('user/passwordform');
			 return false;
 }

					 
	        $this->db->select('*');
	        $this->db->from('user');
	        $this ->db -> where('password', ($oldpassword));
	         $this->db->where('id',$id);
	        $query = $this->db->get();
	        if($query->num_rows()==1)
	        {
	            $data = array(
	                           'password' => ($newpassword),
	                           'recoverpassword' => ($recoverpassword)
	                           //'password' => md5($npass)
                       
                        );
            $this->db->where('id',$id);
             $updated=$this->db->update('user', $data); 


            //return "true";
             if($updated){


   redirect('user/logout');
 					$this->session->set_flashdata('msg', '<div class="alert alert-success fade in">
						  <a href="#" class="close" data-dismiss="alert">&times;</a>
						 
						<strong>Success!</strong> You have successfully changed your password! Just now login with new one</div>');

          
             }else{

             $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> Password was not changed. Try again</div>');
              redirect('user/passwordform');

             }
		 
						
        	}else     
        	 {


            //return "false";


             $this->session->set_flashdata('msg', '<div class="alert alert-danger fade in">
						 <a href="#" class="close" data-dismiss="alert">&times;</a>
						  <strong>Error!</strong> Old Password not match</div>');
              redirect('user/passwordform');
        			}
    		}
	}	}
}
		
		
		
		
		
		
		
 
    public function logout() {
	
        $data = ['id', 'regno'];
        $this->session->unset_userdata('logged_in');
		session_destroy();

  		 redirect('login', 'refresh');
       
    }
}
 