<?php

function fetchUsers($conn_rjllearning, $institution_id, $search_string, $limit, $offset)
{

    $sql = "SELECT  count(*) as total_rows FROM  users where institution_id =?";

    $stmt = $conn_rjllearning->prepare($sql);
    $stmt->execute(array($institution_id));
    $result_total = $stmt->fetchAll();
    $totalrows = $result_total[0]['total_rows'];

    // reeturns all data from the specific row of the profile
    $sql_users = "SELECT * FROM users where institution_id =?  and (name LIKE ? or username LIKE ?) LIMIT " . $offset . " ," . $limit . " ";

    $stmt = $conn_rjllearning->prepare($sql_users);
    $stmt->execute(array($institution_id, '%' . $search_string . '%', '%' . $search_string . '%'));

    $users_rows = $stmt->fetchAll();
    //print_r($company_rows);die();

    $array_return = [];

    foreach ($users_rows as $rows) {
        $array_return[] =  array(
            'user_id' => $rows['id'], 'name' => $rows['name'], 'dob' => $rows['dob'], 'gender' => $rows['gender'], 'username' => $rows['username'], 'email' => $rows['email'], 'mobile' => $rows['mobile'], 'role' => $rows['role'], 'designation' => $rows['designation'], 'status' => $rows['status']
        );
    }

    if (count($users_rows) < 50) {

        return array('total_rows' => $totalrows, 'users_info' => $array_return, 'maxreach' => 1);
    } else {
        return array('total_rows' => $totalrows, 'users_info' => $array_return, 'maxreach' => 0);
    }
}


function fetchSpecificUsers($conn_rjllearning, $user_id,$institution_id)
{

    $sql = $conn_rjllearning->prepare("SELECT * FROM  users  where id = ? and institution_id=?");
    $sql->execute(array($user_id, $institution_id));
    $rows = $sql->fetchAll();

    if (count($rows) == 1) {
        return array(
            'user_id' => $rows[0]['id'], 'name' => $rows[0]['name'], 'dob' => $rows[0]['dob'], 'gender' => $rows[0]['gender'], 'username' => $rows[0]['username'], 'email' => $rows[0]['email'], 'mobile' => $rows[0]['mobile'], 'role' => $rows[0]['role'], 'designation' => $rows[0]['designation'], 'status' => $rows[0]['status']
        );
    } else {
        errorreturn("Enter Invalid User ID");
    }
}

$global_usernames_array = [];
$global_names_for_user_array = [];

function getSpecificUsernameName($conn_rjllearning, $institution_id,$workspace_id, $username)
{
    if (in_array($username, $GLOBALS['global_usernames_array'])) {
        return $GLOBALS['global_names_for_user_array'][array_search($username, $GLOBALS['global_usernames_array']) . ''];
    } else {
        $sql = $conn_rjllearning->prepare("SELECT name from users where username = ? and workspace_id =?");
        $sql->execute(array($username,$workspace_id));
        $rows = $sql->fetchAll();
       
        if (count($rows) == 1) {

            $GLOBALS['global_usernames_array'][] = $username;
            $GLOBALS['global_names_for_user_array'] = $rows[0]['name'];
            return $rows[0]['name'];
        } else {
            errorreturn("Enter valid User ID " . $username);
        }
    }
}
