Is there a function to retrieve all current logged-in users? - drupal

Is there a function to retrieve all current logged-in users for Drupal 7?
I tried to build a query based on Get all the logged-in user.
$query->join('users', 'u', 's.uid = u.uid'); //JOIN sessions with users
$query->fields('u',array('name')) //SELECT the fields from node
->orderBy('timestamp', 'DESC') //ORDER BY timestamp
->range(0,10); //LIMIT to 10 records
->execute()
->fetchAll();
$result = $query->execute();
I'm still missing something on to retrieve all names from $result as an array, and on how to limit the query to return the users who were active in the last 5 minutes.
I need to put these into an array with their usernames in #title and their user IDs in #href.

Retrieve users logged in last 5 minutes :
$users_results = db_query('SELECT u.uid , u.name FROM users u
WHERE ( UNIX_TIMESTAMP(NOW()) - u.access ) < (5*60)')
->fetchAllKeyed();
dsm($users_results);
EDIT
Like asked :
foreach($users_results as $uid => $name){
$users[$name] = array('#title' => $name , '#href' => $uid);
}

Related

Regenerating WooCommerce Download Permissions on older orders

I am trying to add some download permissions to all previous orders via a script to do them in batch. The script seems to work fine expect for one thing. Here is the script…
function update_download_permissions(){
$orders = get_posts( array(
'post_type' => 'shop_order',
'post_status' => 'wc-completed',
'posts_per_page' => -1
) );
foreach ( $orders as $order ) {
wc_downloadable_product_permissions( $order->ID, true );
}
}
The problem is the wc_downloadable_product_permissions function is producing duplicate entries in the wp_woocommerce_downloadable_product_permissions table.
I tried to set the second argument to false (the default) but that resulted in no permissions being created.
Does anybody have any ideas as to why duplicate download permissions are being set?
Cheers!
I came across your question after digging through some of the WooCommerce source code, while attempting to add an item to an existing order and then regenerate the permissions.
The reason wc_downloadable_product_permissions() will create duplicate permission entries is because it does not check for any existing permissions. It simply inserts another entry into the permissions table for every item in the order, which is no good because this will then show up as another download in both the admin and user account frontend.
The second force parameter (poorly documented), is related to a boolean flag that indicates whether wc_downloadable_product_permissions() has run before. The boolean is set to true at the end of the function via the set_download_permissions_granted method. If force is true, it will ignore the boolean. If force is false, and the boolean is true, the function will return near the start.
I created this function which uses the same functions as used by the admin Order action "Regenerate download permissions":
/**
* Regenerate the WooCommerce download permissions for an order
* #param Integer $order_id
*/
function regen_woo_downloadable_product_permissions( $order_id ){
// Remove all existing download permissions for this order.
// This uses the same code as the "regenerate download permissions" action in the WP admin (https://github.com/woocommerce/woocommerce/blob/3.5.2/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php#L129-L131)
// An instance of the download's Data Store (WC_Customer_Download_Data_Store) is created and
// uses its method to delete a download permission from the database by order ID.
$data_store = WC_Data_Store::load( 'customer-download' );
$data_store->delete_by_order_id( $order_id );
// Run WooCommerce's built in function to create the permissions for an order (https://docs.woocommerce.com/wc-apidocs/function-wc_downloadable_product_permissions.html)
// Setting the second "force" argument to true makes sure that this ignores the fact that permissions
// have already been generated on the order.
wc_downloadable_product_permissions( $order_id, true );
}
I found the best way to update order download is to hook into the save_post action hook and check if it's a product that's being updated
there you can get order ids by product id and update just orders that relate to that specific product.
it's more efficient
function get_orders_ids_by_product_id($product_id) {
global $wpdb;
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";
return $wpdb->get_col(
"
SELECT DISTINCT woi.order_id
FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p
WHERE woi.order_item_id = woim.order_item_id
AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses )
AND woim.meta_key IN ( '_product_id', '_variation_id' )
AND woim.meta_value LIKE '$product_id'
ORDER BY woi.order_item_id DESC"
);
}
// if you don't add 3 as as 4th argument, this will not work as expected
add_action('save_post', 'prefix_on_post_update', 10, 3);
function prefix_on_post_update($post_id, $post, $update) {
if ($post->post_type == 'product') {
$orders_ids = get_orders_ids_by_product_id($post_id);
foreach ($orders_ids as $order_id) {
$data_store = WC_Data_Store::load('customer-download');
$data_store->delete_by_order_id($order_id);
wc_downloadable_product_permissions($order_id, true);
}
}
}

check if member is of a group

I use buddypress with multiple groups. Every user can also join multiple groups as a member. I use custom profile fields for e.g. the address. Every member can select, who is able to view his profile. This try is based on the plugin 'User Profile Visibility Manager'. Now I need an additional option: View the profile to "Group Members Only"
My plan is:
Get the user_id of the 'visitor' - works perfect with:
$user_id_visitor = bp_loggedin_user_id();
Get the user_id of the 'profile owner' - works perfect with:
$user_id_profile_owner = bp_displayed_user_id();
Get the group(s) by the profile owner´s user_id:
Here, I´ve tried a lot. With this function, I´m able to print all groups in which the 'profile owner' is a member. But I don´t need to print it, it is only for a test:
function bp_groups_profileowner( $user_id_profile_owner ) {
global $wpdb;
$groups = $wpdb->get_results( $wpdb->prepare( "SELECT group_id FROM wp_bp_groups_members WHERE user_id = %d", $user_id_profile_owner ) );
if ( $groups ) {
foreach ( $groups as $group ) {
echo '<li>' . $group->group_id . '</li>';
}
}
check all members of the profile_owner´s group(s) and check, if the visitor is also member the group(s).
My new option in the selectbox:
<option value="groupmembers" <?php echo selected('groupmembers',bp_profile_visibility_get_settings($user_id,'bp_profile_visibility' ));?>><?php _e('Group Members Only','bp-profile-visibility');?></option>
This is the code snippet, coming from the plugin, which checks and protects user account visibility:
/**
* Checks and protects user account visibility
* #return type
*/
function check_profile_access(){
if(!bp_is_user() || is_super_admin())
return;
//if we are on user profile page
$privacy = bp_profile_visibility_get_settings(bp_displayed_user_id(), 'bp_profile_visibility');
//if privacy is public, everyone can see
if( 'public' == $privacy )
return;
$referrer=wp_get_referer();
if($referrer)
$referrer=bp_core_get_root_domain ();
//in all other cases, user must be logged in
if(!is_user_logged_in()){
bp_core_add_message(__('Please login to view profile','bp-profile-visibility'),'error');
wp_safe_redirect($referrer);
exit(0);
return ;
}
//if we are here, the person is logged in, let us see if the visibility is set to logged in
if( 'loggedin' == $privacy )
return ;
//if this is my profile, do not prevent user
if(bp_is_my_profile())
return ;
//now, since we have already tested for login , we just need to test for the friends only and me
if( 'friends' == $privacy && function_exists('friends_check_friendship') && friends_check_friendship(bp_displayed_user_id(), get_current_user_id()) )
return;
//now, we just need to test for the group members
if( 'groupmembers' ... )
//if we are here, don't show the profile
bp_core_add_message(__('This User\'s privacy settings does not allow you to view the profile.','bp-profile-visibility'),'error');
wp_safe_redirect($referrer);
exit(0);
return ;
}
I found the solution by myself:
//now, we just need to test for the group members only and me
if( 'groupmembers' == $privacy )
/**
* Check if visitor is a member of one of the profile owners groups mm
*
*/
$user_id_visitor = bp_loggedin_user_id();
$user_id_profile_owner = bp_displayed_user_id();
$all_groups = BP_Groups_Member::get_group_ids( $user_id_profile_owner);
if ( $all_groups ) {
foreach($all_groups[groups] AS $profile_owner_groups_id)
{
if (groups_is_user_member( $user_id_visitor, $profile_owner_groups_id ))
// break if visitor is member of one of the profie owners group //
return;
}
}

How to get drupal custom profile values?

There is a checkbox added in user profile form, subscribe to daily newsletter. I want to get list of users who have subscribed (checkbox ON) and send them daily newsletter. How can I do this in drupal-6. (1) to get list of all subscribed users (2) Send them Email.
If you can explain at code level, I 'll really appreciate that.
If your profile field is profile_newsletter, your :
<?php
function mymodule_get_subscribers() {
$query = 'select * from {profile_fields} where `name`="%s";';
$result = db_query($query,'profile_newsletter');
$field = db_fetch_object($result);
$user_query = 'select * from {profile_values} where `fid`=%d and `value`=1;';
$result = db_query($user_query,$field->fid);
$subscribers = array();
while($subscriber = db_fetch_object($result)) {
$subscribers[] = $subscriber->uid;
}
return $subscribers; // Your array of subscriber user IDs
}
?>
This code was quick, is untested and should probably contain a few sanity-checks. But it should work.
For the sending of newsletters and such I'd recommend using a pre-rolled module. I haven't tried any for Drupal, but Simplenews seems to do the trick. http://drupal.org/project/simplenews
If nothing else it probably contains a good sending-function. Otherwise just use PHP mail()-function.
function your_custom_function ($user_id, "Field you want to get") {
$result = db_query("SELECT t2.value FROM profile_fields t1, profile_values t2 where
t1.title='Field you want to get' and t2.uid=".$user_id." and t1.fid=t2.fid");
$field = db_fetch_object($result);
$profile_type =$field->value;
return $profile_type;
}

Get all the logged-in user

I want to have my own chat. (I can't use the Chat module because I have to personalize it.) I have to retrieve all the users who are online, but I can't see any variable for that.
I am only able to get the name of the currently logged-in user, but not the rest of the logged-in users.
You can fetch a list of all logged in users by querying the sessions table. I'm assuming you're using Drupal 6.
<?php
$result = db_query('SELECT uid FROM {sessions} WHERE uid != 0');
$users = array();
while($user = db_fetch_array($result)) {
$users[] = user_load($user);
}
The query excludes sessions for uid = 0 as these are anonymous users. $users is the array of user objects as described in the Drupal API Docs.
You can optimize this if you already know what part of the user objects you will use (e.g. just the user id and name) by removing the user_load() in while loop and adding to the query a join with the users table, as each user_load() makes one additional query. The following would get you a list of logged in users' id and names:
<?php
$result = db_query('SELECT u.uid, u.name FROM {sessions} s INNER JOIN {users} u ON u.uid = s.uid WHERE s.uid != 0');
$users = array();
while($users[] = db_fetch_array($result));
Since logged in users never time out (you can stay logged in indefinitely), it may be useful to exclude logged in users who haven't accessed the site in a while (i.e. maybe an hour of inactivity):
$timestamp = time - 3600; // 3600s is one hour.
$result = db_query('SELECT uid FROM {sessions} WHERE uid != 0 AND timestamp >= %d', $timestamp);
You might also want to limit how many users to return. For example, maybe you want to grab - at most - the last 10 logged in users who accessed the site:
$limit = 10; // Limit to the last 10 users.
$result = db_query_range('SELECT uid FROM {sessions} WHERE uid != 0 ORDER BY timestamp DESC', $timestamp, 0, $limit);
As an aside, if you're going to be using magic numbers (like $limit or the 3600s), you should make them persistent using variable_set(), variable_get(), and variable_del().

Drupal: Modifying a User at Registration

I needed to create a custom select list for the user registration page that pulls a SQL query from nodes I need to link to users. I have successfully accomplished this task.. :)
However, when I submit the value, I can't seem to control where the value is stored. In fact, I can't store the value at all. I have created a custom field for my value, but only the new field name is stored, and it is serialized and stored in the Data column of the user table.
Below is my code, I've commented my issues in it. Any help would be appreciated!
<?php
// $Id$
//create the additional user form field, a select list named "account_name"
//then calls the next function to populate it.
function accountselect_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'register' || 'edit')
$fields['Information']['account_name'] = array(
'#type' => 'select',
'#title' => 'Account',
'#description' => t('Select the account to which the contact belongs'),
'#options' => accountselect_getclubs() ,
);
return $fields;
}
//contains query to pull results to select list...this part is working
function accountselect_getclubs() {
$return = array();
$sql = 'SELECT DISTINCT `title` FROM node WHERE type = \'accounts\' ';
$result = db_query($sql);
while ($row = db_fetch_array($result)) {
$return[] = $row['title'];
}
return $return;
}
//CAN'T GET THIS PART TO WORK - query to update the row - the uid = 29 is for
//testing puposes. Once I get the test value to work
//the test value I will worry about updating the correct user.
function accountselect_submitaccount() {
$sql = "UPDATE users SET account = \'value\' WHERE uid = \'29\'";
db_query($sql);
drupal_set_message(t('The field has been updated.'));
}
//I SUSPECT THE PROBLEM IS HERE...call the submitaccount function.
//I have tried hook_form_alter as well...
function accountselect_submit(&$form, &$form_state) {
if($form_id == 'user-register')
drupal_execute('accountselect_submitaccount');
}
Have you checked Drupal's logs? It should be throwing errors, as this is not a valid query.
$sql = "UPDATE users SET account = \'value\' WHERE uid = \'29\'";
Should be:
$sql = "UPDATE users SET account = 'value' WHERE uid = '29'";
Additionally, in:
function accountselect_submit(&$form, &$form_state) {
if($form_id == 'user-register')
drupal_execute('accountselect_submitaccount');
}
$form_id is never defined.
You say you've created the field in the database, but it must match the name of the Drupal field to be automatically handled. You've got two different names for it - account_name in the Drupal field, but account in the database. Make them consistent and it should be automatically handled, no submit functions required.

Resources