Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
1.6k views
in Q2A Core by
edited by

I was able to get blocked users from the database. To make it more efficient I need to understand the flag system better. Once Gidgreen explained it a bit to me.

"you can see the QA_USER_FLAGS_* constants at the top of qa-app-users.php for the bit values." gidgreen

These are the flags:

    define('QA_USER_FLAGS_EMAIL_CONFIRMED', 1);
    define('QA_USER_FLAGS_USER_BLOCKED', 2);
    define('QA_USER_FLAGS_SHOW_AVATAR', 4);
    define('QA_USER_FLAGS_SHOW_GRAVATAR', 8);
    define('QA_USER_FLAGS_NO_MESSAGES', 16);
    define('QA_USER_FLAGS_NO_MAILINGS', 32);
    define('QA_USER_FLAGS_WELCOME_NOTICE', 64);
    define('QA_USER_FLAGS_MUST_CONFIRM', 128);
 

I try to find the blocked users by the flag which would be "2". How to see if a "2" is contained in the flag number. E.g. "3" is email confirmed (1) + user blocked (2)?

1 Answer

+2 votes
by
edited by

okay, I needed some more brain work smiley see the following as a tutorial for newbies

Decimal Binary
1 1
2 10
4 100
8 1000
16 10000
32 100000
64 1000000
128 10000000

 

So I get a number such as 5 and convert it to binary: 0101

Thus I see the flags 1 and 4!


next step... OR statement. To transfer the value. I want to add flag 8 to 5.

example: 8 OR 5 = 8|5 is 1000 | 0101 results in

1 | 0 → 1
0 | 1 → 1
0 | 0 → 0
0 | 1 → 1

and get 1101 → 2^3+2^2+2^1+2^0 = 13

getting it slowly :)

 

next: how to check if value is inside we use AND

example: is flag 4 inside 13?

13: 1101
  4: 0100

using the logical AND statement

1 & 0 → 0
1 & 1 → 1
0 & 0 → 0
1 & 0 → 0

we get 0100 which is 2^2 = 4.

so in PHP: (4 & 13) returns 4, then flag is inside!

if you try (4 & 16) it returns 0, flag not inside.

after writing this I found the php: bitwise operators reference. laugh

by
I think you overcomplicated things there. If the user's flag number is stored in a variable $userflags, then you can simply do:

QA_USER_FLAGS_USER_BLOCKED & $userflags

Note the single &. If that statement comes out to true (or rather, 1) then the flag is set. In other words:

if ( QA_USER_FLAGS_USER_BLOCKED & $userflags ) {
  // the user is blocked
}
by
Just to add more information, if you want to check if the user has uploaded an avatar, you can use:

if( !(qa_get_logged_in_flags() & QA_USER_FLAGS_SHOW_AVATAR)) {
   //... do something
}
...