<?php
/****************************
 * File : common.php
 * Author : Cygnusa
 * @2018
 ****************************/

//SECURITY
if (!defined('APPLICATION_PATH')) {
    exit('No direct script access allowed');
}

class common
{

    //FUNCTION LISTS
    public function __construct($parent)
    {
        //GET PAGE MESSAGE
        $this->page_data = globalfunc::getMessage($parent, $parent->lang, 'common');
        if (count($parent->params) == 0) {
            // $this->dashboardfunc($parent);
        } else {
            $qry_params = explode("?", $parent->params[0]);
            $parameter_val = strtolower($qry_params[0]);

            switch ($parameter_val) {
                case "zone":
                case "state":
                case "city":
                case "dealer":
                case "learning":
                case "show":
                case "salutation":
                case "trainerzoneam":
                case "trainerrm":
                case "traineram":
                case "trainerdealer":

                case "dselist":
                case "amlist":
                case "ilearnuserlist":

                    $this->$parameter_val($parent);
                    break;
                default:
                    break;
            }

        }

        $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
    }

    /* /////////////////////////////// ZONE //////////////////////////////////////////////// */
    private function zone($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $query_str = "SELECT zone FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' " . $cond . " GROUP BY zone ";
        $data_arr = $parent->getQueryValue($query_str);

        //LOG
        $logstr = 'COMMON ZONE';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// STATE //////////////////////////////////////////////// */
    private function state($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $cond = '';

        //ZONE
        if ((isset($parent->_request["zone"]))) {
            $zone = (isset($parent->_request["zone"])) ? escape_str($parent->_request["zone"]) : '';
            if ($zone) {
                $zone_arr = @explode(',', $zone);
                $zone_quotes = array_map("quotes_array", $zone_arr);
                $cond .= " AND zone IN (" . @implode(',', $zone_quotes) . ")";
            } else {
                $cond .= " AND zone IN ('')";
            }
        }
        //TRAINER ID
        if (isset($parent->_request["trainer_id"])) {
            $trainer_id = (isset($parent->_request["trainer_id"])) ? escape_str($parent->_request["trainer_id"]) : '';
            if ($trainer_id) {
                $query_str = "SELECT h.zone FROM " . DMS_HOUSER . " as h JOIN " . TABLE_USER . " as u ON h.emp_code=u.emp_code WHERE u.status='1' AND u.is_deleted='0' AND u.userid='" . $trainer_id . "' AND u.role='5' LIMIT 1";
                $arr = $parent->getQueryValue($query_str);
                if (count($arr) > 0) {
                    $cond .= " AND zone='" . $arr[0]->zone . "'";
                } else {
                    $cond .= " AND zone=''";
                }
            } else {
                //$cond=" AND state IN ('')";
            }
        }

        $query_str = "SELECT state FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' " . $cond . " GROUP BY state LIMIT 2 ";
        $data_arr = $parent->getQueryValue($query_str);

        //LOG
        $logstr = 'COMMON STATE';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// CITY //////////////////////////////////////////////// */
    private function city($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $cond = '';
        //ZONE
        if ((isset($parent->_request["zone"]))) {
            $zone = (isset($parent->_request["zone"])) ? escape_str($parent->_request["zone"]) : '';
            if ($zone) {
                $zone_arr = @explode(',', $zone);
                $zone_quotes = array_map("quotes_array", $zone_arr);
                $cond .= " AND zone IN (" . @implode(',', $zone_quotes) . ")";
            } else {
                $cond .= " AND zone IN ('')";
            }
        }
        //STATE
        if (isset($parent->_request["state"])) {
            $state = (isset($parent->_request["state"])) ? escape_str($parent->_request["state"]) : '';
            if ($state) {
                $state_arr = @explode(',', $state);
                $state_quotes = array_map("quotes_array", $state_arr);
                $cond .= " AND state IN (" . @implode(',', $state_quotes) . ")";
            } else {
                $cond .= " AND state IN ('')";
            }
        }

        //TRAINER ID
        if (isset($parent->_request["trainer_id"])) {
            $trainer_id = (isset($parent->_request["trainer_id"])) ? escape_str($parent->_request["trainer_id"]) : '';
            if ($trainer_id) {
                $query_str = "SELECT h.zone FROM " . DMS_HOUSER . " as h JOIN " . TABLE_USER . " as u ON h.emp_code=u.emp_code WHERE u.status='1' AND u.is_deleted='0' AND u.userid='" . $trainer_id . "' AND u.role='5' LIMIT 1";
                $arr = $parent->getQueryValue($query_str);
                if (count($arr) > 0) {
                    $cond .= " AND zone='" . $arr[0]->zone . "'";
                } else {
                    $cond .= " AND zone=''";
                }
            } else {
                //$cond=" AND state IN ('')";
            }
        }

        $query_str = "SELECT city FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' " . $cond . " GROUP BY city ";
        $data_arr = $parent->getQueryValue($query_str);

        //LOG
        $logstr = 'COMMON CITY';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// DEALER //////////////////////////////////////////////// */
    private function dealer($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $cond = '';
        //ZONE
        if ((isset($parent->_request["zone"]))) {
            $zone = (isset($parent->_request["zone"])) ? escape_str($parent->_request["zone"]) : '';
            if ($zone) {
                $zone_arr = @explode(',', $zone);
                $zone_quotes = array_map("quotes_array", $zone_arr);
                $cond .= " AND zone IN (" . @implode(',', $zone_quotes) . ")";
            } else {
                $cond .= " AND zone IN ('')";
            }
        }
        //STATE
        if (isset($parent->_request["state"])) {
            $state = (isset($parent->_request["state"])) ? escape_str($parent->_request["state"]) : '';
            if ($state) {
                $state_arr = @explode(',', $state);
                $cond .= " AND zone IN (" . @implode(',', $zone_quotes) . ")";
            } else {
                $cond .= " AND state IN ('')";
            }
        }

        //TRAINER ID
        if (isset($parent->_request["trainer_id"])) {
            $trainer_id = (isset($parent->_request["trainer_id"])) ? escape_str($parent->_request["trainer_id"]) : '';
            if ($trainer_id) {
                $query_str = "SELECT h.zone FROM " . DMS_HOUSER . " as h JOIN " . TABLE_USER . " as u ON h.emp_code=u.emp_code WHERE u.status='1' AND u.is_deleted='0' AND u.userid='" . $trainer_id . "' AND u.role='5' LIMIT 1";
                $arr = $parent->getQueryValue($query_str);
                if (count($arr) > 0) {
                    $cond .= " AND zone='" . $arr[0]->zone . "'";
                } else {
                    $cond .= " AND zone=''";
                }
            } else {
                //$cond=" AND state IN ('')";
            }
        }

        //CITY
        if (isset($parent->_request["city"])) {
            $state = (isset($parent->_request["city"])) ? escape_str($parent->_request["city"]) : '';
            if ($state) {
                $city_arr = @explode(',', $city);
                $city_quotes = array_map("quotes_array", $city_arr);
                $cond .= " AND city IN (" . @implode(',', $city_quotes) . ")";
            } else {
                $cond .= " AND city IN ('')";
            }
        }

        $query_str = "SELECT `dealer_code`, `dealer_name` FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' " . $cond . "  GROUP BY dealer_code LIMIT 3";
        $data_arr = $parent->getQueryValue($query_str);

        //LOG
        $logstr = 'COMMON DEALER';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// LEARNING //////////////////////////////////////////////// */
    private function learning($parent)
    {
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $query_str = "SELECT `lvl_id`, `lvl_name`, `lvl_points` FROM " . TABLE_TRAINING_LEVEL . " ";
        $data_arr = $parent->getQueryValue($query_str);

        //LOG
        $logstr = 'LEARNING LEVEL';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// SHOW //////////////////////////////////////////////// */
    private function show($parent)
    {
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);

        $data_arr = array('is_show' => 1);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// SALUTATION //////////////////////////////////////////////// */
    private function salutation($parent)
    {
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);

        $data_arr = array('Mr', 'Mrs', 'Ms', 'Dr');
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// TRAINERRM //////////////////////////////////////////////// */
    private function trainerrm($parent)
    {

        globalfunc::checkMethodType($parent, array('POST'));
        //globalfunc::checkParamCount($parent,1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        $zone = $parent->_request["zone"];

        projectfunc::authorizedRole($parent, array('4', '5')); //ONLY FOR ZM,TRAINER

        if ($parent->current_user_role == '4') {
            $dcond = " and zm_email='" . $parent->current_user_data->emailid . "'";
        } else {
            if (empty($zone)) {
                $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $zone = (count($trainer_arr) > 0) ? $trainer_arr[0]->zone : '';
            }
            $dcond = " and zone='" . $zone . "'";
        }

        $query_str = "SELECT rm_email,rm_name FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' " . $dcond . " GROUP BY rm_email ";
        $data_arr = $parent->getQueryValue($query_str);

        //LOG
        $logstr = 'TRAINER RM';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// TRAINER AM //////////////////////////////////////////////// */
    private function traineram($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('4', '5', '6'));

        $rm_email_str = $parent->_request["rm_email"];
        $rm_email = json_decode($rm_email_str);

        if (!(is_array($rm_email) && count($rm_email) > 0)) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
        }

        $rmemail_quotes = array_map("quotes_array", $rm_email);
        $query_str = "SELECT am_email,am_name, rm_email FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' and rm_email IN (" . @implode(',', $rmemail_quotes) . ") GROUP BY am_email ORDER BY `am_name` ASC ";
        $data_arr = $parent->getQueryValue($query_str);
        $data_arr = (count($data_arr) > 0) ? $data_arr : array();

        //LOG
        $logstr = 'TRAINER AM';
        globalfunc::insertUserLog($parent, $logstr);

        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// TRAINER AM //////////////////////////////////////////////// */
    private function trainerzoneam($parent)
    {
        globalfunc::checkMethodType($parent, array('GET'));
        //globalfunc::checkParamCount($parent,1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('4', '5', '6'));

        $zone = escape_str($parent->params[1]);

        $user_emp_code = $parent->current_user_data->emp_code;

        if (empty($user_emp_code)) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
        }

        //$query_str = "SELECT am_email,am_name FROM ".DMS_DEALER." WHERE status='1' AND is_deleted='0' and zone IN (SELECT zone from " . DMS_HOUSER . " WHERE emp_code = '" . $user_emp_code . "' ) GROUP BY am_email ORDER BY `am_name` ASC ";
        if (!empty($zone)) {
            $query_str = "SELECT am_email,am_name FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' and zone = '" . $zone . "' GROUP BY am_email ORDER BY `am_name` ASC ";
        } else {
            $query_str = "SELECT am_email,am_name FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' and zone IN (SELECT zone from " . DMS_HOUSER . " WHERE emp_code = '" . $user_emp_code . "' ) GROUP BY am_email ORDER BY `am_name` ASC ";
        }
        $data_arr = $parent->getQueryValue($query_str);
        $data_arr = (count($data_arr) > 0) ? $data_arr : array();

        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// TRAINER DEALER //////////////////////////////////////////////// */
    private function trainerdealer($parent)
    {

        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('4', '5', '6', '7'));

        $am_email = $parent->_request["am_email"];
        $segment = $parent->_request["segment"];
        $am_email = json_decode($am_email);
        if (!(is_array($am_email) && count($am_email) > 0)) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
        }

        $amemail_quotes = array_map("quotes_array", $am_email);
        $query_str = "SELECT dealer_code,dealer_name,address, am_email FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' and am_email IN (" . @implode(',', $amemail_quotes) . ") GROUP BY dealer_code ORDER BY `dealer_name` ASC ";
        $data_arr = $parent->getQueryValue($query_str);
        //print_r($data_arr) . '<br/><br/><br/>';
        foreach ($data_arr as $key => $dealers) {
            $dealer_code = $dealers->dealer_code;
            $parent->_request["dealer_code"] = '["' . $dealer_code . '"]';
            $check_dselist = $this->dselist($parent, 1, $segment);
            if (!empty($check_dselist)) {
                $dealer_data[] = $dealers;
            }
        }
        //print_r($dealer_data);
        //print_r($data_arr);
        $data_arr = (count($dealer_data) > 0) ? $dealer_data : array();

        //LOG
        $logstr = 'TRAINER DEALER';
        globalfunc::insertUserLog($parent, $logstr);

        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// DSELIST //////////////////////////////////////////////// */
    private function dselist($parent, $check = '', $dealer_segment = '')
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7', '14'));

        $dealer_code = $parent->_request["dealer_code"];
        $typeid = $parent->_request["typeid"];
        $segment = $parent->_request["segment"];
        $dealer_code = json_decode($dealer_code);
        if (!(is_array($dealer_code) && count($dealer_code) > 0)) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
        }

        $dealer_code_quotes = array_map("quotes_array", $dealer_code);
        $usercond = "AND d.dealer_code IN (" . @implode(',', $dealer_code_quotes) . ")";

        // Segment Condition
        if ($dealer_segment == 1 or $dealer_segment == 2) {
            $segment_cond = " AND u.segment = '" . $dealer_segment . "' ";
        }
        if ($segment == 1 or $segment == 2) {
            $segment_cond = " AND u.segment = '" . $segment . "' ";
        }

        switch ($typeid) {
            case "base":
                $usercond .= " and l.lvl_id=1 AND l.current_status=1";
                $jointable = "JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin";
                break;
            case "bronze":
                $usercond .= " and l.lvl_id=2 AND l.current_status=1";
                $jointable = "JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin";
                break;
            case "silverlevel1":
                //$usercond.=" and l.lvl_id=3 AND l.current_status=1 and l.status in (0,5,6)";
                $usercond .= " and l.lvl_id=3 AND l.current_status=1";
                $jointable = "JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin";
                break;
            case "silverlevel2":
                //$usercond.=" and l.lvl_id=3 AND l.current_status=1 and l.status in (4,2,3,7)";
                $usercond .= " and l.lvl_id=4 AND l.current_status=1";
                $jointable = "JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin";
                break;
            case "goldlevel1":
                //$usercond.=" and l.lvl_id=4 AND l.current_status=1 and l.status in (0,5,6)";
                $usercond .= " and l.lvl_id=5 AND l.current_status=1";
                $jointable = "JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin";
                break;
            case "goldlevel2":
                //$usercond.=" and l.lvl_id=4 and l.status in (4,2,3,7)";
                $usercond .= " and l.lvl_id=6 AND l.current_status=1";
                $jointable = "JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin";
                break;
            default:
                $jointable = '';
                break;
        }
        $query_str = "SELECT u.userid,u.fname,u.lname, concat(u.fname, ' ', u.lname) as name, u.smpin,u.dealer_code,d.dealer_name,u.segment,CASE WHEN u.segment=0 THEN 'Student Learner' WHEN u.segment=1 THEN 'Parents/Guardian' WHEN u.segment=2 THEN 'Instructor' END as segment_str,u.profile_photo FROM " . TABLE_USER . " u LEFT JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code " . $jointable . " WHERE e.is_current='1' AND u.status='1' AND u.is_deleted='0' AND u.role>7 AND u.role<14 " . $segment_cond . " " . $usercond . " GROUP BY u.userid ORDER BY u.dealer_code ASC, u.fname ASC, u.userid ASC";
        $data_arr = $parent->getQueryValue($query_str);
        $data_arr = (count($data_arr) > 0) ? $data_arr : array();

        if (count($data_arr) > 0) {
            //CONNECT S3
            $s3 = awsfileserver::connectS3();
            foreach ($data_arr as $key => $res) {

                if ($res->profile_photo != '') {
                    $data_arr[$key]->profile = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_PROFILE_IMAGE_URL . $res->profile_photo);} else {
                    $data_arr[$key]->profile = USER_NOIMAGE_URL;
                }
            }
        }
        if (!empty($check)) {
            return $data_arr;
        } else {
            //LOG
            $logstr = 'DSE LIST';
            globalfunc::insertUserLog($parent, $logstr);

            $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
        }
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// AM LIST //////////////////////////////////////////////// */
    private function amlist($parent)
    {

        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        //projectfunc::authorizedRole($parent,array('4','5','6','7','14'));

        $rm_email_str = $parent->_request["rm_email"];
        $rm_email_arr = json_decode($rm_email_str);

        $dealer_code = $parent->_request["dealer_code"];
        //$typeid=$parent->_request["typeid"];
        //$dealer_code=json_decode($dealer_code);
        if (!(is_array($rm_email_arr) && count($rm_email_arr) > 0)) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
        }

        $rm_email_quotes = array_map("quotes_array", $rm_email_arr);
        $usercond = "AND d.rm_email IN (" . @implode(',', $rm_email_quotes) . ")";

        $query_str = "SELECT u.userid,u.fname,u.lname,u.emp_code as smpin,'' as  dealer_code,'' as  dealer_name,'' as  segment,''  as segment_str,u.profile_photo FROM " . TABLE_USER . " u LEFT JOIN " . DMS_DEALER . " d ON u.emailid=d.am_email   WHERE  u.status='1' AND u.is_deleted='0' AND u.role=7 " . $usercond . " GROUP BY u.userid ORDER BY u.fname ASC,u.userid ASC";
        $data_arr = $parent->getQueryValue($query_str);
        $data_arr = (count($data_arr) > 0) ? $data_arr : array();

        if (count($data_arr) > 0) {
            //CONNECT S3
            $s3 = awsfileserver::connectS3();
            foreach ($data_arr as $key => $res) {

                if ($res->profile_photo != '') {
                    $data_arr[$key]->profile = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_PROFILE_IMAGE_URL . $res->profile_photo);} else {
                    $data_arr[$key]->profile = USER_NOIMAGE_URL;
                }
            }
        }

        //LOG
        $logstr = 'AM LIST';
        globalfunc::insertUserLog($parent, $logstr);

        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// I LEARN USER LIST //////////////////////////////////////////////// */
    private function ilearnuserlist($parent)
    {

        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('5'));

        $REQUEST_KEYS = array('role' => 'number');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

        $role = escape_str($parent->_request["role"]);
        //GET TRAINER ZONE
        $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
        $trainer_arr = $parent->getQueryValue($get_trainer_str);
        if (!count($trainer_arr) > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
        }

        switch ($role) {
            case "4":
                $dealer_cond = " ON u.emailid=d.zm_email ";
                $usercond = " AND u.role=4 ";
                break;
            case "6":
                $dealer_cond = " ON u.emailid=d.rm_email ";
                $usercond = " AND u.role=6 AND d.zone='" . $trainer_arr[0]->zone . "'";
                break;
            case "7":
                $dealer_cond = " ON u.emailid=d.am_email ";
                $usercond = " AND u.role=7 AND d.zone='" . $trainer_arr[0]->zone . "'";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
                break;
        }

        $query_str = "SELECT u.userid,u.fname,u.lname,u.emp_code as smpin,'' as  dealer_code,'' as  dealer_name,'' as  segment,''  as segment_str,u.profile_photo FROM " . TABLE_USER . " u LEFT JOIN " . DMS_DEALER . " d " . $dealer_cond . "  WHERE  u.status='1' AND u.is_deleted='0' " . $usercond . " GROUP BY u.userid ORDER BY u.fname ASC,u.userid ASC";
        $data_arr = $parent->getQueryValue($query_str);
        $data_arr = (count($data_arr) > 0) ? $data_arr : array();

        if (count($data_arr) > 0) {
            //CONNECT S3
            $s3 = awsfileserver::connectS3();
            foreach ($data_arr as $key => $res) {

                if ($res->profile_photo != '') {
                    $data_arr[$key]->profile = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_PROFILE_IMAGE_URL . $res->profile_photo);} else {
                    $data_arr[$key]->profile = USER_NOIMAGE_URL;
                }
            }
        }

        //LOG
        $logstr = 'ILEARN USER LIST';
        globalfunc::insertUserLog($parent, $logstr);

        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */
}
