<?php

if (!defined('ABSPATH'))
    die('Restricted Access');

class JSLearnManagerUser {

    private $currentuser = null;
    public $socialmedia = null;

    function __construct() {
        $db = new jslearnmanagerdb();
        if (is_user_logged_in()) { // wp user logged in
            $wpuserid = get_current_user_id();
            if (!is_numeric($wpuserid))
                return false;
            $query = "SELECT * FROM `#__js_learnmanager_user` WHERE uid = " . $wpuserid;
            $db->setQuery($query);
            $currentuser = $db->loadObject();
            $jslearnmanager_registerform = JSLEARNMANAGERrequest::getVar('jslearnmanager_registerform');

            // if($currentuser == '' AND ! ($jslearnmanager_registerform)){// inserting a record in our table
            //     $userdata = get_userdata($wpuserid);
            //     $profile['id'] = '';

            //     $profile['username'] = $userdata->display_name;
            //     if($userdata->display_name == ''){
            //         $profile['username'] = $userdata->user_nicename;
            //     }
            //     $profile['email'] = $userdata->user_email;
            //     $profile['uid'] = $wpuserid;
            //     $profile['created_at'] = date_i18n('Y-m-d H:i:s');
            //     $profile['updated_at'] = date_i18n('Y-m-d H:i:s');
            //     $profile['status'] = 1;
            //     $profile['user_role_id'] = $userdata->user_role;
            //     $profile['autogenerated'] = 1;
            //     $result = JSLEARNMANAGERincluder::getJSModel('user')->storeProfile($profile,2);
            //     if(is_numeric($result)){
            //         $query = "SELECT * FROM `#__js_learnmanager_user` WHERE id = " . $result;
            //         $db->setQuery($query);
            //         $currentuser = $db->loadObject();
            //     }
            // }
            $this->currentuser = $currentuser;
        }else { // wp user is not logged in
            if (isset($_SESSION['jslearnmanager-socialid']) && !empty($_SESSION['jslearnmanager-socialid'])) { // social user is logged in
                $query = "SELECT * FROM `#__js_learnmanager_user` WHERE socialid = '" . sanitize_key($_SESSION['jslearnmanager-socialid']) . "'";
                $db->setQuery($query);
                $this->currentuser = $db->loadObject();
                $this->socialmedia = isset($_SESSION['jslearnmanager-socialmedia']) ? sanitize_key($_SESSION['jslearnmanager-socialmedia']) : '';
            }
        }
    }

    function isguest() {
        if (isset($_SESSION['jslearnmanager-socialid']) && !empty($_SESSION['jslearnmanager-socialid'])) {
            return false;
        } elseif ($this->currentuser == null && !is_user_logged_in()) { // current user is guest
            return true;
        } else {
            return false;
        }
    }

    function isdisabled() {
        if ($this->currentuser != null && $this->currentuser->status == 0) { // current user is disabled
            return true;
        } else {
            return false;
        }
    }

    function uid() {
        if ($this->currentuser != null) {
            return $this->currentuser->id;
        }
    }

    function emailaddress() {
        if ($this->currentuser == null) { // current user is guest
            return false;
        } else {
            return $this->currentuser->emailaddress;
        }
    }

    function fullname() {
        if ($this->currentuser == null) { // current user is guest
            return false;
        } else {
            $name = $this->currentuser->name;
            return $name;
        }
    }

    function isJSLearnManagerUser() {
        $db = new jslearnmanagerdb();
        if (is_user_logged_in()) { // wp user logged in
            $wpuserid = get_current_user_id();
            if (!is_numeric($wpuserid))
                return false;
            $query = "SELECT COUNT(id) FROM `#__js_learnmanager_user` WHERE uid = " . $wpuserid;
            $db->setQuery($query);
            $result = $db->loadResult();
            if ($result > 0) {
                return true;
            } else {
                return false;
            }
        }else {
            if (isset($_SESSION['jslearnmanager-socialid']) && !empty($_SESSION['jslearnmanager-socialid'])) { // social user is logged in
                $query = "SELECT COUNT(id) FROM `#__js_learnmanager_user` WHERE socialid = '" . sanitize_key($_SESSION['jslearnmanager-socialid']) . "'";
                $db->setQuery($query);
                $result = $db->loadResult();
                if ($result > 0) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }

    function getjslearnmanageruidbyuserid($userid){
        if(!is_numeric($userid)) return false;
        $db = new jslearnmanagerdb();
        $query = "SELECT id FROM `#__js_learnmanager_user` WHERE uid = ".$userid;
        $db->setQuery($query);
        $uid = $db->loadResult();
        return $uid;
    }

    function getjslearnmanagerwpidbyuserid($userid){
        if(!is_numeric($userid)) return false;
        $db = new jslearnmanagerdb();
        $query = "SELECT uid FROM `#__js_learnmanager_user` WHERE id = ".$userid;
        $db->setQuery($query);
        $uid = $db->loadResult();
        return $uid;
    }

    function isInstructor($userid){

        if(!is_numeric($userid) || $userid == ''){
            return false;
        }
        $db = new jslearnmanagerdb();
        $query = "SELECT COUNT(i.id)
                    FROM `#__js_learnmanager_user` AS u
                        INNER JOIN `#__js_learnmanager_instructor` AS i ON u.id = i.user_id AND u.id =" .$userid;
        $db->setQuery($query);

        $result = $db->loadResult();
        if($result > 0){
            return true;
        }else{
            return false;
        }
    }

    function isStudent($userid){
        if(!is_numeric($userid) || $userid == ''){
            return false;
        }
        $db = new jslearnmanagerdb();
        $query = "SELECT COUNT(s.id)
                    FROM `#__js_learnmanager_user` AS u
                        INNER JOIN `#__js_learnmanager_student` AS s ON u.id = s.user_id AND u.id =" .$userid;
        $db->setQuery($query);
        $result = $db->loadResult();
        if($result > 0){
            return true;
        }else{
            return false;
        }
    }

    function getjslearnmanagerusertypebywpid($userid){
        if(!is_numeric($userid) || $userid == ''){
            return false;
        }
        $db = new jslearnmanagerdb();
        $query = "SELECT r.role
                    FROM `#__js_learnmanager_user` AS u
                        INNER JOIN `#__js_learnmanager_user_role` AS r ON r.id = u.user_role_id AND u.uid =" .$userid;
        $db->setQuery($query);
        $result = $db->loadObject();
        return $result;
    }

    function getjslearnmanagerusertypeByuserid($userid){ // By jslearnmanager user id
        if(!is_numeric($userid) || $userid == ''){
            return false;
        }
        $db = new jslearnmanagerdb();
        $query = "SELECT r.role
                    FROM `#__js_learnmanager_user` AS u
                        INNER JOIN `#__js_learnmanager_user_role` AS r ON r.id = u.user_role_id AND u.id =" .$userid;
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result;
    }

    function isSocialLogin() {
        if(in_array('sociallogin', jslearnmanager::$_active_addons)){
            if (isset($_SESSION['jslearnmanager-socialid']) && !empty($_SESSION['jslearnmanager-socialid'])) {
                return true;
            }
        }
        return false;
    }
}
?>