excluding users from the members list

Permalink
I tried setting up a checkbox as a user attribute and then filtering on that, like:
$userList->filterByExcludeFromDirectory(1, '!=');


Oddly, that would only filter out the super user, and no others...

So I came up with this kind of janky way: creating a user group to exclude from the main list:
// there's got to be a better way to exclude people
      $g = Group::getByName('Exclude from Member Directory');
      $hideThese = $g->getGroupMembers();
      $uIDs = array();
      foreach ($hideThese as $u) {
         $uIDs[] = $u->getUserID();
      }
      $userList->excludeUsers($uIDs);


Ideally I'd like to use the checkbox method, rather than the group method.
Maybe more so... I don't know why the checkbox method isn't working, and that really chaps my hide.

Can anyone enlighten me?

kirkroberts
 
shadowcomputers replied on at Permalink Reply
shadowcomputers
I think this post should answer your question:http://www.concrete5.org/community/forums/customizing_c5/members-fi...
kirkroberts replied on at Permalink Reply
kirkroberts
I really appreciate your reply, but I don't think that quite hits the mark.

I'm 99% sure I'm using the filterByAttribute method correctly (or, in this case, the "magic method"), but I think there is some oddness with the checkbox/boolean attribute type. If I'm wrong it wouldn't be the first time, though!

As in the thread you referenced, there is a way to filter a user list to just those IN a particular group, but apparently not a way to show those NOT in a particular group. Digging in the code I see a way to show users who are not in NO groups, but that won't work for me, either :-(
shadowcomputers replied on at Permalink Reply
shadowcomputers
This page describes using the UserList object:http://www.concrete5.org/documentation/developers/permissions/user-...

Using the following function should help you:
$ul->filterByAttribute($attributeKeyHandle, $value, $comparison = '=')


You can also expand the library use by using:http://www.concrete5.org/documentation/developers/system/searching-...

So maybe something like:
Loader::model('user_list');
  $userList = new UserList(); 
  $userlist->filterByAttribute('ExcludeFromDirectory', 0, $comparison = '=');
kirkroberts replied on at Permalink Reply
kirkroberts
Thanks so much for your guidance in my hour of need!

I've tried the code similar to what you suggest and unfortunately it doesn't work for me. Tried comparing to 0, 1, true, false, 'true', 'false', 'yes', 'no' ... even though in the database it's clearly stored as 1, you never know! Tried =, !=, >, < ... all kinds of things. It either filters none, all (empty list), or just the superuser. The superuser ID is 1... I wonder if that has something to do with it.

At this point I need someone to say "I've done something like this, and it works, there must be something wrong with your code". I feel like I've done due diligence on research and testing and am starting to wonder if this might be a bug... not likely, I know, so I'm ready to be wrong :-)
shadowcomputers replied on at Permalink Reply
shadowcomputers
Can you provide the code you are using and exact steps to add the same attribute you have? - I can then have a look and test it my end for you.
kirkroberts replied on at Permalink Reply
kirkroberts
Wow, what an awesome offer! Thanks!

Here are the steps:
- set up a user attribute (checkbox type): handle is 'exclude_from_directory'
- within a user, check the box (make sure to do this on at least two users, so you get one that isn't the superuser)
- copy /concrete/controllers/members.php to /controllers/members.php
- around line 13 or so (after the sortBy method call) add either
$userList->filterByExcludeFromDirectory(1, '!=');
// OR (not both... these are just some of the many permutations I tried)
      $userList->filterByAttribute('exclude_from_directory', 0);

- look at the Members page to see if the filtering is working as expected.

Seems that the test needs to be for NOT 1, as the value may be nothing (not 0) if the attribute is never set up for that particular user but maybe that would equate to zero anyway. Not sure, so I tested LOTS of variations :-)

Thanks so much! I hope that's enough info. Let me know if it works for you!
Shotster replied on at Permalink Reply
Shotster
Hi Kirk,

I tried what you suggested, and I think the problem is that you're editing the wrong file. I think the one you want is...

/concrete/controllers/dashboard/users/search.php

I haven't actually tried it yet, but I think that's the ticket.

EDIT: Ok, just tried it. I think you want to edit the getRequestedSearchResults() method in the aforementioned file.

-Steve
kirkroberts replied on at Permalink Reply
kirkroberts
Steve — I think we're talking about two different things.
I'm referring to the Members page on the public-facing site, not the Users page in the Dashboard.
Shotster replied on at Permalink Reply
Shotster
Sorry, my bad!

:-/

-Steve
shadowcomputers replied on at Permalink Best Answer Reply
shadowcomputers
Hi,

I have tested it on one of my installations and the following works:

<?php 
defined('C5_EXECUTE') or die("Access Denied."); 
Loader::model('user_list');  
class MembersController extends Controller {
   public function view() {
      $userList = new UserList(); 
      $userList->sortBy('uName', 'asc');
      $userList->filterByAttribute('exclude_from_directory', 0, '='); 
      $keywords = $this->get('keywords');
      if ($keywords != '') {
         $userList->filterByKeywords($keywords);
      }
      $users = $userList->getPage();
      $this->set('userList', $userList);                  
      $this->set('users', $users);


The above code is the entire members.php that is in the SITEROOT/controllers/ directory.

Do you want to give it a try and see if it works for you?

Kind regards, Steve R
Shotster replied on at Permalink Reply
Shotster
Hi Kirk,

I've not actually tried to do what you want to do, but I'm curious as to why you want to avoid groups.

Interestingly, I just submitted a user list add-on to the marketplace. While it allows for group unions and intersections, it does not currently allow you to filter users that are NOT in one or more groups. However, your problem has given me an idea for a feature enhancement.

If I learn anything that might help, I'll post back.

-Steve
kirkroberts replied on at Permalink Reply
kirkroberts
Hey Steve — I'm not averse to groups, but I do want to have the Members list people not HAVE to be in a group. It's just easier to add someone and not have to remember to put them in a group. Yeah, there are ways to do that automatically, but I'd rather the client just have to choose who to exclude.

Since I've found a way to get what I need by using a group and a-bit-more-code-than-seems-necessary, it's academic to find out why filtering on a checkbox attribute doesn't seem to work.
Shotster replied on at Permalink Reply
Shotster
> I'd rather the client just have to choose who to exclude.

Gothca. Personally, I think the UI for adding and removing users to/from groups could be improved dramatically. It's too cumbersome as is. If the process was simpler and more intuitive, it would make working with groups much more pleasant.

:-/

-Steve
shadowcomputers replied on at Permalink Reply
shadowcomputers
Well hopefully my solution above might work for you the way you want it to. Let me know how you get on.

Steve R.
kirkroberts replied on at Permalink Reply
kirkroberts
Steve H — your answer works (I tried it on a different c5 installation), but something else is getting in the way on the site I'm building. Not your fault, so I award you the answer! As for me, I'll just stick with the solution I found using groups and call it a win :-)

Thanks again for your help.
shadowcomputers replied on at Permalink Reply
shadowcomputers
No worries. Let me know if I can help any further.
Shotster replied on at Permalink Reply 1 Attachment
Shotster
> it does not currently allow you to filter users that are NOT in one or more groups

Scratch that, it does now! Thanks to Kirk's inquiry, I've added new functionality to my user list add-on (which is currently pending approval).

Anyway, just wanted to set the record straight in case someone stumbles upon this thread down the road.

See attached image for a snippet of the UI.

-Steve
kirkroberts replied on at Permalink Reply
kirkroberts
Ah, very cool. So if there are any users who are in NO groups, they can now be displayed using your block. Nice!
Shotster replied on at Permalink Reply
Shotster
Yes, that's correct. The "exclusion" option will display users in all unselected groups PLUS all users who aren't in any groups. If you wanted ONLY the users who aren't in any groups, you would simply select all the groups and choose the "exclusion" option.

Anyway, thanks.

-Steve
Shotster replied on at Permalink Reply
Shotster
For the sake of follow-through, my add-on is now available in the marketplace...

http://www.concrete5.org/marketplace/addons/enhanced-user-list/...

-Steve