Admin search for last login

I am using 1.7.11 and would like to delete users who have only logged in once and never returned in over a year.

Is there a way I can search for the last time a user logged in?  I can find this in the database by reordering the "last_login" column in "user_entity" but it does not seem to be in any kind of date order that I can determine.  There has to be a better way of performing this kind of search.

I am not going to delete them in the database, I will delete them from within the site.  I am using the database only as a way to find their last login.

  • Good decision Sam for not deleting users through the DB. For every action you are doing on Elgg, do it through proper Elgg way, because elgg's data is distributed over different tables.

    You can use the following code to remove the users who are not logged in for past 1yr

        $one_year = strtotime("-1 year");
        $users = elgg_get_entities(array('type' => 'user','where' => "last_action < $one_year"));
        if($users){
            foreach($users as $user){
                $user->delete();
            }
        }       

  • this is also something i was looking for! where would this code go?

  • @TW - beautiful elgg coding there. ;)

    @Sam - if you're doing this on a production site you might wanna think about 'customer service'. Why not send them a notification first + set special metadata on the user. I imagine you're doing this all through a cron job so you can then add the following to enhance it:

    $one_year = strtotime("-1 year");

    $users = elgg_get_entities(array('type' => 'user','where' => "last_action < $one_year"));

    if($users){

        foreach($users as $user){

        if(!$user->inactive){

        elgg_send_email ($CONFIG->email, $user->email, "Where have you been?", "EMAIL TEXT HERE", NULL);

        $user->inactive = "yes";

        }else{

    $user->delete();

    }

        }

    }

    So this sends an email to the user's registered email address saying whatever you like and sets metadata on them as "inactive". Then the next time the cron job runs if the user already has inactive metadata set it deletes them.

    The next bit to do is an event catcher so that if they login and have 'inactive' metadata set we remove it.

    elgg_register_event_handler('login','user','inactive_user_check');

    function inactive_user_check($event,$object_type,$object){

    $inactive = $object->inactive;

    if($inactive){

    delete_metadata($inactive->id);

    }

    return true;

     You need to stick the above event handler into your plugin's start file. Run the cron job weekly so it gives the inactive user a week to login. If they login within that week they aren't deleted, otherwise they are.

    Edit: the reason for doing 'elgg_send_email' instead of 'notidy_user' is because if they have their notifications set to site instead of email and haven't logged in for over a year, chances are they aren't gonna see the nice reminder you send them. So force an email notification to give it best chance.

    ;)

     

     

  • @TW and Trajan, thanks guys this is just what I was looking for.  It seems others can use this as well.  I do have a question or two.  What happens to their pics and or emails that are stored in the database?  Are they removed as well or will I have to run the database utility to remove them?

    Thanks so much, this great,  Sam

  • @Sam : this is why we tell you to do all actions in the Elgg way. When you remove users through Elgg all there contents in the site are removed completely.

  • thanks trojen, thanks Team webgalli