<?php

class SPW_Notification_Model extends CI_Model
{
    public $id; 
    public $from; 
    public $to_project;
    public $to_user; 
    public $subject;
    public $body; 
    public $type;
    public $is_read_flag;
    public $datetime;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('spw_user_model');
        $this->load->model('spw_project_model');
    }

    public function get_total_notifications($user_id)
    {
        $query = $this->db
                      ->where('to_user', $user_id)
                      ->where('is_read_flag',0)
                      ->get('spw_notification');

        return $query->num_rows();
    }

    public function get_notifications_to_user($user_id)
    {
        $query = $this->db
                      ->where('to_user', $user_id)
                      ->where('is_read_flag',0)
                      ->get('spw_notification');

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return false; 
        }
    }

    public function get_notification_by_id($notification_id)
    {
        $notification = new SPW_Notification_Model();
        $query = $this->db
                      ->where('id', $notification_id)
                      ->get('spw_notification');

        if($query->num_rows() > 0)
        {
            $notification->from = $query->row()->from; 
            $notification->to_project = $query->row()->to_project;
            $notification->to_user = $query->row()->to_user;
            $notification->subject = $query->row()->subject;
            $notification->body = $query->row()->body;
            $notification->type = $query->row()->type;
            $notification->is_read_flag = $query->row()->is_read_flag;

            return $notification;
        }
        else
        {
            return null;
        }              
   
    }

    public function get_active_join_notification_for_project_from_user($from_user, $project_id)
    {
         $query = $this->db
                      ->where('from', $from_user)
                      ->where('to_project', $project_id)
                      ->where('is_read_flag', 0)
                      ->get('spw_notification');

        if($query->num_rows() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public function set_notification_to_read($notificationId)
    {
        $data = array(
            'is_read_flag' => 1
        );

        $this->db->where('id',$notificationId);
        $this->db->update('spw_notification', $data);
    }

    public function set_join_notification_to_read($from_user, $to_user, $to_project)
    {

        $query = $this->db
                     ->where('from', $from_user)
                     ->where('to_user', $to_user)
                     ->where('to_project', $to_project)
                     ->where('type', 'join')
                     ->select('id')
                     ->get('spw_notification'); 


        if($query->num_rows() > 0)
        {

            $this->set_notification_to_read($query->row()->id);
        }
        else
        {
            return false;
        }
    }

    public function create_leave_notification_for_user($from_user_id, $to_user_id, $project_id)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);

        $fullname = $this->spw_user_model->get_fullname($from_user_id);
        $data = array(
                    'from' => $from_user_id,
                    'to_user'  => $to_user_id,
                    'to_project'  => $project_id,
                    'body'      => $fullname." left your project ".$project_title,
                    'type'    => 'leave',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

    public function create_join_notification_for_user($from_user_id, $to_user_id, $project_id)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);
        $fullname = $this->spw_user_model->get_fullname($from_user_id);


        $data = array(
                    'from' => $from_user_id,
                    'to_user' => $to_user_id,
                    'to_project'  => $project_id,
                    'body'    => $fullname." has joined your project ".$project_title,
                    'type'    => 'join',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

    public function create_join_approved_notification_for_user($from_user_id, $to_user_id, $project_id)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);

        $fullname = $this->spw_user_model->get_fullname($from_user_id);
        $data = array(
                    'from' => $from_user_id,
                    'to_user' => $to_user_id,
                    'to_project'  => $project_id,
                    'body'    => "Congratulations! You have been accepted by ".$fullname." to join the project ".$project_title,
                    'type'    => 'join_approved',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

    public function create_change_project_notification_for_user($approver_user, $approved_user, $project_id)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);

        $fullname = $this->spw_user_model->get_fullname($approver_user);
        $data = array(
                    'from' => $approver_user,
                    'to_user' => $approved_user,
                    'to_project'  => $project_id,
                    'body'    => "Congratulations! You have been accepted by ".$fullname." to join the project ".$project_title. "<br/>Please click \"Accept\" if you want to leave your current ptoject and join this one. Click \"Reject\" otherwise",
                    'type'    => 'change_project',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

    public function create_change_project_accepted_notification_for_project($approver_user,$approved_user, $project_id)
    {
         $team_members = $this->spw_project_model->get_team_members($project_id);
         for($i = 0; $i < count($team_members); $i++) {
            if($team_members[$i] != $approved_user)
                $this->create_change_project_accepted_notification_for_user($approved_user, $team_members[$i], $project_id);
         }
    }

    private function create_change_project_accepted_notification_for_user($approved_user, $to_user, $project_id)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);

        $fullname = $this->spw_user_model->get_fullname($approved_user);
        $data = array(
                    'from' => $approved_user,
                    'to_user' => $to_user,
                    'to_project'  => $project_id,
                    'body'    => $fullname." joined your project",
                    'type'    => 'join_approved',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

    public function create_join_rejected_notification_for_user($from_user_id, $to_user_id, $project_id)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);

        $fullname = $this->spw_user_model->get_fullname($from_user_id);
        $data = array(
                    'from' => $from_user_id,
                    'to_user' => $to_user_id,
                    'to_project'  => $project_id,
                    'body'    =>  "Your request to join the project ".$project_title. "has been denied by".$fullname,
                    'type'    => 'join_rejected',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

//    public function create_professor_approval_project($from_user, $new_project_id)
//    {
//        $professor_id = $this->spw_user_model->get_head_professor();
//        $project_title = $this->spw_project_model->get_project_title($new_project_id);
//
//        $data = array(
//                    'from'    => $from_user,  
//                    'to_user' => $professor_id,
//                    'to_project'  => $new_project_id,
//                    'body'    =>  $project_title." needs your approval",
//                    'type'    => 'professor_approval',
//                    'datetime' => date("Y-m-d H:i:s", time())
//                    );
//
//        $this->db->insert('spw_notification',$data);
//    }

     public function create_professor_deleted_project_notification($project_id , $professor)
     {
         //$professor = $this->spw_user_model->get_head_professor();

         $team_members = $this->spw_project_model->get_team_members($project_id);

         for($i = 0; $i < count($team_members); $i++) {
            if($team_members[$i] != $professor)
                $this->create_professor_deleted_project_notification_to_user( $team_members[$i], $project_id , $professor);
         }
     }   
      private function create_professor_deleted_project_notification_to_user($user_id, $project_id , $professor)
      {
        $project_title = $this->spw_project_model->get_project_title($project_id);        
        $professor_fullname = $this->spw_user_model->get_fullname($professor);
         
        //send email
         $email = $this->spw_user_model->getUserInfo($user_id);
         $email = $email->email;
         

         $base_url = $this->config->base_url();
         
        $message = '<html >  <head><title></title></head>
                        <body>
                            <h2>Project Deleted!! </h2>
                            <p>  Project "' . $project_title .'" has been deleted by ' . $professor_fullname . '.</p>
                            <p><a href="' . $base_url . '">SeniorProjectWebsite</a></p>
                        </body>
                    </html>';

          send_email($this, $email, 'Senior Project Website: Project deleted', $message);
        
//        $data = array(
//                    'from' => $professor,
//                    'to_user' => $user_id,
//                    'to_project'  => $project_id,
//                    'body'    =>  "Your project ".$project_title." has been deleted by professor ".$professor_fullname,
//                    'type'    => 'professor_project_deleted',
//                     'datetime' => date("Y-m-d H:i:s", time())
//                    );

//        $this->db->insert('spw_notification',$data);
    }
    
    public function create_professor_approval_rejected_notification($project_id , $professor)
    {
         //$professor = $this->spw_user_model->get_head_professor();

         $team_members = $this->spw_project_model->get_team_members($project_id);

         for($i = 0; $i < count($team_members); $i++) {
            if($team_members[$i] != $professor)
                $this->create_professor_approval_rejected_notification_to_user( $team_members[$i], $project_id , $professor);
         }
    }   
     private function create_professor_approval_rejected_notification_to_user($user_id, $project_id , $professor)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);        
        $professor_fullname = $this->spw_user_model->get_fullname($professor);

        $data = array(
                    'from' => $professor,
                    'to_user' => $user_id,
                    'to_project'  => $project_id,
                    'body'    =>  'Your project "'.$project_title. '" has been rejected by professor '.$professor_fullname,
                    'type'    => 'professor_approval_rejected',
                     'datetime' => date("Y-m-d H:i:s", time())
                    );

        $this->db->insert('spw_notification',$data);
         //send email        
         $email = $this->spw_user_model->getUserInfo($user_id);
         $email = $email->email;
         
         $base_url = $this->config->base_url();
         
        $message = '<html >  <head><title></title></head>
                        <body>
                            <h2>Project Rejected!! </h2>
                            <p>  Project "' . $project_title .'" has been rejected by ' . $professor_fullname . '.</p>
                            <p><a href="' . $base_url . '">SeniorProjectWebsite</a></p>
                        </body>
                    </html>';

          send_email($this, $email, 'Senior Project Website: Project Rejected', $message);
    }
    
     public function create_professor_approval_approved_notification($project_id)
    {
        $professor = $this->spw_user_model->get_head_professor();

         $team_members = $this->spw_project_model->get_team_members($project_id);
         for($i = 0; $i < count($team_members); $i++) {
             if($team_members[$i] != $professor)
                $this->create_professor_approval_approved_notification_to_user( $team_members[$i], $project_id);
         }
    }    
     private function create_professor_approval_approved_notification_to_user($to_user_id, $project_id)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);
        $professor_id = $this->spw_user_model->get_head_professor();
        $fullname = $this->spw_user_model->get_fullname($professor_id);
        $data = array(
                    'from' => $professor_id,
                    'to_user' => $to_user_id,
                    'to_project'  => $project_id,
                    'body'    =>  "Congratulations! Your project ".$project_title." has been approved by the professor",
                    'type'    => 'professor_approval_approved',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

        $this->db->insert('spw_notification',$data);
        //send email        
         $email = $this->spw_user_model->getUserInfo($to_user_id);
         $email = $email->email;     
         
         $base_url = $this->config->base_url();
         
        $message = '<html >  <head><title></title></head>
                        <body>
                            <h2>Project Approved!! </h2>
                            <p>  Project "' . $project_title .'" has been approved by ' . $fullname . '.</p>
                            <p><a href="' . $base_url . '">SeniorProjectWebsite</a></p>
                        </body>
                    </html>';

          send_email($this, $email, 'Senior Project Website: Project Approval', $message);
    }

    public function create_professor_pending_approval_notification($user_id, $project_id)
    {
        $project_title = $this->spw_project_model->get_project_title($project_id);
        $professor_id = $this->spw_user_model->get_head_professor();
        $professor_fullname = $this->spw_user_model->get_fullname($professor_id);
        
        $data = array(
                    'from' => $user_id,
                    'to_user' => $professor_id,
                    'to_project'  => $project_id,
                    'body'    =>  'Project "' .$project_title.'" is waiting for your approval.',
                    'type'    => 'professor_approval_pending',
                     'datetime' => date("Y-m-d H:i:s", time())
                    );

        $this->db->insert('spw_notification',$data);
        
        //send email
        $email = $this->spw_user_model->getUserInfo($professor_id);
        $email = $email->email;
        
        $base_url = $this->config->base_url();
        
        $message = '<html >  <head><title></title></head>
                                    <body>
                                        <h2>New project proposed!! </h2>
                                        <p> Project "' . $project_title . '" is waiting for your approval.</p>
                                        <p><a href="' . $base_url . '">SeniorProjectWebsite</a></p>
                                    </body>
                                </html>';

         send_email($this, $email, 'Senior Project Website: New project waiting your approval', $message);
                    
    }

    public function create_member_added_notification_for_project($approver_user, $approved_user, $project_id)
    {
         $team_members = $this->spw_project_model->get_team_members($project_id);

         for($i = 0; $i < count($team_members); $i++) {
            if($team_members[$i] != $approver_user && $team_members[$i] != $approved_user){
                $this->create_member_added_notification_for_user($approver_user, $approved_user, $team_members[$i], $project_id);
            }
                
         }
    }

    private function create_member_added_notification_for_user($approver_user, $approved_user, $user_id, $project_id)
    {

        $approver_user_fullname = $this->spw_user_model->get_fullname($approver_user);
        $approved_user_fullname = $this->spw_user_model->get_fullname($approved_user);
        $project_title = $this->spw_project_model->get_project_title($project_id);

        $data = array(
                    'from'    => $approver_user,  
                    'to_user' => $user_id,
                    'to_project'  => $project_id,
                    'body'    =>  $approved_user_fullname." was approved by ".$approver_user_fullname." to join your project ". $project_title,
                    'type'    => 'member_added',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

    public function create_member_rejected_notification_for_project($approver_user, $approved_user, $project_id)
    {
         $team_members = $this->spw_project_model->get_team_members($project_id);
         for($i = 0; $i < count($team_members); $i++) {
            if($team_members[$i] != $approver_user){
                $this->create_member_rejected_notification_for_user($approver_user, $approved_user, $team_members[$i] , $project_id);
            }
        }
    }

    private function create_member_rejected_notification_for_user($approver_user, $approved_user, $user_id, $project_id)
    {
        $approver_user_fullname = $this->spw_user_model->get_fullname($approver_user);
        $approved_user_fullname = $this->spw_user_model->get_fullname($approved_user);
        $project_title = $this->spw_project_model->get_project_title($project_id);

        $data = array(
                    'from'    => $approver_user,  
                    'to_user' => $user_id,
                    'to_project'  => $project_id,
                    'body'    =>  $approved_user_fullname." was rejected by ".$approver_user_fullname." to join your project ". $project_title,
                    'type'    => 'member_added',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

    public function get_active_notification_for_user($user_id)
    {
        $query = $this->db
                      ->where('to_user', $user_id)
                      ->where('is_read_flag', 0)
                      ->order_by('id', "desc")
                      ->get('spw_notification');

        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return null;
        }
    }

    public function create_invite_project_notification($currentUserId, $invitedUserId, $member_id, $invitedProjectId)
    {
        $project_title = $this->spw_project_model->get_project_title($invitedProject);
        $current_user_fullname  = $this->spw_user_model->get_fullname($currentUserId);
        $invited_user_fullname  = $this->spw_user_model->get_fullname($invitedUserId);


        $data = array(
                    'from'    => $currentUserId,  
                    'to_user' => $member_id,
                    'to_project'  => $invitedProject,
                    'body'    =>  $invited_user_fullname." has been invited by ".$current_user_fullname." to join your project".$project_title,
                    'type'    => 'invite_project',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

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

    public function create_invite_user_notification($currentUserId, $invitedUserId, $invitedProject)
    {
        $project_title = $this->spw_project_model->get_project_title($invitedProject);
        $current_user_fullname  = $this->spw_user_model->get_fullname($currentUserId);
        $invited_user_fullname  = $this->spw_user_model->get_fullname($invitedUserId);


        $data = array(
                    'from'    => $currentUserId,  
                    'to_user' => $invitedUserId,
                    'to_project'  => $invitedProject,
                    'body'    =>  "You have been invited by ". $current_user_fullname ." to join the ".$project_title." project",
                    'type'    => 'invite_user',
                    'datetime' => date("Y-m-d H:i:s", time())
                    );

        $this->db->insert('spw_notification',$data);   
    }
}
    
?>
