<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Dashboard_model
 *
 * @author mzq163
 */
class Dashboard_model extends CI_Model {

    var $db_sbcc;
    var $status;
    var $message;
    var $response_data = array();

    public function __construct() {
        parent::__construct();
        $this->db_sbcc = $this->load->database('db_temp', true);
        $this->status = false;
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getSingleUserSessionPlayModel($districtId = null) {
        $this->db_sbcc->select('EndUser.singleUserName as name,EndUser.singleModeId,EndUser.singleModeGender,EndUser.createAt,'
                . 'EndUser.singleModeGender as gender,EndUser.sessionId,'
                . 'EndUser.audienceType,'
                . 'UserRegistration.userName as userName');
        $this->db_sbcc->from('EndUser');
        $this->db_sbcc->join('UserRegistration', 'UserRegistration.userId=EndUser.userId', 'INNER');

        if (($districtId !== null and $districtId !== 'all')) {
            $this->db_sbcc->where('UserRegistration.district', $districtId);
        }
        $this->db_sbcc->order_by('EndUser.createAt', 'desc');
        $querydata = $this->getData($this->db_sbcc->get());
        if ($querydata) {
            return $this->getModuleStatus($querydata);
        } else {
            return $querydata;
        }
        //return $this->getModuleStatus($this->getData($this->db_sbcc->get()));
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getSubModuleCountByDistrict($distrcitName = null) {
        if ($distrcitName !== null) {
            $this->db_sbcc->select('SessionDetail.subModuleNo');
            $this->db_sbcc->from('SessionDetail');
            $this->db_sbcc->join('UserRegistration', 'SessionDetail.userId=UserRegistration.userId', 'INNER');
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName));
            $query = $this->getData($this->db_sbcc->get());
            return $query ? count($query) : 0;
        } else {
            return 0;
        }
    }

    public function getUniqueSessionCountByDistrictGroup($distrcitName = null) {
        if ($distrcitName !== null) {
            $this->db_sbcc->select('SessionDetail.sessionId');
            $this->db_sbcc->distinct();
            $this->db_sbcc->from('SessionDetail');
            $this->db_sbcc->join('EndGroupUser', 'SessionDetail.userId=EndGroupUser.userId', 'INNER');

            $this->db_sbcc->join('UserRegistration', 'SessionDetail.userId=UserRegistration.userId', 'INNER');
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName));
            return $this->getData($this->db_sbcc->get());
        } else {
            return false;
        }
    }

    public function getUniqueSessionCountByDistrictSingle($distrcitName = null) {
        if ($distrcitName !== null) {
            $this->db_sbcc->select('SessionDetail.sessionId');
            $this->db_sbcc->distinct();
            $this->db_sbcc->from('SessionDetail');
            $this->db_sbcc->join('EndUser', 'SessionDetail.userId=EndUser.userId', 'INNER');

            $this->db_sbcc->join('UserRegistration', 'SessionDetail.userId=UserRegistration.userId', 'INNER');
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName));
            return $this->getData($this->db_sbcc->get());
        } else {
            return false;
        }
    }

    public function getUniqueModuleIdCountBySessionId($sessionIdList) {
        $countModule = 0;
        foreach ($sessionIdList as $value) {
            $this->db_sbcc->select('SessionDetail.moduleNo');
            $this->db_sbcc->distinct();
            $this->db_sbcc->from('SessionDetail');
            $this->db_sbcc->where('sessionId', $value['sessionId']);
            $uniqueModuleList = $this->getData($this->db_sbcc->get());
            if ($uniqueModuleList) {
                $countModule += count($uniqueModuleList);
            }
        }
        return $countModule;
    }

    public function getCountModuleSubmoduleSesssion($distrcitName = null) {
        $seeionCountGroup = 0;
        $moduleCountGroup = 0;

        $uniqueSessionListGroup = $this->getUniqueSessionCountByDistrictGroup($distrcitName);
        if ($uniqueSessionListGroup) {
            $seeionCountGroup = count($uniqueSessionListGroup);
            $moduleCountGroup = $this->getUniqueModuleIdCountBySessionId($uniqueSessionListGroup);
        }
        $subModuleCountGroup = $this->getSubModuleCountByDistrict($distrcitName);
        
        $seeionCountSingle = 0;
        $moduleCountSingle = 0;

        $uniqueSessionListSingle = $this->getUniqueSessionCountByDistrictSingle($distrcitName);
        if ($uniqueSessionListSingle) {
            $seeionCountSingle = count($uniqueSessionListSingle);
            $moduleCountSingle = $this->getUniqueModuleIdCountBySessionId($uniqueSessionListSingle);
        }
        $subModuleCountSingle = $this->getSubModuleCountByDistrict($distrcitName);
        return array(
            'session_count' => $seeionCountGroup,
            'module_count' => $moduleCountGroup,
            'sub_module_count' => $subModuleCountGroup,
            'session_count_s' => $seeionCountGroup,
            'module_count_s' => $moduleCountGroup,
            'sub_module_count_s' => $subModuleCountSingle
        );
    }

    /**
     * 
     * 
     * 
     */
    public function getSingleDetailCountByAdmin($distrcitName, $adminName) {
        $admin_name = '';
        $boy_count = 0;
        $girl_count = 0;
        $user_count = 0;
        $this->db_sbcc->select('*');
        $this->db_sbcc->from('EndUser');
        $this->db_sbcc->join('UserRegistration', 'UserRegistration.userid=EndUser.userId', 'INNER');
        if ($adminName == 'आशा') {
            $admin_name = 'Asha';
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName, 'UserRegistration.userName' => $adminName));
        } elseif ($adminName == 'ए एन एम') {
            $admin_name = 'Anm';
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName, 'UserRegistration.userName' => $adminName));
        } else {
            $admin_name = 'Other';
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName, 'UserRegistration.userName !=' => 'ए एन एम', 'UserRegistration.userName !=' => 'आशा'));
        }
        $query_data = $this->getData($this->db_sbcc->get());

        if ($query_data) {
            $user_count = count($query_data);
            $malefemale = $this->getMaleFemale($query_data);
            $boy_count = $malefemale['male'];
            $girl_count = $malefemale['female'];
        }

        return array('by' => $admin_name, 'user' => $user_count, 'male' => $boy_count, 'female' => $girl_count);
    }

    private function getMaleFemale($mixedList = null) {
        $maleCount = 0;
        $femaleCount = 0;
        foreach ($mixedList as $value) {
            if ($value['singleModeGender'] == 'महिला') {
                $femaleCount += 1;
            } else {
                $maleCount += 1;
            }
        }
        return array("male" => $maleCount, 'female' => $femaleCount);
    }

    /**
     * 
     * 
     * 
     */
    public function getGroupDetailCountByAdmin($distrcitName, $adminName) {
        $admin_name = '';
        $women_count = 0;
        $men_count = 0;
        $boy_count = 0;
        $girl_count = 0;
        $group_count = 0;
        $this->db_sbcc->select('count(*) as group_admin,SUM(noOfFemale) as women, SUM(noOfMale) as men,SUM(noOfGirl) as girl,SUM(noOfBoy) as boy');
        $this->db_sbcc->from('EndGroupUser');
        $this->db_sbcc->join('UserRegistration', 'UserRegistration.userid=EndGroupUser.userId', 'INNER');
        if ($adminName == 'आशा') {
            $admin_name = 'Asha';
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName, 'UserRegistration.userName' => $adminName));
        } elseif ($adminName == 'ए एन एम') {
            $admin_name = 'Anm';
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName, 'UserRegistration.userName' => $adminName));
        } else {
            $admin_name = 'Other';
            $this->db_sbcc->where(array('UserRegistration.district' => $distrcitName, 'UserRegistration.userName !=' => 'ए एन एम', 'UserRegistration.userName !=' => 'आशा'));
        }
        $query_data = $this->getData($this->db_sbcc->get());

        if ($query_data) {
            $group_count = $query_data[0]['group_admin'] ? $query_data[0]['group_admin'] : 0;
            $women_count = $query_data[0]['women'] ? $query_data[0]['women'] : 0;
            $men_count = $query_data[0]['men'] ? $query_data[0]['men'] : 0;
            $girl_count = $query_data[0]['girl'] ? $query_data[0]['girl'] : 0;
            $boy_count = $query_data[0]['boy'] ? $query_data[0]['boy'] : 0;
        }

        return array('by' => $admin_name, 'group' => $group_count, 'women' => $women_count, 'men' => $men_count, 'boy' => $boy_count, 'girl' => $girl_count);
    }

    /**
     * 
     * 
     * 
     */
    public function getSingleUserSessionCountModel() {

        $countArray = array("user_count" => 0, 'submodule_count' => 0, 'gender' => array('male' => 0, 'female' => 0, 'other' => 0), 'session_count' => 0);
        $this->db_sbcc->select('*');
        $this->db_sbcc->from('EndUser');
        $tempData = $this->getData($this->db_sbcc->get());

        if ($tempData) {
            $countArray['user_count'] = count($tempData);
            $tempUserid = array();
            $tempMaleCount = 0;
            $tempFemaleCount = 0;
            foreach ($tempData as $key => $value) {
                $tempUserid[] = $value['userId'];
                if ($value['singleModeGender'] == 'किशोर' or $value['singleModeGender'] == 'पुरुष') {
                    $tempMaleCount += 1;
                } else {
                    $tempFemaleCount += 1;
                }
            }
            $countArray['gender']['male'] = $tempMaleCount;
            $countArray['gender']['female'] = $tempFemaleCount;
            $this->db_sbcc->select('*');
            $this->db_sbcc->from('SessionDetail');
            $this->db_sbcc->where_in('userId', $tempUserid);
            $tempDataSession = $this->getData($this->db_sbcc->get());
            if ($tempDataSession) {
                $countArray['session_count'] = count($tempDataSession);
                $countArray['submodule_count'] = count($tempDataSession);
            }
        }
        return $countArray;
    }

    /**
     * 
     * 
     * 
     */
    public function getGroupUserSessionCountModel() {
        $countArray = array("group_count" => 0, 'module_count' => 0, 'participant' => array('male' => 0, 'female' => 0), 'session_count' => 0);
        $this->db_sbcc->select('*');
        $this->db_sbcc->from('EndGroupUser');
        $tempData = $this->getData($this->db_sbcc->get());

        if ($tempData) {
            $countArray['group_count'] = count($tempData);
            $tempUserid = array();
            $tempMale = 0;
            $tempFemale = 0;
            foreach ($tempData as $key => $value) {
                $tempUserid[] = $value['userId'];
                $tempMale += $value['noOfMale'] + $value['noOfBoy'];
                $tempFemale += $value['noOfFemale'] + $value['noOfGirl'];
            }
            $countArray['gender']['male'] = $tempMale;
            $countArray['gender']['female'] = $tempFemale;
            $this->db_sbcc->select('*');
            $this->db_sbcc->from('SessionDetail');
            $this->db_sbcc->where_in('userId', $tempUserid);
            $tempDataSession = $this->getData($this->db_sbcc->get());
            if ($tempDataSession) {
                $countArray['session_count'] = count($tempDataSession);
                $countArray['module_count'] = count($tempDataSession);
            }
        }
        return $countArray;
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function getModuleStatus($data) {
        for ($i = 0; $i < count($data); $i++) {
            for ($j = 1; $j < 13; $j++) {
                $data[$i][$j] = 'No';
            }
            $this->db_sbcc->select('*');
            $this->db_sbcc->from('SessionDetail');
            $this->db_sbcc->where(array("sessionId" => $data[$i]['sessionId']));
            $this->db_sbcc->order_by('SessionDetail.createAt', 'desc');
            $temp_data = $this->getData($this->db_sbcc->get());
            if ($temp_data) {
                foreach ($temp_data as $value) {
                    $data[$i][$value['moduleNo']] = "Yes";
                }
            }
        }
        return $data;
    }

    /**
     * 
     * @param type $sessionId
     * 
     * 
     * 
     */
    public function getModuleDetailBySessionIdModel($sessionId = null) {
        if ($sessionId != null) {
            $this->db_sbcc->select('*');
            $this->db_sbcc->from('SessionDetail');
            $this->db_sbcc->where(array("sessionId" => $sessionId));
            $this->db_sbcc->order_by('SessionDetail.createAt', 'desc');
            $temp_data = $this->getData($this->db_sbcc->get());
            if ($temp_data) {
                for ($i = 0; $i < count($temp_data); $i++) {
                    $temp_data[$i]['time'] = date('H:i:s', (strtotime($temp_data[$i]['storyEndTime']) - strtotime($temp_data[$i]['storyStartTime'])));
                }
                return $temp_data;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    public function getGroupUserSessionPlayModel($districtId = null) {
        $this->db_sbcc->select('EndGroupUser.*,UserRegistration.userName as userName');
        $this->db_sbcc->from('EndGroupUser');
        $this->db_sbcc->join('UserRegistration', 'UserRegistration.userId=EndGroupUser.userId', 'INNER');
        if (($districtId !== null and $districtId !== 'all')) {
            $this->db_sbcc->where('UserRegistration.district', $districtId);
        }
        $this->db_sbcc->order_by('EndGroupUser.creatAt', 'desc');
        $querydata = $this->getData($this->db_sbcc->get());
        if ($querydata) {
            return $this->getModuleStatus($querydata);
        } else {
            return $querydata;
        }
    }

    /**
     * 
     * 
     * 
     * 
     */
    private function getData($query = null) {
        if ($query != null and $query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return false;
        }
    }

}
