How would you suggest I go about allowing users to hide themselves from the "Who's Online" block? I'm using a custom Who's Online block so I can add code in there anywhere I want. I'm fairly proficient with PHP and Drupal module building.

Right now, I'm thinking I should save the user 'online/offline' status in a database table so that users can keep the same settings the next time they log in. Then - before listing the name of the user that is online - I could check to see if they want to be displayed. And I could have a link (with JQuery-toggled text ["go online"/"go offline"]) which sends the user to a "fake" page that just switches their status.

If I do it this way, would it be better (read: more efficient in both time and performance) to do it in a module or write it into the block manually?

And there's another option. Would this be better saved in a cookie? I don't really know how to do that, or what the benefits might be, although I think that sites like Facebook do it that way. So - if this is a better way to do it, how would I go about it?

By the way, the block only shows for logged-in users. Drupal 5.10, PHP 5.2, MySQL 5, Apache.

Thanks in advance.

Comments

Bananoide’s picture

This is exactly what i was looking for. It would be nice to create a view for that and then filter people by an option on the profile. Is there anyone who can write me a code for that "view"?

mradcliffe’s picture

You can do it in views, just took about 15 minutes or so to write up a views $data object for the 'sessions' table. Although you wouldn't have the aggregate stuff.

i.e. make your own module sessions_views or insert it into the views user.views.inc or another views include file.

    $data['sessions']['table']['group'] = t('User');

    $data['sessions']['table']['join'] = array(
        'users' => array(
            'left_field' => 'uid',
            'field' => 'uid',
            'type' => 'INNER',
        ),  
    );  

    $data['sessions']['timestamp'] = array(
        'title' => t('User Session: Timestamp'),
        'help' => t('The timestamp for the session of a user.'),
        'field' => array(
            'handler' => 'views_handler_field_date',
            'click sortable' => TRUE,
        ),  
        'filter' => array(
            'handler' => 'views_handler_filter_date',
        ),  
        'sort' => array(
            'handler' => 'views_handler_sort_date',
        ),  
    );  
icecreamyou’s picture

Views doesn't help because it requires users to visit their profile to change their online/offline status. It would be nice to allow them to click a link directly from the "Who's Online" block.

If I could figure out how to make the switching happen it would be easy for me to just write a module... unfortunately I have issues with the whole hook_menu() concept.

mradcliffe’s picture

Yes that reply was meant for Bananoide who wanted a sessions integrated with views.

A module is the route I would take for yours. What sort of trouble are you having with hook_menu()?

icecreamyou’s picture

Well, for this to work nicely, the user would click on a link next to their name in the Who's Online block, and then that user's online/offline status would automagically switch. I'm under the impression that for this to work, the link would send the user to a "fake" page that would just query the database and switch the status... but at that point I have no idea how to send the user back to where they were before.

mradcliffe’s picture

Instead of making it a link how about making it a form?

Use drupal_get_form('my_module_form') to return your form output. Just make it a radio button or select. Then just return a drupal_set_message in your submit function.

I'm not sure if this will change pages for a block, but for a node I'm able push a form directly on the node/3 with node_api. I assume it may work similarly with the 'view' op for a block.

icecreamyou’s picture

Hmm, good idea. Will probably try tonight and report my results.

icecreamyou’s picture

It worked. Just a 'select' and 'submit' form item, along with a two-column table with UID and status (0 or 1) did the trick. Thanks.

mrwhizkid’s picture

Hello IceCreamYou,

Could you give me a few more details as to exactly how to implement this? I want to put this in a block. Which code would I use for this?

I am using Drupal 6.

Thanks very much.

WorldFallz’s picture

You can actually do this with a views block and no code if you like.

j0e’s picture

Don't see a setting in Views that would enable you to hide from the view. The User: Is Online filter gets you one part of the equation, meaning, the actual view, but allowing users to flip off detection, how you can do this in Views or otherwise without any code?

WorldFallz’s picture

I usually do it with an option in the user's profile and just add that as a filter to the view.